PcapPlusPlus  24.09
BgpLayer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 #include "Layer.h"
5 #include "IpAddress.h"
6 
19 namespace pcpp
20 {
21 
27  class BgpLayer : public Layer
28  {
29  public:
34  {
36  Open = 1,
38  Update = 2,
42  Keepalive = 4,
45  };
46 
47 #pragma pack(push, 1)
53  {
55  uint8_t marker[16];
57  uint16_t length;
59  uint8_t messageType;
60  };
61 #pragma pack(pop)
62 
66  virtual BgpMessageType getBgpMessageType() const = 0;
67 
72  std::string getMessageTypeAsString() const;
73 
80  static bool isBgpPort(uint16_t portSrc, uint16_t portDst)
81  {
82  return portSrc == 179 || portDst == 179;
83  }
84 
95  static BgpLayer* parseBgpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
96 
97  // implement abstract methods
98 
102  size_t getHeaderLen() const;
103 
109 
110  std::string toString() const;
111 
113  {
115  }
116 
124 
125  protected:
126  // protected c'tors, this class cannot be instantiated by users
127  BgpLayer()
128  {}
129  BgpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
130  : Layer(data, dataLen, prevLayer, packet, BGP)
131  {}
132 
133  bgp_common_header* getBasicHeader() const
134  {
135  return (bgp_common_header*)m_Data;
136  }
137 
138  void setBgpFields(size_t messageLen = 0);
139  };
140 
146  {
147  public:
148 #pragma pack(push, 1)
154  {
156  uint8_t version;
160  uint16_t holdTime;
162  uint32_t bgpId;
166 #pragma pack(pop)
167 
173  {
175  uint8_t type;
177  uint8_t length;
179  uint8_t value[32];
180 
185  {}
186 
193  optional_parameter(uint8_t typeVal, const std::string& valueAsHexString);
194  };
195 
203  BgpOpenMessageLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
204  : BgpLayer(data, dataLen, prevLayer, packet)
205  {}
206 
215  BgpOpenMessageLayer(uint16_t myAutonomousSystem, uint16_t holdTime, const IPv4Address& bgpId,
216  const std::vector<optional_parameter>& optionalParams = std::vector<optional_parameter>());
217 
224  {
225  return (bgp_open_message*)m_Data;
226  }
227 
232  {
233  return IPv4Address(getOpenMsgHeader()->bgpId);
234  }
235 
240  void setBgpId(const IPv4Address& newBgpId);
241 
247  void getOptionalParameters(std::vector<optional_parameter>& optionalParameters);
248 
253 
263  bool setOptionalParameters(const std::vector<optional_parameter>& optionalParameters);
264 
272 
273  // implement abstract methods
274 
276  {
277  return BgpLayer::Open;
278  }
279 
280  private:
281  size_t optionalParamsToByteArray(const std::vector<optional_parameter>& optionalParams, uint8_t* resultByteArr,
282  size_t maxByteArrSize);
283  };
284 
290  {
291  public:
298  {
300  uint8_t prefix;
303 
308  {}
309 
315  prefix_and_ip(uint8_t prefixVal, const std::string& ipAddrVal) : prefix(prefixVal), ipAddr(ipAddrVal)
316  {}
317  };
318 
324  {
326  uint8_t flags;
328  uint8_t type;
330  uint8_t length;
332  uint8_t data[32];
333 
338  {}
339 
348  path_attribute(uint8_t flagsVal, uint8_t typeVal, const std::string& dataAsHexString);
349  };
350 
358  BgpUpdateMessageLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
359  : BgpLayer(data, dataLen, prevLayer, packet)
360  {}
361 
368  static bool isDataValid(const uint8_t* data, size_t dataSize);
369 
380  const std::vector<prefix_and_ip>& withdrawnRoutes = std::vector<prefix_and_ip>(),
381  const std::vector<path_attribute>& pathAttributes = std::vector<path_attribute>(),
382  const std::vector<prefix_and_ip>& nlri = std::vector<prefix_and_ip>());
383 
390  {
391  return (bgp_common_header*)m_Data;
392  }
393 
397  size_t getWithdrawnRoutesLength() const;
398 
403  void getWithdrawnRoutes(std::vector<prefix_and_ip>& withdrawnRoutes);
404 
413  bool setWithdrawnRoutes(const std::vector<prefix_and_ip>& withdrawnRoutes);
414 
422 
426  size_t getPathAttributesLength() const;
427 
432  void getPathAttributes(std::vector<path_attribute>& pathAttributes);
433 
442  bool setPathAttributes(const std::vector<path_attribute>& pathAttributes);
443 
451 
456 
461  void getNetworkLayerReachabilityInfo(std::vector<prefix_and_ip>& nlri);
462 
471  bool setNetworkLayerReachabilityInfo(const std::vector<prefix_and_ip>& nlri);
472 
480 
481  // implement abstract methods
482 
484  {
485  return BgpLayer::Update;
486  }
487 
488  private:
489  void parsePrefixAndIPData(uint8_t* dataPtr, size_t dataLen, std::vector<prefix_and_ip>& result);
490 
491  size_t prefixAndIPDataToByteArray(const std::vector<prefix_and_ip>& prefixAndIpData, uint8_t* resultByteArr,
492  size_t maxByteArrSize);
493 
494  size_t pathAttributesToByteArray(const std::vector<path_attribute>& pathAttributes, uint8_t* resultByteArr,
495  size_t maxByteArrSize);
496  };
497 
503  {
504  public:
505 #pragma pack(push, 1)
511  {
513  uint8_t errorCode;
515  uint8_t errorSubCode;
517 #pragma pack(pop)
518 
526  BgpNotificationMessageLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
527  : BgpLayer(data, dataLen, prevLayer, packet)
528  {}
529 
535  BgpNotificationMessageLayer(uint8_t errorCode, uint8_t errorSubCode);
536 
544  BgpNotificationMessageLayer(uint8_t errorCode, uint8_t errorSubCode, const uint8_t* notificationData,
545  size_t notificationDataLen);
546 
555  BgpNotificationMessageLayer(uint8_t errorCode, uint8_t errorSubCode, const std::string& notificationData);
556 
563  {
564  return (bgp_notification_message*)m_Data;
565  }
566 
571  size_t getNotificationDataLen() const;
572 
577  uint8_t* getNotificationData() const;
578 
583  std::string getNotificationDataAsHexString() const;
584 
594  bool setNotificationData(const uint8_t* newNotificationData, size_t newNotificationDataLen);
595 
605  bool setNotificationData(const std::string& newNotificationDataAsHexString);
606 
607  // implement abstract methods
608 
610  {
611  return BgpLayer::Notification;
612  }
613 
614  private:
615  void initMessageData(uint8_t errorCode, uint8_t errorSubCode, const uint8_t* notificationData,
616  size_t notificationDataLen);
617  };
618 
624  {
625  public:
631 
639  BgpKeepaliveMessageLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
640  : BgpLayer(data, dataLen, prevLayer, packet)
641  {}
642 
647 
654  {
655  return (bgp_keepalive_message*)getBasicHeader();
656  }
657 
658  // implement abstract methods
659 
661  {
662  return BgpLayer::Keepalive;
663  }
664  };
665 
671  {
672  public:
673 #pragma pack(push, 1)
679  {
681  uint16_t afi;
683  uint8_t reserved;
685  uint8_t safi;
687 #pragma pack(pop)
688 
696  BgpRouteRefreshMessageLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
697  : BgpLayer(data, dataLen, prevLayer, packet)
698  {}
699 
705  BgpRouteRefreshMessageLayer(uint16_t afi, uint8_t safi);
706 
713  {
714  return (bgp_route_refresh_message*)getBasicHeader();
715  }
716 
717  // implement abstract methods
718 
720  {
721  return BgpLayer::RouteRefresh;
722  }
723  };
724 
725 } // namespace pcpp
Definition: BgpLayer.h:624
bgp_common_header bgp_keepalive_message
Definition: BgpLayer.h:630
BgpKeepaliveMessageLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: BgpLayer.h:639
BgpMessageType getBgpMessageType() const
Definition: BgpLayer.h:660
bgp_keepalive_message * getKeepaliveHeader() const
Definition: BgpLayer.h:653
Definition: BgpLayer.h:28
std::string toString() const
static bool isBgpPort(uint16_t portSrc, uint16_t portDst)
Definition: BgpLayer.h:80
BgpMessageType
Definition: BgpLayer.h:34
@ RouteRefresh
Definition: BgpLayer.h:44
@ Update
Definition: BgpLayer.h:38
@ Keepalive
Definition: BgpLayer.h:42
@ Notification
Definition: BgpLayer.h:40
@ Open
Definition: BgpLayer.h:36
std::string getMessageTypeAsString() const
void parseNextLayer()
void computeCalculateFields()
size_t getHeaderLen() const
OsiModelLayer getOsiModelLayer() const
Definition: BgpLayer.h:112
static BgpLayer * parseBgpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
virtual BgpMessageType getBgpMessageType() const =0
Definition: BgpLayer.h:503
BgpNotificationMessageLayer(uint8_t errorCode, uint8_t errorSubCode)
BgpNotificationMessageLayer(uint8_t errorCode, uint8_t errorSubCode, const std::string &notificationData)
bool setNotificationData(const std::string &newNotificationDataAsHexString)
bgp_notification_message * getNotificationMsgHeader() const
Definition: BgpLayer.h:562
std::string getNotificationDataAsHexString() const
bool setNotificationData(const uint8_t *newNotificationData, size_t newNotificationDataLen)
BgpNotificationMessageLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: BgpLayer.h:526
BgpNotificationMessageLayer(uint8_t errorCode, uint8_t errorSubCode, const uint8_t *notificationData, size_t notificationDataLen)
BgpMessageType getBgpMessageType() const
Definition: BgpLayer.h:609
uint8_t * getNotificationData() const
Definition: BgpLayer.h:146
void getOptionalParameters(std::vector< optional_parameter > &optionalParameters)
BgpOpenMessageLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: BgpLayer.h:203
BgpMessageType getBgpMessageType() const
Definition: BgpLayer.h:275
void setBgpId(const IPv4Address &newBgpId)
bool setOptionalParameters(const std::vector< optional_parameter > &optionalParameters)
BgpOpenMessageLayer(uint16_t myAutonomousSystem, uint16_t holdTime, const IPv4Address &bgpId, const std::vector< optional_parameter > &optionalParams=std::vector< optional_parameter >())
bgp_open_message * getOpenMsgHeader() const
Definition: BgpLayer.h:223
IPv4Address getBgpId() const
Definition: BgpLayer.h:231
size_t getOptionalParametersLength()
Definition: BgpLayer.h:671
BgpRouteRefreshMessageLayer(uint16_t afi, uint8_t safi)
BgpMessageType getBgpMessageType() const
Definition: BgpLayer.h:719
BgpRouteRefreshMessageLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: BgpLayer.h:696
bgp_route_refresh_message * getRouteRefreshHeader() const
Definition: BgpLayer.h:712
Definition: BgpLayer.h:290
void getPathAttributes(std::vector< path_attribute > &pathAttributes)
BgpMessageType getBgpMessageType() const
Definition: BgpLayer.h:483
BgpUpdateMessageLayer(const std::vector< prefix_and_ip > &withdrawnRoutes=std::vector< prefix_and_ip >(), const std::vector< path_attribute > &pathAttributes=std::vector< path_attribute >(), const std::vector< prefix_and_ip > &nlri=std::vector< prefix_and_ip >())
static bool isDataValid(const uint8_t *data, size_t dataSize)
void getWithdrawnRoutes(std::vector< prefix_and_ip > &withdrawnRoutes)
bool setPathAttributes(const std::vector< path_attribute > &pathAttributes)
bool setNetworkLayerReachabilityInfo(const std::vector< prefix_and_ip > &nlri)
size_t getNetworkLayerReachabilityInfoLength() const
bool setWithdrawnRoutes(const std::vector< prefix_and_ip > &withdrawnRoutes)
size_t getPathAttributesLength() const
size_t getWithdrawnRoutesLength() const
bgp_common_header * getBasicMsgHeader() const
Definition: BgpLayer.h:389
void getNetworkLayerReachabilityInfo(std::vector< prefix_and_ip > &nlri)
BgpUpdateMessageLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: BgpLayer.h:358
Definition: IpAddress.h:32
Definition: Layer.h:70
Definition: Packet.h:27
The main namespace for the PcapPlusPlus lib.
const ProtocolType BGP
Definition: ProtocolType.h:233
OsiModelLayer
Definition: ProtocolType.h:354
@ OsiModelApplicationLayer
Definition: ProtocolType.h:368
Definition: BgpLayer.h:53
uint8_t messageType
Definition: BgpLayer.h:59
uint8_t marker[16]
Definition: BgpLayer.h:55
uint16_t length
Definition: BgpLayer.h:57
uint16_t holdTime
Definition: BgpLayer.h:160
uint8_t version
Definition: BgpLayer.h:156
uint32_t bgpId
Definition: BgpLayer.h:162
uint16_t myAutonomousSystem
Definition: BgpLayer.h:158
uint8_t optionalParameterLength
Definition: BgpLayer.h:164
optional_parameter(uint8_t typeVal, const std::string &valueAsHexString)
uint8_t type
Definition: BgpLayer.h:175
uint8_t length
Definition: BgpLayer.h:177
optional_parameter()
Definition: BgpLayer.h:184
uint8_t value[32]
Definition: BgpLayer.h:179
path_attribute()
Definition: BgpLayer.h:337
uint8_t type
Definition: BgpLayer.h:328
uint8_t flags
Definition: BgpLayer.h:326
uint8_t data[32]
Definition: BgpLayer.h:332
path_attribute(uint8_t flagsVal, uint8_t typeVal, const std::string &dataAsHexString)
uint8_t length
Definition: BgpLayer.h:330
uint8_t prefix
Definition: BgpLayer.h:300
prefix_and_ip()
Definition: BgpLayer.h:307
prefix_and_ip(uint8_t prefixVal, const std::string &ipAddrVal)
Definition: BgpLayer.h:315
IPv4Address ipAddr
Definition: BgpLayer.h:302