PcapPlusPlus  Next
GreLayer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Layer.h"
4 
6 
9 namespace pcpp
10 {
13 #pragma pack(push, 1)
15  {
16 #if (BYTE_ORDER == LITTLE_ENDIAN)
18  uint8_t recursionControl : 3,
24  keyBit : 1,
29 #else
31  uint8_t checksumBit : 1,
33  routingBit : 1,
35  keyBit : 1,
37  sequenceNumBit : 1,
41  recursionControl : 3;
42 #endif
43 #if (BYTE_ORDER == LITTLE_ENDIAN)
45  uint8_t version : 3,
47  flags : 4,
50 #else
52  uint8_t ackSequenceNumBit : 1,
54  flags : 4,
56  version : 3;
57 #endif
58 
60  uint16_t protocol;
61  };
62 #pragma pack(pop)
63  static_assert(sizeof(gre_basic_header) == 4, "gre_basic_header size is not 4 bytes");
64 
67 #pragma pack(push, 1)
69  {
71  uint16_t payloadLength;
73  uint16_t callID;
74  };
75 #pragma pack(pop)
76  static_assert(sizeof(gre1_header) == 8, "gre1_header size is not 8 bytes");
77 
80 #pragma pack(push, 1)
82  {
84  uint8_t address;
86  uint8_t control;
88  uint16_t protocol;
89  };
90 #pragma pack(pop)
91  static_assert(sizeof(ppp_pptp_header) == 4, "ppp_pptp_header size is not 4 bytes");
92 
96  class GreLayer : public Layer
97  {
98  public:
99  ~GreLayer() override = default;
100 
106  static ProtocolType getGREVersion(uint8_t* greData, size_t greDataLen);
107 
112  bool getSequenceNumber(uint32_t& seqNumber) const;
113 
119  bool setSequenceNumber(uint32_t seqNumber);
120 
125 
126  // implement abstract methods
127 
131  void parseNextLayer() override;
132 
134  size_t getHeaderLen() const override;
135 
137  {
138  return OsiModelNetworkLayer;
139  }
140 
145  {
147  static std::vector<FieldDescriptor> all()
148  {
149  auto result = Layer::SerializedFields::all();
150  std::initializer_list<FieldDescriptor> extra{
153  };
154  std::copy(extra.begin(), extra.end(), std::back_inserter(result));
155  return result;
156  }
157 
160 
163 
165  static const FieldDescriptor KeyBit;
166 
169 
172 
175 
177  static const FieldDescriptor Flags;
178 
180  static const FieldDescriptor Version;
181 
183  static const FieldDescriptor Protocol;
184 
187 
188  // cppcheck-suppress duplInheritedMember
190  static constexpr uint16_t MaxID = Layer::SerializedFields::MaxID + 10;
191  };
192 
193  protected:
194  void serializeLayer(ObjectScope& serializer) const override;
195 
196  GreLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, ProtocolType protocol)
197  : Layer(data, dataLen, prevLayer, packet, protocol)
198  {}
199 
200  GreLayer()
201  {}
202 
203  enum GreField
204  {
205  GreChecksumOrRouting = 0,
206  GreKey = 1,
207  GreSeq = 2,
208  GreAck = 3
209  };
210 
211  uint8_t* getFieldValue(GreField field, bool returnOffsetEvenIfFieldMissing) const;
212 
213  void computeCalculateFieldsInner();
214  };
215 
220  class GREv0Layer : public GreLayer
221  {
222  public:
228  GREv0Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
229  : GreLayer(data, dataLen, prevLayer, packet, GREv0)
230  {}
231 
234 
235  ~GREv0Layer() override = default;
236 
245  {
246  return reinterpret_cast<gre_basic_header*>(m_Data);
247  }
248 
253  bool getChecksum(uint16_t& checksum) const;
254 
263  bool setChecksum(uint16_t checksum);
264 
271 
277  bool getOffset(uint16_t& offset) const;
278 
283  bool getKey(uint32_t& key) const;
284 
290  bool setKey(uint32_t key);
291 
295  bool unsetKey();
296 
301  static inline bool isDataValid(const uint8_t* data, size_t dataLen)
302  {
303  return data && dataLen >= sizeof(gre_basic_header);
304  }
305 
306  // implement abstract methods
307 
311  void computeCalculateFields() override;
312 
313  std::string toString() const override;
314 
319  {
321  static std::vector<FieldDescriptor> all()
322  {
323  auto result = GreLayer::SerializedFields::all();
324  std::initializer_list<FieldDescriptor> extra{ Checksum, Offset, Key };
325  std::copy(extra.begin(), extra.end(), std::back_inserter(result));
326  return result;
327  }
328 
331  static const FieldDescriptor Checksum;
332 
334  static const FieldDescriptor Offset;
335 
337  static const FieldDescriptor Key;
338 
339  // cppcheck-suppress duplInheritedMember
341  static constexpr uint16_t MaxID = GreLayer::SerializedFields::MaxID + 3;
342  };
343 
344  protected:
345  void serializeLayer(ObjectScope& serializer) const override;
346  };
347 
350  class GREv1Layer : public GreLayer
351  {
352  public:
358  GREv1Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
359  : GreLayer(data, dataLen, prevLayer, packet, GREv1)
360  {}
361 
364  explicit GREv1Layer(uint16_t callID);
365 
366  ~GREv1Layer() override = default;
367 
377  {
378  return reinterpret_cast<gre1_header*>(m_Data);
379  }
380 
385  bool getAcknowledgmentNum(uint32_t& ackNum) const;
386 
392  bool setAcknowledgmentNum(uint32_t ackNum);
393 
398 
403  static inline bool isDataValid(const uint8_t* data, size_t dataLen)
404  {
405  return data && dataLen >= sizeof(gre1_header);
406  }
407 
408  // implement abstract methods
409 
413  void computeCalculateFields() override;
414 
415  std::string toString() const override;
416 
421  {
423  static std::vector<FieldDescriptor> all()
424  {
425  auto result = GreLayer::SerializedFields::all();
426  std::initializer_list<FieldDescriptor> extra{ AckSequenceNumBit, PayloadLength, CallID,
428  std::copy(extra.begin(), extra.end(), std::back_inserter(result));
429  return result;
430  }
431 
434 
437 
439  static const FieldDescriptor CallID;
440 
443 
444  // cppcheck-suppress duplInheritedMember
446  static constexpr uint16_t MaxID = GreLayer::SerializedFields::MaxID + 4;
447  };
448 
449  protected:
450  void serializeLayer(ObjectScope& serializer) const override;
451  };
452 
456  class PPP_PPTPLayer : public Layer
457  {
458  public:
464  PPP_PPTPLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
465  : Layer(data, dataLen, prevLayer, packet, PPP_PPTP)
466  {}
467 
471  PPP_PPTPLayer(uint8_t address, uint8_t control);
472 
473  ~PPP_PPTPLayer() override = default;
474 
479  {
480  return reinterpret_cast<ppp_pptp_header*>(m_Data);
481  }
482 
487  static inline bool isDataValid(const uint8_t* data, size_t dataLen);
488 
489  // implement abstract methods
490 
492  void parseNextLayer() override;
493 
495  size_t getHeaderLen() const override
496  {
497  return sizeof(ppp_pptp_header);
498  }
499 
502  void computeCalculateFields() override;
503 
504  std::string toString() const override
505  {
506  return "PPP for PPTP Layer";
507  }
508 
510  {
511  return OsiModelSessionLayer;
512  }
513 
518  {
520  static std::vector<FieldDescriptor> all()
521  {
522  auto result = Layer::SerializedFields::all();
523  std::initializer_list<FieldDescriptor> extra{ Address, Control, Protocol };
524  std::copy(extra.begin(), extra.end(), std::back_inserter(result));
525  return result;
526  }
527 
529  static const FieldDescriptor Address;
530 
532  static const FieldDescriptor Control;
533 
535  static const FieldDescriptor Protocol;
536 
537  // cppcheck-suppress duplInheritedMember
539  static constexpr uint16_t MaxID = Layer::SerializedFields::MaxID + 3;
540  };
541 
542  protected:
543  void serializeLayer(ObjectScope& serializer) const override;
544  };
545 
546  bool PPP_PPTPLayer::isDataValid(const uint8_t* data, size_t dataLen)
547  {
548  return data && dataLen >= sizeof(ppp_pptp_header);
549  }
550 
551 } // namespace pcpp
Definition: GreLayer.h:221
void serializeLayer(ObjectScope &serializer) const override
std::string toString() const override
bool getChecksum(uint16_t &checksum) const
GREv0Layer()
A constructor that creates a new GREv0 header and allocates the data.
bool setKey(uint32_t key)
gre_basic_header * getGreHeader() const
Definition: GreLayer.h:244
GREv0Layer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: GreLayer.h:228
bool getKey(uint32_t &key) const
bool getOffset(uint16_t &offset) const
void computeCalculateFields() override
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: GreLayer.h:301
bool setChecksum(uint16_t checksum)
Definition: GreLayer.h:351
bool setAcknowledgmentNum(uint32_t ackNum)
void serializeLayer(ObjectScope &serializer) const override
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: GreLayer.h:403
bool getAcknowledgmentNum(uint32_t &ackNum) const
GREv1Layer(uint16_t callID)
void computeCalculateFields() override
bool unsetAcknowledgmentNum()
std::string toString() const override
gre1_header * getGreHeader() const
Definition: GreLayer.h:376
GREv1Layer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: GreLayer.h:358
Definition: GreLayer.h:97
void serializeLayer(ObjectScope &serializer) const override
bool unsetSequenceNumber()
bool getSequenceNumber(uint32_t &seqNumber) const
void parseNextLayer() override
OsiModelLayer getOsiModelLayer() const override
Definition: GreLayer.h:136
bool setSequenceNumber(uint32_t seqNumber)
static ProtocolType getGREVersion(uint8_t *greData, size_t greDataLen)
size_t getHeaderLen() const override
Definition: Layer.h:117
Definition: Serializers.h:230
Definition: GreLayer.h:457
size_t getHeaderLen() const override
Definition: GreLayer.h:495
void computeCalculateFields() override
PPP_PPTPLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: GreLayer.h:464
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: GreLayer.h:546
ppp_pptp_header * getPPP_PPTPHeader() const
Definition: GreLayer.h:478
OsiModelLayer getOsiModelLayer() const override
Definition: GreLayer.h:509
std::string toString() const override
Definition: GreLayer.h:504
PPP_PPTPLayer(uint8_t address, uint8_t control)
void parseNextLayer() override
Currently identifies the following next layers: IPv4Layer, IPv6Layer. Otherwise sets PayloadLayer.
void serializeLayer(ObjectScope &serializer) const override
Definition: Packet.h:49
uint8_t ProtocolType
Definition: ProtocolType.h:38
const ProtocolType GREv0
GRE version 0 protocol.
Definition: ProtocolType.h:99
const ProtocolType PPP_PPTP
PPP for PPTP protocol.
Definition: ProtocolType.h:108
const ProtocolType GREv1
GRE version 1 protocol.
Definition: ProtocolType.h:102
The main namespace for the PcapPlusPlus lib.
Definition: AssertionUtils.h:19
OsiModelLayer
An enum representing OSI model layers.
Definition: ProtocolType.h:420
@ OsiModelNetworkLayer
Network layer (layer 3)
Definition: ProtocolType.h:426
@ OsiModelSessionLayer
Session layer (layer 5)
Definition: ProtocolType.h:430
Definition: Serializers.h:18
Definition: GreLayer.h:319
static const FieldDescriptor Offset
Routing offset, in host byte order, in bytes (written if routingBit is set)
Definition: GreLayer.h:334
static const FieldDescriptor Key
Key value, in host byte order (written if keyBit is set)
Definition: GreLayer.h:337
static constexpr uint16_t MaxID
Maximum field ID used by the layer.
Definition: GreLayer.h:341
static const FieldDescriptor Checksum
Checksum value, in host byte order, written as a hexadecimal string (written if checksumBit is set)
Definition: GreLayer.h:331
static std::vector< FieldDescriptor > all()
Definition: GreLayer.h:321
Definition: GreLayer.h:421
static const FieldDescriptor AcknowledgmentNumber
Acknowledgment sequence number, in host byte order (written if ackSequenceNumBit is set)
Definition: GreLayer.h:442
static const FieldDescriptor AckSequenceNumBit
Acknowledgment number present bit (1 if set, 0 otherwise)
Definition: GreLayer.h:433
static constexpr uint16_t MaxID
Maximum field ID used by the layer.
Definition: GreLayer.h:446
static const FieldDescriptor CallID
Peer Call ID for the PPTP session, in host byte order.
Definition: GreLayer.h:439
static std::vector< FieldDescriptor > all()
Definition: GreLayer.h:423
static const FieldDescriptor PayloadLength
Size of the payload not including the GRE header, in bytes and in host byte order.
Definition: GreLayer.h:436
Definition: GreLayer.h:145
static const FieldDescriptor Flags
Reserved flags (bits 8 to 12)
Definition: GreLayer.h:177
static const FieldDescriptor KeyBit
Key present bit (1 if set, 0 otherwise)
Definition: GreLayer.h:165
static constexpr uint16_t MaxID
Maximum field ID used by the layer.
Definition: GreLayer.h:190
static const FieldDescriptor StrictSourceRouteBit
Strict source route bit (1 if set, 0 otherwise)
Definition: GreLayer.h:159
static const FieldDescriptor ChecksumBit
Checksum present bit (1 if set, 0 otherwise)
Definition: GreLayer.h:171
static const FieldDescriptor Protocol
Encapsulated protocol EtherType, in host byte order.
Definition: GreLayer.h:183
static const FieldDescriptor Version
GRE protocol version (3 bits: 0 for GREv0, 1 for GREv1)
Definition: GreLayer.h:180
static const FieldDescriptor RecursionControl
Number of additional encapsulations permitted (3-bit counter)
Definition: GreLayer.h:174
static const FieldDescriptor SequenceNumBit
Sequence number present bit (1 if set, 0 otherwise)
Definition: GreLayer.h:162
static std::vector< FieldDescriptor > all()
Definition: GreLayer.h:147
static const FieldDescriptor SequenceNumber
Sequence number, in host byte order (written if sequenceNumBit is set)
Definition: GreLayer.h:186
static const FieldDescriptor RoutingBit
Routing present bit (1 if set, 0 otherwise)
Definition: GreLayer.h:168
Definition: Layer.h:212
static std::vector< FieldDescriptor > all()
Definition: Layer.h:214
static constexpr uint16_t MaxID
Maximum field ID used by the layer.
Definition: Layer.h:229
Definition: GreLayer.h:518
static constexpr uint16_t MaxID
Maximum field ID used by PPP_PPTPLayer.
Definition: GreLayer.h:539
static const FieldDescriptor Control
Control byte.
Definition: GreLayer.h:532
static const FieldDescriptor Address
Broadcast address byte.
Definition: GreLayer.h:529
static const FieldDescriptor Protocol
Protocol type of the next layer, in host byte order.
Definition: GreLayer.h:535
static std::vector< FieldDescriptor > all()
Definition: GreLayer.h:520
Definition: GreLayer.h:69
uint16_t payloadLength
Size of the payload not including the GRE header.
Definition: GreLayer.h:71
uint16_t callID
Contains the Peer's Call ID for the session to which this packet belongs.
Definition: GreLayer.h:73
Definition: GreLayer.h:15
uint8_t recursionControl
Number of additional encapsulations which are permitted. 0 is the default value.
Definition: GreLayer.h:18
uint8_t sequenceNumBit
Set if sequence number exists.
Definition: GreLayer.h:22
uint8_t strictSourceRouteBit
Strict source routing bit (GRE v0 only)
Definition: GreLayer.h:20
uint8_t routingBit
Set if routing exists (GRE v0 only)
Definition: GreLayer.h:26
uint8_t ackSequenceNumBit
Set if acknowledgment number is set (GRE v1 only)
Definition: GreLayer.h:49
uint8_t keyBit
Set if key exists.
Definition: GreLayer.h:24
uint8_t flags
Reserved.
Definition: GreLayer.h:47
uint16_t protocol
Protocol type of the next layer.
Definition: GreLayer.h:60
uint8_t checksumBit
Set if checksum exists (GRE v0 only)
Definition: GreLayer.h:28
uint8_t version
GRE version - can be 0 or 1.
Definition: GreLayer.h:45
Definition: GreLayer.h:82
uint16_t protocol
Protocol type of the next layer (see PPP_* macros at PPPoELayer.h)
Definition: GreLayer.h:88
uint8_t address
Broadcast address.
Definition: GreLayer.h:84
uint8_t control
Control byte.
Definition: GreLayer.h:86