PcapPlusPlus  20.08
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 
237 
246  bool setOptionalParameters(const std::vector<optional_parameter>& optionalParameters);
247 
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 
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 
420 
425  void getNetworkLayerReachabilityInfo(std::vector<prefix_and_ip>& nlri);
426 
435  bool setNetworkLayerReachabilityInfo(const std::vector<prefix_and_ip>& nlri);
436 
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
pcpp::OsiModelLayer
OsiModelLayer
Definition: ProtocolType.h:233
pcpp::BgpNotificationMessageLayer::setNotificationData
bool setNotificationData(const uint8_t *newNotificationData, size_t newNotificationDataLen)
pcpp::IPv4Address
Definition: IpAddress.h:26
pcpp::BgpKeepaliveMessageLayer
Definition: BgpLayer.h:577
pcpp::Packet
Definition: Packet.h:26
pcpp::BgpOpenMessageLayer::getBgpId
IPv4Address getBgpId() const
Definition: BgpLayer.h:218
pcpp::BgpLayer::BgpMessageType
BgpMessageType
Definition: BgpLayer.h:35
pcpp::BgpLayer::RouteRefresh
@ RouteRefresh
Definition: BgpLayer.h:46
pcpp::BgpUpdateMessageLayer::BgpUpdateMessageLayer
BgpUpdateMessageLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: BgpLayer.h:339
pcpp::BgpUpdateMessageLayer::path_attribute::type
uint8_t type
Definition: BgpLayer.h:311
pcpp::OsiModelApplicationLayer
@ OsiModelApplicationLayer
Definition: ProtocolType.h:248
pcpp::BgpOpenMessageLayer::optional_parameter
Definition: BgpLayer.h:166
pcpp::BgpUpdateMessageLayer::path_attribute::flags
uint8_t flags
Definition: BgpLayer.h:309
pcpp::BGP
const ProtocolType BGP
Definition: ProtocolType.h:228
pcpp::BgpNotificationMessageLayer::getNotificationData
uint8_t * getNotificationData() const
pcpp::BgpNotificationMessageLayer::BgpNotificationMessageLayer
BgpNotificationMessageLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: BgpLayer.h:490
pcpp::BgpUpdateMessageLayer::prefix_and_ip::prefix
uint8_t prefix
Definition: BgpLayer.h:284
pcpp::BgpUpdateMessageLayer::prefix_and_ip::prefix_and_ip
prefix_and_ip()
Definition: BgpLayer.h:291
Layer.h
pcpp::BgpNotificationMessageLayer::bgp_notification_message::errorSubCode
uint8_t errorSubCode
Definition: BgpLayer.h:479
pcpp::BgpLayer::Notification
@ Notification
Definition: BgpLayer.h:42
pcpp::BgpUpdateMessageLayer::setNetworkLayerReachabilityInfo
bool setNetworkLayerReachabilityInfo(const std::vector< prefix_and_ip > &nlri)
pcpp::BgpUpdateMessageLayer::path_attribute::path_attribute
path_attribute()
Definition: BgpLayer.h:320
pcpp::BgpUpdateMessageLayer
Definition: BgpLayer.h:272
pcpp::BgpRouteRefreshMessageLayer::getRouteRefreshHeader
bgp_route_refresh_message * getRouteRefreshHeader() const
Definition: BgpLayer.h:659
pcpp::BgpUpdateMessageLayer::path_attribute
Definition: BgpLayer.h:306
pcpp::BgpNotificationMessageLayer
Definition: BgpLayer.h:465
pcpp::BgpLayer::getMessageTypeAsString
std::string getMessageTypeAsString() const
pcpp::BgpLayer::Update
@ Update
Definition: BgpLayer.h:40
pcpp::BgpOpenMessageLayer::clearOptionalParameters
bool clearOptionalParameters()
pcpp::BgpLayer::bgp_common_header::length
uint16_t length
Definition: BgpLayer.h:59
pcpp::BgpOpenMessageLayer::getOptionalParametersLength
size_t getOptionalParametersLength()
pcpp::BgpLayer::getOsiModelLayer
OsiModelLayer getOsiModelLayer() const
Definition: BgpLayer.h:110
pcpp::BgpUpdateMessageLayer::setPathAttributes
bool setPathAttributes(const std::vector< path_attribute > &pathAttributes)
pcpp::BgpKeepaliveMessageLayer::getBgpMessageType
BgpMessageType getBgpMessageType() const
Definition: BgpLayer.h:609
pcpp::BgpNotificationMessageLayer::bgp_notification_message::errorCode
uint8_t errorCode
Definition: BgpLayer.h:477
pcpp::BgpRouteRefreshMessageLayer::bgp_route_refresh_message
Definition: BgpLayer.h:628
pcpp::BgpUpdateMessageLayer::prefix_and_ip::prefix_and_ip
prefix_and_ip(uint8_t prefixVal, const std::string &ipAddrVal)
Definition: BgpLayer.h:298
pcpp::BgpLayer::parseBgpLayer
static BgpLayer * parseBgpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
pcpp::BgpNotificationMessageLayer::bgp_notification_message
Definition: BgpLayer.h:474
pcpp::BgpOpenMessageLayer::optional_parameter::length
uint8_t length
Definition: BgpLayer.h:171
pcpp::BgpUpdateMessageLayer::getWithdrawnRoutes
void getWithdrawnRoutes(std::vector< prefix_and_ip > &withdrawnRoutes)
pcpp::BgpUpdateMessageLayer::clearPathAttributes
bool clearPathAttributes()
pcpp::BgpUpdateMessageLayer::getBgpMessageType
BgpMessageType getBgpMessageType() const
Definition: BgpLayer.h:447
pcpp::BgpRouteRefreshMessageLayer::bgp_route_refresh_message::reserved
uint8_t reserved
Definition: BgpLayer.h:633
pcpp::BgpOpenMessageLayer::setBgpId
void setBgpId(const IPv4Address &newBgpId)
pcpp::BgpLayer::computeCalculateFields
void computeCalculateFields()
pcpp::BgpOpenMessageLayer::optional_parameter::optional_parameter
optional_parameter()
Definition: BgpLayer.h:178
pcpp::BgpUpdateMessageLayer::clearWithdrawnRoutes
bool clearWithdrawnRoutes()
pcpp::BgpNotificationMessageLayer::getNotificationDataAsHexString
std::string getNotificationDataAsHexString() const
pcpp::BgpOpenMessageLayer::getOptionalParameters
void getOptionalParameters(std::vector< optional_parameter > &optionalParameters)
pcpp::BgpLayer::isBgpPort
static bool isBgpPort(uint16_t portSrc, uint16_t portDst)
Definition: BgpLayer.h:81
pcpp::BgpOpenMessageLayer::bgp_open_message::version
uint8_t version
Definition: BgpLayer.h:150
pcpp::BgpLayer::bgp_common_header
Definition: BgpLayer.h:54
pcpp::BgpLayer::getHeaderLen
size_t getHeaderLen() const
pcpp::BgpKeepaliveMessageLayer::bgp_keepalive_message
bgp_common_header bgp_keepalive_message
Definition: BgpLayer.h:585
IpAddress.h
pcpp::BgpOpenMessageLayer::getBgpMessageType
BgpMessageType getBgpMessageType() const
Definition: BgpLayer.h:258
pcpp
The main namespace for the PcapPlusPlus lib.
pcpp::BgpRouteRefreshMessageLayer::getBgpMessageType
BgpMessageType getBgpMessageType() const
Definition: BgpLayer.h:663
pcpp::BgpRouteRefreshMessageLayer::bgp_route_refresh_message::safi
uint8_t safi
Definition: BgpLayer.h:635
pcpp::BgpOpenMessageLayer::BgpOpenMessageLayer
BgpOpenMessageLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: BgpLayer.h:196
pcpp::BgpLayer::getBgpMessageType
virtual BgpMessageType getBgpMessageType() const =0
pcpp::BgpNotificationMessageLayer::getBgpMessageType
BgpMessageType getBgpMessageType() const
Definition: BgpLayer.h:563
pcpp::BgpUpdateMessageLayer::prefix_and_ip::ipAddr
IPv4Address ipAddr
Definition: BgpLayer.h:286
pcpp::BgpLayer::parseNextLayer
void parseNextLayer()
pcpp::BgpNotificationMessageLayer::getNotificationDataLen
size_t getNotificationDataLen() const
pcpp::BgpOpenMessageLayer
Definition: BgpLayer.h:138
pcpp::BgpKeepaliveMessageLayer::getKeepaliveHeader
bgp_keepalive_message * getKeepaliveHeader() const
Definition: BgpLayer.h:605
pcpp::BgpRouteRefreshMessageLayer::BgpRouteRefreshMessageLayer
BgpRouteRefreshMessageLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: BgpLayer.h:646
pcpp::Layer
Definition: Layer.h:70
pcpp::BgpLayer::bgp_common_header::marker
uint8_t marker[16]
Definition: BgpLayer.h:57
pcpp::BgpOpenMessageLayer::bgp_open_message::myAutonomousSystem
uint16_t myAutonomousSystem
Definition: BgpLayer.h:152
pcpp::BgpUpdateMessageLayer::getNetworkLayerReachabilityInfoLength
size_t getNetworkLayerReachabilityInfoLength() const
pcpp::BgpUpdateMessageLayer::getBasicMsgHeader
bgp_common_header * getBasicMsgHeader() const
Definition: BgpLayer.h:356
pcpp::BgpOpenMessageLayer::bgp_open_message::optionalParameterLength
uint8_t optionalParameterLength
Definition: BgpLayer.h:158
pcpp::BgpLayer::Keepalive
@ Keepalive
Definition: BgpLayer.h:44
pcpp::BgpUpdateMessageLayer::setWithdrawnRoutes
bool setWithdrawnRoutes(const std::vector< prefix_and_ip > &withdrawnRoutes)
pcpp::BgpNotificationMessageLayer::getNotificationMsgHeader
bgp_notification_message * getNotificationMsgHeader() const
Definition: BgpLayer.h:521
pcpp::BgpLayer
Definition: BgpLayer.h:28
pcpp::BgpUpdateMessageLayer::getPathAttributes
void getPathAttributes(std::vector< path_attribute > &pathAttributes)
pcpp::BgpOpenMessageLayer::optional_parameter::type
uint8_t type
Definition: BgpLayer.h:169
pcpp::BgpUpdateMessageLayer::path_attribute::length
uint8_t length
Definition: BgpLayer.h:313
pcpp::BgpOpenMessageLayer::setOptionalParameters
bool setOptionalParameters(const std::vector< optional_parameter > &optionalParameters)
pcpp::BgpKeepaliveMessageLayer::BgpKeepaliveMessageLayer
BgpKeepaliveMessageLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: BgpLayer.h:594
pcpp::BgpUpdateMessageLayer::path_attribute::data
uint8_t data[32]
Definition: BgpLayer.h:315
pcpp::BgpOpenMessageLayer::getOpenMsgHeader
bgp_open_message * getOpenMsgHeader() const
Definition: BgpLayer.h:213
pcpp::BgpLayer::Open
@ Open
Definition: BgpLayer.h:38
pcpp::BgpRouteRefreshMessageLayer
Definition: BgpLayer.h:619
pcpp::BgpKeepaliveMessageLayer::BgpKeepaliveMessageLayer
BgpKeepaliveMessageLayer()
pcpp::BgpLayer::bgp_common_header::messageType
uint8_t messageType
Definition: BgpLayer.h:61
pcpp::BgpUpdateMessageLayer::prefix_and_ip
Definition: BgpLayer.h:281
pcpp::BgpLayer::toString
std::string toString() const
pcpp::BgpUpdateMessageLayer::getPathAttributesLength
size_t getPathAttributesLength() const
pcpp::BgpUpdateMessageLayer::getWithdrawnRoutesLength
size_t getWithdrawnRoutesLength() const
pcpp::BgpOpenMessageLayer::optional_parameter::value
uint8_t value[32]
Definition: BgpLayer.h:173
pcpp::BgpUpdateMessageLayer::clearNetworkLayerReachabilityInfo
bool clearNetworkLayerReachabilityInfo()
pcpp::BgpRouteRefreshMessageLayer::bgp_route_refresh_message::afi
uint16_t afi
Definition: BgpLayer.h:631
pcpp::BgpOpenMessageLayer::bgp_open_message::holdTime
uint16_t holdTime
Definition: BgpLayer.h:154
pcpp::BgpUpdateMessageLayer::getNetworkLayerReachabilityInfo
void getNetworkLayerReachabilityInfo(std::vector< prefix_and_ip > &nlri)
pcpp::BgpOpenMessageLayer::bgp_open_message
Definition: BgpLayer.h:147
pcpp::BgpOpenMessageLayer::bgp_open_message::bgpId
uint32_t bgpId
Definition: BgpLayer.h:156