PcapPlusPlus  20.08+
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 "IPv6Extensions.h"
6 #include "IpAddress.h"
7 
9 
14 namespace pcpp
15 {
16 
21 #pragma pack(push, 1)
22  struct ip6_hdr {
23  #if (BYTE_ORDER == LITTLE_ENDIAN)
24 
25  uint8_t trafficClass:4,
27  ipVersion:4;
28  #else
29 
30  uint8_t ipVersion:4,
32  trafficClass:4;
33  #endif
34 
35  uint8_t flowLabel[3];
37  uint16_t payloadLength;
39  uint8_t nextHeader;
41  uint8_t hopLimit;
43  uint8_t ipSrc[16];
45  uint8_t ipDst[16];
46  };
47 #pragma pack(pop)
48 
49 
54  class IPv6Layer : public Layer
55  {
56  public:
64  IPv6Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
65 
69  IPv6Layer();
70 
76  IPv6Layer(const IPv6Address& srcIP, const IPv6Address& dstIP);
77 
81  IPv6Layer(const IPv6Layer& other);
82 
86  ~IPv6Layer();
87 
91  IPv6Layer& operator=(const IPv6Layer& other);
92 
97  ip6_hdr* getIPv6Header() const { return (ip6_hdr*)m_Data; }
98 
103  IPv6Address getSrcIpAddress() const { return getIPv6Header()->ipSrc; }
104 
109  IPv6Address getDstIpAddress() const { return getIPv6Header()->ipDst; }
110 
114  size_t getExtensionCount() const;
115 
121  template<class TIPv6Extension>
122  TIPv6Extension* getExtensionOfType() const;
123 
133  template<class TIPv6Extension>
134  TIPv6Extension* addExtension(const TIPv6Extension& extensionHeader);
135 
139  void removeAllExtensions();
140 
144  bool isFragment() const;
145 
152  static inline bool isDataValid(const uint8_t* data, size_t dataLen);
153 
154 
155  // implement abstract methods
156 
160  void parseNextLayer();
161 
165  size_t getHeaderLen() const { return sizeof(ip6_hdr) + m_ExtensionsLen; }
166 
173  void computeCalculateFields();
174 
175  std::string toString() const;
176 
178 
179  private:
180  void initLayer();
181  void parseExtensions();
182  void deleteExtensions();
183 
184  IPv6Extension* m_FirstExtension;
185  IPv6Extension* m_LastExtension;
186  size_t m_ExtensionsLen;
187  };
188 
189 
190  template<class TIPv6Extension>
191  TIPv6Extension* IPv6Layer::getExtensionOfType() const
192  {
193  IPv6Extension* curExt = m_FirstExtension;
194  while (curExt != NULL && dynamic_cast<TIPv6Extension*>(curExt) == NULL)
195  curExt = curExt->getNextHeader();
196 
197  return (TIPv6Extension*)curExt;
198  }
199 
200  template<class TIPv6Extension>
201  TIPv6Extension* IPv6Layer::addExtension(const TIPv6Extension& extensionHeader)
202  {
203  int offsetToAddHeader = (int)getHeaderLen();
204  if (!extendLayer(offsetToAddHeader, extensionHeader.getExtensionLen()))
205  {
206  return NULL;
207  }
208 
209  TIPv6Extension* newHeader = new TIPv6Extension(this, (size_t)offsetToAddHeader);
210  (*newHeader) = extensionHeader;
211 
212  if (m_FirstExtension != NULL)
213  {
214  newHeader->getBaseHeader()->nextHeader = m_LastExtension->getBaseHeader()->nextHeader;
215  m_LastExtension->getBaseHeader()->nextHeader = newHeader->getExtensionType();
216  m_LastExtension->setNextHeader(newHeader);
217  m_LastExtension = newHeader;
218  }
219  else
220  {
221  m_FirstExtension = newHeader;
222  m_LastExtension = newHeader;
223  newHeader->getBaseHeader()->nextHeader = getIPv6Header()->nextHeader;
224  getIPv6Header()->nextHeader = newHeader->getExtensionType();
225  }
226 
227  m_ExtensionsLen += newHeader->getExtensionLen();
228 
229  return newHeader;
230  }
231 
232  // implementation of inline methods
233 
234  bool IPv6Layer::isDataValid(const uint8_t* data, size_t dataLen)
235  {
236  return dataLen >= sizeof(ip6_hdr);
237  }
238 
239 } // namespace pcpp
240 
241 #endif /* PACKETPP_IPV6_LAYER */
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:233
Definition: IPv6Extensions.h:23
Definition: Layer.h:70
uint8_t ipSrc[16]
Definition: IPv6Layer.h:43
Definition: Packet.h:26
uint16_t payloadLength
Definition: IPv6Layer.h:37
TIPv6Extension * addExtension(const TIPv6Extension &extensionHeader)
Definition: IPv6Layer.h:201
Definition: IPv6Layer.h:22
Definition: IPv6Layer.h:54
uint8_t trafficClass
Definition: IPv6Layer.h:25
Definition: ProtocolType.h:240
uint8_t flowLabel[3]
Definition: IPv6Layer.h:35
uint8_t ipDst[16]
Definition: IPv6Layer.h:45
size_t getHeaderLen() const
Definition: IPv6Layer.h:165
Definition: IpAddress.h:140
IPv6Address getSrcIpAddress() const
Definition: IPv6Layer.h:103
uint8_t hopLimit
Definition: IPv6Layer.h:41
IPv6Extension * getNextHeader() const
Definition: IPv6Extensions.h:66
TIPv6Extension * getExtensionOfType() const
Definition: IPv6Layer.h:191
ip6_hdr * getIPv6Header() const
Definition: IPv6Layer.h:97
uint8_t ipVersion
Definition: IPv6Layer.h:25
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: IPv6Layer.h:234
IPv6Address getDstIpAddress() const
Definition: IPv6Layer.h:109
OsiModelLayer getOsiModelLayer() const
Definition: IPv6Layer.h:177
uint8_t nextHeader
Definition: IPv6Layer.h:39