/src/PcapPlusPlus/Packet++/header/BgpLayer.h
Line | Count | Source |
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 | 511k | { |
67 | 511k | return portSrc == 179 || portDst == 179; |
68 | 511k | } |
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 | 23.0k | { |
93 | 23.0k | return OsiModelApplicationLayer; |
94 | 23.0k | } |
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 | 194k | : Layer(data, dataLen, prevLayer, packet, BGP) |
108 | 194k | {} |
109 | | |
110 | | bgp_common_header* getBasicHeader() const |
111 | 1.76M | { |
112 | 1.76M | return reinterpret_cast<bgp_common_header*>(m_Data); |
113 | 1.76M | } |
114 | | |
115 | | void setBgpFields(size_t messageLen = 0); |
116 | | |
117 | | bool extendLayer(int offsetInLayer, size_t numOfBytesToExtend) override; |
118 | | |
119 | | bool shortenLayer(int offsetInLayer, size_t numOfBytesToShorten) override; |
120 | | }; |
121 | | |
122 | | /// @class BgpOpenMessageLayer |
123 | | /// Represents a BGP v4 OPEN message |
124 | | class BgpOpenMessageLayer : public BgpLayer |
125 | | { |
126 | | public: |
127 | | #pragma pack(push, 1) |
128 | | /// @struct bgp_open_message |
129 | | /// BGP OPEN message structure |
130 | | typedef struct bgp_open_message : bgp_common_header |
131 | | { |
132 | | /// BGP version number |
133 | | uint8_t version; |
134 | | /// Autonomous System number of the sender |
135 | | uint16_t myAutonomousSystem; |
136 | | /// The number of seconds the sender proposes for the value of the Hold Timer |
137 | | uint16_t holdTime; |
138 | | /// BGP Identifier of the sender |
139 | | uint32_t bgpId; |
140 | | /// The total length of the Optional Parameters field |
141 | | uint8_t optionalParameterLength; |
142 | | } bgp_open_message; |
143 | | #pragma pack(pop) |
144 | | |
145 | | /// @struct optional_parameter |
146 | | /// A structure that represents BGP OPEN message optional parameters |
147 | | struct optional_parameter |
148 | | { |
149 | | /// Parameter type |
150 | | uint8_t type = 0; |
151 | | /// Parameter length |
152 | | uint8_t length = 0; |
153 | | /// Parameter data |
154 | | uint8_t value[32] = {}; |
155 | | |
156 | | /// A default c'tor that zeroes all data |
157 | 16.2k | optional_parameter() = default; |
158 | | |
159 | | /// A c'tor that initializes the values of the struct |
160 | | /// @param[in] typeVal Parameter type value |
161 | | /// @param[in] valueAsHexString Parameter data as hex string. The length field will be set accordingly. |
162 | | /// If this parameter is not a valid hex string the data will remain zeroed and length will be also zero |
163 | | optional_parameter(uint8_t typeVal, const std::string& valueAsHexString); |
164 | | }; |
165 | | |
166 | | /// A constructor that creates the layer from an existing packet raw data |
167 | | /// @param[in] data A pointer to the raw data |
168 | | /// @param[in] dataLen Size of the data in bytes |
169 | | /// @param[in] prevLayer A pointer to the previous layer |
170 | | /// @param[in] packet A pointer to the Packet instance where layer will be stored in |
171 | | BgpOpenMessageLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) |
172 | 27.8k | : BgpLayer(data, dataLen, prevLayer, packet) |
173 | 27.8k | {} |
174 | | |
175 | | /// A static method that takes a byte array and detects whether it is a BgpOpenMessage |
176 | | /// @param[in] data A byte array |
177 | | /// @param[in] dataSize The byte array size (in bytes) |
178 | | /// @return True if the data looks like a valid BgpOpenMessage layer |
179 | | static bool isDataValid(const uint8_t* data, size_t dataSize); |
180 | | |
181 | | /// A c'tor that creates a new BGP OPEN message |
182 | | /// @param[in] myAutonomousSystem The Autonomous System number of the sender |
183 | | /// @param[in] holdTime The number of seconds the sender proposes for the value of the Hold Timer |
184 | | /// @param[in] bgpId The BGP Identifier of the sender |
185 | | /// @param[in] optionalParams A vector of optional parameters. This parameter is optional and if not provided no |
186 | | /// parameters will be set on the message |
187 | | BgpOpenMessageLayer(uint16_t myAutonomousSystem, uint16_t holdTime, const IPv4Address& bgpId, |
188 | | const std::vector<optional_parameter>& optionalParams = std::vector<optional_parameter>()); |
189 | | |
190 | | /// Get a pointer to the open message data. Notice this points directly to the data, so any change will modify |
191 | | /// the actual packet data |
192 | | /// @return A pointer to a bgp_open_message structure containing the data |
193 | | bgp_open_message* getOpenMsgHeader() const |
194 | 38.1k | { |
195 | 38.1k | return reinterpret_cast<bgp_open_message*>(m_Data); |
196 | 38.1k | } |
197 | | |
198 | | /// @return The BGP identifier as IPv4Address object |
199 | | IPv4Address getBgpId() const |
200 | 0 | { |
201 | 0 | return IPv4Address(getOpenMsgHeader()->bgpId); |
202 | 0 | } |
203 | | |
204 | | /// Set the BGP identifier |
205 | | /// @param[in] newBgpId BGP identifier to set. If value is not a valid IPv4 address it won't be set |
206 | | void setBgpId(const IPv4Address& newBgpId); |
207 | | |
208 | | /// Get a vector of the optional parameters in the message |
209 | | /// @param[out] optionalParameters The vector where the optional parameters will be written to. This method |
210 | | /// doesn't remove any existing data on this vector before pushing data to it |
211 | | void getOptionalParameters(std::vector<optional_parameter>& optionalParameters); |
212 | | |
213 | | /// @return The length in [bytes] of the optional parameters data in the message |
214 | | size_t getOptionalParametersLength(); |
215 | | |
216 | | /// Set optional parameters in the message. This method will override all existing optional parameters currently |
217 | | /// in the message. If the input is an empty vector all optional parameters will be cleared. This method |
218 | | /// automatically sets the bgp_common_header#length and the bgp_open_message#optionalParameterLength fields on |
219 | | /// the message |
220 | | /// @param[in] optionalParameters A vector of new optional parameters to set in the message |
221 | | /// @return True if all optional parameters were set successfully or false otherwise. In case of an error an |
222 | | /// appropriate message will be printed to log |
223 | | bool setOptionalParameters(const std::vector<optional_parameter>& optionalParameters); |
224 | | |
225 | | /// Clear all optional parameters currently in the message. This is equivalent to calling |
226 | | /// setOptionalParameters() with an empty vector as a parameter |
227 | | /// @return True if all optional parameters were successfully cleared or false otherwise. In case of an error an |
228 | | /// appropriate message will be printed to log |
229 | | bool clearOptionalParameters(); |
230 | | |
231 | | // implement abstract methods |
232 | | |
233 | | BgpMessageType getBgpMessageType() const override |
234 | 16.1k | { |
235 | 16.1k | return BgpLayer::Open; |
236 | 16.1k | } |
237 | | |
238 | | private: |
239 | | size_t optionalParamsToByteArray(const std::vector<optional_parameter>& optionalParams, uint8_t* resultByteArr, |
240 | | size_t maxByteArrSize); |
241 | | }; |
242 | | |
243 | | /// @class BgpUpdateMessageLayer |
244 | | /// Represents a BGP v4 UPDATE message |
245 | | class BgpUpdateMessageLayer : public BgpLayer |
246 | | { |
247 | | public: |
248 | | /// @struct prefix_and_ip |
249 | | /// A structure that contains IPv4 address and IP address mask (prefix) information. |
250 | | /// It's used to represent BGP Withdrawn Routes and Network Layer Reachability Information (NLRI) |
251 | | struct prefix_and_ip |
252 | | { |
253 | | /// IPv4 address mask, must contain one of the values: 8, 16, 24, 32 |
254 | | uint8_t prefix; |
255 | | /// IPv4 address |
256 | | IPv4Address ipAddr; |
257 | | |
258 | | /// A default c'tor that zeroes all data |
259 | 18.0k | prefix_and_ip() : prefix(0), ipAddr(IPv4Address::Zero) |
260 | 18.0k | {} |
261 | | |
262 | | /// A c'tor that initializes the values of the struct |
263 | | /// @param[in] prefixVal IPv4 address mask value |
264 | | /// @param[in] ipAddrVal IPv4 address |
265 | 81.6k | prefix_and_ip(uint8_t prefixVal, const std::string& ipAddrVal) : prefix(prefixVal), ipAddr(ipAddrVal) |
266 | 81.6k | {} |
267 | | }; |
268 | | |
269 | | /// @struct path_attribute |
270 | | /// A structure that represents BGP OPEN message Path Attributes information |
271 | | struct path_attribute |
272 | | { |
273 | | /// Path attribute flags |
274 | | uint8_t flags; |
275 | | /// Path attribute type |
276 | | uint8_t type; |
277 | | /// Path attribute length |
278 | | uint8_t length; |
279 | | /// Path attribute data. Max supported data length is 32 bytes |
280 | | uint8_t data[32]; |
281 | | |
282 | | // FIXME: This does not actually zero the data. |
283 | | /// A default c'tor that zeroes all data |
284 | | path_attribute() |
285 | 43.0k | {} |
286 | | |
287 | | /// A c'tor that initializes the values of the struct |
288 | | /// @param[in] flagsVal Path attribute flags value |
289 | | /// @param[in] typeVal Path attribute type value |
290 | | /// @param[in] dataAsHexString Path attribute data as hex string. The path_attribute#length field will be |
291 | | /// set accordingly. If this parameter is not a valid hex string the data will remain zeroed and length will |
292 | | /// be also set to zero |
293 | | path_attribute(uint8_t flagsVal, uint8_t typeVal, const std::string& dataAsHexString); |
294 | | }; |
295 | | |
296 | | /// A constructor that creates the layer from an existing packet raw data |
297 | | /// @param[in] data A pointer to the raw data |
298 | | /// @param[in] dataLen Size of the data in bytes |
299 | | /// @param[in] prevLayer A pointer to the previous layer |
300 | | /// @param[in] packet A pointer to the Packet instance where layer will be stored in |
301 | | BgpUpdateMessageLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) |
302 | 153k | : BgpLayer(data, dataLen, prevLayer, packet) |
303 | 153k | {} |
304 | | |
305 | | /// A static method that takes a byte array and detects whether it is a BgpUpdateMessage |
306 | | /// @param[in] data A byte array |
307 | | /// @param[in] dataSize The byte array size (in bytes) |
308 | | /// @return True if the data looks like a valid BgpUpdateMessage layer |
309 | | static bool isDataValid(const uint8_t* data, size_t dataSize); |
310 | | |
311 | | /// A c'tor that creates a new BGP UPDATE message |
312 | | /// @param[in] withdrawnRoutes A vector of withdrawn routes data. If left empty (which is the default value) no |
313 | | /// withdrawn route information will be written to the message |
314 | | /// @param[in] pathAttributes A vector of path attributes data. If left empty (which is the default value) no |
315 | | /// path attribute information will be written to the message |
316 | | /// @param[in] nlri A vector of network layer reachability data. If left empty (which is the default value) no |
317 | | /// reachability information will be written to the message |
318 | | explicit BgpUpdateMessageLayer( |
319 | | const std::vector<prefix_and_ip>& withdrawnRoutes = std::vector<prefix_and_ip>(), |
320 | | const std::vector<path_attribute>& pathAttributes = std::vector<path_attribute>(), |
321 | | const std::vector<prefix_and_ip>& nlri = std::vector<prefix_and_ip>()); |
322 | | |
323 | | /// Get a pointer to the basic BGP message data. Notice this points directly to the data, so any change will |
324 | | /// modify the actual packet data |
325 | | /// @return A pointer to a bgp_common_header structure containing the data |
326 | | bgp_common_header* getBasicMsgHeader() const |
327 | 0 | { |
328 | 0 | return reinterpret_cast<bgp_common_header*>(m_Data); |
329 | 0 | } |
330 | | |
331 | | /// @return The size in [bytes] of the Withdrawn Routes data |
332 | | size_t getWithdrawnRoutesLength() const; |
333 | | |
334 | | /// Get a vector of the Withdrawn Routes currently in the message |
335 | | /// @param[out] withdrawnRoutes A reference to a vector the Withdrawn Routes data will be written to |
336 | | void getWithdrawnRoutes(std::vector<prefix_and_ip>& withdrawnRoutes); |
337 | | |
338 | | /// Set Withdrawn Routes in this message. This method will override any existing Withdrawn Routes in the |
339 | | /// message. If the input is an empty vector all Withdrawn Routes will be removed. This method automatically |
340 | | /// sets the bgp_common_header#length and the Withdrawn Routes length fields in the message |
341 | | /// @param[in] withdrawnRoutes New Withdrawn Routes to set in the message |
342 | | /// @return True if all Withdrawn Routes were set successfully or false otherwise. In case of an error an |
343 | | /// appropriate message will be printed to log |
344 | | bool setWithdrawnRoutes(const std::vector<prefix_and_ip>& withdrawnRoutes); |
345 | | |
346 | | /// Clear all Withdrawn Routes data currently in the message. This is equivalent to calling setWithdrawnRoutes() |
347 | | /// with an empty vector as a parameter |
348 | | /// @return True if all Withdrawn Routes were successfully cleared or false otherwise. In case of an error an |
349 | | /// appropriate message will be printed to log |
350 | | bool clearWithdrawnRoutes(); |
351 | | |
352 | | /// @return The size in [bytes] of the Path Attributes data |
353 | | size_t getPathAttributesLength() const; |
354 | | |
355 | | /// Get a vector of the Path Attributes currently in the message |
356 | | /// @param[out] pathAttributes A reference to a vector the Path Attributes data will be written to |
357 | | void getPathAttributes(std::vector<path_attribute>& pathAttributes); |
358 | | |
359 | | /// Set Path Attributes in this message. This method will override any existing Path Attributes in the message. |
360 | | /// If the input is an empty vector all Path Attributes will be removed. This method automatically sets the |
361 | | /// bgp_common_header#length and the Path Attributes length fields in the message |
362 | | /// @param[in] pathAttributes New Path Attributes to set in the message |
363 | | /// @return True if all Path Attributes were set successfully or false otherwise. In case of an error an |
364 | | /// appropriate message will be printed to log |
365 | | bool setPathAttributes(const std::vector<path_attribute>& pathAttributes); |
366 | | |
367 | | /// Clear all Path Attributes data currently in the message. This is equivalent to calling setPathAttributes() |
368 | | /// with an empty vector as a parameter |
369 | | /// @return True if all Path Attributes were successfully cleared or false otherwise. In case of an error an |
370 | | /// appropriate message will be printed to log |
371 | | bool clearPathAttributes(); |
372 | | |
373 | | /// @return The size in [bytes] of the Network Layer Reachability Info |
374 | | size_t getNetworkLayerReachabilityInfoLength() const; |
375 | | |
376 | | /// Get a vector of the Network Layer Reachability Info currently in the message |
377 | | /// @param[out] nlri A reference to a vector the NLRI data will be written to |
378 | | void getNetworkLayerReachabilityInfo(std::vector<prefix_and_ip>& nlri); |
379 | | |
380 | | /// Set NLRI data in this message. This method will override any existing NLRI data in the message. |
381 | | /// If the input is an empty vector all NLRI data will be removed. This method automatically sets the |
382 | | /// bgp_common_header#length field in the message |
383 | | /// @param[in] nlri New NLRI data to set in the message |
384 | | /// @return True if all NLRI data was set successfully or false otherwise. In case of an error an appropriate |
385 | | /// message will be printed to log |
386 | | bool setNetworkLayerReachabilityInfo(const std::vector<prefix_and_ip>& nlri); |
387 | | |
388 | | /// Clear all NLRI data currently in the message. This is equivalent to calling |
389 | | /// setNetworkLayerReachabilityInfo() with an empty vector as a parameter |
390 | | /// @return True if all NLRI were successfully cleared or false otherwise. In case of an error an appropriate |
391 | | /// message will be printed to log |
392 | | bool clearNetworkLayerReachabilityInfo(); |
393 | | |
394 | | // implement abstract methods |
395 | | |
396 | | BgpMessageType getBgpMessageType() const override |
397 | 65.2k | { |
398 | 65.2k | return BgpLayer::Update; |
399 | 65.2k | } |
400 | | |
401 | | private: |
402 | | void parsePrefixAndIPData(uint8_t* dataPtr, size_t dataLen, std::vector<prefix_and_ip>& result); |
403 | | |
404 | | size_t prefixAndIPDataToByteArray(const std::vector<prefix_and_ip>& prefixAndIpData, uint8_t* resultByteArr, |
405 | | size_t maxByteArrSize); |
406 | | |
407 | | size_t pathAttributesToByteArray(const std::vector<path_attribute>& pathAttributes, uint8_t* resultByteArr, |
408 | | size_t maxByteArrSize); |
409 | | }; |
410 | | |
411 | | /// @class BgpNotificationMessageLayer |
412 | | /// Represents a BGP v4 NOTIFICATION message |
413 | | class BgpNotificationMessageLayer : public BgpLayer |
414 | | { |
415 | | public: |
416 | | #pragma pack(push, 1) |
417 | | /// @struct bgp_notification_message |
418 | | /// BGP NOTIFICATION message structure |
419 | | typedef struct bgp_notification_message : bgp_common_header |
420 | | { |
421 | | /// BGP notification error code |
422 | | uint8_t errorCode; |
423 | | /// BGP notification error sub-code |
424 | | uint8_t errorSubCode; |
425 | | } bgp_notification_message; |
426 | | #pragma pack(pop) |
427 | | |
428 | | /// A constructor that creates the layer from an existing packet raw data |
429 | | /// @param[in] data A pointer to the raw data |
430 | | /// @param[in] dataLen Size of the data in bytes |
431 | | /// @param[in] prevLayer A pointer to the previous layer |
432 | | /// @param[in] packet A pointer to the Packet instance where layer will be stored in |
433 | | BgpNotificationMessageLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) |
434 | 4.01k | : BgpLayer(data, dataLen, prevLayer, packet) |
435 | 4.01k | {} |
436 | | |
437 | | /// A c'tor that creates a new BGP NOTIFICATION message |
438 | | /// @param[in] errorCode BGP notification error code |
439 | | /// @param[in] errorSubCode BGP notification error sub code |
440 | | BgpNotificationMessageLayer(uint8_t errorCode, uint8_t errorSubCode); |
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 byte array that contains the notification data |
446 | | /// @param[in] notificationDataLen The size of the byte array that contains the notification data |
447 | | BgpNotificationMessageLayer(uint8_t errorCode, uint8_t errorSubCode, const uint8_t* notificationData, |
448 | | size_t notificationDataLen); |
449 | | |
450 | | /// A c'tor that creates a new BGP Notification message |
451 | | /// @param[in] errorCode BGP notification error code |
452 | | /// @param[in] errorSubCode BGP notification error sub code |
453 | | /// @param[in] notificationData A hex string that contains the notification data. This string will be converted |
454 | | /// to a byte array that will be added to the message. If the input isn't a valid hex string notification data |
455 | | /// will remain empty and an error will be printed to log |
456 | | BgpNotificationMessageLayer(uint8_t errorCode, uint8_t errorSubCode, const std::string& notificationData); |
457 | | |
458 | | /// Get a pointer to the notification message data. Notice this points directly to the data, so any change will |
459 | | /// modify the actual packet data |
460 | | /// @return A pointer to a bgp_notification_message structure containing the data |
461 | | bgp_notification_message* getNotificationMsgHeader() const |
462 | 0 | { |
463 | 0 | return reinterpret_cast<bgp_notification_message*>(m_Data); |
464 | 0 | } |
465 | | |
466 | | /// @return The size in [bytes] of the notification data. Notification data is a variable-length field used to |
467 | | /// diagnose the reason for the BGP NOTIFICATION |
468 | | size_t getNotificationDataLen() const; |
469 | | |
470 | | /// @return A pointer to the notification data. Notification data is a variable-length field used to diagnose |
471 | | /// the reason for the BGP NOTIFICATION |
472 | | uint8_t* getNotificationData() const; |
473 | | |
474 | | /// @return A hex string which represents the notification data. Notification data is a variable-length field |
475 | | /// used to diagnose the reason for the BGP NOTIFICATION |
476 | | std::string getNotificationDataAsHexString() const; |
477 | | |
478 | | /// Set the notification data. This method will extend or shorten the existing layer to include the new |
479 | | /// notification data. If newNotificationData is nullptr or newNotificationDataLen is zero then notification |
480 | | /// data will be set to none. |
481 | | /// @param[in] newNotificationData A byte array containing the new notification data |
482 | | /// @param[in] newNotificationDataLen The size of the byte array |
483 | | /// @return True if notification data was set successfully or false if any error occurred. In case of an error |
484 | | /// an appropriate error message will be printed to log |
485 | | bool setNotificationData(const uint8_t* newNotificationData, size_t newNotificationDataLen); |
486 | | |
487 | | /// Set the notification data. This method will extend or shorten the existing layer to include the new |
488 | | /// notification data. If newNotificationDataAsHexString is an empty string then notification data will be set |
489 | | /// to none. |
490 | | /// @param[in] newNotificationDataAsHexString A hex string representing the new notification data. If the string |
491 | | /// is not a valid hex string no data will be changed and an error will be returned |
492 | | /// @return True if notification data was set successfully or false if any error occurred or if the string is |
493 | | /// not a valid hex string. In case of an error an appropriate error message will be printed to log |
494 | | bool setNotificationData(const std::string& newNotificationDataAsHexString); |
495 | | |
496 | | // implement abstract methods |
497 | | |
498 | | BgpMessageType getBgpMessageType() const override |
499 | 2.68k | { |
500 | 2.68k | return BgpLayer::Notification; |
501 | 2.68k | } |
502 | | |
503 | | private: |
504 | | void initMessageData(uint8_t errorCode, uint8_t errorSubCode, const uint8_t* notificationData, |
505 | | size_t notificationDataLen); |
506 | | }; |
507 | | |
508 | | /// @class BgpKeepaliveMessageLayer |
509 | | /// Represents a BGP v4 KEEPALIVE message |
510 | | class BgpKeepaliveMessageLayer : public BgpLayer |
511 | | { |
512 | | public: |
513 | | /// @typedef bgp_keepalive_message |
514 | | /// BGP KEEPALIVE message structure |
515 | | typedef bgp_common_header bgp_keepalive_message; |
516 | | |
517 | | /// A constructor that creates the layer from an existing packet raw data |
518 | | /// @param[in] data A pointer to the raw data |
519 | | /// @param[in] dataLen Size of the data in bytes |
520 | | /// @param[in] prevLayer A pointer to the previous layer |
521 | | /// @param[in] packet A pointer to the Packet instance where layer will be stored in |
522 | | BgpKeepaliveMessageLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) |
523 | 6.91k | : BgpLayer(data, dataLen, prevLayer, packet) |
524 | 6.91k | {} |
525 | | |
526 | | /// A c'tor that creates a new BGP KEEPALIVE message |
527 | | BgpKeepaliveMessageLayer(); |
528 | | |
529 | | /// Get a pointer to the KeepAlive message data. Notice this points directly to the data, so any change will |
530 | | /// modify the actual packet data |
531 | | /// @return A pointer to a bgp_keepalive_message structure containing the data |
532 | | bgp_keepalive_message* getKeepaliveHeader() const |
533 | 0 | { |
534 | 0 | return reinterpret_cast<bgp_keepalive_message*>(getBasicHeader()); |
535 | 0 | } |
536 | | |
537 | | // implement abstract methods |
538 | | |
539 | | BgpMessageType getBgpMessageType() const override |
540 | 5.44k | { |
541 | 5.44k | return BgpLayer::Keepalive; |
542 | 5.44k | } |
543 | | }; |
544 | | |
545 | | /// @class BgpRouteRefreshMessageLayer |
546 | | /// Represents a BGP v4 ROUTE-REFRESH message |
547 | | class BgpRouteRefreshMessageLayer : public BgpLayer |
548 | | { |
549 | | public: |
550 | | #pragma pack(push, 1) |
551 | | /// @struct bgp_route_refresh_message |
552 | | /// BGP ROUTE-REFRESH message structure |
553 | | typedef struct bgp_route_refresh_message : bgp_common_header |
554 | | { |
555 | | /// Address Family Identifier |
556 | | uint16_t afi; |
557 | | /// Reserved field |
558 | | uint8_t reserved; |
559 | | /// Subsequent Address Family Identifier |
560 | | uint8_t safi; |
561 | | } bgp_route_refresh_message; |
562 | | #pragma pack(pop) |
563 | | |
564 | | /// A constructor that creates the layer from an existing packet raw data |
565 | | /// @param[in] data A pointer to the raw data |
566 | | /// @param[in] dataLen Size of the data in bytes |
567 | | /// @param[in] prevLayer A pointer to the previous layer |
568 | | /// @param[in] packet A pointer to the Packet instance where layer will be stored in |
569 | | BgpRouteRefreshMessageLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) |
570 | 3.08k | : BgpLayer(data, dataLen, prevLayer, packet) |
571 | 3.08k | {} |
572 | | |
573 | | /// A c'tor that creates a new BGP ROUTE-REFRESH message |
574 | | /// @param[in] afi The Address Family Identifier (AFI) value to set in the message |
575 | | /// @param[in] safi The Subsequent Address Family Identifier (SAFI) value to set in the message |
576 | | BgpRouteRefreshMessageLayer(uint16_t afi, uint8_t safi); |
577 | | |
578 | | /// Get a pointer to the ROUTE-REFRESH message data. Notice this points directly to the data, so any change will |
579 | | /// modify the actual packet data |
580 | | /// @return A pointer to a bgp_route_refresh_message structure containing the data |
581 | | bgp_route_refresh_message* getRouteRefreshHeader() const |
582 | 0 | { |
583 | 0 | return reinterpret_cast<bgp_route_refresh_message*>(getBasicHeader()); |
584 | 0 | } |
585 | | |
586 | | // implement abstract methods |
587 | | |
588 | | BgpMessageType getBgpMessageType() const override |
589 | 2.46k | { |
590 | 2.46k | return BgpLayer::RouteRefresh; |
591 | 2.46k | } |
592 | | }; |
593 | | |
594 | | } // namespace pcpp |