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,
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 
104 
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 */
IPv6Extensions.h
pcpp::OsiModelLayer
OsiModelLayer
Definition: ProtocolType.h:233
pcpp::IPv6Address
Definition: IpAddress.h:140
pcpp::Packet
Definition: Packet.h:26
pcpp::IPv6Layer::getIPv6Header
ip6_hdr * getIPv6Header() const
Definition: IPv6Layer.h:97
pcpp::IPv6Layer::parseNextLayer
void parseNextLayer()
pcpp::IPv6Layer::computeCalculateFields
void computeCalculateFields()
Layer.h
pcpp::IPv6Layer::addExtension
TIPv6Extension * addExtension(const TIPv6Extension &extensionHeader)
Definition: IPv6Layer.h:201
pcpp::ip6_hdr::ipSrc
uint8_t ipSrc[16]
Definition: IPv6Layer.h:43
pcpp::IPv6Layer::getDstIpAddress
IPv6Address getDstIpAddress() const
Definition: IPv6Layer.h:109
pcpp::ip6_hdr::payloadLength
uint16_t payloadLength
Definition: IPv6Layer.h:37
pcpp::IPv6Layer::toString
std::string toString() const
pcpp::IPv6Layer::getSrcIpAddress
IPv6Address getSrcIpAddress() const
Definition: IPv6Layer.h:103
pcpp::OsiModelNetworkLayer
@ OsiModelNetworkLayer
Definition: ProtocolType.h:240
pcpp::IPv6Extension
Definition: IPv6Extensions.h:23
pcpp::ip6_hdr::flowLabel
uint8_t flowLabel[3]
Definition: IPv6Layer.h:35
pcpp::IPv6Layer::isDataValid
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: IPv6Layer.h:234
pcpp::ip6_hdr::trafficClass
uint8_t trafficClass
Definition: IPv6Layer.h:25
pcpp::IPv6Layer::removeAllExtensions
void removeAllExtensions()
pcpp::ip6_hdr::ipDst
uint8_t ipDst[16]
Definition: IPv6Layer.h:45
pcpp::ip6_hdr::hopLimit
uint8_t hopLimit
Definition: IPv6Layer.h:41
IpAddress.h
pcpp
The main namespace for the PcapPlusPlus lib.
pcpp::IPv6Layer::getExtensionCount
size_t getExtensionCount() const
pcpp::Layer
Definition: Layer.h:70
pcpp::ip6_hdr::ipVersion
uint8_t ipVersion
Definition: IPv6Layer.h:27
pcpp::IPv6Layer::operator=
IPv6Layer & operator=(const IPv6Layer &other)
pcpp::ip6_hdr
Definition: IPv6Layer.h:22
pcpp::IPv6Extension::getNextHeader
IPv6Extension * getNextHeader() const
Definition: IPv6Extensions.h:66
pcpp::IPv6Layer::~IPv6Layer
~IPv6Layer()
pcpp::IPv6Layer::getExtensionOfType
TIPv6Extension * getExtensionOfType() const
Definition: IPv6Layer.h:191
pcpp::IPv6Layer
Definition: IPv6Layer.h:54
pcpp::IPv6Layer::getHeaderLen
size_t getHeaderLen() const
Definition: IPv6Layer.h:165
pcpp::IPv6Layer::getOsiModelLayer
OsiModelLayer getOsiModelLayer() const
Definition: IPv6Layer.h:177
pcpp::IPv6Layer::IPv6Layer
IPv6Layer()
pcpp::IPv6Layer::isFragment
bool isFragment() const
pcpp::ip6_hdr::nextHeader
uint8_t nextHeader
Definition: IPv6Layer.h:39