/src/PcapPlusPlus/Packet++/header/BgpLayer.h
Line | Count | Source (jump to first uncovered line) |
1 | | #pragma once |
2 | | |
3 | | #include <vector> |
4 | | #include "Layer.h" |
5 | | #include "IpAddress.h" |
6 | | |
7 | | /// @file |
8 | | /// This file contains classes for parsing, creating and editing Border Gateway Protocol (BGP) version 4 packets. |
9 | | /// It contains an abstract class named BgpLayer which has common functionality and 5 inherited classes that |
10 | | /// represent the different BGP message types: OPEN, UPDATE, NOTIFICATION, KEEPALIVE and ROUTE-REFRESH. |
11 | | /// Each of these classes contains unique functionality for parsing. creating and editing of these message. |
12 | | |
13 | | /// @namespace pcpp |
14 | | /// @brief The main namespace for the PcapPlusPlus lib |
15 | | namespace pcpp |
16 | | { |
17 | | |
18 | | /// @class BgpLayer |
19 | | /// Represents Border Gateway Protocol (BGP) v4 protocol layer. This is an abstract class that cannot be |
20 | | /// instantiated, and contains functionality which is common to all BGP message types. |
21 | | class BgpLayer : public Layer |
22 | | { |
23 | | public: |
24 | | /// An enum representing BGP message types |
25 | | enum BgpMessageType |
26 | | { |
27 | | /// BGP OPEN message |
28 | | Open = 1, |
29 | | /// BGP UPDATE message |
30 | | Update = 2, |
31 | | /// BGP NOTIFICATION message |
32 | | Notification = 3, |
33 | | /// BGP KEEPALIVE message |
34 | | Keepalive = 4, |
35 | | /// BGP ROUTE-REFRESH message |
36 | | RouteRefresh = 5, |
37 | | }; |
38 | | |
39 | | #pragma pack(push, 1) |
40 | | /// @struct bgp_common_header |
41 | | /// Represents the common fields of a BGP 4 message |
42 | | struct bgp_common_header |
43 | | { |
44 | | /// 16-octet marker |
45 | | uint8_t marker[16]; |
46 | | /// Total length of the message, including the header |
47 | | uint16_t length; |
48 | | /// BGP message type |
49 | | uint8_t messageType; |
50 | | }; |
51 | | #pragma pack(pop) |
52 | | static_assert(sizeof(bgp_common_header) == 19, "bgp_common_header size is not 19 bytes"); |
53 | | |
54 | | /// @return BGP message type |
55 | | virtual BgpMessageType getBgpMessageType() const = 0; |
56 | | |
57 | | /// @return BGP message type as string. Return value can be one of the following: |
58 | | /// "OPEN", "UPDATE", "NOTIFICATION", "KEEPALIVE", "ROUTE-REFRESH", "Unknown" |
59 | | std::string getMessageTypeAsString() const; |
60 | | |
61 | | /// A static method that checks whether a source or dest port match those associated with the BGP protocol |
62 | | /// @param[in] portSrc Source port number to check |
63 | | /// @param[in] portDst Dest port number to check |
64 | | /// @return True if the source or dest port match those associated with the BGP protocol |
65 | | static bool isBgpPort(uint16_t portSrc, uint16_t portDst) |
66 | 100k | { |
67 | 100k | return portSrc == 179 || portDst == 179; |
68 | 100k | } |
69 | | |
70 | | /// A method that creates a BGP layer from packet raw data |
71 | | /// @param[in] data A pointer to the raw data |
72 | | /// @param[in] dataLen Size of the data in bytes |
73 | | /// @param[in] prevLayer A pointer to the previous layer |
74 | | /// @param[in] packet A pointer to the Packet instance where layer will be stored |
75 | | /// @return A newly allocated BGP layer of one of the following types (according to the message type): |
76 | | /// BgpOpenMessageLayer, BgpUpdateMessageLayer, BgpNotificationMessageLayer, BgpKeepaliveMessageLayer, |
77 | | /// BgpRouteRefreshMessageLayer |
78 | | static BgpLayer* parseBgpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet); |
79 | | |
80 | | // implement abstract methods |
81 | | |
82 | | /// @return The size of the BGP message |
83 | | size_t getHeaderLen() const override; |
84 | | |
85 | | /// Multiple BGP messages can reside in a single packet, and the only layer that can come after a BGP message |
86 | | /// is another BGP message. This method checks for remaining data and parses it as another BGP layer |
87 | | void parseNextLayer() override; |
88 | | |
89 | | std::string toString() const override; |
90 | | |
91 | | OsiModelLayer getOsiModelLayer() const override |
92 | 7.39k | { |
93 | 7.39k | return OsiModelApplicationLayer; |
94 | 7.39k | } |
95 | | |
96 | | /// Calculates the basic BGP fields: |
97 | | /// - Set marker to all ones |
98 | | /// - Set message type value |
99 | | /// - Set message length |
100 | | void computeCalculateFields() override; |
101 | | |
102 | | protected: |
103 | | // protected c'tors, this class cannot be instantiated by users |
104 | | BgpLayer() |
105 | 0 | {} |
106 | | BgpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) |
107 | 69.4k | : Layer(data, dataLen, prevLayer, packet, BGP) |
108 | 69.4k | {} |
109 | | |
110 | | bgp_common_header* getBasicHeader() const |
111 | 633k | { |
112 | 633k | return reinterpret_cast<bgp_common_header*>(m_Data); |
113 | 633k | } |
114 | | |
115 | | void setBgpFields(size_t messageLen = 0); |
116 | | }; |
117 | | |
118 | | /// @class BgpOpenMessageLayer |
119 | | /// Represents a BGP v4 OPEN message |
120 | | class BgpOpenMessageLayer : public BgpLayer |
121 | | { |
122 | | public: |
123 | | #pragma pack(push, 1) |
124 | | /// @struct bgp_open_message |
125 | | /// BGP OPEN message structure |
126 | | typedef struct bgp_open_message : bgp_common_header |
127 | | { |
128 | | /// BGP version number |
129 | | uint8_t version; |
130 | | /// Autonomous System number of the sender |
131 | | uint16_t myAutonomousSystem; |
132 | | /// The number of seconds the sender proposes for the value of the Hold Timer |
133 | | uint16_t holdTime; |
134 | | /// BGP Identifier of the sender |
135 | | uint32_t bgpId; |
136 | | /// The total length of the Optional Parameters field |
137 | | uint8_t optionalParameterLength; |
138 | | } bgp_open_message; |
139 | | #pragma pack(pop) |
140 | | |
141 | | /// @struct optional_parameter |
142 | | /// A structure that represents BGP OPEN message optional parameters |
143 | | struct optional_parameter |
144 | | { |
145 | | /// Parameter type |
146 | | uint8_t type; |
147 | | /// Parameter length |
148 | | uint8_t length; |
149 | | /// Parameter data |
150 | | uint8_t value[32]; |
151 | | |
152 | | // FIXME: This does not actually zero the data. |
153 | | /// A default c'tor that zeroes all data |
154 | | optional_parameter() |
155 | 5.30k | {} |
156 | | |
157 | | /// A c'tor that initializes the values of the struct |
158 | | /// @param[in] typeVal Parameter type value |
159 | | /// @param[in] valueAsHexString Parameter data as hex string. The length field will be set accordingly. |
160 | | /// If this parameter is not a valid hex string the data will remain zeroed and length will be also zero |
161 | | optional_parameter(uint8_t typeVal, const std::string& valueAsHexString); |
162 | | }; |
163 | | |
164 | | /// A constructor that creates the layer from an existing packet raw data |
165 | | /// @param[in] data A pointer to the raw data |
166 | | /// @param[in] dataLen Size of the data in bytes |
167 | | /// @param[in] prevLayer A pointer to the previous layer |
168 | | /// @param[in] packet A pointer to the Packet instance where layer will be stored in |
169 | | BgpOpenMessageLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) |
170 | 7.76k | : BgpLayer(data, dataLen, prevLayer, packet) |
171 | 7.76k | {} |
172 | | |
173 | | /// A c'tor that creates a new BGP OPEN message |
174 | | /// @param[in] myAutonomousSystem The Autonomous System number of the sender |
175 | | /// @param[in] holdTime The number of seconds the sender proposes for the value of the Hold Timer |
176 | | /// @param[in] bgpId The BGP Identifier of the sender |
177 | | /// @param[in] optionalParams A vector of optional parameters. This parameter is optional and if not provided no |
178 | | /// parameters will be set on the message |
179 | | BgpOpenMessageLayer(uint16_t myAutonomousSystem, uint16_t holdTime, const IPv4Address& bgpId, |
180 | | const std::vector<optional_parameter>& optionalParams = std::vector<optional_parameter>()); |
181 | | |
182 | | /// Get a pointer to the open message data. Notice this points directly to the data, so any change will modify |
183 | | /// the actual packet data |
184 | | /// @return A pointer to a bgp_open_message structure containing the data |
185 | | bgp_open_message* getOpenMsgHeader() const |
186 | 10.6k | { |
187 | 10.6k | return reinterpret_cast<bgp_open_message*>(m_Data); |
188 | 10.6k | } |
189 | | |
190 | | /// @return The BGP identifier as IPv4Address object |
191 | | IPv4Address getBgpId() const |
192 | 0 | { |
193 | 0 | return IPv4Address(getOpenMsgHeader()->bgpId); |
194 | 0 | } |
195 | | |
196 | | /// Set the BGP identifier |
197 | | /// @param[in] newBgpId BGP identifier to set. If value is not a valid IPv4 address it won't be set |
198 | | void setBgpId(const IPv4Address& newBgpId); |
199 | | |
200 | | /// Get a vector of the optional parameters in the message |
201 | | /// @param[out] optionalParameters The vector where the optional parameters will be written to. This method |
202 | | /// doesn't remove any existing data on this vector before pushing data to it |
203 | | void getOptionalParameters(std::vector<optional_parameter>& optionalParameters); |
204 | | |
205 | | /// @return The length in [bytes] of the optional parameters data in the message |
206 | | size_t getOptionalParametersLength(); |
207 | | |
208 | | /// Set optional parameters in the message. This method will override all existing optional parameters currently |
209 | | /// in the message. If the input is an empty vector all optional parameters will be cleared. This method |
210 | | /// automatically sets the bgp_common_header#length and the bgp_open_message#optionalParameterLength fields on |
211 | | /// the message |
212 | | /// @param[in] optionalParameters A vector of new optional parameters to set in the message |
213 | | /// @return True if all optional parameters were set successfully or false otherwise. In case of an error an |
214 | | /// appropriate message will be printed to log |
215 | | bool setOptionalParameters(const std::vector<optional_parameter>& optionalParameters); |
216 | | |
217 | | /// Clear all optional parameters currently in the message. This is equivalent to calling |
218 | | /// setOptionalParameters() with an empty vector as a parameter |
219 | | /// @return True if all optional parameters were successfully cleared or false otherwise. In case of an error an |
220 | | /// appropriate message will be printed to log |
221 | | bool clearOptionalParameters(); |
222 | | |
223 | | // implement abstract methods |
224 | | |
225 | | BgpMessageType getBgpMessageType() const override |
226 | 5.24k | { |
227 | 5.24k | return BgpLayer::Open; |
228 | 5.24k | } |
229 | | |
230 | | private: |
231 | | size_t optionalParamsToByteArray(const std::vector<optional_parameter>& optionalParams, uint8_t* resultByteArr, |
232 | | size_t maxByteArrSize); |
233 | | }; |
234 | | |
235 | | /// @class BgpUpdateMessageLayer |
236 | | /// Represents a BGP v4 UPDATE message |
237 | | class BgpUpdateMessageLayer : public BgpLayer |
238 | | { |
239 | | public: |
240 | | /// @struct prefix_and_ip |
241 | | /// A structure that contains IPv4 address and IP address mask (prefix) information. |
242 | | /// It's used to represent BGP Withdrawn Routes and Network Layer Reachability Information (NLRI) |
243 | | struct prefix_and_ip |
244 | | { |
245 | | /// IPv4 address mask, must contain one of the values: 8, 16, 24, 32 |
246 | | uint8_t prefix; |
247 | | /// IPv4 address |
248 | | IPv4Address ipAddr; |
249 | | |
250 | | /// A default c'tor that zeroes all data |
251 | 6.54k | prefix_and_ip() : prefix(0), ipAddr(IPv4Address::Zero) |
252 | 6.54k | {} |
253 | | |
254 | | /// A c'tor that initializes the values of the struct |
255 | | /// @param[in] prefixVal IPv4 address mask value |
256 | | /// @param[in] ipAddrVal IPv4 address |
257 | 23.3k | prefix_and_ip(uint8_t prefixVal, const std::string& ipAddrVal) : prefix(prefixVal), ipAddr(ipAddrVal) |
258 | 23.3k | {} |
259 | | }; |
260 | | |
261 | | /// @struct path_attribute |
262 | | /// A structure that represents BGP OPEN message Path Attributes information |
263 | | struct path_attribute |
264 | | { |
265 | | /// Path attribute flags |
266 | | uint8_t flags; |
267 | | /// Path attribute type |
268 | | uint8_t type; |
269 | | /// Path attribute length |
270 | | uint8_t length; |
271 | | /// Path attribute data. Max supported data length is 32 bytes |
272 | | uint8_t data[32]; |
273 | | |
274 | | // FIXME: This does not actually zero the data. |
275 | | /// A default c'tor that zeroes all data |
276 | | path_attribute() |
277 | 15.8k | {} |
278 | | |
279 | | /// A c'tor that initializes the values of the struct |
280 | | /// @param[in] flagsVal Path attribute flags value |
281 | | /// @param[in] typeVal Path attribute type value |
282 | | /// @param[in] dataAsHexString Path attribute data as hex string. The path_attribute#length field will be |
283 | | /// set accordingly. If this parameter is not a valid hex string the data will remain zeroed and length will |
284 | | /// be also set to zero |
285 | | path_attribute(uint8_t flagsVal, uint8_t typeVal, const std::string& dataAsHexString); |
286 | | }; |
287 | | |
288 | | /// A constructor that creates the layer from an existing packet raw data |
289 | | /// @param[in] data A pointer to the raw data |
290 | | /// @param[in] dataLen Size of the data in bytes |
291 | | /// @param[in] prevLayer A pointer to the previous layer |
292 | | /// @param[in] packet A pointer to the Packet instance where layer will be stored in |
293 | | BgpUpdateMessageLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) |
294 | 54.5k | : BgpLayer(data, dataLen, prevLayer, packet) |
295 | 54.5k | {} |
296 | | |
297 | | /// A static method that takes a byte array and detects whether it is a BgpUpdateMessage |
298 | | /// @param[in] data A byte array |
299 | | /// @param[in] dataSize The byte array size (in bytes) |
300 | | /// @return True if the data looks like a valid BgpUpdateMessage layer |
301 | | static bool isDataValid(const uint8_t* data, size_t dataSize); |
302 | | |
303 | | /// A c'tor that creates a new BGP UPDATE message |
304 | | /// @param[in] withdrawnRoutes A vector of withdrawn routes data. If left empty (which is the default value) no |
305 | | /// withdrawn route information will be written to the message |
306 | | /// @param[in] pathAttributes A vector of path attributes data. If left empty (which is the default value) no |
307 | | /// path attribute information will be written to the message |
308 | | /// @param[in] nlri A vector of network layer reachability data. If left empty (which is the default value) no |
309 | | /// reachability information will be written to the message |
310 | | explicit BgpUpdateMessageLayer( |
311 | | const std::vector<prefix_and_ip>& withdrawnRoutes = std::vector<prefix_and_ip>(), |
312 | | const std::vector<path_attribute>& pathAttributes = std::vector<path_attribute>(), |
313 | | const std::vector<prefix_and_ip>& nlri = std::vector<prefix_and_ip>()); |
314 | | |
315 | | /// Get a pointer to the basic BGP message data. Notice this points directly to the data, so any change will |
316 | | /// modify the actual packet data |
317 | | /// @return A pointer to a bgp_common_header structure containing the data |
318 | | bgp_common_header* getBasicMsgHeader() const |
319 | 0 | { |
320 | 0 | return reinterpret_cast<bgp_common_header*>(m_Data); |
321 | 0 | } |
322 | | |
323 | | /// @return The size in [bytes] of the Withdrawn Routes data |
324 | | size_t getWithdrawnRoutesLength() const; |
325 | | |
326 | | /// Get a vector of the Withdrawn Routes currently in the message |
327 | | /// @param[out] withdrawnRoutes A reference to a vector the Withdrawn Routes data will be written to |
328 | | void getWithdrawnRoutes(std::vector<prefix_and_ip>& withdrawnRoutes); |
329 | | |
330 | | /// Set Withdrawn Routes in this message. This method will override any existing Withdrawn Routes in the |
331 | | /// message. If the input is an empty vector all Withdrawn Routes will be removed. This method automatically |
332 | | /// sets the bgp_common_header#length and the Withdrawn Routes length fields in the message |
333 | | /// @param[in] withdrawnRoutes New Withdrawn Routes to set in the message |
334 | | /// @return True if all Withdrawn Routes were set successfully or false otherwise. In case of an error an |
335 | | /// appropriate message will be printed to log |
336 | | bool setWithdrawnRoutes(const std::vector<prefix_and_ip>& withdrawnRoutes); |
337 | | |
338 | | /// Clear all Withdrawn Routes data currently in the message. This is equivalent to calling setWithdrawnRoutes() |
339 | | /// with an empty vector as a parameter |
340 | | /// @return True if all Withdrawn Routes were successfully cleared or false otherwise. In case of an error an |
341 | | /// appropriate message will be printed to log |
342 | | bool clearWithdrawnRoutes(); |
343 | | |
344 | | /// @return The size in [bytes] of the Path Attributes data |
345 | | size_t getPathAttributesLength() const; |
346 | | |
347 | | /// Get a vector of the Path Attributes currently in the message |
348 | | /// @param[out] pathAttributes A reference to a vector the Path Attributes data will be written to |
349 | | void getPathAttributes(std::vector<path_attribute>& pathAttributes); |
350 | | |
351 | | /// Set Path Attributes in this message. This method will override any existing Path Attributes in the message. |
352 | | /// If the input is an empty vector all Path Attributes will be removed. This method automatically sets the |
353 | | /// bgp_common_header#length and the Path Attributes length fields in the message |
354 | | /// @param[in] pathAttributes New Path Attributes to set in the message |
355 | | /// @return True if all Path Attributes were set successfully or false otherwise. In case of an error an |
356 | | /// appropriate message will be printed to log |
357 | | bool setPathAttributes(const std::vector<path_attribute>& pathAttributes); |
358 | | |
359 | | /// Clear all Path Attributes data currently in the message. This is equivalent to calling setPathAttributes() |
360 | | /// with an empty vector as a parameter |
361 | | /// @return True if all Path Attributes were successfully cleared or false otherwise. In case of an error an |
362 | | /// appropriate message will be printed to log |
363 | | bool clearPathAttributes(); |
364 | | |
365 | | /// @return The size in [bytes] of the Network Layer Reachability Info |
366 | | size_t getNetworkLayerReachabilityInfoLength() const; |
367 | | |
368 | | /// Get a vector of the Network Layer Reachability Info currently in the message |
369 | | /// @param[out] nlri A reference to a vector the NLRI data will be written to |
370 | | void getNetworkLayerReachabilityInfo(std::vector<prefix_and_ip>& nlri); |
371 | | |
372 | | /// Set NLRI data in this message. This method will override any existing NLRI data in the message. |
373 | | /// If the input is an empty vector all NLRI data will be removed. This method automatically sets the |
374 | | /// bgp_common_header#length field in the message |
375 | | /// @param[in] nlri New NLRI data to set in the message |
376 | | /// @return True if all NLRI data was set successfully or false otherwise. In case of an error an appropriate |
377 | | /// message will be printed to log |
378 | | bool setNetworkLayerReachabilityInfo(const std::vector<prefix_and_ip>& nlri); |
379 | | |
380 | | /// Clear all NLRI data currently in the message. This is equivalent to calling |
381 | | /// setNetworkLayerReachabilityInfo() with an empty vector as a parameter |
382 | | /// @return True if all NLRI were successfully cleared or false otherwise. In case of an error an appropriate |
383 | | /// message will be printed to log |
384 | | bool clearNetworkLayerReachabilityInfo(); |
385 | | |
386 | | // implement abstract methods |
387 | | |
388 | | BgpMessageType getBgpMessageType() const override |
389 | 18.7k | { |
390 | 18.7k | return BgpLayer::Update; |
391 | 18.7k | } |
392 | | |
393 | | private: |
394 | | void parsePrefixAndIPData(uint8_t* dataPtr, size_t dataLen, std::vector<prefix_and_ip>& result); |
395 | | |
396 | | size_t prefixAndIPDataToByteArray(const std::vector<prefix_and_ip>& prefixAndIpData, uint8_t* resultByteArr, |
397 | | size_t maxByteArrSize); |
398 | | |
399 | | size_t pathAttributesToByteArray(const std::vector<path_attribute>& pathAttributes, uint8_t* resultByteArr, |
400 | | size_t maxByteArrSize); |
401 | | }; |
402 | | |
403 | | /// @class BgpNotificationMessageLayer |
404 | | /// Represents a BGP v4 NOTIFICATION message |
405 | | class BgpNotificationMessageLayer : public BgpLayer |
406 | | { |
407 | | public: |
408 | | #pragma pack(push, 1) |
409 | | /// @struct bgp_notification_message |
410 | | /// BGP NOTIFICATION message structure |
411 | | typedef struct bgp_notification_message : bgp_common_header |
412 | | { |
413 | | /// BGP notification error code |
414 | | uint8_t errorCode; |
415 | | /// BGP notification error sub-code |
416 | | uint8_t errorSubCode; |
417 | | } bgp_notification_message; |
418 | | #pragma pack(pop) |
419 | | |
420 | | /// A constructor that creates the layer from an existing packet raw data |
421 | | /// @param[in] data A pointer to the raw data |
422 | | /// @param[in] dataLen Size of the data in bytes |
423 | | /// @param[in] prevLayer A pointer to the previous layer |
424 | | /// @param[in] packet A pointer to the Packet instance where layer will be stored in |
425 | | BgpNotificationMessageLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) |
426 | 917 | : BgpLayer(data, dataLen, prevLayer, packet) |
427 | 917 | {} |
428 | | |
429 | | /// A c'tor that creates a new BGP NOTIFICATION message |
430 | | /// @param[in] errorCode BGP notification error code |
431 | | /// @param[in] errorSubCode BGP notification error sub code |
432 | | BgpNotificationMessageLayer(uint8_t errorCode, uint8_t errorSubCode); |
433 | | |
434 | | /// A c'tor that creates a new BGP Notification message |
435 | | /// @param[in] errorCode BGP notification error code |
436 | | /// @param[in] errorSubCode BGP notification error sub code |
437 | | /// @param[in] notificationData A byte array that contains the notification data |
438 | | /// @param[in] notificationDataLen The size of the byte array that contains the notification data |
439 | | BgpNotificationMessageLayer(uint8_t errorCode, uint8_t errorSubCode, const uint8_t* notificationData, |
440 | | size_t notificationDataLen); |
441 | | |
442 | | /// A c'tor that creates a new BGP Notification message |
443 | | /// @param[in] errorCode BGP notification error code |
444 | | /// @param[in] errorSubCode BGP notification error sub code |
445 | | /// @param[in] notificationData A hex string that contains the notification data. This string will be converted |
446 | | /// to a byte array that will be added to the message. If the input isn't a valid hex string notification data |
447 | | /// will remain empty and an error will be printed to log |
448 | | BgpNotificationMessageLayer(uint8_t errorCode, uint8_t errorSubCode, const std::string& notificationData); |
449 | | |
450 | | /// Get a pointer to the notification message data. Notice this points directly to the data, so any change will |
451 | | /// modify the actual packet data |
452 | | /// @return A pointer to a bgp_notification_message structure containing the data |
453 | | bgp_notification_message* getNotificationMsgHeader() const |
454 | 0 | { |
455 | 0 | return reinterpret_cast<bgp_notification_message*>(m_Data); |
456 | 0 | } |
457 | | |
458 | | /// @return The size in [bytes] of the notification data. Notification data is a variable-length field used to |
459 | | /// diagnose the reason for the BGP NOTIFICATION |
460 | | size_t getNotificationDataLen() const; |
461 | | |
462 | | /// @return A pointer to the notification data. Notification data is a variable-length field used to diagnose |
463 | | /// the reason for the BGP NOTIFICATION |
464 | | uint8_t* getNotificationData() const; |
465 | | |
466 | | /// @return A hex string which represents the notification data. Notification data is a variable-length field |
467 | | /// used to diagnose the reason for the BGP NOTIFICATION |
468 | | std::string getNotificationDataAsHexString() const; |
469 | | |
470 | | /// Set the notification data. This method will extend or shorten the existing layer to include the new |
471 | | /// notification data. If newNotificationData is nullptr or newNotificationDataLen is zero then notification |
472 | | /// data will be set to none. |
473 | | /// @param[in] newNotificationData A byte array containing the new notification data |
474 | | /// @param[in] newNotificationDataLen The size of the byte array |
475 | | /// @return True if notification data was set successfully or false if any error occurred. In case of an error |
476 | | /// an appropriate error message will be printed to log |
477 | | bool setNotificationData(const uint8_t* newNotificationData, size_t newNotificationDataLen); |
478 | | |
479 | | /// Set the notification data. This method will extend or shorten the existing layer to include the new |
480 | | /// notification data. If newNotificationDataAsHexString is an empty string then notification data will be set |
481 | | /// to none. |
482 | | /// @param[in] newNotificationDataAsHexString A hex string representing the new notification data. If the string |
483 | | /// is not a valid hex string no data will be changed and an error will be returned |
484 | | /// @return True if notification data was set successfully or false if any error occurred or if the string is |
485 | | /// not a valid hex string. In case of an error an appropriate error message will be printed to log |
486 | | bool setNotificationData(const std::string& newNotificationDataAsHexString); |
487 | | |
488 | | // implement abstract methods |
489 | | |
490 | | BgpMessageType getBgpMessageType() const override |
491 | 672 | { |
492 | 672 | return BgpLayer::Notification; |
493 | 672 | } |
494 | | |
495 | | private: |
496 | | void initMessageData(uint8_t errorCode, uint8_t errorSubCode, const uint8_t* notificationData, |
497 | | size_t notificationDataLen); |
498 | | }; |
499 | | |
500 | | /// @class BgpKeepaliveMessageLayer |
501 | | /// Represents a BGP v4 KEEPALIVE message |
502 | | class BgpKeepaliveMessageLayer : public BgpLayer |
503 | | { |
504 | | public: |
505 | | /// @typedef bgp_keepalive_message |
506 | | /// BGP KEEPALIVE message structure |
507 | | typedef bgp_common_header bgp_keepalive_message; |
508 | | |
509 | | /// A constructor that creates the layer from an existing packet raw data |
510 | | /// @param[in] data A pointer to the raw data |
511 | | /// @param[in] dataLen Size of the data in bytes |
512 | | /// @param[in] prevLayer A pointer to the previous layer |
513 | | /// @param[in] packet A pointer to the Packet instance where layer will be stored in |
514 | | BgpKeepaliveMessageLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) |
515 | 5.18k | : BgpLayer(data, dataLen, prevLayer, packet) |
516 | 5.18k | {} |
517 | | |
518 | | /// A c'tor that creates a new BGP KEEPALIVE message |
519 | | BgpKeepaliveMessageLayer(); |
520 | | |
521 | | /// Get a pointer to the KeepAlive message data. Notice this points directly to the data, so any change will |
522 | | /// modify the actual packet data |
523 | | /// @return A pointer to a bgp_keepalive_message structure containing the data |
524 | | bgp_keepalive_message* getKeepaliveHeader() const |
525 | 0 | { |
526 | 0 | return reinterpret_cast<bgp_keepalive_message*>(getBasicHeader()); |
527 | 0 | } |
528 | | |
529 | | // implement abstract methods |
530 | | |
531 | | BgpMessageType getBgpMessageType() const override |
532 | 4.14k | { |
533 | 4.14k | return BgpLayer::Keepalive; |
534 | 4.14k | } |
535 | | }; |
536 | | |
537 | | /// @class BgpRouteRefreshMessageLayer |
538 | | /// Represents a BGP v4 ROUTE-REFRESH message |
539 | | class BgpRouteRefreshMessageLayer : public BgpLayer |
540 | | { |
541 | | public: |
542 | | #pragma pack(push, 1) |
543 | | /// @struct bgp_route_refresh_message |
544 | | /// BGP ROUTE-REFRESH message structure |
545 | | typedef struct bgp_route_refresh_message : bgp_common_header |
546 | | { |
547 | | /// Address Family Identifier |
548 | | uint16_t afi; |
549 | | /// Reserved field |
550 | | uint8_t reserved; |
551 | | /// Subsequent Address Family Identifier |
552 | | uint8_t safi; |
553 | | } bgp_route_refresh_message; |
554 | | #pragma pack(pop) |
555 | | |
556 | | /// A constructor that creates the layer from an existing packet raw data |
557 | | /// @param[in] data A pointer to the raw data |
558 | | /// @param[in] dataLen Size of the data in bytes |
559 | | /// @param[in] prevLayer A pointer to the previous layer |
560 | | /// @param[in] packet A pointer to the Packet instance where layer will be stored in |
561 | | BgpRouteRefreshMessageLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) |
562 | 1.00k | : BgpLayer(data, dataLen, prevLayer, packet) |
563 | 1.00k | {} |
564 | | |
565 | | /// A c'tor that creates a new BGP ROUTE-REFRESH message |
566 | | /// @param[in] afi The Address Family Identifier (AFI) value to set in the message |
567 | | /// @param[in] safi The Subsequent Address Family Identifier (SAFI) value to set in the message |
568 | | BgpRouteRefreshMessageLayer(uint16_t afi, uint8_t safi); |
569 | | |
570 | | /// Get a pointer to the ROUTE-REFRESH message data. Notice this points directly to the data, so any change will |
571 | | /// modify the actual packet data |
572 | | /// @return A pointer to a bgp_route_refresh_message structure containing the data |
573 | | bgp_route_refresh_message* getRouteRefreshHeader() const |
574 | 0 | { |
575 | 0 | return reinterpret_cast<bgp_route_refresh_message*>(getBasicHeader()); |
576 | 0 | } |
577 | | |
578 | | // implement abstract methods |
579 | | |
580 | | BgpMessageType getBgpMessageType() const override |
581 | 804 | { |
582 | 804 | return BgpLayer::RouteRefresh; |
583 | 804 | } |
584 | | }; |
585 | | |
586 | | } // namespace pcpp |