PcapPlusPlus  21.05
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 
9 #ifndef PCPP_DEPRECATED
10 #if defined(__GNUC__) || defined(__clang__)
11 #define PCPP_DEPRECATED __attribute__((deprecated))
12 #elif defined(_MSC_VER)
13 #define PCPP_DEPRECATED __declspec(deprecated)
14 #else
15 #pragma message("WARNING: DEPRECATED feature is not implemented for this compiler")
16 #define PCPP_DEPRECATED
17 #endif
18 #endif
19 
21 
26 namespace pcpp
27 {
28 
33 #pragma pack(push, 1)
34  struct ip6_hdr {
35  #if (BYTE_ORDER == LITTLE_ENDIAN)
36 
37  uint8_t trafficClass:4,
39  ipVersion:4;
40  #else
41 
42  uint8_t ipVersion:4,
44  trafficClass:4;
45  #endif
46 
47  uint8_t flowLabel[3];
49  uint16_t payloadLength;
51  uint8_t nextHeader;
53  uint8_t hopLimit;
55  uint8_t ipSrc[16];
57  uint8_t ipDst[16];
58  };
59 #pragma pack(pop)
60 
61 
66  class IPv6Layer : public Layer, public IPLayer
67  {
68  public:
76  IPv6Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
77 
81  IPv6Layer();
82 
88  IPv6Layer(const IPv6Address& srcIP, const IPv6Address& dstIP);
89 
93  IPv6Layer(const IPv6Layer& other);
94 
98  ~IPv6Layer();
99 
103  IPv6Layer& operator=(const IPv6Layer& other);
104 
109  ip6_hdr* getIPv6Header() const { return (ip6_hdr*)m_Data; }
110 
116  IPAddress getSrcIPAddress() const { return getSrcIPv6Address(); }
117 
121  PCPP_DEPRECATED IPv6Address getSrcIpAddress() const { return getSrcIPv6Address(); }
122 
127  IPv6Address getSrcIPv6Address() const { return getIPv6Header()->ipSrc; }
128 
133  void setSrcIPv6Address(const IPv6Address& ipAddr) { ipAddr.copyTo(getIPv6Header()->ipSrc); }
134 
135 
140  void setDstIPv6Address(const IPv6Address& ipAddr) { ipAddr.copyTo(getIPv6Header()->ipDst); }
141 
147  IPAddress getDstIPAddress() const { return getDstIPv6Address(); }
148 
152  PCPP_DEPRECATED IPv6Address getDstIpAddress() const { return getDstIPv6Address(); }
153 
158  IPv6Address getDstIPv6Address() const { return getIPv6Header()->ipDst; }
159 
163  size_t getExtensionCount() const;
164 
170  template<class TIPv6Extension>
171  TIPv6Extension* getExtensionOfType() const;
172 
182  template<class TIPv6Extension>
183  TIPv6Extension* addExtension(const TIPv6Extension& extensionHeader);
184 
188  void removeAllExtensions();
189 
193  bool isFragment() const;
194 
201  static inline bool isDataValid(const uint8_t* data, size_t dataLen);
202 
203 
204  // implement abstract methods
205 
218  void parseNextLayer();
219 
223  size_t getHeaderLen() const { return sizeof(ip6_hdr) + m_ExtensionsLen; }
224 
231  void computeCalculateFields();
232 
233  std::string toString() const;
234 
236 
237  private:
238  void initLayer();
239  void parseExtensions();
240  void deleteExtensions();
241 
242  IPv6Extension* m_FirstExtension;
243  IPv6Extension* m_LastExtension;
244  size_t m_ExtensionsLen;
245  };
246 
247 
248  template<class TIPv6Extension>
249  TIPv6Extension* IPv6Layer::getExtensionOfType() const
250  {
251  IPv6Extension* curExt = m_FirstExtension;
252  while (curExt != NULL && dynamic_cast<TIPv6Extension*>(curExt) == NULL)
253  curExt = curExt->getNextHeader();
254 
255  return (TIPv6Extension*)curExt;
256  }
257 
258  template<class TIPv6Extension>
259  TIPv6Extension* IPv6Layer::addExtension(const TIPv6Extension& extensionHeader)
260  {
261  int offsetToAddHeader = (int)getHeaderLen();
262  if (!extendLayer(offsetToAddHeader, extensionHeader.getExtensionLen()))
263  {
264  return NULL;
265  }
266 
267  TIPv6Extension* newHeader = new TIPv6Extension(this, (size_t)offsetToAddHeader);
268  (*newHeader) = extensionHeader;
269 
270  if (m_FirstExtension != NULL)
271  {
272  newHeader->getBaseHeader()->nextHeader = m_LastExtension->getBaseHeader()->nextHeader;
273  m_LastExtension->getBaseHeader()->nextHeader = newHeader->getExtensionType();
274  m_LastExtension->setNextHeader(newHeader);
275  m_LastExtension = newHeader;
276  }
277  else
278  {
279  m_FirstExtension = newHeader;
280  m_LastExtension = newHeader;
281  newHeader->getBaseHeader()->nextHeader = getIPv6Header()->nextHeader;
282  getIPv6Header()->nextHeader = newHeader->getExtensionType();
283  }
284 
285  m_ExtensionsLen += newHeader->getExtensionLen();
286 
287  return newHeader;
288  }
289 
290  // implementation of inline methods
291 
292  bool IPv6Layer::isDataValid(const uint8_t* data, size_t dataLen)
293  {
294  return dataLen >= sizeof(ip6_hdr);
295  }
296 
297 } // namespace pcpp
298 
299 #endif /* PACKETPP_IPV6_LAYER */
IPAddress getSrcIPAddress() const
Definition: IPv6Layer.h:116
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:253
Definition: IPv6Extensions.h:23
Definition: Layer.h:70
Definition: IPLayer.h:22
uint8_t ipSrc[16]
Definition: IPv6Layer.h:55
Definition: Packet.h:26
uint16_t payloadLength
Definition: IPv6Layer.h:49
TIPv6Extension * addExtension(const TIPv6Extension &extensionHeader)
Definition: IPv6Layer.h:259
Definition: IPv6Layer.h:34
Definition: IPv6Layer.h:66
uint8_t trafficClass
Definition: IPv6Layer.h:37
Definition: ProtocolType.h:260
uint8_t flowLabel[3]
Definition: IPv6Layer.h:47
uint8_t ipDst[16]
Definition: IPv6Layer.h:57
size_t getHeaderLen() const
Definition: IPv6Layer.h:223
Definition: IpAddress.h:160
uint8_t hopLimit
Definition: IPv6Layer.h:53
IPv6Extension * getNextHeader() const
Definition: IPv6Extensions.h:66
TIPv6Extension * getExtensionOfType() const
Definition: IPv6Layer.h:249
IPv6Address getDstIPv6Address() const
Definition: IPv6Layer.h:158
ip6_hdr * getIPv6Header() const
Definition: IPv6Layer.h:109
void setSrcIPv6Address(const IPv6Address &ipAddr)
Definition: IPv6Layer.h:133
void setDstIPv6Address(const IPv6Address &ipAddr)
Definition: IPv6Layer.h:140
uint8_t ipVersion
Definition: IPv6Layer.h:37
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: IPv6Layer.h:292
IPAddress getDstIPAddress() const
Definition: IPv6Layer.h:147
void copyTo(uint8_t **arr, size_t &length) const
Definition: IpAddress.h:265
IPv6Address getSrcIPv6Address() const
Definition: IPv6Layer.h:127
OsiModelLayer getOsiModelLayer() const
Definition: IPv6Layer.h:235
uint8_t nextHeader
Definition: IPv6Layer.h:51