PcapPlusPlus  23.09
IPv6Layer.h
Go to the documentation of this file.
1 #ifndef PACKETPP_IPV6_LAYER
2 #define PACKETPP_IPV6_LAYER
3 
4 #include "Layer.h"
5 #include "IPLayer.h"
6 #include "IPv6Extensions.h"
7 #include "IpAddress.h"
8 
10 
15 namespace pcpp
16 {
17 
22 #pragma pack(push, 1)
23  struct ip6_hdr
24  {
25  #if (BYTE_ORDER == LITTLE_ENDIAN)
26 
27  uint8_t trafficClass:4,
29  ipVersion:4;
30  #else
31 
32  uint8_t ipVersion:4,
34  trafficClass:4;
35  #endif
36 
37  uint8_t flowLabel[3];
39  uint16_t payloadLength;
41  uint8_t nextHeader;
43  uint8_t hopLimit;
45  uint8_t ipSrc[16];
47  uint8_t ipDst[16];
48  };
49 #pragma pack(pop)
50 
51 
56  class IPv6Layer : public Layer, public IPLayer
57  {
58  public:
66  IPv6Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
67 
71  IPv6Layer();
72 
78  IPv6Layer(const IPv6Address& srcIP, const IPv6Address& dstIP);
79 
83  IPv6Layer(const IPv6Layer& other);
84 
88  ~IPv6Layer();
89 
93  IPv6Layer& operator=(const IPv6Layer& other);
94 
99  ip6_hdr* getIPv6Header() const { return (ip6_hdr*)m_Data; }
100 
106  IPAddress getSrcIPAddress() const { return getSrcIPv6Address(); }
107 
112  IPv6Address getSrcIPv6Address() const { return getIPv6Header()->ipSrc; }
113 
118  void setSrcIPv6Address(const IPv6Address& ipAddr) { ipAddr.copyTo(getIPv6Header()->ipSrc); }
119 
120 
125  void setDstIPv6Address(const IPv6Address& ipAddr) { ipAddr.copyTo(getIPv6Header()->ipDst); }
126 
132  IPAddress getDstIPAddress() const { return getDstIPv6Address(); }
133 
138  IPv6Address getDstIPv6Address() const { return getIPv6Header()->ipDst; }
139 
143  size_t getExtensionCount() const;
144 
150  template<class TIPv6Extension>
151  TIPv6Extension* getExtensionOfType() const;
152 
162  template<class TIPv6Extension>
163  TIPv6Extension* addExtension(const TIPv6Extension& extensionHeader);
164 
168  void removeAllExtensions();
169 
173  bool isFragment() const;
174 
181  static inline bool isDataValid(const uint8_t* data, size_t dataLen);
182 
183 
184  // implement abstract methods
185 
198  void parseNextLayer();
199 
203  size_t getHeaderLen() const { return sizeof(ip6_hdr) + m_ExtensionsLen; }
204 
211  void computeCalculateFields();
212 
213  std::string toString() const;
214 
216 
217  private:
218  void initLayer();
219  void parseExtensions();
220  void deleteExtensions();
221 
222  IPv6Extension* m_FirstExtension;
223  IPv6Extension* m_LastExtension;
224  size_t m_ExtensionsLen;
225  };
226 
227 
228  template<class TIPv6Extension>
229  TIPv6Extension* IPv6Layer::getExtensionOfType() const
230  {
231  IPv6Extension* curExt = m_FirstExtension;
232  while (curExt != NULL && dynamic_cast<TIPv6Extension*>(curExt) == NULL)
233  curExt = curExt->getNextHeader();
234 
235  return (TIPv6Extension*)curExt;
236  }
237 
238  template<class TIPv6Extension>
239  TIPv6Extension* IPv6Layer::addExtension(const TIPv6Extension& extensionHeader)
240  {
241  int offsetToAddHeader = (int)getHeaderLen();
242  if (!extendLayer(offsetToAddHeader, extensionHeader.getExtensionLen()))
243  {
244  return NULL;
245  }
246 
247  TIPv6Extension* newHeader = new TIPv6Extension(this, (size_t)offsetToAddHeader);
248  (*newHeader) = extensionHeader;
249 
250  if (m_FirstExtension != NULL)
251  {
252  newHeader->getBaseHeader()->nextHeader = m_LastExtension->getBaseHeader()->nextHeader;
253  m_LastExtension->getBaseHeader()->nextHeader = newHeader->getExtensionType();
254  m_LastExtension->setNextHeader(newHeader);
255  m_LastExtension = newHeader;
256  }
257  else
258  {
259  m_FirstExtension = newHeader;
260  m_LastExtension = newHeader;
261  newHeader->getBaseHeader()->nextHeader = getIPv6Header()->nextHeader;
262  getIPv6Header()->nextHeader = newHeader->getExtensionType();
263  }
264 
265  m_ExtensionsLen += newHeader->getExtensionLen();
266 
267  return newHeader;
268  }
269 
270  // implementation of inline methods
271 
272  bool IPv6Layer::isDataValid(const uint8_t* data, size_t dataLen)
273  {
274  return data && dataLen >= sizeof(ip6_hdr);
275  }
276 
277 } // namespace pcpp
278 
279 #endif /* PACKETPP_IPV6_LAYER */
IPAddress getSrcIPAddress() const
Definition: IPv6Layer.h:106
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:333
Definition: IPv6Extensions.h:23
Definition: Layer.h:70
Definition: IPLayer.h:22
uint8_t ipSrc[16]
Definition: IPv6Layer.h:45
Definition: Packet.h:26
uint16_t payloadLength
Definition: IPv6Layer.h:39
TIPv6Extension * addExtension(const TIPv6Extension &extensionHeader)
Definition: IPv6Layer.h:239
Definition: IPv6Layer.h:23
Definition: IPv6Layer.h:56
uint8_t trafficClass
Definition: IPv6Layer.h:27
Definition: ProtocolType.h:340
uint8_t flowLabel[3]
Definition: IPv6Layer.h:37
uint8_t ipDst[16]
Definition: IPv6Layer.h:47
size_t getHeaderLen() const
Definition: IPv6Layer.h:203
Definition: IpAddress.h:192
uint8_t hopLimit
Definition: IPv6Layer.h:43
IPv6Extension * getNextHeader() const
Definition: IPv6Extensions.h:66
TIPv6Extension * getExtensionOfType() const
Definition: IPv6Layer.h:229
IPv6Address getDstIPv6Address() const
Definition: IPv6Layer.h:138
ip6_hdr * getIPv6Header() const
Definition: IPv6Layer.h:99
void setSrcIPv6Address(const IPv6Address &ipAddr)
Definition: IPv6Layer.h:118
void setDstIPv6Address(const IPv6Address &ipAddr)
Definition: IPv6Layer.h:125
uint8_t ipVersion
Definition: IPv6Layer.h:27
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: IPv6Layer.h:272
IPAddress getDstIPAddress() const
Definition: IPv6Layer.h:132
void copyTo(uint8_t **arr, size_t &length) const
Definition: IpAddress.h:321
IPv6Address getSrcIPv6Address() const
Definition: IPv6Layer.h:112
OsiModelLayer getOsiModelLayer() const
Definition: IPv6Layer.h:215
uint8_t nextHeader
Definition: IPv6Layer.h:41