PcapPlusPlus  23.09
BgpLayer.h
Go to the documentation of this file.
1 #ifndef PACKETPP_BGP_LAYER
2 #define PACKETPP_BGP_LAYER
3 
4 #include <vector>
5 #include "Layer.h"
6 #include "IpAddress.h"
7 
20 namespace pcpp
21 {
22 
28 class BgpLayer : public Layer
29 {
30 public:
31 
36  {
38  Open = 1,
40  Update = 2,
44  Keepalive = 4,
47  };
48 
53  #pragma pack(push, 1)
55  {
57  uint8_t marker[16];
59  uint16_t length;
61  uint8_t messageType;
62  };
63  #pragma pack(pop)
64 
68  virtual BgpMessageType getBgpMessageType() const = 0;
69 
74  std::string getMessageTypeAsString() const;
75 
82  static bool isBgpPort(uint16_t portSrc, uint16_t portDst) { return portSrc == 179 || portDst == 179; }
83 
94  static BgpLayer* parseBgpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
95 
96  // implement abstract methods
97 
101  size_t getHeaderLen() const;
102 
107  void parseNextLayer();
108 
109  std::string toString() const;
110 
112 
119  void computeCalculateFields();
120 
121 protected:
122 
123  // protected c'tors, this class cannot be instantiated by users
124  BgpLayer() {}
125  BgpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) : Layer(data, dataLen, prevLayer, packet) { m_Protocol = BGP; }
126 
127  bgp_common_header* getBasicHeader() const { return (bgp_common_header*)m_Data; }
128 
129  void setBgpFields(size_t messageLen = 0);
130 
131 };
132 
133 
134 
140 {
141 public:
142 
147  #pragma pack(push, 1)
148  typedef struct bgp_open_message : bgp_common_header
149  {
151  uint8_t version;
155  uint16_t holdTime;
157  uint32_t bgpId;
161  #pragma pack(pop)
162 
168  {
170  uint8_t type;
172  uint8_t length;
174  uint8_t value[32];
175 
180 
187  optional_parameter(uint8_t typeVal, const std::string& valueAsHexString);
188  };
189 
197  BgpOpenMessageLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) : BgpLayer(data, dataLen, prevLayer, packet) {}
198 
207  BgpOpenMessageLayer(uint16_t myAutonomousSystem, uint16_t holdTime, const IPv4Address& bgpId,
208  const std::vector<optional_parameter>& optionalParams = std::vector<optional_parameter>());
209 
214  bgp_open_message* getOpenMsgHeader() const { return (bgp_open_message*)m_Data; }
215 
219  IPv4Address getBgpId() const { return IPv4Address(getOpenMsgHeader()->bgpId); }
220 
225  void setBgpId(const IPv4Address& newBgpId);
226 
232  void getOptionalParameters(std::vector<optional_parameter>& optionalParameters);
233 
237  size_t getOptionalParametersLength();
238 
247  bool setOptionalParameters(const std::vector<optional_parameter>& optionalParameters);
248 
255  bool clearOptionalParameters();
256 
257  // implement abstract methods
258 
260 
261 private:
262 
263  size_t optionalParamsToByteArray(const std::vector<optional_parameter>& optionalParams, uint8_t* resultByteArr, size_t maxByteArrSize);
264 
265 };
266 
267 
268 
274 {
275 public:
276 
283  {
285  uint8_t prefix;
288 
292  prefix_and_ip(): prefix(0), ipAddr(IPv4Address::Zero) {}
293 
299  prefix_and_ip(uint8_t prefixVal, const std::string& ipAddrVal): prefix(prefixVal), ipAddr(ipAddrVal) {}
300  };
301 
302 
308  {
310  uint8_t flags;
312  uint8_t type;
314  uint8_t length;
316  uint8_t data[32];
317 
322 
330  path_attribute(uint8_t flagsVal, uint8_t typeVal, const std::string& dataAsHexString);
331  };
332 
340  BgpUpdateMessageLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) : BgpLayer(data, dataLen, prevLayer, packet) {}
341 
348  explicit BgpUpdateMessageLayer(
349  const std::vector<prefix_and_ip>& withdrawnRoutes = std::vector<prefix_and_ip>(),
350  const std::vector<path_attribute>& pathAttributes = std::vector<path_attribute>(),
351  const std::vector<prefix_and_ip>& nlri = std::vector<prefix_and_ip>());
352 
358 
362  size_t getWithdrawnRoutesLength() const;
363 
368  void getWithdrawnRoutes(std::vector<prefix_and_ip>& withdrawnRoutes);
369 
378  bool setWithdrawnRoutes(const std::vector<prefix_and_ip>& withdrawnRoutes);
379 
386  bool clearWithdrawnRoutes();
387 
391  size_t getPathAttributesLength() const;
392 
397  void getPathAttributes(std::vector<path_attribute>& pathAttributes);
398 
407  bool setPathAttributes(const std::vector<path_attribute>& pathAttributes);
408 
415  bool clearPathAttributes();
416 
420  size_t getNetworkLayerReachabilityInfoLength() const;
421 
426  void getNetworkLayerReachabilityInfo(std::vector<prefix_and_ip>& nlri);
427 
436  bool setNetworkLayerReachabilityInfo(const std::vector<prefix_and_ip>& nlri);
437 
444  bool clearNetworkLayerReachabilityInfo();
445 
446  // implement abstract methods
447 
449 
450 private:
451 
452  void parsePrefixAndIPData(uint8_t* dataPtr, size_t dataLen, std::vector<prefix_and_ip>& result);
453 
454  size_t prefixAndIPDataToByteArray(const std::vector<prefix_and_ip>& prefixAndIpData, uint8_t* resultByteArr, size_t maxByteArrSize);
455 
456  size_t pathAttributesToByteArray(const std::vector<path_attribute>& pathAttributes, uint8_t* resultByteArr, size_t maxByteArrSize);
457 
458 };
459 
460 
461 
467 {
468 public:
469 
474  #pragma pack(push, 1)
476  {
478  uint8_t errorCode;
480  uint8_t errorSubCode;
482  #pragma pack(pop)
483 
491  BgpNotificationMessageLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) : BgpLayer(data, dataLen, prevLayer, packet) {}
492 
498  BgpNotificationMessageLayer(uint8_t errorCode, uint8_t errorSubCode);
499 
507  BgpNotificationMessageLayer(uint8_t errorCode, uint8_t errorSubCode, const uint8_t* notificationData, size_t notificationDataLen);
508 
516  BgpNotificationMessageLayer(uint8_t errorCode, uint8_t errorSubCode, const std::string& notificationData);
517 
523 
528  size_t getNotificationDataLen() const;
529 
534  uint8_t* getNotificationData() const;
535 
540  std::string getNotificationDataAsHexString() const;
541 
550  bool setNotificationData(const uint8_t* newNotificationData, size_t newNotificationDataLen);
551 
560  bool setNotificationData(const std::string& newNotificationDataAsHexString);
561 
562  // implement abstract methods
563 
565 
566 private:
567 
568  void initMessageData(uint8_t errorCode, uint8_t errorSubCode, const uint8_t* notificationData, size_t notificationDataLen);
569 
570 };
571 
572 
573 
579 {
580 public:
581 
587 
595  BgpKeepaliveMessageLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) : BgpLayer(data, dataLen, prevLayer, packet) {}
596 
601 
606  bgp_keepalive_message* getKeepaliveHeader() const { return (bgp_keepalive_message*)getBasicHeader(); }
607 
608  // implement abstract methods
609 
611 
612 };
613 
614 
615 
621 {
622 public:
623 
628  #pragma pack(push, 1)
630  {
632  uint16_t afi;
634  uint8_t reserved;
636  uint8_t safi;
638  #pragma pack(pop)
639 
647  BgpRouteRefreshMessageLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) : BgpLayer(data, dataLen, prevLayer, packet) {}
648 
654  BgpRouteRefreshMessageLayer(uint16_t afi, uint8_t safi);
655 
661 
662  // implement abstract methods
663 
665 
666 };
667 
668 }
669 
670 #endif //PACKETPP_BGP_LAYER
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:333
uint8_t length
Definition: BgpLayer.h:314
BgpMessageType getBgpMessageType() const
Definition: BgpLayer.h:610
uint16_t holdTime
Definition: BgpLayer.h:155
static bool isBgpPort(uint16_t portSrc, uint16_t portDst)
Definition: BgpLayer.h:82
prefix_and_ip()
Definition: BgpLayer.h:292
void parseNextLayer()
Definition: BgpLayer.h:273
uint8_t prefix
Definition: BgpLayer.h:285
uint16_t length
Definition: BgpLayer.h:59
uint8_t type
Definition: BgpLayer.h:170
OsiModelLayer getOsiModelLayer() const
Definition: BgpLayer.h:111
size_t getHeaderLen() const
void computeCalculateFields()
Definition: BgpLayer.h:40
BgpMessageType getBgpMessageType() const
Definition: BgpLayer.h:259
Definition: ProtocolType.h:348
std::string toString() const
Definition: Layer.h:70
BgpRouteRefreshMessageLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: BgpLayer.h:647
uint8_t optionalParameterLength
Definition: BgpLayer.h:159
BgpMessageType getBgpMessageType() const
Definition: BgpLayer.h:564
Definition: BgpLayer.h:466
Definition: Packet.h:26
BgpMessageType getBgpMessageType() const
Definition: BgpLayer.h:664
bgp_common_header bgp_keepalive_message
Definition: BgpLayer.h:586
uint8_t flags
Definition: BgpLayer.h:310
BgpMessageType
Definition: BgpLayer.h:35
path_attribute()
Definition: BgpLayer.h:321
prefix_and_ip(uint8_t prefixVal, const std::string &ipAddrVal)
Definition: BgpLayer.h:299
Definition: BgpLayer.h:46
Definition: BgpLayer.h:38
BgpUpdateMessageLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: BgpLayer.h:340
bgp_keepalive_message * getKeepaliveHeader() const
Definition: BgpLayer.h:606
bgp_common_header * getBasicMsgHeader() const
Definition: BgpLayer.h:357
Definition: BgpLayer.h:139
IPv4Address getBgpId() const
Definition: BgpLayer.h:219
bgp_open_message * getOpenMsgHeader() const
Definition: BgpLayer.h:214
static BgpLayer * parseBgpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
std::string getMessageTypeAsString() const
bgp_notification_message * getNotificationMsgHeader() const
Definition: BgpLayer.h:522
Definition: IpAddress.h:42
Definition: BgpLayer.h:54
Definition: BgpLayer.h:28
uint8_t version
Definition: BgpLayer.h:151
uint8_t length
Definition: BgpLayer.h:172
BgpOpenMessageLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: BgpLayer.h:197
uint8_t messageType
Definition: BgpLayer.h:61
BgpMessageType getBgpMessageType() const
Definition: BgpLayer.h:448
IPv4Address ipAddr
Definition: BgpLayer.h:287
Definition: BgpLayer.h:578
Definition: BgpLayer.h:44
Definition: BgpLayer.h:620
optional_parameter()
Definition: BgpLayer.h:179
uint16_t myAutonomousSystem
Definition: BgpLayer.h:153
BgpNotificationMessageLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: BgpLayer.h:491
bgp_route_refresh_message * getRouteRefreshHeader() const
Definition: BgpLayer.h:660
uint8_t marker[16]
Definition: BgpLayer.h:57
uint8_t type
Definition: BgpLayer.h:312
BgpKeepaliveMessageLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: BgpLayer.h:595
Definition: BgpLayer.h:42
const ProtocolType BGP
Definition: ProtocolType.h:228
uint32_t bgpId
Definition: BgpLayer.h:157
virtual BgpMessageType getBgpMessageType() const =0