PcapPlusPlus  24.09
IPv6Extensions.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 #include "IpAddress.h"
5 #include "Layer.h"
6 #include "TLVData.h"
7 
9 
14 namespace pcpp
15 {
16 
23  {
24  friend class IPv6Layer;
25 
26  public:
31  {
44  };
45 
49  virtual size_t getExtensionLen() const
50  {
51  return 8 * (getBaseHeader()->headerLen + 1);
52  }
53 
58  {
59  return m_ExtType;
60  }
61 
65  virtual ~IPv6Extension();
66 
71  {
72  return m_NextHeader;
73  }
74 
75  protected:
76  struct ipv6_ext_base_header
77  {
78  uint8_t nextHeader;
79  uint8_t headerLen;
80  };
81 
82  // protected c'tor
83  IPv6Extension(IDataContainer* dataContainer, size_t offset)
84  : m_NextHeader(nullptr), m_ExtType(IPv6ExtensionUnknown), m_DataContainer(dataContainer), m_Offset(offset),
85  m_ShadowData(nullptr)
86  {}
87 
88  // protected empty c'tor
89  IPv6Extension()
90  : m_NextHeader(nullptr), m_ExtType(IPv6ExtensionUnknown), m_DataContainer(nullptr), m_Offset(0),
91  m_ShadowData(nullptr)
92  {}
93 
94  // protected assignment operator
95  IPv6Extension& operator=(const IPv6Extension& other);
96 
97  uint8_t* getDataPtr() const;
98 
99  void initShadowPtr(size_t size);
100 
101  ipv6_ext_base_header* getBaseHeader() const
102  {
103  return (ipv6_ext_base_header*)getDataPtr();
104  }
105 
106  void setNextHeader(IPv6Extension* nextHeader)
107  {
108  m_NextHeader = nextHeader;
109  }
110 
111  IPv6Extension* m_NextHeader;
112  IPv6ExtensionType m_ExtType;
113 
114  private:
115  IDataContainer* m_DataContainer;
116  size_t m_Offset;
117  uint8_t* m_ShadowData;
118  };
119 
125  {
126  friend class IPv6Layer;
127 
128  public:
134  {
136  uint8_t nextHeader;
138  uint8_t headerLen;
143  uint32_t id;
144  };
145 
153  IPv6FragmentationHeader(uint32_t fragId, uint16_t fragOffset, bool lastFragment);
154 
161  {
162  return (ipv6_frag_header*)getDataPtr();
163  }
164 
168  bool isFirstFragment() const;
169 
173  bool isLastFragment() const;
174 
178  bool isMoreFragments() const;
179 
183  uint16_t getFragmentOffset() const;
184 
185  private:
186  IPv6FragmentationHeader(IDataContainer* dataContainer, size_t offset) : IPv6Extension(dataContainer, offset)
187  {
188  m_ExtType = IPv6Fragmentation;
189  }
190  };
191 
198  {
199  friend class IPv6Layer;
200 
201  public:
208  class IPv6Option : public TLVRecord<uint8_t, uint8_t>
209  {
210  public:
211  static const uint8_t Pad0OptionType = 0;
212  static const uint8_t PadNOptionType = 1;
213 
218  explicit IPv6Option(uint8_t* optionRawData) : TLVRecord(optionRawData)
219  {}
220 
225  {}
226 
233  static bool canAssign(const uint8_t* recordRawData, size_t tlvDataLen)
234  {
235  auto data = (TLVRawData*)recordRawData;
236  if (data == nullptr)
237  return false;
238 
239  if (tlvDataLen < sizeof(TLVRawData::recordType))
240  return false;
241 
242  if (data->recordType == Pad0OptionType)
243  return true;
244 
245  return TLVRecord<uint8_t, uint8_t>::canAssign(recordRawData, tlvDataLen);
246  }
247 
248  // implement abstract methods
249 
250  size_t getTotalSize() const
251  {
252  if (m_Data == nullptr)
253  return 0;
254 
255  if (m_Data->recordType == Pad0OptionType)
256  return sizeof(uint8_t);
257 
258  return (size_t)(m_Data->recordLen + sizeof(uint16_t));
259  }
260 
261  size_t getDataSize() const
262  {
263  if (m_Data == nullptr || m_Data->recordType == Pad0OptionType)
264  return 0;
265 
266  return (size_t)m_Data->recordLen;
267  }
268  };
269 
276  {
277  public:
286  IPv6TLVOptionBuilder(uint8_t optType, const uint8_t* optValue, uint8_t optValueLen)
287  : TLVRecordBuilder(optType, optValue, optValueLen)
288  {}
289 
296  IPv6TLVOptionBuilder(uint8_t optType, uint8_t optValue) : TLVRecordBuilder(optType, optValue)
297  {}
298 
305  IPv6TLVOptionBuilder(uint8_t optType, uint16_t optValue) : TLVRecordBuilder(optType, optValue)
306  {}
307 
314  {}
315 
321  {
322  TLVRecordBuilder::operator=(other);
323  return *this;
324  }
325 
330  IPv6Option build() const;
331  };
332 
339  IPv6Option getOption(uint8_t optionType) const;
340 
346 
355 
359  size_t getOptionCount() const;
360 
361  protected:
363  explicit IPv6TLVOptionHeader(const std::vector<IPv6TLVOptionBuilder>& options);
364 
365  IPv6TLVOptionHeader(IDataContainer* dataContainer, size_t offset);
366 
367  private:
368  TLVRecordReader<IPv6Option> m_OptionReader;
369  };
370 
377  {
378  friend class IPv6Layer;
379 
380  public:
387  explicit IPv6HopByHopHeader(const std::vector<IPv6TLVOptionBuilder>& options) : IPv6TLVOptionHeader(options)
388  {
389  m_ExtType = IPv6HopByHop;
390  }
391 
392  private:
393  IPv6HopByHopHeader(IDataContainer* dataContainer, size_t offset) : IPv6TLVOptionHeader(dataContainer, offset)
394  {
395  m_ExtType = IPv6HopByHop;
396  }
397  };
398 
405  {
406  friend class IPv6Layer;
407 
408  public:
415  explicit IPv6DestinationHeader(const std::vector<IPv6TLVOptionBuilder>& options) : IPv6TLVOptionHeader(options)
416  {
417  m_ExtType = IPv6Destination;
418  }
419 
420  private:
421  IPv6DestinationHeader(IDataContainer* dataContainer, size_t offset) : IPv6TLVOptionHeader(dataContainer, offset)
422  {
423  m_ExtType = IPv6Destination;
424  }
425  };
426 
432  {
433  friend class IPv6Layer;
434 
435  public:
441  {
443  uint8_t nextHeader;
445  uint8_t headerLen;
447  uint8_t routingType;
449  uint8_t segmentsLeft;
450  };
451 
461  IPv6RoutingHeader(uint8_t routingType, uint8_t segmentsLeft, const uint8_t* additionalRoutingData,
462  size_t additionalRoutingDataLen);
463 
470  {
471  return (ipv6_routing_header*)getDataPtr();
472  }
473 
478  uint8_t* getRoutingAdditionalData() const;
479 
484 
498 
499  private:
500  IPv6RoutingHeader(IDataContainer* dataContainer, size_t offset) : IPv6Extension(dataContainer, offset)
501  {
502  m_ExtType = IPv6Routing;
503  }
504  };
505 
512  {
513  friend class IPv6Layer;
514 
515  public:
521  {
523  uint8_t nextHeader;
527  uint8_t headerLen;
529  uint16_t reserved;
534  uint32_t sequenceNumber;
535  };
536 
548  IPv6AuthenticationHeader(uint32_t securityParametersIndex, uint32_t sequenceNumber,
549  const uint8_t* integrityCheckValue, size_t integrityCheckValueLen);
550 
557  {
558  return (ipv6_authentication_header*)getDataPtr();
559  }
560 
565  uint8_t* getIntegrityCheckValue() const;
566 
571 
572  // overridden methods
573 
579  size_t getExtensionLen() const
580  {
581  return 4 * (getBaseHeader()->headerLen + 2);
582  }
583 
584  private:
585  IPv6AuthenticationHeader(IDataContainer* dataContainer, size_t offset) : IPv6Extension(dataContainer, offset)
586  {
587  m_ExtType = IPv6AuthenticationHdr;
588  }
589  };
590 
591 } // namespace pcpp
Definition: Layer.h:23
Definition: IpAddress.h:199
Definition: IPv6Extensions.h:512
size_t getIntegrityCheckValueLength() const
size_t getExtensionLen() const
Definition: IPv6Extensions.h:579
uint8_t * getIntegrityCheckValue() const
ipv6_authentication_header * getAuthHeader() const
Definition: IPv6Extensions.h:556
IPv6AuthenticationHeader(uint32_t securityParametersIndex, uint32_t sequenceNumber, const uint8_t *integrityCheckValue, size_t integrityCheckValueLen)
Definition: IPv6Extensions.h:405
IPv6DestinationHeader(const std::vector< IPv6TLVOptionBuilder > &options)
Definition: IPv6Extensions.h:415
Definition: IPv6Extensions.h:23
IPv6ExtensionType
Definition: IPv6Extensions.h:31
@ IPv6HopByHop
Definition: IPv6Extensions.h:33
@ IPv6Routing
Definition: IPv6Extensions.h:35
@ IPv6Fragmentation
Definition: IPv6Extensions.h:37
@ IPv6ExtensionUnknown
Definition: IPv6Extensions.h:43
@ IPv6Destination
Definition: IPv6Extensions.h:41
@ IPv6AuthenticationHdr
Definition: IPv6Extensions.h:39
virtual ~IPv6Extension()
IPv6Extension * getNextHeader() const
Definition: IPv6Extensions.h:70
IPv6ExtensionType getExtensionType() const
Definition: IPv6Extensions.h:57
virtual size_t getExtensionLen() const
Definition: IPv6Extensions.h:49
Definition: IPv6Extensions.h:125
ipv6_frag_header * getFragHeader() const
Definition: IPv6Extensions.h:160
IPv6FragmentationHeader(uint32_t fragId, uint16_t fragOffset, bool lastFragment)
uint16_t getFragmentOffset() const
Definition: IPv6Extensions.h:377
IPv6HopByHopHeader(const std::vector< IPv6TLVOptionBuilder > &options)
Definition: IPv6Extensions.h:387
Definition: IPv6Layer.h:55
Definition: IPv6Extensions.h:432
ipv6_routing_header * getRoutingHeader() const
Definition: IPv6Extensions.h:469
IPv6RoutingHeader(uint8_t routingType, uint8_t segmentsLeft, const uint8_t *additionalRoutingData, size_t additionalRoutingDataLen)
size_t getRoutingAdditionalDataLength() const
IPv6Address getRoutingAdditionalDataAsIPv6Address(size_t offset=0) const
uint8_t * getRoutingAdditionalData() const
Definition: IPv6Extensions.h:209
IPv6Option(uint8_t *optionRawData)
Definition: IPv6Extensions.h:218
size_t getTotalSize() const
Definition: IPv6Extensions.h:250
size_t getDataSize() const
Definition: IPv6Extensions.h:261
~IPv6Option()
Definition: IPv6Extensions.h:224
static bool canAssign(const uint8_t *recordRawData, size_t tlvDataLen)
Definition: IPv6Extensions.h:233
Definition: IPv6Extensions.h:276
IPv6TLVOptionBuilder(uint8_t optType, uint8_t optValue)
Definition: IPv6Extensions.h:296
IPv6TLVOptionBuilder(uint8_t optType, uint16_t optValue)
Definition: IPv6Extensions.h:305
IPv6TLVOptionBuilder & operator=(const IPv6TLVOptionBuilder &other)
Definition: IPv6Extensions.h:320
IPv6TLVOptionBuilder(const IPv6TLVOptionBuilder &other)
Definition: IPv6Extensions.h:313
IPv6TLVOptionBuilder(uint8_t optType, const uint8_t *optValue, uint8_t optValueLen)
Definition: IPv6Extensions.h:286
Definition: IPv6Extensions.h:198
size_t getOptionCount() const
IPv6TLVOptionHeader(const std::vector< IPv6TLVOptionBuilder > &options)
IPv6Option getNextOption(IPv6Option &option) const
IPv6Option getOption(uint8_t optionType) const
IPv6Option getFirstOption() const
Definition: TLVData.h:413
Definition: TLVData.h:23
static bool canAssign(const uint8_t *recordRawData, size_t tlvDataLen)
Definition: TLVData.h:79
Definition: TLVData.h:239
The main namespace for the PcapPlusPlus lib.
uint8_t headerLen
Definition: IPv6Extensions.h:527
uint32_t sequenceNumber
Definition: IPv6Extensions.h:534
uint8_t nextHeader
Definition: IPv6Extensions.h:523
uint16_t reserved
Definition: IPv6Extensions.h:529
uint32_t securityParametersIndex
Definition: IPv6Extensions.h:532
Definition: IPv6Extensions.h:134
uint8_t headerLen
Definition: IPv6Extensions.h:138
uint16_t fragOffsetAndFlags
Definition: IPv6Extensions.h:141
uint32_t id
Definition: IPv6Extensions.h:143
uint8_t nextHeader
Definition: IPv6Extensions.h:136
Definition: IPv6Extensions.h:441
uint8_t segmentsLeft
Definition: IPv6Extensions.h:449
uint8_t nextHeader
Definition: IPv6Extensions.h:443
uint8_t routingType
Definition: IPv6Extensions.h:447
uint8_t headerLen
Definition: IPv6Extensions.h:445