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