PcapPlusPlus  24.09
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 
52  {}
53 
59  {
60  if (m_Data == nullptr)
61  return NDPNeighborOptionTypes::NDP_OPTION_UNKNOWN;
62 
63  return static_cast<NDPNeighborOptionTypes>(m_Data->recordType);
64  }
65 
66  // implement abstract methods
67 
68  size_t getTotalSize() const
69  {
70  if (m_Data == nullptr)
71  return (size_t)0;
72 
73  return (size_t)m_Data->recordLen * 8;
74  }
75 
76  size_t getDataSize() const
77  {
78  if (m_Data == nullptr)
79  {
80  return 0;
81  }
82 
83  // length value is stored in units of 8 octets
84  return (size_t)m_Data->recordLen * 8 - (2 * sizeof(uint8_t));
85  }
86  };
87 
94  {
95  public:
104  NdpOptionBuilder(NDPNeighborOptionTypes optionType, const uint8_t* optionValue, uint8_t optionValueLen)
105  : TLVRecordBuilder((uint8_t)optionType, optionValue, optionValueLen)
106  {}
107 
113  NdpOption build() const;
114  };
115 
120  class NDPLayerBase : public IcmpV6Layer
121  {
122  public:
123  virtual ~NDPLayerBase()
124  {}
125 
129  size_t getNdpOptionCount() const;
130 
138 
144 
153 
161  NdpOption addNdpOption(const NdpOptionBuilder& optionBuilder);
162 
169 
170  protected:
171  NDPLayerBase() = default;
172 
173  NDPLayerBase(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
174  : IcmpV6Layer(data, dataLen, prevLayer, packet)
175  {}
176 
177  private:
178  TLVRecordReader<NdpOption> m_OptionReader;
179 
180  virtual size_t getNdpHeaderLen() const = 0;
181  virtual uint8_t* getNdpOptionsBasePtr() const
182  {
183  return m_Data + getNdpHeaderLen();
184  };
185  NdpOption addNdpOptionAt(const NdpOptionBuilder& optionBuilder, int offset);
186  };
187 
193  {
194  public:
199 #pragma pack(push, 1)
201  {
203  uint32_t reserved;
205  uint8_t targetIP[16];
206  };
207 #pragma pack(pop)
208 
216  NDPNeighborSolicitationLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
217  : NDPLayerBase(data, dataLen, prevLayer, packet)
218  {}
219 
225  NDPNeighborSolicitationLayer(uint8_t code, const IPv6Address& targetIP);
226 
233  NDPNeighborSolicitationLayer(uint8_t code, const IPv6Address& targetIP, const MacAddress& srcMac);
234 
236  {}
237 
242  {
243  return IPv6Address(getNdpHeader()->targetIP);
244  };
245 
250  bool hasLinkLayerAddress() const;
251 
257 
258  std::string toString() const;
259 
260  private:
261  void initLayer(uint8_t code, const IPv6Address& targetIP);
262  ndpneighborsolicitationhdr* getNdpHeader() const
263  {
264  return (ndpneighborsolicitationhdr*)m_Data;
265  }
266  size_t getNdpHeaderLen() const
267  {
268  return sizeof(ndpneighborsolicitationhdr);
269  };
270  };
271 
277  {
278  public:
283 #pragma pack(push, 1)
285  {
286 #if (BYTE_ORDER == LITTLE_ENDIAN)
287  uint32_t
289  reserved : 5,
291  override : 1,
296  router : 1,
298  reserved2 : 24;
299 #else
300  uint32_t
302  router : 1,
305  solicited : 1,
307  override : 1,
309  reserved : 29;
310 #endif
312  uint8_t targetIP[16];
313  };
314 #pragma pack(pop)
315 
323  NDPNeighborAdvertisementLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
324  : NDPLayerBase(data, dataLen, prevLayer, packet)
325  {}
326 
337  NDPNeighborAdvertisementLayer(uint8_t code, const IPv6Address& targetIP, const MacAddress& targetMac,
338  bool routerFlag, bool unicastFlag, bool overrideFlag);
339 
349  NDPNeighborAdvertisementLayer(uint8_t code, const IPv6Address& targetIP, bool routerFlag, bool unicastFlag,
350  bool overrideFlag);
351 
353  {}
354 
359 
364  {
365  return IPv6Address(getNdpHeader()->targetIP);
366  }
367 
371  bool hasTargetMacInfo() const;
372 
376  bool getRouterFlag() const
377  {
378  return getNdpHeader()->router;
379  }
380 
384  bool getUnicastFlag() const
385  {
386  return getNdpHeader()->solicited;
387  }
388 
392  bool getOverrideFlag() const
393  {
394  return getNdpHeader()->override;
395  }
396 
397  std::string toString() const;
398 
399  private:
400  void initLayer(uint8_t code, const IPv6Address& targetIP, bool routerFlag, bool unicastFlag, bool overrideFlag);
401  ndpneighboradvertisementhdr* getNdpHeader() const
402  {
403  return (ndpneighboradvertisementhdr*)m_Data;
404  }
405  size_t getNdpHeaderLen() const
406  {
407  return sizeof(ndpneighboradvertisementhdr);
408  };
409  };
410 
411 } // namespace pcpp
Definition: IpAddress.h:199
Definition: IcmpV6Layer.h:133
Definition: Layer.h:70
Definition: MacAddress.h:25
Definition: NdpLayer.h:121
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:277
IPv6Address getTargetIP() const
Definition: NdpLayer.h:363
NDPNeighborAdvertisementLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: NdpLayer.h:323
bool getRouterFlag() const
Definition: NdpLayer.h:376
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:384
bool getOverrideFlag() const
Definition: NdpLayer.h:392
Definition: NdpLayer.h:193
NDPNeighborSolicitationLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: NdpLayer.h:216
IPv6Address getTargetIP() const
Definition: NdpLayer.h:241
NDPNeighborSolicitationLayer(uint8_t code, const IPv6Address &targetIP)
MacAddress getLinkLayerAddress() const
NDPNeighborSolicitationLayer(uint8_t code, const IPv6Address &targetIP, const MacAddress &srcMac)
Definition: NdpLayer.h:94
NdpOption build() const
NdpOptionBuilder(NDPNeighborOptionTypes optionType, const uint8_t *optionValue, uint8_t optionValueLen)
Definition: NdpLayer.h:104
Definition: NdpLayer.h:39
NDPNeighborOptionTypes getNdpOptionType() const
Definition: NdpLayer.h:58
~NdpOption()
Definition: NdpLayer.h:51
size_t getTotalSize() const
Definition: NdpLayer.h:68
size_t getDataSize() const
Definition: NdpLayer.h:76
NdpOption(uint8_t *optionRawData)
Definition: NdpLayer.h:45
Definition: Packet.h:27
Definition: TLVData.h:413
Definition: TLVData.h:23
Definition: TLVData.h:239
The main namespace for the PcapPlusPlus lib.
NDPNeighborOptionTypes
Definition: NdpLayer.h:24
Definition: IcmpV6Layer.h:103