PcapPlusPlus
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  inline ip6_hdr* getIPv6Header() { return (ip6_hdr*)m_Data; }
98 
103  inline IPv6Address getSrcIpAddress() { return IPv6Address(getIPv6Header()->ipSrc); }
104 
109  inline IPv6Address getDstIpAddress() { return IPv6Address(getIPv6Header()->ipDst); }
110 
114  size_t getExtensionCount();
115 
121  template<class TIPv6Extension>
122  TIPv6Extension* getExtensionOfType();
123 
133  template<class TIPv6Extension>
134  TIPv6Extension* addExtension(const TIPv6Extension& extensionHeader);
135 
139  void removeAllExtensions();
140 
144  bool isFragment();
145 
146 
147  // implement abstract methods
148 
152  void parseNextLayer();
153 
157  inline size_t getHeaderLen() { return sizeof(ip6_hdr) + m_ExtensionsLen; }
158 
165  void computeCalculateFields();
166 
167  std::string toString();
168 
170 
171  private:
172  void initLayer();
173  void parseExtensions();
174  void deleteExtensions();
175 
176  IPv6Extension* m_FirstExtension;
177  IPv6Extension* m_LastExtension;
178  size_t m_ExtensionsLen;
179  };
180 
181 
182  template<class TIPv6Extension>
184  {
185  IPv6Extension* curExt = m_FirstExtension;
186  while (curExt != NULL && dynamic_cast<TIPv6Extension*>(curExt) == NULL)
187  curExt = curExt->getNextHeader();
188 
189  return (TIPv6Extension*)curExt;
190  }
191 
192  template<class TIPv6Extension>
193  TIPv6Extension* IPv6Layer::addExtension(const TIPv6Extension& extensionHeader)
194  {
195  int offsetToAddHeader = (int)getHeaderLen();
196  if (!extendLayer(offsetToAddHeader, extensionHeader.getExtensionLen()))
197  {
198  return NULL;
199  }
200 
201  TIPv6Extension* newHeader = new TIPv6Extension(this, (size_t)offsetToAddHeader);
202  (*newHeader) = extensionHeader;
203 
204  if (m_FirstExtension != NULL)
205  {
206  newHeader->getBaseHeader()->nextHeader = m_LastExtension->getBaseHeader()->nextHeader;
207  m_LastExtension->getBaseHeader()->nextHeader = newHeader->getExtensionType();
208  m_LastExtension->setNextHeader(newHeader);
209  m_LastExtension = newHeader;
210  }
211  else
212  {
213  m_FirstExtension = newHeader;
214  m_LastExtension = newHeader;
215  newHeader->getBaseHeader()->nextHeader = getIPv6Header()->nextHeader;
216  getIPv6Header()->nextHeader = newHeader->getExtensionType();
217  }
218 
219  m_ExtensionsLen += newHeader->getExtensionLen();
220 
221  return newHeader;
222  }
223 
224 } // namespace pcpp
225 
226 #endif /* PACKETPP_IPV6_LAYER */
uint8_t nextHeader
Definition: IPv6Layer.h:39
TIPv6Extension * getExtensionOfType()
Definition: IPv6Layer.h:183
uint16_t payloadLength
Definition: IPv6Layer.h:37
OsiModelLayer getOsiModelLayer()
Definition: IPv6Layer.h:169
uint8_t ipSrc[16]
Definition: IPv6Layer.h:43
Definition: IPv6Layer.h:54
std::string toString()
void removeAllExtensions()
TIPv6Extension * addExtension(const TIPv6Extension &extensionHeader)
Definition: IPv6Layer.h:193
ip6_hdr * getIPv6Header()
Definition: IPv6Layer.h:97
Definition: Layer.h:70
uint8_t ipVersion
Definition: IPv6Layer.h:25
size_t getHeaderLen()
Definition: IPv6Layer.h:157
Definition: Packet.h:26
void computeCalculateFields()
OsiModelLayer
Definition: ProtocolType.h:213
Definition: IPv6Layer.h:22
uint8_t trafficClass
Definition: IPv6Layer.h:25
Definition: ProtocolType.h:220
IPv6Address getDstIpAddress()
Definition: IPv6Layer.h:109
IPv6Address getSrcIpAddress()
Definition: IPv6Layer.h:103
Definition: IpAddress.h:212
uint8_t flowLabel[3]
Definition: IPv6Layer.h:35
size_t getExtensionCount()
IPv6Layer & operator=(const IPv6Layer &other)
void parseNextLayer()
IPv6Extension * getNextHeader()
Definition: IPv6Extensions.h:66
uint8_t ipDst[16]
Definition: IPv6Layer.h:45
Definition: IPv6Extensions.h:23
The main namespace for the PcapPlusPlus lib.
uint8_t hopLimit
Definition: IPv6Layer.h:41