PcapPlusPlus  24.09
IPv6Layer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Layer.h"
4 #include "IPLayer.h"
5 #include "IPv6Extensions.h"
6 #include "IpAddress.h"
7 
9 
14 namespace pcpp
15 {
16 
21 #pragma pack(push, 1)
22  struct ip6_hdr
23  {
24 #if (BYTE_ORDER == LITTLE_ENDIAN)
26  uint8_t trafficClass : 4,
28  ipVersion : 4;
29 #else
31  uint8_t ipVersion : 4,
33  trafficClass : 4;
34 #endif
36  uint8_t flowLabel[3];
38  uint16_t payloadLength;
40  uint8_t nextHeader;
42  uint8_t hopLimit;
44  uint8_t ipSrc[16];
46  uint8_t ipDst[16];
47  };
48 #pragma pack(pop)
49 
54  class IPv6Layer : public Layer, public IPLayer
55  {
56  public:
64  IPv6Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
65 
70 
76  IPv6Layer(const IPv6Address& srcIP, const IPv6Address& dstIP);
77 
81  IPv6Layer(const IPv6Layer& other);
82 
87 
92  IPv6Layer& operator=(const IPv6Layer& other);
93 
100  {
101  return (ip6_hdr*)m_Data;
102  }
103 
110  {
111  return getSrcIPv6Address();
112  }
113 
119  {
120  return getIPv6Header()->ipSrc;
121  }
122 
127  void setSrcIPv6Address(const IPv6Address& ipAddr)
128  {
129  ipAddr.copyTo(getIPv6Header()->ipSrc);
130  }
131 
136  void setDstIPv6Address(const IPv6Address& ipAddr)
137  {
138  ipAddr.copyTo(getIPv6Header()->ipDst);
139  }
140 
147  {
148  return getDstIPv6Address();
149  }
150 
156  {
157  return getIPv6Header()->ipDst;
158  }
159 
163  size_t getExtensionCount() const;
164 
171  template <class TIPv6Extension> TIPv6Extension* getExtensionOfType() const;
172 
182  template <class TIPv6Extension> TIPv6Extension* addExtension(const TIPv6Extension& extensionHeader);
183 
188 
192  bool isFragment() const;
193 
200  static inline bool isDataValid(const uint8_t* data, size_t dataLen);
201 
202  // implement abstract methods
203 
217 
221  size_t getHeaderLen() const
222  {
223  return sizeof(ip6_hdr) + m_ExtensionsLen;
224  }
225 
234 
235  std::string toString() const;
236 
238  {
239  return OsiModelNetworkLayer;
240  }
241 
242  private:
243  void initLayer();
244  void parseExtensions();
245  void deleteExtensions();
246 
247  IPv6Extension* m_FirstExtension;
248  IPv6Extension* m_LastExtension;
249  size_t m_ExtensionsLen;
250  };
251 
252  template <class TIPv6Extension> TIPv6Extension* IPv6Layer::getExtensionOfType() const
253  {
254  IPv6Extension* curExt = m_FirstExtension;
255  while (curExt != nullptr && dynamic_cast<TIPv6Extension*>(curExt) == nullptr)
256  curExt = curExt->getNextHeader();
257 
258  return static_cast<TIPv6Extension*>(curExt);
259  }
260 
261  template <class TIPv6Extension> TIPv6Extension* IPv6Layer::addExtension(const TIPv6Extension& extensionHeader)
262  {
263  int offsetToAddHeader = static_cast<int>(getHeaderLen());
264  if (!extendLayer(offsetToAddHeader, extensionHeader.getExtensionLen()))
265  {
266  return nullptr;
267  }
268 
269  TIPv6Extension* newHeader = new TIPv6Extension(this, static_cast<size_t>(offsetToAddHeader));
270  (*newHeader) = extensionHeader;
271 
272  if (m_FirstExtension != nullptr)
273  {
274  newHeader->getBaseHeader()->nextHeader = m_LastExtension->getBaseHeader()->nextHeader;
275  m_LastExtension->getBaseHeader()->nextHeader = newHeader->getExtensionType();
276  m_LastExtension->setNextHeader(newHeader);
277  m_LastExtension = newHeader;
278  }
279  else
280  {
281  m_FirstExtension = newHeader;
282  m_LastExtension = newHeader;
283  newHeader->getBaseHeader()->nextHeader = getIPv6Header()->nextHeader;
284  getIPv6Header()->nextHeader = newHeader->getExtensionType();
285  }
286 
287  m_ExtensionsLen += newHeader->getExtensionLen();
288 
289  return newHeader;
290  }
291 
292  // implementation of inline methods
293 
294  bool IPv6Layer::isDataValid(const uint8_t* data, size_t dataLen)
295  {
296  return data && dataLen >= sizeof(ip6_hdr);
297  }
298 
299 } // namespace pcpp
Definition: IpAddress.h:358
Definition: IPLayer.h:22
Definition: IpAddress.h:199
void copyTo(uint8_t **arr, size_t &length) const
Definition: IPv6Extensions.h:23
IPv6Extension * getNextHeader() const
Definition: IPv6Extensions.h:70
Definition: IPv6Layer.h:55
IPv6Address getSrcIPv6Address() const
Definition: IPv6Layer.h:118
IPAddress getDstIPAddress() const
Definition: IPv6Layer.h:146
std::string toString() const
IPv6Layer(const IPv6Address &srcIP, const IPv6Address &dstIP)
IPv6Layer(const IPv6Layer &other)
IPv6Address getDstIPv6Address() const
Definition: IPv6Layer.h:155
void removeAllExtensions()
OsiModelLayer getOsiModelLayer() const
Definition: IPv6Layer.h:237
void parseNextLayer()
TIPv6Extension * getExtensionOfType() const
Definition: IPv6Layer.h:252
bool isFragment() const
ip6_hdr * getIPv6Header() const
Definition: IPv6Layer.h:99
void setDstIPv6Address(const IPv6Address &ipAddr)
Definition: IPv6Layer.h:136
IPv6Layer & operator=(const IPv6Layer &other)
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: IPv6Layer.h:294
void computeCalculateFields()
IPAddress getSrcIPAddress() const
Definition: IPv6Layer.h:109
void setSrcIPv6Address(const IPv6Address &ipAddr)
Definition: IPv6Layer.h:127
IPv6Layer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
size_t getExtensionCount() const
TIPv6Extension * addExtension(const TIPv6Extension &extensionHeader)
Definition: IPv6Layer.h:261
size_t getHeaderLen() const
Definition: IPv6Layer.h:221
Definition: Layer.h:70
Definition: Packet.h:27
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:354
@ OsiModelNetworkLayer
Definition: ProtocolType.h:360
Definition: IPv6Layer.h:23
uint8_t trafficClass
Definition: IPv6Layer.h:26
uint8_t flowLabel[3]
Definition: IPv6Layer.h:36
uint8_t hopLimit
Definition: IPv6Layer.h:42
uint8_t ipSrc[16]
Definition: IPv6Layer.h:44
uint16_t payloadLength
Definition: IPv6Layer.h:38
uint8_t ipVersion
Definition: IPv6Layer.h:28
uint8_t nextHeader
Definition: IPv6Layer.h:40
uint8_t ipDst[16]
Definition: IPv6Layer.h:46