Coverage Report

Created: 2026-10-17 07:09

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