PcapPlusPlus  Next
PPPoELayer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Layer.h"
4 #include "TLVData.h"
5 #include <vector>
6 #include <string.h>
7 
9 
14 namespace pcpp
15 {
16 
21 #pragma pack(push, 1)
22  struct pppoe_header
23  {
24 #if (BYTE_ORDER == LITTLE_ENDIAN)
26  uint8_t version : 4;
28  uint8_t type : 4;
30  uint8_t code;
31 #else
33  uint16_t version : 4;
35  uint16_t type : 4;
37  uint16_t code : 8;
38 #endif
40  uint16_t sessionId;
42  uint16_t payloadLength;
43  };
44 #pragma pack(pop)
45 
51  class PPPoELayer : public Layer
52  {
53  public:
57  enum PPPoECode
58  {
80  PPPOE_CODE_PADN = 0xd4
81  };
82 
83  ~PPPoELayer() override = default;
84 
91  {
92  return reinterpret_cast<pppoe_header*>(m_Data);
93  }
94 
95  // abstract methods implementation
96 
100  void computeCalculateFields() override;
101 
103  {
104  return OsiModelDataLinkLayer;
105  }
106 
107  protected:
108  // protected c'tor as this class shouldn't be instantiated
109  PPPoELayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, ProtocolType protocol)
110  : Layer(data, dataLen, prevLayer, packet, protocol)
111  {}
112 
113  // protected c'tor as this class shouldn't be instantiated
114  PPPoELayer(uint8_t version, uint8_t type, PPPoELayer::PPPoECode code, uint16_t sessionId,
115  size_t additionalBytesToAllocate = 0);
116  };
117 
123  {
124  public:
132  PPPoESessionLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
133  : PPPoELayer(data, dataLen, prevLayer, packet, PPPoESession)
134  {}
135 
144  PPPoESessionLayer(uint8_t version, uint8_t type, uint16_t sessionId, uint16_t pppNextProtocol)
145  : PPPoELayer(version, type, PPPoELayer::PPPOE_CODE_SESSION, sessionId, sizeof(uint16_t))
146  {
147  setPPPNextProtocol(pppNextProtocol);
148  }
149 
150  ~PPPoESessionLayer() override = default;
151 
157  uint16_t getPPPNextProtocol() const;
158 
163  void setPPPNextProtocol(uint16_t nextProtocol);
164 
171  static inline bool isDataValid(const uint8_t* data, size_t dataLen);
172 
173  // abstract methods implementation
174 
178  void parseNextLayer() override;
179 
183  size_t getHeaderLen() const override
184  {
185  return sizeof(pppoe_header) + sizeof(uint16_t);
186  }
187 
188  std::string toString() const override;
189  };
190 
196  {
197  public:
202  {
204  PPPOE_TAG_EOL = 0x0000,
226  PPPOE_TAG_HURL = 0x0111,
228  PPPOE_TAG_MOTM = 0x0112,
238  PPPOE_TAG_GENERIC_ERR = 0x0203
239  };
240 
245  class PPPoETag : public TLVRecord<uint16_t, uint16_t>
246  {
247  public:
252  explicit PPPoETag(uint8_t* tagRawData) : TLVRecord(tagRawData)
253  {}
254 
258  ~PPPoETag() override = default;
259 
264 
269  std::string getValueAsString() const
270  {
271  size_t dataSize = getDataSize();
272  if (dataSize < 1)
273  return "";
274 
275  return std::string(reinterpret_cast<const char*>(m_Data->recordValue), dataSize);
276  }
277 
278  // implement abstract methods
279 
280  size_t getTotalSize() const override;
281 
282  size_t getDataSize() const override;
283  };
284 
291  {
292  public:
298  explicit PPPoETagBuilder(PPPoETagTypes tagType)
299  : TLVRecordBuilder(static_cast<uint16_t>(tagType), nullptr, 0)
300  {}
301 
308  PPPoETagBuilder(PPPoETagTypes tagType, uint32_t tagValue)
309  : TLVRecordBuilder(static_cast<uint16_t>(tagType), tagValue)
310  {}
311 
319  PPPoETagBuilder(PPPoETagTypes tagType, uint8_t* tagValue, uint8_t tagValueLen)
320  : TLVRecordBuilder(static_cast<uint16_t>(tagType), tagValue, tagValueLen)
321  {}
322 
327  PPPoETag build() const;
328  };
329 
337  PPPoEDiscoveryLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
338  : PPPoELayer(data, dataLen, prevLayer, packet, PPPoEDiscovery)
339  {
340  m_DataLen = getHeaderLen();
341  }
342 
350  PPPoEDiscoveryLayer(uint8_t version, uint8_t type, PPPoELayer::PPPoECode code, uint16_t sessionId)
351  : PPPoELayer(version, type, code, sessionId)
352  {
353  m_Protocol = PPPoEDiscovery;
354  }
355 
363 
369 
377  PPPoETag getNextTag(const PPPoETag& tag) const;
378 
382  int getTagCount() const;
383 
390  PPPoETag addTag(const PPPoETagBuilder& tagBuilder);
391 
399  PPPoETag addTagAfter(const PPPoETagBuilder& tagBuilder, PPPoETagTypes prevTagType);
400 
408 
415 
422  static inline bool isDataValid(const uint8_t* data, size_t dataLen);
423 
424  // abstract methods implementation
425 
429  void parseNextLayer() override
430  {}
431 
435  size_t getHeaderLen() const override;
436 
437  std::string toString() const override
438  {
439  return "PPP-over-Ethernet Discovery (" + codeToString((PPPoELayer::PPPoECode)getPPPoEHeader()->code) + ")";
440  }
441 
442  private:
443  TLVRecordReader<PPPoETag> m_TagReader;
444 
445  PPPoETag addTagAt(const PPPoETagBuilder& tagBuilder, int offset);
446 
447  uint8_t* getTagBasePtr() const
448  {
449  return m_Data + sizeof(pppoe_header);
450  }
451 
452  std::string codeToString(PPPoECode code) const;
453  };
454 
455  // implementation of inline methods
456 
457  bool PPPoESessionLayer::isDataValid(const uint8_t* data, size_t dataLen)
458  {
459  return data && dataLen >= sizeof(pppoe_header) + sizeof(uint16_t);
460  }
461 
462  bool PPPoEDiscoveryLayer::isDataValid(const uint8_t* data, size_t dataLen)
463  {
464  return data && dataLen >= sizeof(pppoe_header);
465  }
466 
467  // Copied from Wireshark: ppptypes.h
468 
470 #define PCPP_PPP_PADDING 0x1
472 #define PCPP_PPP_ROHC_SCID 0x3
474 #define PCPP_PPP_ROHC_LCID 0x5
476 #define PCPP_PPP_IP 0x21
478 #define PCPP_PPP_OSI 0x23
480 #define PCPP_PPP_XNSIDP 0x25
482 #define PCPP_PPP_DEC4 0x27
484 #define PCPP_PPP_AT 0x29
486 #define PCPP_PPP_IPX 0x2b
488 #define PCPP_PPP_VJC_COMP 0x2d
490 #define PCPP_PPP_VJC_UNCOMP 0x2f
492 #define PCPP_PPP_BCP 0x31
494 #define PCPP_PPP_ST 0x33
496 #define PCPP_PPP_VINES 0x35
498 #define PCPP_PPP_AT_EDDP 0x39
500 #define PCPP_PPP_AT_SB 0x3b
502 #define PCPP_PPP_MP 0x3d
504 #define PCPP_PPP_NB 0x3f
506 #define PCPP_PPP_CISCO 0x41
508 #define PCPP_PPP_ASCOM 0x43
510 #define PCPP_PPP_LBLB 0x45
512 #define PCPP_PPP_RL 0x47
514 #define PCPP_PPP_SDTP 0x49
516 #define PCPP_PPP_LLC 0x4b
518 #define PCPP_PPP_SNA 0x4d
520 #define PCPP_PPP_IPV6HC 0x4f
522 #define PCPP_PPP_KNX 0x51
524 #define PCPP_PPP_ENCRYPT 0x53
526 #define PCPP_PPP_ILE 0x55
528 #define PCPP_PPP_IPV6 0x57
530 #define PCPP_PPP_MUX 0x59
532 #define PCPP_PPP_VSNP 0x5b
534 #define PCPP_PPP_TNP 0x5d
536 #define PCPP_PPP_RTP_FH 0x61
538 #define PCPP_PPP_RTP_CTCP 0x63
540 #define PCPP_PPP_RTP_CNTCP 0x65
542 #define PCPP_PPP_RTP_CUDP8 0x67
544 #define PCPP_PPP_RTP_CRTP8 0x69
546 #define PCPP_PPP_STAMPEDE 0x6f
548 #define PCPP_PPP_MPPLUS 0x73
550 #define PCPP_PPP_NTCITS_IPI 0xc1
552 #define PCPP_PPP_ML_SLCOMP 0xfb
554 #define PCPP_PPP_COMP 0xfd
556 #define PCPP_PPP_STP_HELLO 0x0201
558 #define PCPP_PPP_IBM_SR 0x0203
560 #define PCPP_PPP_DEC_LB 0x0205
562 #define PCPP_PPP_CDP 0x0207
564 #define PCPP_PPP_NETCS 0x0209
566 #define PCPP_PPP_STP 0x020b
568 #define PCPP_PPP_EDP 0x020d
570 #define PCPP_PPP_OSCP 0x0211
572 #define PCPP_PPP_OSCP2 0x0213
574 #define PCPP_PPP_LUXCOM 0x0231
576 #define PCPP_PPP_SIGMA 0x0233
578 #define PCPP_PPP_ACSP 0x0235
580 #define PCPP_PPP_MPLS_UNI 0x0281
582 #define PCPP_PPP_MPLS_MULTI 0x0283
584 #define PCPP_PPP_P12844 0x0285
586 #define PCPP_PPP_TETRA 0x0287
588 #define PCPP_PPP_MFTP 0x0289
590 #define PCPP_PPP_RTP_CTCPND 0x2063
592 #define PCPP_PPP_RTP_CS 0x2065
594 #define PCPP_PPP_RTP_CUDP16 0x2067
596 #define PCPP_PPP_RTP_CRDP16 0x2069
598 #define PCPP_PPP_CCCP 0x4001
600 #define PCPP_PPP_CDPD_MNRP 0x4003
602 #define PCPP_PPP_EXPANDAP 0x4005
604 #define PCPP_PPP_ODSICP 0x4007
606 #define PCPP_PPP_DOCSIS 0x4009
608 #define PCPP_PPP_CETACEANNDP 0x400b
610 #define PCPP_PPP_LZS 0x4021
612 #define PCPP_PPP_REFTEK 0x4023
614 #define PCPP_PPP_FC 0x4025
616 #define PCPP_PPP_EMIT 0x4027
618 #define PCPP_PPP_VSP 0x405b
620 #define PCPP_PPP_TLSP 0x405d
622 #define PCPP_PPP_IPCP 0x8021
624 #define PCPP_PPP_OSINLCP 0x8023
626 #define PCPP_PPP_XNSIDPCP 0x8025
628 #define PCPP_PPP_DECNETCP 0x8027
630 #define PCPP_PPP_ATCP 0x8029
632 #define PCPP_PPP_IPXCP 0x802b
634 #define PCPP_PPP_BRIDGENCP 0x8031
636 #define PCPP_PPP_SPCP 0x8033
638 #define PCPP_PPP_BVCP 0x8035
640 #define PCPP_PPP_MLCP 0x803d
642 #define PCPP_PPP_NBCP 0x803f
644 #define PCPP_PPP_CISCOCP 0x8041
646 #define PCPP_PPP_ASCOMCP 0x8043
648 #define PCPP_PPP_LBLBCP 0x8045
650 #define PCPP_PPP_RLNCP 0x8047
652 #define PCPP_PPP_SDCP 0x8049
654 #define PCPP_PPP_LLCCP 0x804b
656 #define PCPP_PPP_SNACP 0x804d
658 #define PCPP_PPP_IP6HCCP 0x804f
660 #define PCPP_PPP_KNXCP 0x8051
662 #define PCPP_PPP_ECP 0x8053
664 #define PCPP_PPP_ILECP 0x8055
666 #define PCPP_PPP_IPV6CP 0x8057
668 #define PCPP_PPP_MUXCP 0x8059
670 #define PCPP_PPP_VSNCP 0x805b
672 #define PCPP_PPP_TNCP 0x805d
674 #define PCPP_PPP_STAMPEDECP 0x806f
676 #define PCPP_PPP_MPPCP 0x8073
678 #define PCPP_PPP_IPICP 0x80c1
680 #define PCPP_PPP_SLCC 0x80fb
682 #define PCPP_PPP_CCP 0x80fd
684 #define PCPP_PPP_CDPCP 0x8207
686 #define PCPP_PPP_NETCSCP 0x8209
688 #define PCPP_PPP_STPCP 0x820b
690 #define PCPP_PPP_EDPCP 0x820d
692 #define PCPP_PPP_ACSPC 0x8235
694 #define PCPP_PPP_MPLSCP 0x8281
696 #define PCPP_PPP_P12844CP 0x8285
698 #define PCPP_PPP_TETRACP 0x8287
700 #define PCPP_PPP_MFTPCP 0x8289
702 #define PCPP_PPP_LCP 0xc021
704 #define PCPP_PPP_PAP 0xc023
706 #define PCPP_PPP_LQR 0xc025
708 #define PCPP_PPP_SPAP 0xc027
710 #define PCPP_PPP_CBCP 0xc029
712 #define PCPP_PPP_BACP 0xc02b
714 #define PCPP_PPP_BAP 0xc02d
716 #define PCPP_PPP_VSAP 0xc05b
718 #define PCPP_PPP_CONTCP 0xc081
720 #define PCPP_PPP_CHAP 0xc223
722 #define PCPP_PPP_RSAAP 0xc225
724 #define PCPP_PPP_EAP 0xc227
726 #define PCPP_PPP_SIEP 0xc229
728 #define PCPP_PPP_SBAP 0xc26f
730 #define PCPP_PPP_PRPAP 0xc281
732 #define PCPP_PPP_PRPAP2 0xc283
734 #define PCPP_PPP_PRPNIAP 0xc481
735 
736 } // namespace pcpp
Definition: Layer.h:69
Definition: PPPoELayer.h:291
PPPoETagBuilder(PPPoETagTypes tagType)
Definition: PPPoELayer.h:298
PPPoETagBuilder(PPPoETagTypes tagType, uint8_t *tagValue, uint8_t tagValueLen)
Definition: PPPoELayer.h:319
PPPoETagBuilder(PPPoETagTypes tagType, uint32_t tagValue)
Definition: PPPoELayer.h:308
Definition: PPPoELayer.h:246
std::string getValueAsString() const
Definition: PPPoELayer.h:269
PPPoEDiscoveryLayer::PPPoETagTypes getType() const
PPPoETag(uint8_t *tagRawData)
Definition: PPPoELayer.h:252
size_t getDataSize() const override
size_t getTotalSize() const override
Definition: PPPoELayer.h:196
PPPoETag addTag(const PPPoETagBuilder &tagBuilder)
PPPoEDiscoveryLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: PPPoELayer.h:337
size_t getHeaderLen() const override
PPPoETag getFirstTag() const
PPPoETag addTagAfter(const PPPoETagBuilder &tagBuilder, PPPoETagTypes prevTagType)
std::string toString() const override
Definition: PPPoELayer.h:437
bool removeTag(PPPoEDiscoveryLayer::PPPoETagTypes tagType)
PPPoEDiscoveryLayer(uint8_t version, uint8_t type, PPPoELayer::PPPoECode code, uint16_t sessionId)
Definition: PPPoELayer.h:350
void parseNextLayer() override
Definition: PPPoELayer.h:429
PPPoETagTypes
Definition: PPPoELayer.h:202
@ PPPOE_TAG_SVC_ERR
Definition: PPPoELayer.h:234
@ PPPOE_TAG_HURL
Definition: PPPoELayer.h:226
@ PPPOE_TAG_EOL
Definition: PPPoELayer.h:204
@ PPPOE_TAG_CRED_SCALE
Definition: PPPoELayer.h:222
@ PPPOE_TAG_METRICS
Definition: PPPoELayer.h:218
@ PPPOE_TAG_RELAY_ID
Definition: PPPoELayer.h:224
@ PPPOE_TAG_GENERIC_ERR
Definition: PPPoELayer.h:238
@ PPPOE_TAG_SVC_NAME
Definition: PPPoELayer.h:206
@ PPPOE_TAG_CREDITS
Definition: PPPoELayer.h:216
@ PPPOE_TAG_AC_NAME
Definition: PPPoELayer.h:208
@ PPPOE_TAG_MOTM
Definition: PPPoELayer.h:228
@ PPPOE_TAG_SEQ_NUM
Definition: PPPoELayer.h:220
@ PPPOE_TAG_HOST_UNIQ
Definition: PPPoELayer.h:210
@ PPPOE_TAG_MAX_PAYLD
Definition: PPPoELayer.h:230
@ PPPOE_TAG_IP_RT_ADD
Definition: PPPoELayer.h:232
@ PPPOE_TAG_AC_COOKIE
Definition: PPPoELayer.h:212
@ PPPOE_TAG_AC_ERR
Definition: PPPoELayer.h:236
@ PPPOE_TAG_VENDOR
Definition: PPPoELayer.h:214
PPPoETag getNextTag(const PPPoETag &tag) const
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: PPPoELayer.h:462
PPPoETag getTag(PPPoEDiscoveryLayer::PPPoETagTypes tagType) const
Definition: PPPoELayer.h:52
OsiModelLayer getOsiModelLayer() const override
Definition: PPPoELayer.h:102
PPPoECode
Definition: PPPoELayer.h:58
@ PPPOE_CODE_PADR
Definition: PPPoELayer.h:72
@ PPPOE_CODE_PADQ
Definition: PPPoELayer.h:70
@ PPPOE_CODE_PADI
Definition: PPPoELayer.h:64
@ PPPOE_CODE_PADS
Definition: PPPoELayer.h:74
@ PPPOE_CODE_PADN
Definition: PPPoELayer.h:80
@ PPPOE_CODE_PADC
Definition: PPPoELayer.h:68
@ PPPOE_CODE_SESSION
Definition: PPPoELayer.h:60
@ PPPOE_CODE_PADM
Definition: PPPoELayer.h:78
@ PPPOE_CODE_PADT
Definition: PPPoELayer.h:76
@ PPPOE_CODE_PADO
Definition: PPPoELayer.h:62
@ PPPOE_CODE_PADG
Definition: PPPoELayer.h:66
void computeCalculateFields() override
pppoe_header * getPPPoEHeader() const
Definition: PPPoELayer.h:90
Definition: PPPoELayer.h:123
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: PPPoELayer.h:457
PPPoESessionLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: PPPoELayer.h:132
std::string toString() const override
uint16_t getPPPNextProtocol() const
PPPoESessionLayer(uint8_t version, uint8_t type, uint16_t sessionId, uint16_t pppNextProtocol)
Definition: PPPoELayer.h:144
void parseNextLayer() override
void setPPPNextProtocol(uint16_t nextProtocol)
size_t getHeaderLen() const override
Definition: PPPoELayer.h:183
Definition: Packet.h:27
Definition: TLVData.h:419
Definition: TLVData.h:23
Definition: TLVData.h:246
The main namespace for the PcapPlusPlus lib.
uint8_t ProtocolType
Definition: ProtocolType.h:17
const ProtocolType PPPoESession
Definition: ProtocolType.h:93
OsiModelLayer
Definition: ProtocolType.h:364
@ OsiModelDataLinkLayer
Definition: ProtocolType.h:368
const ProtocolType PPPoEDiscovery
Definition: ProtocolType.h:98
Definition: PPPoELayer.h:23
uint16_t payloadLength
Definition: PPPoELayer.h:42
uint8_t type
Definition: PPPoELayer.h:28
uint8_t version
Definition: PPPoELayer.h:26
uint8_t code
Definition: PPPoELayer.h:30
uint16_t sessionId
Definition: PPPoELayer.h:40