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 
134  IPAddress getDstIPAddress() const { return getDstIPv6Address(); }
135 
139  PCPP_DEPRECATED IPv6Address getDstIpAddress() const { return getDstIPv6Address(); }
140 
145  IPv6Address getDstIPv6Address() const { return getIPv6Header()->ipDst; }
146 
150  size_t getExtensionCount() const;
151 
157  template<class TIPv6Extension>
158  TIPv6Extension* getExtensionOfType() const;
159 
169  template<class TIPv6Extension>
170  TIPv6Extension* addExtension(const TIPv6Extension& extensionHeader);
171 
175  void removeAllExtensions();
176 
180  bool isFragment() const;
181 
188  static inline bool isDataValid(const uint8_t* data, size_t dataLen);
189 
190 
191  // implement abstract methods
192 
205  void parseNextLayer();
206 
210  size_t getHeaderLen() const { return sizeof(ip6_hdr) + m_ExtensionsLen; }
211 
218  void computeCalculateFields();
219 
220  std::string toString() const;
221 
223 
224  private:
225  void initLayer();
226  void parseExtensions();
227  void deleteExtensions();
228 
229  IPv6Extension* m_FirstExtension;
230  IPv6Extension* m_LastExtension;
231  size_t m_ExtensionsLen;
232  };
233 
234 
235  template<class TIPv6Extension>
236  TIPv6Extension* IPv6Layer::getExtensionOfType() const
237  {
238  IPv6Extension* curExt = m_FirstExtension;
239  while (curExt != NULL && dynamic_cast<TIPv6Extension*>(curExt) == NULL)
240  curExt = curExt->getNextHeader();
241 
242  return (TIPv6Extension*)curExt;
243  }
244 
245  template<class TIPv6Extension>
246  TIPv6Extension* IPv6Layer::addExtension(const TIPv6Extension& extensionHeader)
247  {
248  int offsetToAddHeader = (int)getHeaderLen();
249  if (!extendLayer(offsetToAddHeader, extensionHeader.getExtensionLen()))
250  {
251  return NULL;
252  }
253 
254  TIPv6Extension* newHeader = new TIPv6Extension(this, (size_t)offsetToAddHeader);
255  (*newHeader) = extensionHeader;
256 
257  if (m_FirstExtension != NULL)
258  {
259  newHeader->getBaseHeader()->nextHeader = m_LastExtension->getBaseHeader()->nextHeader;
260  m_LastExtension->getBaseHeader()->nextHeader = newHeader->getExtensionType();
261  m_LastExtension->setNextHeader(newHeader);
262  m_LastExtension = newHeader;
263  }
264  else
265  {
266  m_FirstExtension = newHeader;
267  m_LastExtension = newHeader;
268  newHeader->getBaseHeader()->nextHeader = getIPv6Header()->nextHeader;
269  getIPv6Header()->nextHeader = newHeader->getExtensionType();
270  }
271 
272  m_ExtensionsLen += newHeader->getExtensionLen();
273 
274  return newHeader;
275  }
276 
277  // implementation of inline methods
278 
279  bool IPv6Layer::isDataValid(const uint8_t* data, size_t dataLen)
280  {
281  return dataLen >= sizeof(ip6_hdr);
282  }
283 
284 } // namespace pcpp
285 
286 #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:246
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:210
Definition: IpAddress.h:160
uint8_t hopLimit
Definition: IPv6Layer.h:53
IPv6Extension * getNextHeader() const
Definition: IPv6Extensions.h:66
TIPv6Extension * getExtensionOfType() const
Definition: IPv6Layer.h:236
IPv6Address getDstIPv6Address() const
Definition: IPv6Layer.h:145
ip6_hdr * getIPv6Header() const
Definition: IPv6Layer.h:109
uint8_t ipVersion
Definition: IPv6Layer.h:37
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: IPv6Layer.h:279
IPAddress getDstIPAddress() const
Definition: IPv6Layer.h:134
Definition: IpAddress.h:265
IPv6Address getSrcIPv6Address() const
Definition: IPv6Layer.h:127
OsiModelLayer getOsiModelLayer() const
Definition: IPv6Layer.h:222
uint8_t nextHeader
Definition: IPv6Layer.h:51