PcapPlusPlus  Next
NdpLayer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "IcmpV6Layer.h"
4 #include "IpAddress.h"
5 #include "Layer.h"
6 #include "MacAddress.h"
7 #include "TLVData.h"
8 
9 #include <vector>
10 
12 
17 namespace pcpp
18 {
19 
23  enum class NDPNeighborOptionTypes : int
24  {
25  NDP_OPTION_SOURCE_LINK_LAYER = 1,
26  NDP_OPTION_TARGET_LINK_LAYER = 2,
27  NDP_OPTION_PREFIX_INFORMATION = 3,
28  NDP_OPTION_REDIRECTED_HEADER = 4,
29  NDP_OPTION_MTU = 5,
30  NDP_OPTION_UNKNOWN = 255
31  };
32 
38  class NdpOption : public TLVRecord<uint8_t, uint8_t>
39  {
40  public:
45  explicit NdpOption(uint8_t* optionRawData) : TLVRecord(optionRawData)
46  {}
47 
51  ~NdpOption() override = default;
52 
58  {
59  if (m_Data == nullptr)
60  return NDPNeighborOptionTypes::NDP_OPTION_UNKNOWN;
61 
62  return static_cast<NDPNeighborOptionTypes>(m_Data->recordType);
63  }
64 
65  // implement abstract methods
66 
67  size_t getTotalSize() const override
68  {
69  if (m_Data == nullptr)
70  return 0;
71 
72  return static_cast<size_t>(m_Data->recordLen) * 8;
73  }
74 
75  size_t getDataSize() const override
76  {
77  if (m_Data == nullptr)
78  {
79  return 0;
80  }
81 
82  // length value is stored in units of 8 octets
83  return static_cast<size_t>(m_Data->recordLen) * 8 - (2 * sizeof(uint8_t));
84  }
85  };
86 
93  {
94  public:
103  NdpOptionBuilder(NDPNeighborOptionTypes optionType, const uint8_t* optionValue, uint8_t optionValueLen)
104  : TLVRecordBuilder((uint8_t)optionType, optionValue, optionValueLen)
105  {}
106 
112  NdpOption build() const;
113  };
114 
119  class NDPLayerBase : public IcmpV6Layer
120  {
121  public:
122  ~NDPLayerBase() override = default;
123 
127  size_t getNdpOptionCount() const;
128 
136 
142 
151 
159  NdpOption addNdpOption(const NdpOptionBuilder& optionBuilder);
160 
167 
168  protected:
169  NDPLayerBase() = default;
170 
171  NDPLayerBase(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
172  : IcmpV6Layer(data, dataLen, prevLayer, packet)
173  {}
174 
175  private:
176  TLVRecordReader<NdpOption> m_OptionReader;
177 
178  virtual size_t getNdpHeaderLen() const = 0;
179  virtual uint8_t* getNdpOptionsBasePtr() const
180  {
181  return m_Data + getNdpHeaderLen();
182  };
183  NdpOption addNdpOptionAt(const NdpOptionBuilder& optionBuilder, int offset);
184  };
185 
191  {
192  public:
197 #pragma pack(push, 1)
199  {
201  uint32_t reserved;
203  uint8_t targetIP[16];
204  };
205 #pragma pack(pop)
206 
214  NDPNeighborSolicitationLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
215  : NDPLayerBase(data, dataLen, prevLayer, packet)
216  {}
217 
223  NDPNeighborSolicitationLayer(uint8_t code, const IPv6Address& targetIP);
224 
231  NDPNeighborSolicitationLayer(uint8_t code, const IPv6Address& targetIP, const MacAddress& srcMac);
232 
233  ~NDPNeighborSolicitationLayer() override = default;
234 
239  {
240  return IPv6Address(getNdpHeader()->targetIP);
241  };
242 
247  bool hasLinkLayerAddress() const;
248 
254 
255  std::string toString() const override;
256 
257  private:
258  void initLayer(uint8_t code, const IPv6Address& targetIP);
259  ndpneighborsolicitationhdr* getNdpHeader() const
260  {
261  return reinterpret_cast<ndpneighborsolicitationhdr*>(m_Data);
262  }
263  size_t getNdpHeaderLen() const override
264  {
265  return sizeof(ndpneighborsolicitationhdr);
266  };
267  };
268 
274  {
275  public:
280 #pragma pack(push, 1)
282  {
283 #if (BYTE_ORDER == LITTLE_ENDIAN)
284  uint32_t
286  reserved : 5,
288  override : 1,
293  router : 1,
295  reserved2 : 24;
296 #else
297  uint32_t
299  router : 1,
302  solicited : 1,
304  override : 1,
306  reserved : 29;
307 #endif
309  uint8_t targetIP[16];
310  };
311 #pragma pack(pop)
312 
320  NDPNeighborAdvertisementLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
321  : NDPLayerBase(data, dataLen, prevLayer, packet)
322  {}
323 
334  NDPNeighborAdvertisementLayer(uint8_t code, const IPv6Address& targetIP, const MacAddress& targetMac,
335  bool routerFlag, bool unicastFlag, bool overrideFlag);
336 
346  NDPNeighborAdvertisementLayer(uint8_t code, const IPv6Address& targetIP, bool routerFlag, bool unicastFlag,
347  bool overrideFlag);
348 
349  ~NDPNeighborAdvertisementLayer() override = default;
350 
355 
360  {
361  return IPv6Address(getNdpHeader()->targetIP);
362  }
363 
367  bool hasTargetMacInfo() const;
368 
372  bool getRouterFlag() const
373  {
374  return getNdpHeader()->router;
375  }
376 
380  bool getUnicastFlag() const
381  {
382  return getNdpHeader()->solicited;
383  }
384 
388  bool getOverrideFlag() const
389  {
390  return getNdpHeader()->override;
391  }
392 
393  std::string toString() const override;
394 
395  private:
396  void initLayer(uint8_t code, const IPv6Address& targetIP, bool routerFlag, bool unicastFlag, bool overrideFlag);
397  ndpneighboradvertisementhdr* getNdpHeader() const
398  {
399  return reinterpret_cast<ndpneighboradvertisementhdr*>(m_Data);
400  }
401  size_t getNdpHeaderLen() const override
402  {
403  return sizeof(ndpneighboradvertisementhdr);
404  };
405  };
406 
407 } // namespace pcpp
Definition: IpAddress.h:199
Definition: IcmpV6Layer.h:133
Definition: Layer.h:69
Definition: MacAddress.h:25
Definition: NdpLayer.h:120
NdpOption getFirstNdpOption() const
size_t getNdpOptionCount() const
bool removeAllNdpOptions()
NdpOption getNdpOption(NDPNeighborOptionTypes option) const
NdpOption addNdpOption(const NdpOptionBuilder &optionBuilder)
NdpOption getNextNdpOption(NdpOption &option) const
Definition: NdpLayer.h:274
IPv6Address getTargetIP() const
Definition: NdpLayer.h:359
NDPNeighborAdvertisementLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: NdpLayer.h:320
std::string toString() const override
bool getRouterFlag() const
Definition: NdpLayer.h:372
NDPNeighborAdvertisementLayer(uint8_t code, const IPv6Address &targetIP, const MacAddress &targetMac, bool routerFlag, bool unicastFlag, bool overrideFlag)
NDPNeighborAdvertisementLayer(uint8_t code, const IPv6Address &targetIP, bool routerFlag, bool unicastFlag, bool overrideFlag)
bool getUnicastFlag() const
Definition: NdpLayer.h:380
bool getOverrideFlag() const
Definition: NdpLayer.h:388
Definition: NdpLayer.h:191
NDPNeighborSolicitationLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: NdpLayer.h:214
IPv6Address getTargetIP() const
Definition: NdpLayer.h:238
NDPNeighborSolicitationLayer(uint8_t code, const IPv6Address &targetIP)
MacAddress getLinkLayerAddress() const
std::string toString() const override
NDPNeighborSolicitationLayer(uint8_t code, const IPv6Address &targetIP, const MacAddress &srcMac)
Definition: NdpLayer.h:93
NdpOption build() const
NdpOptionBuilder(NDPNeighborOptionTypes optionType, const uint8_t *optionValue, uint8_t optionValueLen)
Definition: NdpLayer.h:103
Definition: NdpLayer.h:39
NDPNeighborOptionTypes getNdpOptionType() const
Definition: NdpLayer.h:57
~NdpOption() override=default
NdpOption(uint8_t *optionRawData)
Definition: NdpLayer.h:45
size_t getDataSize() const override
Definition: NdpLayer.h:75
size_t getTotalSize() const override
Definition: NdpLayer.h:67
Definition: Packet.h:27
Definition: TLVData.h:419
Definition: TLVData.h:23
Definition: TLVData.h:246
The main namespace for the PcapPlusPlus lib.
NDPNeighborOptionTypes
Definition: NdpLayer.h:24
Definition: IcmpV6Layer.h:103