PcapPlusPlus
IPv6Extensions.h
Go to the documentation of this file.
1 #ifndef PACKETPP_IPV6_EXTENSION
2 #define PACKETPP_IPV6_EXTENSION
3 
4 #include <vector>
5 #include "IpAddress.h"
6 #include "Layer.h"
7 
9 
14 namespace pcpp
15 {
16 
23  {
24  friend class IPv6Layer;
25 
26  public:
27 
32  {
45  };
46 
50  virtual inline size_t getExtensionLen() const { return 8 * (getBaseHeader()->headerLen+1); }
51 
55  inline IPv6ExtensionType getExtensionType() { return m_ExtType; }
56 
60  virtual ~IPv6Extension();
61 
62  protected:
63 
64  struct ipv6_ext_base_header
65  {
66  uint8_t nextHeader;
67  uint8_t headerLen;
68  };
69 
70  // protected c'tor
71  IPv6Extension(IDataContainer* dataContainer, size_t offset) :
72  m_NextHeader(NULL), m_ExtType(IPv6ExtensionUnknown), m_DataContainer(dataContainer), m_Offset(offset), m_ShadowData(NULL) {}
73 
74  // protected empty c'tor
75  IPv6Extension() :
76  m_NextHeader(NULL), m_ExtType(IPv6ExtensionUnknown), m_DataContainer(NULL), m_Offset(0), m_ShadowData(NULL) {}
77 
78  // protected assignment operator
79  IPv6Extension& operator=(const IPv6Extension& other);
80 
81  uint8_t* getDataPtr() const;
82 
83  void initShadowPtr(size_t size);
84 
85  ipv6_ext_base_header* getBaseHeader() const { return (ipv6_ext_base_header*)getDataPtr(); }
86 
87  inline void setNextHeader(IPv6Extension* nextHeader) { m_NextHeader = nextHeader; }
88 
89  inline IPv6Extension* getNextHeader() { return m_NextHeader; }
90 
91  IPv6Extension* m_NextHeader;
92  IPv6ExtensionType m_ExtType;
93 
94  private:
95  IDataContainer* m_DataContainer;
96  size_t m_Offset;
97  uint8_t* m_ShadowData;
98 
99  };
100 
101 
102 
108  {
109  friend class IPv6Layer;
110 
111  public:
112 
118  {
120  uint8_t nextHeader;
122  uint8_t headerLen;
127  uint32_t id;
128  };
129 
137  IPv6FragmentationHeader(uint32_t fragId, uint16_t fragOffset, bool lastFragment);
138 
144  ipv6_frag_header* getFragHeader() { return (ipv6_frag_header*)getDataPtr(); }
145 
149  bool isFirstFragment();
150 
154  bool isLastFragment();
155 
159  bool isMoreFragments();
160 
164  uint16_t getFragmentOffset();
165 
166  private:
167 
168  IPv6FragmentationHeader(IDataContainer* dataContainer, size_t offset) : IPv6Extension(dataContainer, offset)
169  {
170  m_ExtType = IPv6Fragmentation;
171  }
172 
173  };
174 
175 
182  {
183  friend class IPv6Layer;
184 
185  public:
186 
192  struct TLVOption
193  {
194  public:
196  uint8_t optionType;
198  uint8_t optionLen;
200  uint8_t optionData[];
201 
202  static const uint8_t Pad0OptionType = 0;
203  static const uint8_t PadNOptionType = 1;
204 
211  template<typename T>
213  {
214  if (getDataSize() < sizeof(T))
215  return 0;
216 
217  T result;
218  memcpy(&result, optionData, sizeof(T));
219  return result;
220  }
221 
225  size_t getTotalSize() const
226  {
227  if (optionType == Pad0OptionType)
228  return sizeof(uint8_t);
229 
230  return (size_t)(optionLen + sizeof(uint16_t));
231  }
232 
236  size_t getDataSize()
237  {
238  if (optionType == Pad0OptionType)
239  return (size_t)0;
240 
241  return (size_t)optionLen;
242  }
243  };
244 
245 
251  {
252  public:
253 
261  TLVOptionBuilder(uint8_t optType, uint8_t optDataLen, const uint8_t* optValue);
262 
269  TLVOptionBuilder(uint8_t optType, uint8_t optValue);
270 
277  TLVOptionBuilder(uint8_t optType, uint16_t optValue);
278 
283  TLVOptionBuilder(const TLVOptionBuilder& other);
284 
288  ~TLVOptionBuilder() { delete [] m_OptionBuffer; }
289 
295  TLVOption* build() const { return (TLVOption*)m_OptionBuffer; }
296 
300  uint8_t* getRawBuffer() const { return m_OptionBuffer; }
301 
302  private:
303 
304  void init(uint8_t optType, uint8_t optDataLen, const uint8_t* optValue);
305  uint8_t* m_OptionBuffer;
306  };
307 
313  TLVOption* getOption(uint8_t optionType);
314 
318  TLVOption* getFirstOption();
319 
326  TLVOption* getNextOption(TLVOption* option);
327 
331  size_t getOptionCount();
332 
333  protected:
334 
336  IPv6TLVOptionHeader(const std::vector<TLVOptionBuilder>& options);
337 
338  IPv6TLVOptionHeader(IDataContainer* dataContainer, size_t offset);
339 
340  private:
341 
342  size_t m_OptionCount;
343  };
344 
345 
346 
352  {
353  friend class IPv6Layer;
354 
355  public:
356 
363  IPv6HopByHopHeader(const std::vector<TLVOptionBuilder>& options) : IPv6TLVOptionHeader(options) { m_ExtType = IPv6HopByHop; }
364 
365  private:
366 
367  IPv6HopByHopHeader(IDataContainer* dataContainer, size_t offset) : IPv6TLVOptionHeader(dataContainer, offset) { m_ExtType = IPv6HopByHop; }
368  };
369 
370 
371 
377  {
378  friend class IPv6Layer;
379 
380  public:
381 
388  IPv6DestinationHeader(const std::vector<TLVOptionBuilder>& options) : IPv6TLVOptionHeader(options) { m_ExtType = IPv6Destination; }
389 
390  private:
391 
392  IPv6DestinationHeader(IDataContainer* dataContainer, size_t offset) : IPv6TLVOptionHeader(dataContainer, offset) { m_ExtType = IPv6Destination; }
393  };
394 
395 
396 
402  {
403  friend class IPv6Layer;
404 
405  public:
406 
412  {
414  uint8_t nextHeader;
416  uint8_t headerLen;
418  uint8_t routingType;
420  uint8_t segmentsLeft;
421  };
422 
432  IPv6RoutingHeader(uint8_t routingType, uint8_t segmentsLeft, const uint8_t* additionalRoutingData, size_t additionalRoutingDataLen);
433 
440 
445  uint8_t* getRoutingAdditionalData();
446 
451 
462 
463  private:
464 
465  IPv6RoutingHeader(IDataContainer* dataContainer, size_t offset) : IPv6Extension(dataContainer, offset) { m_ExtType = IPv6Routing; }
466 
467  };
468 
469 
475  {
476  friend class IPv6Layer;
477 
478  public:
479 
485  {
487  uint8_t nextHeader;
490  uint8_t headerLen;
492  uint16_t reserved;
496  uint32_t sequenceNumber;
497  };
498 
508  IPv6AuthenticationHeader(uint32_t securityParametersIndex, uint32_t sequenceNumber, const uint8_t* integrityCheckValue, size_t integrityCheckValueLen);
509 
516 
521  uint8_t* getIntegrityCheckValue();
522 
527 
528  // overridden methods
529 
535  size_t getExtensionLen() const { return 4 * (getBaseHeader()->headerLen+2); }
536 
537  private:
538 
539  IPv6AuthenticationHeader(IDataContainer* dataContainer, size_t offset) : IPv6Extension(dataContainer, offset) { m_ExtType = IPv6AuthenticationHdr; }
540  };
541 
542 }
543 
544 #endif // PACKETPP_IPV6_EXTENSION
Definition: IpAddress.h:212
size_t getExtensionLen() const
Definition: IPv6Extensions.h:535
Definition: IPv6Extensions.h:376
IPv6ExtensionType
Definition: IPv6Extensions.h:31
uint32_t sequenceNumber
Definition: IPv6Extensions.h:496
~TLVOptionBuilder()
Definition: IPv6Extensions.h:288
uint8_t optionType
Definition: IPv6Extensions.h:196
uint8_t * getRawBuffer() const
Definition: IPv6Extensions.h:300
size_t getRoutingAdditionalDataLength()
Definition: IPv6Extensions.h:22
TLVOption * getFirstOption()
TLVOption * getNextOption(TLVOption *option)
IPv6FragmentationHeader(uint32_t fragId, uint16_t fragOffset, bool lastFragment)
uint8_t headerLen
Definition: IPv6Extensions.h:416
Definition: IPv6Extensions.h:192
Definition: IPv6Extensions.h:107
ipv6_frag_header * getFragHeader()
Definition: IPv6Extensions.h:144
uint32_t securityParametersIndex
Definition: IPv6Extensions.h:494
The main namespace for the PcapPlusPlus lib.
Definition: IPv6Extensions.h:34
uint8_t segmentsLeft
Definition: IPv6Extensions.h:420
Definition: IPv6Extensions.h:411
uint8_t nextHeader
Definition: IPv6Extensions.h:487
Definition: IPv6Extensions.h:351
Definition: IPv6Extensions.h:181
Definition: IPv6Extensions.h:38
TLVOptionBuilder(uint8_t optType, uint8_t optDataLen, const uint8_t *optValue)
virtual size_t getExtensionLen() const
Definition: IPv6Extensions.h:50
uint8_t nextHeader
Definition: IPv6Extensions.h:120
ipv6_authentication_header * getAuthHeader()
Definition: IPv6Extensions.h:515
Definition: Layer.h:23
uint8_t routingType
Definition: IPv6Extensions.h:418
uint8_t optionLen
Definition: IPv6Extensions.h:198
TLVOption * build() const
Definition: IPv6Extensions.h:295
uint8_t nextHeader
Definition: IPv6Extensions.h:414
Definition: IPv6Extensions.h:401
size_t getDataSize()
Definition: IPv6Extensions.h:236
uint8_t optionData[]
Definition: IPv6Extensions.h:200
Definition: IPv6Extensions.h:250
IPv6RoutingHeader(uint8_t routingType, uint8_t segmentsLeft, const uint8_t *additionalRoutingData, size_t additionalRoutingDataLen)
Definition: IPv6Extensions.h:40
IPv6ExtensionType getExtensionType()
Definition: IPv6Extensions.h:55
Definition: IPv6Layer.h:54
IPv6HopByHopHeader(const std::vector< TLVOptionBuilder > &options)
Definition: IPv6Extensions.h:363
Definition: IPv6Extensions.h:474
uint8_t headerLen
Definition: IPv6Extensions.h:122
uint8_t headerLen
Definition: IPv6Extensions.h:490
uint32_t id
Definition: IPv6Extensions.h:127
IPv6Address getRoutingAdditionalDataAsIPv6Address(size_t offset=0)
IPv6AuthenticationHeader(uint32_t securityParametersIndex, uint32_t sequenceNumber, const uint8_t *integrityCheckValue, size_t integrityCheckValueLen)
uint8_t * getRoutingAdditionalData()
Definition: IPv6Extensions.h:36
uint16_t fragOffsetAndFlags
Definition: IPv6Extensions.h:125
ipv6_routing_header * getRoutingHeader()
Definition: IPv6Extensions.h:439
size_t getTotalSize() const
Definition: IPv6Extensions.h:225
T getValueAs()
Definition: IPv6Extensions.h:212
IPv6TLVOptionHeader(const std::vector< TLVOptionBuilder > &options)
virtual ~IPv6Extension()
Definition: IPv6Extensions.h:44
uint16_t reserved
Definition: IPv6Extensions.h:492
TLVOption * getOption(uint8_t optionType)
Definition: IPv6Extensions.h:117
Definition: IPv6Extensions.h:42
IPv6DestinationHeader(const std::vector< TLVOptionBuilder > &options)
Definition: IPv6Extensions.h:388