PcapPlusPlus  23.09
NdpLayer.h
Go to the documentation of this file.
1 #ifndef PACKETPP_NDP_LAYER
2 #define PACKETPP_NDP_LAYER
3 
4 #include "IcmpV6Layer.h"
5 #include "IpAddress.h"
6 #include "Layer.h"
7 #include "MacAddress.h"
8 #include "TLVData.h"
9 
10 #include <vector>
11 
13 
18 namespace pcpp
19 {
20 
24 enum class NDPNeighborOptionTypes : int
25 {
26  NDP_OPTION_SOURCE_LINK_LAYER = 1,
27  NDP_OPTION_TARGET_LINK_LAYER = 2,
28  NDP_OPTION_PREFIX_INFORMATION = 3,
29  NDP_OPTION_REDIRECTED_HEADER = 4,
30  NDP_OPTION_MTU = 5,
31  NDP_OPTION_UNKNOWN = 255
32 };
33 
39 class NdpOption : public TLVRecord<uint8_t, uint8_t>
40 {
41 public:
46  explicit NdpOption(uint8_t *optionRawData) : TLVRecord(optionRawData) {}
47 
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
68  {
69  if (m_Data == nullptr)
70  return (size_t)0;
71 
72  return (size_t)m_Data->recordLen * 8;
73  }
74 
75  size_t getDataSize() const
76  {
77  if (m_Data == nullptr)
78  return 0;
79 
80  return (size_t)m_Data->recordLen * 8 - (2 * sizeof(uint8_t)); // length value is stored in units of 8 octets
81  }
82 };
83 
90 {
91 public:
100  NdpOptionBuilder(NDPNeighborOptionTypes optionType, const uint8_t *optionValue, uint8_t optionValueLen)
101  : TLVRecordBuilder((uint8_t)optionType, optionValue, optionValueLen) {}
102 
108  NdpOption build() const;
109 };
110 
115 class NDPLayerBase : public IcmpV6Layer
116 {
117 public:
118  virtual ~NDPLayerBase() {}
119 
123  size_t getNdpOptionCount() const;
124 
131  NdpOption getNdpOption(NDPNeighborOptionTypes option) const;
132 
137  NdpOption getFirstNdpOption() const;
138 
146  NdpOption getNextNdpOption(NdpOption &option) const;
147 
155  NdpOption addNdpOption(const NdpOptionBuilder &optionBuilder);
156 
162  bool removeAllNdpOptions();
163 
164 protected:
165  NDPLayerBase() = default;
166 
167  NDPLayerBase(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
168  : IcmpV6Layer(data, dataLen, prevLayer, packet) {}
169 
170 private:
171  TLVRecordReader<NdpOption> m_OptionReader;
172 
173  virtual size_t getNdpHeaderLen() const = 0;
174  virtual uint8_t *getNdpOptionsBasePtr() const { return m_Data + getNdpHeaderLen(); };
175  NdpOption addNdpOptionAt(const NdpOptionBuilder &optionBuilder, int offset);
176 };
177 
183 {
184 public:
189 #pragma pack(push, 1)
191  {
193  uint32_t reserved;
195  uint8_t targetIP[16];
196  };
197 #pragma pack(pop)
198 
206  NDPNeighborSolicitationLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
207  : NDPLayerBase(data, dataLen, prevLayer, packet) {}
208 
214  NDPNeighborSolicitationLayer(uint8_t code, const IPv6Address &targetIP);
215 
222  NDPNeighborSolicitationLayer(uint8_t code, const IPv6Address &targetIP, const MacAddress &srcMac);
223 
224  virtual ~NDPNeighborSolicitationLayer() {}
225 
229  IPv6Address getTargetIP() const { return IPv6Address(getNdpHeader()->targetIP); };
230 
235  bool hasLinkLayerAddress() const;
236 
241  MacAddress getLinkLayerAddress() const;
242 
243  std::string toString() const;
244 
245 private:
246  void initLayer(uint8_t code, const IPv6Address &targetIP);
247  ndpneighborsolicitationhdr *getNdpHeader() const { return (ndpneighborsolicitationhdr *)m_Data; }
248  size_t getNdpHeaderLen() const { return sizeof(ndpneighborsolicitationhdr); };
249 };
250 
256 {
257 public:
262 #pragma pack(push, 1)
264  {
265 #if (BYTE_ORDER == LITTLE_ENDIAN)
266  uint32_t
268  reserved : 5,
270  override : 1,
273  solicited : 1,
275  router : 1,
277  reserved2 : 24;
278 #else
279  uint32_t
281  router : 1,
284  solicited : 1,
286  override : 1,
288  reserved : 29;
289 #endif
290 
291  uint8_t targetIP[16];
292  };
293 #pragma pack(pop)
294 
302  NDPNeighborAdvertisementLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
303  : NDPLayerBase(data, dataLen, prevLayer, packet) {}
304 
315  NDPNeighborAdvertisementLayer(uint8_t code, const IPv6Address &targetIP, const MacAddress &targetMac,
316  bool routerFlag, bool unicastFlag, bool overrideFlag);
317 
327  NDPNeighborAdvertisementLayer(uint8_t code, const IPv6Address &targetIP, bool routerFlag, bool unicastFlag,
328  bool overrideFlag);
329 
330  virtual ~NDPNeighborAdvertisementLayer() {}
331 
335  MacAddress getTargetMac() const;
336 
340  IPv6Address getTargetIP() const { return IPv6Address(getNdpHeader()->targetIP); }
341 
345  bool hasTargetMacInfo() const;
346 
350  bool getRouterFlag() const { return getNdpHeader()->router; }
351 
355  bool getUnicastFlag() const { return getNdpHeader()->solicited; }
356 
360  bool getOverrideFlag() const { return getNdpHeader()->override; }
361 
362  std::string toString() const;
363 
364 private:
365  void initLayer(uint8_t code, const IPv6Address &targetIP, bool routerFlag, bool unicastFlag, bool overrideFlag);
366  ndpneighboradvertisementhdr *getNdpHeader() const { return (ndpneighboradvertisementhdr *)m_Data; }
367  size_t getNdpHeaderLen() const { return sizeof(ndpneighboradvertisementhdr); };
368 };
369 
370 } // namespace pcpp
371 #endif /* PACKETPP_NDP_LAYER */
The main namespace for the PcapPlusPlus lib.
Definition: IcmpV6Layer.h:133
NdpOption(uint8_t *optionRawData)
Definition: NdpLayer.h:46
Definition: TLVData.h:376
NDPNeighborOptionTypes getNdpOptionType() const
Definition: NdpLayer.h:57
Definition: Layer.h:70
Definition: TLVData.h:24
Definition: Packet.h:26
Definition: NdpLayer.h:89
bool getUnicastFlag() const
Definition: NdpLayer.h:355
Definition: IcmpV6Layer.h:103
size_t getDataSize() const
Definition: NdpLayer.h:75
Definition: TLVData.h:217
IPv6Address getTargetIP() const
Definition: NdpLayer.h:229
bool getRouterFlag() const
Definition: NdpLayer.h:350
~NdpOption()
Definition: NdpLayer.h:51
Definition: IpAddress.h:192
NDPNeighborAdvertisementLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: NdpLayer.h:302
Definition: NdpLayer.h:115
Definition: NdpLayer.h:255
Definition: NdpLayer.h:182
NdpOptionBuilder(NDPNeighborOptionTypes optionType, const uint8_t *optionValue, uint8_t optionValueLen)
Definition: NdpLayer.h:100
Definition: NdpLayer.h:39
bool getOverrideFlag() const
Definition: NdpLayer.h:360
IPv6Address getTargetIP() const
Definition: NdpLayer.h:340
size_t getTotalSize() const
Definition: NdpLayer.h:67
Definition: MacAddress.h:28
NDPNeighborOptionTypes
Definition: NdpLayer.h:24
NDPNeighborSolicitationLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: NdpLayer.h:206