PcapPlusPlus  21.05
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 
81  static bool isBgpPort(uint16_t portSrc, uint16_t portDst) { return portSrc == 179 || portDst == 179; }
82 
93  static BgpLayer* parseBgpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
94 
95  // implement abstract methods
96 
100  size_t getHeaderLen() const;
101 
106  void parseNextLayer();
107 
108  std::string toString() const;
109 
111 
118  void computeCalculateFields();
119 
120 protected:
121 
122  // protected c'tors, this class cannot be instanciated by users
123  BgpLayer() {}
124  BgpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) : Layer(data, dataLen, prevLayer, packet) { m_Protocol = BGP; }
125 
126  bgp_common_header* getBasicHeader() const { return (bgp_common_header*)m_Data; }
127 
128  void setBgpFields(size_t messageLen = 0);
129 
130 };
131 
132 
133 
139 {
140 public:
141 
146  #pragma pack(push, 1)
147  typedef struct : bgp_common_header
148  {
150  uint8_t version;
154  uint16_t holdTime;
156  uint32_t bgpId;
160  #pragma pack(pop)
161 
167  {
169  uint8_t type;
171  uint8_t length;
173  uint8_t value[32];
174 
179 
186  optional_parameter(uint8_t typeVal, std::string valueAsHexString);
187  };
188 
196  BgpOpenMessageLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) : BgpLayer(data, dataLen, prevLayer, packet) {}
197 
206  BgpOpenMessageLayer(uint16_t myAutonomousSystem, uint16_t holdTime, const IPv4Address& bgpId,
207  const std::vector<optional_parameter>& optionalParams = std::vector<optional_parameter>());
208 
213  bgp_open_message* getOpenMsgHeader() const { return (bgp_open_message*)m_Data; }
214 
218  IPv4Address getBgpId() const { return IPv4Address(getOpenMsgHeader()->bgpId); }
219 
224  void setBgpId(const IPv4Address& newBgpId);
225 
231  void getOptionalParameters(std::vector<optional_parameter>& optionalParameters);
232 
236  size_t getOptionalParametersLength();
237 
246  bool setOptionalParameters(const std::vector<optional_parameter>& optionalParameters);
247 
254  bool clearOptionalParameters();
255 
256  // implement abstract methods
257 
259 
260 private:
261 
262  size_t optionalParamsToByteArray(const std::vector<optional_parameter>& optionalParams, uint8_t* resultByteArr, size_t maxByteArrSize);
263 
264 };
265 
266 
267 
273 {
274 public:
275 
282  {
284  uint8_t prefix;
287 
291  prefix_and_ip(): prefix(0), ipAddr(IPv4Address::Zero) {}
292 
298  prefix_and_ip(uint8_t prefixVal, const std::string& ipAddrVal): prefix(prefixVal), ipAddr(ipAddrVal) {}
299  };
300 
301 
307  {
309  uint8_t flags;
311  uint8_t type;
313  uint8_t length;
315  uint8_t data[32];
316 
321 
329  path_attribute(uint8_t flagsVal, uint8_t typeVal, std::string dataAsHexString);
330  };
331 
339  BgpUpdateMessageLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) : BgpLayer(data, dataLen, prevLayer, packet) {}
340 
348  const std::vector<prefix_and_ip>& withdrawnRoutes = std::vector<prefix_and_ip>(),
349  const std::vector<path_attribute>& pathAttributes = std::vector<path_attribute>(),
350  const std::vector<prefix_and_ip>& nlri = std::vector<prefix_and_ip>());
351 
357 
361  size_t getWithdrawnRoutesLength() const;
362 
367  void getWithdrawnRoutes(std::vector<prefix_and_ip>& withdrawnRoutes);
368 
377  bool setWithdrawnRoutes(const std::vector<prefix_and_ip>& withdrawnRoutes);
378 
385  bool clearWithdrawnRoutes();
386 
390  size_t getPathAttributesLength() const;
391 
396  void getPathAttributes(std::vector<path_attribute>& pathAttributes);
397 
406  bool setPathAttributes(const std::vector<path_attribute>& pathAttributes);
407 
414  bool clearPathAttributes();
415 
419  size_t getNetworkLayerReachabilityInfoLength() const;
420 
425  void getNetworkLayerReachabilityInfo(std::vector<prefix_and_ip>& nlri);
426 
435  bool setNetworkLayerReachabilityInfo(const std::vector<prefix_and_ip>& nlri);
436 
443  bool clearNetworkLayerReachabilityInfo();
444 
445  // implement abstract methods
446 
448 
449 private:
450 
451  void parsePrefixAndIPData(uint8_t* dataPtr, size_t dataLen, std::vector<prefix_and_ip>& result);
452 
453  size_t prefixAndIPDataToByteArray(const std::vector<prefix_and_ip>& prefixAndIpData, uint8_t* resultByteArr, size_t maxByteArrSize);
454 
455  size_t pathAttributesToByteArray(const std::vector<path_attribute>& pathAttributes, uint8_t* resultByteArr, size_t maxByteArrSize);
456 
457 };
458 
459 
460 
466 {
467 public:
468 
473  #pragma pack(push, 1)
474  typedef struct : bgp_common_header
475  {
477  uint8_t errorCode;
479  uint8_t errorSubCode;
481  #pragma pack(pop)
482 
490  BgpNotificationMessageLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) : BgpLayer(data, dataLen, prevLayer, packet) {}
491 
497  BgpNotificationMessageLayer(uint8_t errorCode, uint8_t errorSubCode);
498 
506  BgpNotificationMessageLayer(uint8_t errorCode, uint8_t errorSubCode, const uint8_t* notificationData, size_t notificationDataLen);
507 
515  BgpNotificationMessageLayer(uint8_t errorCode, uint8_t errorSubCode, const std::string& notificationData);
516 
522 
527  size_t getNotificationDataLen() const;
528 
533  uint8_t* getNotificationData() const;
534 
539  std::string getNotificationDataAsHexString() const;
540 
549  bool setNotificationData(const uint8_t* newNotificationData, size_t newNotificationDataLen);
550 
559  bool setNotificationData(const std::string& newNotificationDataAsHexString);
560 
561  // implement abstract methods
562 
564 
565 private:
566 
567  void initMessageData(uint8_t errorCode, uint8_t errorSubCode, const uint8_t* notificationData, size_t notificationDataLen);
568 
569 };
570 
571 
572 
578 {
579 public:
580 
586 
594  BgpKeepaliveMessageLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) : BgpLayer(data, dataLen, prevLayer, packet) {}
595 
600 
605  bgp_keepalive_message* getKeepaliveHeader() const { return (bgp_keepalive_message*)getBasicHeader(); }
606 
607  // implement abstract methods
608 
610 
611 };
612 
613 
614 
620 {
621 public:
622 
627  #pragma pack(push, 1)
628  typedef struct : bgp_common_header
629  {
631  uint16_t afi;
633  uint8_t reserved;
635  uint8_t safi;
637  #pragma pack(pop)
638 
646  BgpRouteRefreshMessageLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) : BgpLayer(data, dataLen, prevLayer, packet) {}
647 
653  BgpRouteRefreshMessageLayer(uint16_t afi, uint8_t safi);
654 
660 
661  // implement abstract methods
662 
664 
665 };
666 
667 }
668 
669 #endif //PACKETPP_BGP_LAYER
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:253
uint8_t length
Definition: BgpLayer.h:313
BgpMessageType getBgpMessageType() const
Definition: BgpLayer.h:609
uint16_t holdTime
Definition: BgpLayer.h:154
static bool isBgpPort(uint16_t portSrc, uint16_t portDst)
Definition: BgpLayer.h:81
prefix_and_ip()
Definition: BgpLayer.h:291
void parseNextLayer()
Definition: BgpLayer.h:272
uint8_t prefix
Definition: BgpLayer.h:284
uint16_t length
Definition: BgpLayer.h:59
uint8_t type
Definition: BgpLayer.h:169
OsiModelLayer getOsiModelLayer() const
Definition: BgpLayer.h:110
size_t getHeaderLen() const
void computeCalculateFields()
Definition: BgpLayer.h:40
BgpMessageType getBgpMessageType() const
Definition: BgpLayer.h:258
Definition: ProtocolType.h:268
std::string toString() const
Definition: Layer.h:70
BgpRouteRefreshMessageLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: BgpLayer.h:646
uint8_t optionalParameterLength
Definition: BgpLayer.h:158
BgpMessageType getBgpMessageType() const
Definition: BgpLayer.h:563
Definition: BgpLayer.h:465
Definition: Packet.h:26
BgpMessageType getBgpMessageType() const
Definition: BgpLayer.h:663
bgp_common_header bgp_keepalive_message
Definition: BgpLayer.h:585
uint8_t flags
Definition: BgpLayer.h:309
BgpMessageType
Definition: BgpLayer.h:35
path_attribute()
Definition: BgpLayer.h:320
prefix_and_ip(uint8_t prefixVal, const std::string &ipAddrVal)
Definition: BgpLayer.h:298
Definition: BgpLayer.h:46
Definition: BgpLayer.h:38
BgpUpdateMessageLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: BgpLayer.h:339
bgp_keepalive_message * getKeepaliveHeader() const
Definition: BgpLayer.h:605
bgp_common_header * getBasicMsgHeader() const
Definition: BgpLayer.h:356
Definition: BgpLayer.h:138
IPv4Address getBgpId() const
Definition: BgpLayer.h:218
bgp_open_message * getOpenMsgHeader() const
Definition: BgpLayer.h:213
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:521
Definition: IpAddress.h:26
Definition: BgpLayer.h:54
Definition: BgpLayer.h:28
uint8_t version
Definition: BgpLayer.h:150
uint8_t length
Definition: BgpLayer.h:171
BgpOpenMessageLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: BgpLayer.h:196
uint8_t messageType
Definition: BgpLayer.h:61
BgpMessageType getBgpMessageType() const
Definition: BgpLayer.h:447
IPv4Address ipAddr
Definition: BgpLayer.h:286
Definition: BgpLayer.h:577
Definition: BgpLayer.h:44
Definition: BgpLayer.h:619
optional_parameter()
Definition: BgpLayer.h:178
uint16_t myAutonomousSystem
Definition: BgpLayer.h:152
BgpNotificationMessageLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: BgpLayer.h:490
bgp_route_refresh_message * getRouteRefreshHeader() const
Definition: BgpLayer.h:659
uint8_t marker[16]
Definition: BgpLayer.h:57
uint8_t type
Definition: BgpLayer.h:311
BgpKeepaliveMessageLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: BgpLayer.h:594
Definition: BgpLayer.h:42
const ProtocolType BGP
Definition: ProtocolType.h:228
uint32_t bgpId
Definition: BgpLayer.h:156
virtual BgpMessageType getBgpMessageType() const =0