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  {
36  #if (BYTE_ORDER == LITTLE_ENDIAN)
37 
38  uint8_t trafficClass:4,
40  ipVersion:4;
41  #else
42 
43  uint8_t ipVersion:4,
45  trafficClass:4;
46  #endif
47 
48  uint8_t flowLabel[3];
50  uint16_t payloadLength;
52  uint8_t nextHeader;
54  uint8_t hopLimit;
56  uint8_t ipSrc[16];
58  uint8_t ipDst[16];
59  };
60 #pragma pack(pop)
61 
62 
67  class IPv6Layer : public Layer, public IPLayer
68  {
69  public:
77  IPv6Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
78 
82  IPv6Layer();
83 
89  IPv6Layer(const IPv6Address& srcIP, const IPv6Address& dstIP);
90 
94  IPv6Layer(const IPv6Layer& other);
95 
99  ~IPv6Layer();
100 
104  IPv6Layer& operator=(const IPv6Layer& other);
105 
110  ip6_hdr* getIPv6Header() const { return (ip6_hdr*)m_Data; }
111 
117  IPAddress getSrcIPAddress() const { return getSrcIPv6Address(); }
118 
122  PCPP_DEPRECATED IPv6Address getSrcIpAddress() const { return getSrcIPv6Address(); }
123 
128  IPv6Address getSrcIPv6Address() const { return getIPv6Header()->ipSrc; }
129 
134  void setSrcIPv6Address(const IPv6Address& ipAddr) { ipAddr.copyTo(getIPv6Header()->ipSrc); }
135 
136 
141  void setDstIPv6Address(const IPv6Address& ipAddr) { ipAddr.copyTo(getIPv6Header()->ipDst); }
142 
148  IPAddress getDstIPAddress() const { return getDstIPv6Address(); }
149 
153  PCPP_DEPRECATED IPv6Address getDstIpAddress() const { return getDstIPv6Address(); }
154 
159  IPv6Address getDstIPv6Address() const { return getIPv6Header()->ipDst; }
160 
164  size_t getExtensionCount() const;
165 
171  template<class TIPv6Extension>
172  TIPv6Extension* getExtensionOfType() const;
173 
183  template<class TIPv6Extension>
184  TIPv6Extension* addExtension(const TIPv6Extension& extensionHeader);
185 
189  void removeAllExtensions();
190 
194  bool isFragment() const;
195 
202  static inline bool isDataValid(const uint8_t* data, size_t dataLen);
203 
204 
205  // implement abstract methods
206 
219  void parseNextLayer();
220 
224  size_t getHeaderLen() const { return sizeof(ip6_hdr) + m_ExtensionsLen; }
225 
232  void computeCalculateFields();
233 
234  std::string toString() const;
235 
237 
238  private:
239  void initLayer();
240  void parseExtensions();
241  void deleteExtensions();
242 
243  IPv6Extension* m_FirstExtension;
244  IPv6Extension* m_LastExtension;
245  size_t m_ExtensionsLen;
246  };
247 
248 
249  template<class TIPv6Extension>
250  TIPv6Extension* IPv6Layer::getExtensionOfType() const
251  {
252  IPv6Extension* curExt = m_FirstExtension;
253  while (curExt != NULL && dynamic_cast<TIPv6Extension*>(curExt) == NULL)
254  curExt = curExt->getNextHeader();
255 
256  return (TIPv6Extension*)curExt;
257  }
258 
259  template<class TIPv6Extension>
260  TIPv6Extension* IPv6Layer::addExtension(const TIPv6Extension& extensionHeader)
261  {
262  int offsetToAddHeader = (int)getHeaderLen();
263  if (!extendLayer(offsetToAddHeader, extensionHeader.getExtensionLen()))
264  {
265  return NULL;
266  }
267 
268  TIPv6Extension* newHeader = new TIPv6Extension(this, (size_t)offsetToAddHeader);
269  (*newHeader) = extensionHeader;
270 
271  if (m_FirstExtension != NULL)
272  {
273  newHeader->getBaseHeader()->nextHeader = m_LastExtension->getBaseHeader()->nextHeader;
274  m_LastExtension->getBaseHeader()->nextHeader = newHeader->getExtensionType();
275  m_LastExtension->setNextHeader(newHeader);
276  m_LastExtension = newHeader;
277  }
278  else
279  {
280  m_FirstExtension = newHeader;
281  m_LastExtension = newHeader;
282  newHeader->getBaseHeader()->nextHeader = getIPv6Header()->nextHeader;
283  getIPv6Header()->nextHeader = newHeader->getExtensionType();
284  }
285 
286  m_ExtensionsLen += newHeader->getExtensionLen();
287 
288  return newHeader;
289  }
290 
291  // implementation of inline methods
292 
293  bool IPv6Layer::isDataValid(const uint8_t* data, size_t dataLen)
294  {
295  return dataLen >= sizeof(ip6_hdr);
296  }
297 
298 } // namespace pcpp
299 
300 #endif /* PACKETPP_IPV6_LAYER */
IPAddress getSrcIPAddress() const
Definition: IPv6Layer.h:117
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:258
Definition: IPv6Extensions.h:23
Definition: Layer.h:70
Definition: IPLayer.h:22
uint8_t ipSrc[16]
Definition: IPv6Layer.h:56
Definition: Packet.h:26
uint16_t payloadLength
Definition: IPv6Layer.h:50
TIPv6Extension * addExtension(const TIPv6Extension &extensionHeader)
Definition: IPv6Layer.h:260
Definition: IPv6Layer.h:34
Definition: IPv6Layer.h:67
uint8_t trafficClass
Definition: IPv6Layer.h:38
Definition: ProtocolType.h:265
uint8_t flowLabel[3]
Definition: IPv6Layer.h:48
uint8_t ipDst[16]
Definition: IPv6Layer.h:58
size_t getHeaderLen() const
Definition: IPv6Layer.h:224
Definition: IpAddress.h:167
uint8_t hopLimit
Definition: IPv6Layer.h:54
IPv6Extension * getNextHeader() const
Definition: IPv6Extensions.h:66
TIPv6Extension * getExtensionOfType() const
Definition: IPv6Layer.h:250
IPv6Address getDstIPv6Address() const
Definition: IPv6Layer.h:159
ip6_hdr * getIPv6Header() const
Definition: IPv6Layer.h:110
void setSrcIPv6Address(const IPv6Address &ipAddr)
Definition: IPv6Layer.h:134
void setDstIPv6Address(const IPv6Address &ipAddr)
Definition: IPv6Layer.h:141
uint8_t ipVersion
Definition: IPv6Layer.h:38
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: IPv6Layer.h:293
IPAddress getDstIPAddress() const
Definition: IPv6Layer.h:148
void copyTo(uint8_t **arr, size_t &length) const
Definition: IpAddress.h:278
IPv6Address getSrcIPv6Address() const
Definition: IPv6Layer.h:128
OsiModelLayer getOsiModelLayer() const
Definition: IPv6Layer.h:236
uint8_t nextHeader
Definition: IPv6Layer.h:52