PcapPlusPlus  24.09
StpLayer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Layer.h"
4 #include "MacAddress.h"
5 
7 
12 namespace pcpp
13 {
14 
19 #pragma pack(push, 1)
20  struct stp_tcn_bpdu
21  {
23  uint16_t protoId;
25  uint8_t version;
27  uint8_t type;
28  };
29 #pragma pack(pop)
30 
33 
38 #pragma pack(push, 1)
40  {
42  uint8_t flag;
44  uint64_t rootId;
46  uint32_t pathCost;
48  uint64_t bridgeId;
50  uint16_t portId;
52  uint16_t msgAge;
54  uint16_t maxAge;
56  uint16_t helloTime;
58  uint16_t forwardDelay;
59  };
60 #pragma pack(pop)
61 
66 #pragma pack(push, 1)
68  {
70  uint8_t version1Len;
71  };
72 #pragma pack(pop)
73 
78 #pragma pack(push, 1)
80  {
82  uint16_t version3Len;
86  uint8_t mstConfigName[32];
90  uint8_t mstConfigDigest[16];
92  uint32_t irpc;
94  uint64_t cistBridgeId;
96  uint8_t remainId;
97  };
98 #pragma pack(pop)
99 
104 #pragma pack(push, 1)
106  {
108  uint8_t flags;
110  uint64_t regionalRootId;
112  uint32_t pathCost;
114  uint8_t bridgePriority;
116  uint8_t portPriority;
118  uint8_t remainingHops;
119  };
120 #pragma pack(pop)
121 
126  class StpLayer : public Layer
127  {
128  protected:
129  StpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
130  : Layer(data, dataLen, prevLayer, packet, STP)
131  {}
132 
133  explicit StpLayer(size_t dataLen)
134  {
135  m_DataLen = dataLen;
136  m_Data = new uint8_t[dataLen];
137  memset(m_Data, 0, dataLen);
138  m_Protocol = STP;
139  }
140 
141  static pcpp::MacAddress IDtoMacAddress(uint64_t id);
142  static uint64_t macAddressToID(const pcpp::MacAddress& addr);
143 
144  public:
149 
155  {
156  return (stp_header*)(m_Data);
157  }
158 
163  uint16_t getProtoId() const
164  {
165  return getStpHeader()->protoId;
166  }
167 
172  void setProtoId(uint16_t value)
173  {
174  getStpHeader()->protoId = value;
175  }
176 
181  uint8_t getVersion() const
182  {
183  return getStpHeader()->version;
184  }
185 
190  void setVersion(uint8_t value)
191  {
192  getStpHeader()->version = value;
193  }
194 
199  uint8_t getType() const
200  {
201  return getStpHeader()->type;
202  }
203 
208  void setType(uint8_t value)
209  {
210  getStpHeader()->type = value;
211  }
212 
213  // overridden methods
214 
218  size_t getHeaderLen() const
219  {
220  return m_DataLen;
221  }
222 
225  {}
226 
231  {
232  return OsiModelDataLinkLayer;
233  }
234 
241  static bool isDataValid(const uint8_t* data, size_t dataLen);
242 
252  static StpLayer* parseStpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
253  };
254 
260  {
261  protected:
262  explicit StpTopologyChangeBPDULayer(size_t dataLen) : StpLayer(dataLen)
263  {}
264 
265  public:
273  StpTopologyChangeBPDULayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
274  : StpLayer(data, dataLen, prevLayer, packet)
275  {}
276 
282 
288  {
289  return getStpHeader();
290  }
291 
292  // overridden methods
293 
297  size_t getHeaderLen() const
298  {
299  return sizeof(stp_tcn_bpdu);
300  }
301 
304 
308  std::string toString() const
309  {
310  return "Spanning Tree Topology Change Notification";
311  }
312 
319  static bool isDataValid(const uint8_t* data, size_t dataLen)
320  {
321  return data && dataLen >= sizeof(stp_tcn_bpdu);
322  }
323  };
324 
330  {
331  protected:
332  explicit StpConfigurationBPDULayer(size_t dataLen) : StpTopologyChangeBPDULayer(dataLen)
333  {}
334 
335  public:
343  StpConfigurationBPDULayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
344  : StpTopologyChangeBPDULayer(data, dataLen, prevLayer, packet)
345  {}
346 
352 
358  {
359  return (stp_conf_bpdu*)(m_Data);
360  }
361 
366  uint8_t getFlag() const
367  {
368  return getStpConfHeader()->flag;
369  }
370 
375  void setFlag(uint8_t value)
376  {
377  getStpConfHeader()->flag = value;
378  }
379 
384  uint64_t getRootId() const;
385 
390  void setRootId(uint64_t value);
391 
396  uint16_t getRootPriority() const;
397 
402  void setRootPriority(uint16_t value);
403 
408  uint16_t getRootSystemIDExtension() const;
409 
414  void setRootSystemIDExtension(uint16_t value);
415 
421  {
422  return IDtoMacAddress(getRootId());
423  }
424 
429  void setRootSystemID(const pcpp::MacAddress& value);
430 
435  uint32_t getPathCost() const;
436 
441  void setPathCost(uint32_t value);
442 
447  uint64_t getBridgeId() const;
448 
453  void setBridgeId(uint64_t value);
454 
459  uint16_t getBridgePriority() const;
460 
465  void setBridgePriority(uint16_t value);
466 
471  uint16_t getBridgeSystemIDExtension() const;
472 
477  void setBridgeSystemIDExtension(uint16_t value);
478 
484  {
485  return IDtoMacAddress(getBridgeId());
486  }
487 
493 
498  uint16_t getPortId() const;
499 
504  void setPortId(uint16_t value);
505 
510  double getMessageAge() const;
511 
516  void setMessageAge(double value);
517 
522  double getMaximumAge() const;
523 
528  void setMaximumAge(double value);
529 
534  double getTransmissionInterval() const;
535 
540  void setTransmissionInterval(double value);
541 
546  double getForwardDelay() const;
547 
552  void setForwardDelay(double value);
553 
554  // overridden methods
555 
559  size_t getHeaderLen() const
560  {
561  return sizeof(stp_conf_bpdu);
562  }
563 
566 
570  std::string toString() const
571  {
572  return "Spanning Tree Configuration";
573  }
574 
581  static bool isDataValid(const uint8_t* data, size_t dataLen)
582  {
583  return data && dataLen >= sizeof(stp_conf_bpdu);
584  }
585  };
586 
592  {
593  protected:
594  explicit RapidStpLayer(size_t dataLen) : StpConfigurationBPDULayer(dataLen)
595  {}
596 
597  public:
605  RapidStpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
606  : StpConfigurationBPDULayer(data, dataLen, prevLayer, packet)
607  {}
608 
614 
620  {
621  return (rstp_conf_bpdu*)(m_Data);
622  }
623 
628  uint8_t getVersion1Len() const
629  {
630  return getRstpConfHeader()->version1Len;
631  }
632 
637  void setVersion1Len(uint8_t value)
638  {
639  getRstpConfHeader()->version1Len = value;
640  }
641 
642  // overridden methods
643 
647  size_t getHeaderLen() const
648  {
649  return sizeof(rstp_conf_bpdu);
650  }
651 
654 
658  std::string toString() const
659  {
660  return "Rapid Spanning Tree";
661  }
662 
669  static bool isDataValid(const uint8_t* data, size_t dataLen)
670  {
671  return data && dataLen >= sizeof(rstp_conf_bpdu);
672  }
673  };
674 
681  {
682  public:
690  MultipleStpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
691  : RapidStpLayer(data, dataLen, prevLayer, packet)
692  {}
693 
699 
705  {
706  return (mstp_conf_bpdu*)(m_Data);
707  }
708 
712  uint16_t getVersion3Len() const;
713 
718  void setVersion3Len(uint16_t value);
719 
725  {
727  }
728 
734  {
736  }
737 
742  std::string getMstConfigurationName() const;
743 
749  void setMstConfigurationName(const std::string& value);
750 
755  uint16_t getMstConfigRevision() const;
756 
761  void setMstConfigRevision(uint16_t value);
762 
767  uint8_t* getMstConfigDigest() const
768  {
769  return getMstpHeader()->mstConfigDigest;
770  }
771 
777  void setMstConfigDigest(const uint8_t* value, uint8_t len);
778 
783  uint32_t getCISTIrpc() const;
784 
789  void setCISTIrpc(uint32_t value);
790 
795  uint64_t getCISTBridgeId() const;
796 
801  void setCISTBridgeId(uint64_t value);
802 
807  uint16_t getCISTBridgePriority() const;
808 
813  void setCISTBridgePriority(uint16_t value);
814 
820 
825  void setCISTBridgeSystemIDExtension(uint16_t value);
826 
832  {
833  return IDtoMacAddress(getCISTBridgeId());
834  }
835 
841 
846  uint8_t getRemainingHopCount() const
847  {
848  return getMstpHeader()->remainId;
849  }
850 
855  void setRemainingHopCount(uint8_t value)
856  {
857  getMstpHeader()->remainId = value;
858  }
859 
865  {
866  return (getVersion3Len() - (sizeof(mstp_conf_bpdu) - sizeof(rstp_conf_bpdu) - sizeof(uint16_t))) /
867  sizeof(msti_conf_msg);
868  }
869 
876 
877  // overridden methods
878 
881  {}
882 
886  std::string toString() const
887  {
888  return "Multiple Spanning Tree";
889  }
890 
897  static bool isDataValid(const uint8_t* data, size_t dataLen)
898  {
899  return data && dataLen >= sizeof(mstp_conf_bpdu);
900  }
901  };
902 } // namespace pcpp
Definition: Layer.h:70
Definition: MacAddress.h:25
Definition: StpLayer.h:681
void setMstConfigRevision(uint16_t value)
uint8_t getNumberOfMSTIConfMessages() const
Definition: StpLayer.h:864
uint32_t getCISTIrpc() const
void setRemainingHopCount(uint8_t value)
Definition: StpLayer.h:855
mstp_conf_bpdu * getMstpHeader() const
Definition: StpLayer.h:704
uint64_t getCISTBridgeId() const
void setCISTBridgeSystemIDExtension(uint16_t value)
uint16_t getCISTBridgePriority() const
uint16_t getMstConfigRevision() const
void setMstConfigDigest(const uint8_t *value, uint8_t len)
uint8_t getMstConfigurationFormatSelector() const
Definition: StpLayer.h:724
uint8_t * getMstConfigDigest() const
Definition: StpLayer.h:767
msti_conf_msg * getMstiConfMessages() const
void setCISTBridgeId(uint64_t value)
void setCISTBridgeSystemID(const pcpp::MacAddress &value)
void setVersion3Len(uint16_t value)
std::string getMstConfigurationName() const
void setMstConfigurationName(const std::string &value)
pcpp::MacAddress getCISTBridgeSystemID() const
Definition: StpLayer.h:831
void parseNextLayer()
Parses next layer.
Definition: StpLayer.h:880
void setCISTBridgePriority(uint16_t value)
void setCISTIrpc(uint32_t value)
uint16_t getCISTBridgeSystemIDExtension() const
MultipleStpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: StpLayer.h:690
uint16_t getVersion3Len() const
void setMstConfigurationFormatSelector(uint8_t value)
Definition: StpLayer.h:733
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: StpLayer.h:897
std::string toString() const
Definition: StpLayer.h:886
uint8_t getRemainingHopCount() const
Definition: StpLayer.h:846
Definition: Packet.h:27
Definition: StpLayer.h:592
void parseNextLayer()
Parses next layer.
uint8_t getVersion1Len() const
Definition: StpLayer.h:628
size_t getHeaderLen() const
Definition: StpLayer.h:647
std::string toString() const
Definition: StpLayer.h:658
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: StpLayer.h:669
rstp_conf_bpdu * getRstpConfHeader() const
Definition: StpLayer.h:619
RapidStpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: StpLayer.h:605
void setVersion1Len(uint8_t value)
Definition: StpLayer.h:637
Definition: StpLayer.h:330
void setBridgePriority(uint16_t value)
StpConfigurationBPDULayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: StpLayer.h:343
uint8_t getFlag() const
Definition: StpLayer.h:366
void setPathCost(uint32_t value)
void setRootId(uint64_t value)
void setForwardDelay(double value)
pcpp::MacAddress getRootSystemID() const
Definition: StpLayer.h:420
void setMaximumAge(double value)
void setRootPriority(uint16_t value)
uint16_t getBridgeSystemIDExtension() const
void setBridgeSystemIDExtension(uint16_t value)
void setBridgeSystemID(const pcpp::MacAddress &value)
void setMessageAge(double value)
double getTransmissionInterval() const
stp_conf_bpdu * getStpConfHeader() const
Definition: StpLayer.h:357
uint16_t getBridgePriority() const
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: StpLayer.h:581
uint16_t getRootSystemIDExtension() const
void setFlag(uint8_t value)
Definition: StpLayer.h:375
void setRootSystemID(const pcpp::MacAddress &value)
std::string toString() const
Definition: StpLayer.h:570
void setRootSystemIDExtension(uint16_t value)
pcpp::MacAddress getBridgeSystemID() const
Definition: StpLayer.h:483
void parseNextLayer()
Parses next layer.
size_t getHeaderLen() const
Definition: StpLayer.h:559
void setTransmissionInterval(double value)
void setBridgeId(uint64_t value)
void setPortId(uint16_t value)
uint16_t getRootPriority() const
Definition: StpLayer.h:127
static StpLayer * parseStpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
uint8_t getVersion() const
Definition: StpLayer.h:181
uint8_t getType() const
Definition: StpLayer.h:199
static pcpp::MacAddress StpUplinkFastMulticastDstMAC
STP Uplink Fast protocol uses "01:00:0C:CD:CD:CD" as destination MAC.
Definition: StpLayer.h:148
void setProtoId(uint16_t value)
Definition: StpLayer.h:172
void setType(uint8_t value)
Definition: StpLayer.h:208
void computeCalculateFields()
Does nothing for this layer.
Definition: StpLayer.h:224
uint16_t getProtoId() const
Definition: StpLayer.h:163
stp_header * getStpHeader() const
Definition: StpLayer.h:154
OsiModelLayer getOsiModelLayer() const
Definition: StpLayer.h:230
static pcpp::MacAddress StpMulticastDstMAC
STP protocol uses "01:80:C2:00:00:00" multicast address as destination MAC.
Definition: StpLayer.h:146
size_t getHeaderLen() const
Definition: StpLayer.h:218
void setVersion(uint8_t value)
Definition: StpLayer.h:190
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: StpLayer.h:260
StpTopologyChangeBPDULayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: StpLayer.h:273
void parseNextLayer()
Parses next layer.
std::string toString() const
Definition: StpLayer.h:308
stp_tcn_bpdu * getStpTcnHeader()
Definition: StpLayer.h:287
size_t getHeaderLen() const
Definition: StpLayer.h:297
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: StpLayer.h:319
The main namespace for the PcapPlusPlus lib.
const ProtocolType STP
Definition: ProtocolType.h:283
OsiModelLayer
Definition: ProtocolType.h:354
@ OsiModelDataLinkLayer
Definition: ProtocolType.h:358
stp_tcn_bpdu stp_header
Spanning Tree protocol common header.
Definition: StpLayer.h:32
Definition: StpLayer.h:106
uint8_t portPriority
Priority value of port.
Definition: StpLayer.h:116
uint8_t bridgePriority
Priority value of switching device.
Definition: StpLayer.h:114
uint8_t flags
MSTI flags.
Definition: StpLayer.h:108
uint8_t remainingHops
Remaining hops of BPDU.
Definition: StpLayer.h:118
uint64_t regionalRootId
Regional root switching id (Priority (4 bits) + ID (12 bits) + Regional root (48 bits - MAC address))
Definition: StpLayer.h:110
uint32_t pathCost
Total path cost from local port to regional port.
Definition: StpLayer.h:112
Definition: StpLayer.h:80
uint8_t mstConfigFormatSelector
Configuration id format selector.
Definition: StpLayer.h:84
uint32_t irpc
CIST internal root path cost.
Definition: StpLayer.h:92
uint16_t version3Len
Version3 length.
Definition: StpLayer.h:82
uint8_t mstConfigName[32]
Configuration id name.
Definition: StpLayer.h:86
uint64_t cistBridgeId
CIST bridge id.
Definition: StpLayer.h:94
uint8_t remainId
CIST remaining hop count.
Definition: StpLayer.h:96
uint8_t mstConfigDigest[16]
Configuration id digest.
Definition: StpLayer.h:90
uint16_t mstConfigRevision
Configuration id revision.
Definition: StpLayer.h:88
Definition: StpLayer.h:68
uint8_t version1Len
Version1 length. The value is 0x0.
Definition: StpLayer.h:70
Definition: StpLayer.h:40
uint8_t flag
Flag for indicate purpose of BPDU.
Definition: StpLayer.h:42
uint64_t rootId
Root bridge ID.
Definition: StpLayer.h:44
uint64_t bridgeId
Bridge ID.
Definition: StpLayer.h:48
uint16_t helloTime
BPDU transmission interval.
Definition: StpLayer.h:56
uint16_t msgAge
Age of the BPDU.
Definition: StpLayer.h:52
uint16_t portId
Port ID.
Definition: StpLayer.h:50
uint16_t maxAge
Maximum age of the BPDU.
Definition: StpLayer.h:54
uint32_t pathCost
Cost of path.
Definition: StpLayer.h:46
uint16_t forwardDelay
Delay for STP.
Definition: StpLayer.h:58
Definition: StpLayer.h:21
uint8_t version
Protocol version. 0x0 for STP, 0x2 for RSTP, 0x3 for MSTP.
Definition: StpLayer.h:25
uint16_t protoId
Protocol ID. Fixed at 0x0, which represents IEEE 802.1d.
Definition: StpLayer.h:23
uint8_t type
Type of the BPDU. 0x0 for configuration, 0x2 for RSTP/MSTP, 0x80 for TCN.
Definition: StpLayer.h:27