PcapPlusPlus  23.09
StpLayer.h
Go to the documentation of this file.
1 #ifndef PACKETPP_STP_LAYER
2 #define PACKETPP_STP_LAYER
3 
4 #include "Layer.h"
5 #include "MacAddress.h"
6 
8 
13 namespace pcpp
14 {
15 
20 #pragma pack(push, 1)
21  struct stp_tcn_bpdu
22  {
24  uint16_t protoId;
26  uint8_t version;
28  uint8_t type;
29  };
30 #pragma pack(pop)
31 
34 
39 #pragma pack(push, 1)
41  {
43  uint8_t flag;
45  uint64_t rootId;
47  uint32_t pathCost;
49  uint64_t bridgeId;
51  uint16_t portId;
53  uint16_t msgAge;
55  uint16_t maxAge;
57  uint16_t helloTime;
59  uint16_t forwardDelay;
60  };
61 #pragma pack(pop)
62 
67 #pragma pack(push, 1)
69  {
71  uint8_t version1Len;
72  };
73 #pragma pack(pop)
74 
79 #pragma pack(push, 1)
81  {
83  uint16_t version3Len;
87  uint8_t mstConfigName[32];
91  uint8_t mstConfigDigest[16];
93  uint32_t irpc;
95  uint64_t cistBridgeId;
97  uint8_t remainId;
98  };
99 #pragma pack(pop)
100 
105 #pragma pack(push, 1)
107  {
109  uint8_t flags;
111  uint64_t regionalRootId;
113  uint32_t pathCost;
115  uint8_t bridgePriority;
117  uint8_t portPriority;
119  uint8_t remainingHops;
120  };
121 #pragma pack(pop)
122 
127  class StpLayer : public Layer
128  {
129  protected:
130  StpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
131  : Layer(data, dataLen, prevLayer, packet)
132  {
133  m_Protocol = STP;
134  }
135 
136  explicit StpLayer(size_t dataLen)
137  {
138  m_DataLen = dataLen;
139  m_Data = new uint8_t[dataLen];
140  memset(m_Data, 0, dataLen);
141  m_Protocol = STP;
142  }
143 
144  static pcpp::MacAddress IDtoMacAddress(uint64_t id);
145  static uint64_t macAddressToID(const pcpp::MacAddress &addr);
146 
147  public:
152 
157  stp_header *getStpHeader() const { return (stp_header *)(m_Data); }
158 
163  uint16_t getProtoId() const { return getStpHeader()->protoId; }
164 
169  void setProtoId(uint16_t value) { getStpHeader()->protoId = value; }
170 
175  uint8_t getVersion() const { return getStpHeader()->version; }
176 
181  void setVersion(uint8_t value) { getStpHeader()->version = value; }
182 
187  uint8_t getType() const { return getStpHeader()->type; }
188 
193  void setType(uint8_t value) { getStpHeader()->type = value; }
194 
195  // overridden methods
196 
200  size_t getHeaderLen() const { return m_DataLen; }
201 
204 
209 
216  static bool isDataValid(const uint8_t *data, size_t dataLen);
217 
227  static StpLayer *parseStpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet);
228  };
229 
235  {
236  protected:
237  explicit StpTopologyChangeBPDULayer(size_t dataLen) : StpLayer(dataLen) {}
238 
239  public:
247  StpTopologyChangeBPDULayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
248  : StpLayer(data, dataLen, prevLayer, packet)
249  {
250  }
251 
257 
262  stp_tcn_bpdu* getStpTcnHeader() { return getStpHeader(); }
263 
264  // overridden methods
265 
269  size_t getHeaderLen() const { return sizeof(stp_tcn_bpdu); }
270 
272  void parseNextLayer();
273 
277  std::string toString() const { return "Spanning Tree Topology Change Notification"; }
278 
285  static bool isDataValid(const uint8_t *data, size_t dataLen) { return data && dataLen >= sizeof(stp_tcn_bpdu); }
286  };
287 
293  {
294  protected:
295  explicit StpConfigurationBPDULayer(size_t dataLen) : StpTopologyChangeBPDULayer(dataLen) {}
296 
297  public:
305  StpConfigurationBPDULayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
306  : StpTopologyChangeBPDULayer(data, dataLen, prevLayer, packet)
307  {
308  }
309 
315 
320  stp_conf_bpdu *getStpConfHeader() const { return (stp_conf_bpdu *)(m_Data); }
321 
326  uint8_t getFlag() const { return getStpConfHeader()->flag; }
327 
332  void setFlag(uint8_t value) { getStpConfHeader()->flag = value; }
333 
338  uint64_t getRootId() const;
339 
344  void setRootId(uint64_t value);
345 
350  uint16_t getRootPriority() const;
351 
356  void setRootPriority(uint16_t value);
357 
362  uint16_t getRootSystemIDExtension() const;
363 
368  void setRootSystemIDExtension(uint16_t value);
369 
374  pcpp::MacAddress getRootSystemID() const { return IDtoMacAddress(getRootId()); }
375 
380  void setRootSystemID(const pcpp::MacAddress &value);
381 
386  uint32_t getPathCost() const;
387 
392  void setPathCost(uint32_t value);
393 
398  uint64_t getBridgeId() const;
399 
404  void setBridgeId(uint64_t value);
405 
410  uint16_t getBridgePriority() const;
411 
416  void setBridgePriority(uint16_t value);
417 
422  uint16_t getBridgeSystemIDExtension() const;
423 
428  void setBridgeSystemIDExtension(uint16_t value);
429 
434  pcpp::MacAddress getBridgeSystemID() const { return IDtoMacAddress(getBridgeId()); }
435 
440  void setBridgeSystemID(const pcpp::MacAddress &value);
441 
446  uint16_t getPortId() const;
447 
452  void setPortId(uint16_t value);
453 
458  double getMessageAge() const;
459 
464  void setMessageAge(double value);
465 
470  double getMaximumAge() const;
471 
476  void setMaximumAge(double value);
477 
482  double getTransmissionInterval() const;
483 
488  void setTransmissionInterval(double value);
489 
494  double getForwardDelay() const;
495 
500  void setForwardDelay(double value);
501 
502  // overridden methods
503 
507  size_t getHeaderLen() const { return sizeof(stp_conf_bpdu); }
508 
510  void parseNextLayer();
511 
515  std::string toString() const { return "Spanning Tree Configuration"; }
516 
523  static bool isDataValid(const uint8_t *data, size_t dataLen)
524  {
525  return data && dataLen >= sizeof(stp_conf_bpdu);
526  }
527  };
528 
534  {
535  protected:
536  explicit RapidStpLayer(size_t dataLen) : StpConfigurationBPDULayer(dataLen) {}
537 
538  public:
546  RapidStpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
547  : StpConfigurationBPDULayer(data, dataLen, prevLayer, packet)
548  {
549  }
550 
555  RapidStpLayer();
556 
561  rstp_conf_bpdu *getRstpConfHeader() const { return (rstp_conf_bpdu *)(m_Data); }
562 
567  uint8_t getVersion1Len() const { return getRstpConfHeader()->version1Len; }
568 
573  void setVersion1Len(uint8_t value) { getRstpConfHeader()->version1Len = value; }
574 
575  // overridden methods
576 
580  size_t getHeaderLen() const { return sizeof(rstp_conf_bpdu); }
581 
583  void parseNextLayer();
584 
588  std::string toString() const { return "Rapid Spanning Tree"; }
589 
596  static bool isDataValid(const uint8_t *data, size_t dataLen)
597  {
598  return data && dataLen >= sizeof(rstp_conf_bpdu);
599  }
600  };
601 
607  {
608  public:
616  MultipleStpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
617  : RapidStpLayer(data, dataLen, prevLayer, packet)
618  {
619  }
620 
626 
631  mstp_conf_bpdu *getMstpHeader() const { return (mstp_conf_bpdu *)(m_Data); }
632 
636  uint16_t getVersion3Len() const;
637 
642  void setVersion3Len(uint16_t value);
643 
648  uint8_t getMstConfigurationFormatSelector() const { return getMstpHeader()->mstConfigFormatSelector; }
649 
654  void setMstConfigurationFormatSelector(uint8_t value) { getMstpHeader()->mstConfigFormatSelector = value; }
655 
660  std::string getMstConfigurationName() const;
661 
667  void setMstConfigurationName(const std::string &value);
668 
673  uint16_t getMstConfigRevision() const;
674 
679  void setMstConfigRevision(uint16_t value);
680 
685  uint8_t *getMstConfigDigest() const { return getMstpHeader()->mstConfigDigest; }
686 
692  void setMstConfigDigest(const uint8_t *value, uint8_t len);
693 
698  uint32_t getCISTIrpc() const;
699 
704  void setCISTIrpc(uint32_t value);
705 
710  uint64_t getCISTBridgeId() const;
711 
716  void setCISTBridgeId(uint64_t value);
717 
722  uint16_t getCISTBridgePriority() const;
723 
728  void setCISTBridgePriority(uint16_t value);
729 
734  uint16_t getCISTBridgeSystemIDExtension() const;
735 
740  void setCISTBridgeSystemIDExtension(uint16_t value);
741 
746  pcpp::MacAddress getCISTBridgeSystemID() const { return IDtoMacAddress(getCISTBridgeId()); }
747 
752  void setCISTBridgeSystemID(const pcpp::MacAddress &value);
753 
758  uint8_t getRemainingHopCount() const { return getMstpHeader()->remainId; }
759 
764  void setRemainingHopCount(uint8_t value) { getMstpHeader()->remainId = value; }
765 
770  uint8_t getNumberOfMSTIConfMessages() const { return (getVersion3Len() - (sizeof(mstp_conf_bpdu) - sizeof(rstp_conf_bpdu) - sizeof(uint16_t))) / sizeof(msti_conf_msg); }
771 
777  msti_conf_msg *getMstiConfMessages() const;
778 
779  // overridden methods
780 
782  void parseNextLayer() {}
783 
787  std::string toString() const { return "Multiple Spanning Tree"; }
788 
795  static bool isDataValid(const uint8_t *data, size_t dataLen)
796  {
797  return data && dataLen >= sizeof(mstp_conf_bpdu);
798  }
799  };
800 } // namespace pcpp
801 
802 #endif /* PACKETPP_STP_LAYER */
Definition: StpLayer.h:80
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:333
Definition: StpLayer.h:606
uint16_t maxAge
Maximum age of the BPDU.
Definition: StpLayer.h:55
OsiModelLayer getOsiModelLayer() const
Definition: StpLayer.h:208
RapidStpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: StpLayer.h:546
uint32_t pathCost
Cost of path.
Definition: StpLayer.h:47
void setProtoId(uint16_t value)
Definition: StpLayer.h:169
uint8_t getNumberOfMSTIConfMessages() const
Definition: StpLayer.h:770
void parseNextLayer()
Parses next layer.
Definition: StpLayer.h:782
uint16_t helloTime
BPDU transmission interval.
Definition: StpLayer.h:57
Definition: StpLayer.h:40
void setRemainingHopCount(uint8_t value)
Definition: StpLayer.h:764
void setVersion(uint8_t value)
Definition: StpLayer.h:181
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: StpLayer.h:596
stp_tcn_bpdu stp_header
Spanning Tree protocol common header.
Definition: StpLayer.h:33
Definition: ProtocolType.h:338
uint8_t remainId
CIST remaining hop count.
Definition: StpLayer.h:97
size_t getHeaderLen() const
Definition: StpLayer.h:507
Definition: Layer.h:70
uint8_t flags
MSTI flags.
Definition: StpLayer.h:109
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: StpLayer.h:795
uint16_t portId
Port ID.
Definition: StpLayer.h:51
Definition: StpLayer.h:292
Definition: Packet.h:26
size_t getHeaderLen() const
Definition: StpLayer.h:580
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: StpLayer.h:523
const ProtocolType STP
Definition: ProtocolType.h:278
uint8_t * getMstConfigDigest() const
Definition: StpLayer.h:685
void setVersion1Len(uint8_t value)
Definition: StpLayer.h:573
stp_conf_bpdu * getStpConfHeader() const
Definition: StpLayer.h:320
StpTopologyChangeBPDULayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: StpLayer.h:247
Definition: StpLayer.h:127
StpConfigurationBPDULayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: StpLayer.h:305
uint64_t regionalRootId
Regional root switching id (Priority (4 bits) + ID (12 bits) + Regional root (48 bits - MAC address))...
Definition: StpLayer.h:111
uint8_t getType() const
Definition: StpLayer.h:187
Definition: StpLayer.h:68
void setMstConfigurationFormatSelector(uint8_t value)
Definition: StpLayer.h:654
size_t getHeaderLen() const
Definition: StpLayer.h:269
void setType(uint8_t value)
Definition: StpLayer.h:193
std::string toString() const
Definition: StpLayer.h:787
void computeCalculateFields()
Does nothing for this layer.
Definition: StpLayer.h:203
uint8_t getVersion() const
Definition: StpLayer.h:175
uint8_t getVersion1Len() const
Definition: StpLayer.h:567
std::string toString() const
Definition: StpLayer.h:588
uint8_t portPriority
Priority value of port.
Definition: StpLayer.h:117
uint16_t mstConfigRevision
Configuration id revision.
Definition: StpLayer.h:89
Definition: StpLayer.h:21
uint64_t cistBridgeId
CIST bridge id.
Definition: StpLayer.h:95
uint8_t version1Len
Version1 length. The value is 0x0.
Definition: StpLayer.h:71
Definition: StpLayer.h:533
Definition: StpLayer.h:234
uint32_t pathCost
Total path cost from local port to regional port.
Definition: StpLayer.h:113
uint16_t msgAge
Age of the BPDU.
Definition: StpLayer.h:53
uint16_t version3Len
Version3 length.
Definition: StpLayer.h:83
stp_tcn_bpdu * getStpTcnHeader()
Definition: StpLayer.h:262
pcpp::MacAddress getRootSystemID() const
Definition: StpLayer.h:374
uint16_t protoId
Protocol ID. Fixed at 0x0, which represents IEEE 802.1d.
Definition: StpLayer.h:24
std::string toString() const
Definition: StpLayer.h:515
std::string toString() const
Definition: StpLayer.h:277
uint8_t version
Protocol version. 0x0 for STP, 0x2 for RSTP, 0x3 for MSTP.
Definition: StpLayer.h:26
uint8_t type
Type of the BPDU. 0x0 for configuration, 0x2 for RSTP/MSTP, 0x80 for TCN.
Definition: StpLayer.h:28
MultipleStpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: StpLayer.h:616
pcpp::MacAddress getBridgeSystemID() const
Definition: StpLayer.h:434
uint64_t bridgeId
Bridge ID.
Definition: StpLayer.h:49
uint64_t rootId
Root bridge ID.
Definition: StpLayer.h:45
uint8_t getFlag() const
Definition: StpLayer.h:326
uint16_t getProtoId() const
Definition: StpLayer.h:163
Definition: StpLayer.h:106
static pcpp::MacAddress StpMulticastDstMAC
STP protocol uses "01:80:C2:00:00:00" multicast address as destination MAC.
Definition: StpLayer.h:149
mstp_conf_bpdu * getMstpHeader() const
Definition: StpLayer.h:631
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: StpLayer.h:285
uint8_t mstConfigFormatSelector
Configuration id format selector.
Definition: StpLayer.h:85
uint8_t remainingHops
Remaining hops of BPDU.
Definition: StpLayer.h:119
rstp_conf_bpdu * getRstpConfHeader() const
Definition: StpLayer.h:561
uint8_t getRemainingHopCount() const
Definition: StpLayer.h:758
pcpp::MacAddress getCISTBridgeSystemID() const
Definition: StpLayer.h:746
uint32_t irpc
CIST internal root path cost.
Definition: StpLayer.h:93
static pcpp::MacAddress StpUplinkFastMulticastDstMAC
STP Uplink Fast protocol uses "01:00:0C:CD:CD:CD" as destination MAC.
Definition: StpLayer.h:151
void setFlag(uint8_t value)
Definition: StpLayer.h:332
uint8_t flag
Flag for indicate purpose of BPDU.
Definition: StpLayer.h:43
uint8_t bridgePriority
Priority value of switching device.
Definition: StpLayer.h:115
Definition: MacAddress.h:28
stp_header * getStpHeader() const
Definition: StpLayer.h:157
size_t getHeaderLen() const
Definition: StpLayer.h:200
uint8_t getMstConfigurationFormatSelector() const
Definition: StpLayer.h:648
uint16_t forwardDelay
Delay for STP.
Definition: StpLayer.h:59