PcapPlusPlus  19.12
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  ip6_hdr* getIPv6Header() const { return (ip6_hdr*)m_Data; }
98 
103  IPv6Address getSrcIpAddress() const { return IPv6Address(getIPv6Header()->ipSrc); }
104 
109  IPv6Address getDstIpAddress() const { return IPv6Address(getIPv6Header()->ipDst); }
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 
146 
147  // implement abstract methods
148 
152  void parseNextLayer();
153 
157  size_t getHeaderLen() const { return sizeof(ip6_hdr) + m_ExtensionsLen; }
158 
165  void computeCalculateFields();
166 
167  std::string toString() const;
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>
183  TIPv6Extension* IPv6Layer::getExtensionOfType() const
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 */
IPv6Extensions.h
pcpp::OsiModelLayer
OsiModelLayer
Definition: ProtocolType.h:224
pcpp::IPv6Address
Definition: IpAddress.h:229
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:193
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
Definition: ProtocolType.h:231
pcpp::IPv6Extension
Definition: IPv6Extensions.h:23
pcpp::ip6_hdr::flowLabel
uint8_t flowLabel[3]
Definition: IPv6Layer.h:35
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:25
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:183
pcpp::IPv6Layer
Definition: IPv6Layer.h:54
pcpp::IPv6Layer::getHeaderLen
size_t getHeaderLen() const
Definition: IPv6Layer.h:157
pcpp::IPv6Layer::getOsiModelLayer
OsiModelLayer getOsiModelLayer() const
Definition: IPv6Layer.h:169
pcpp::IPv6Layer::IPv6Layer
IPv6Layer()
pcpp::IPv6Layer::isFragment
bool isFragment() const
pcpp::ip6_hdr::nextHeader
uint8_t nextHeader
Definition: IPv6Layer.h:39