PcapPlusPlus  24.09
VrrpLayer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Layer.h"
4 #include "IpAddress.h"
5 #include <vector>
6 
8 
13 namespace pcpp
14 {
22  /* VRRPv2 Packet Format
23  0 1 2 3
24  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
25  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
26  |Version| Type | Virtual Rtr ID| Priority | Count IP Addrs|
27  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
28  | Auth Type | Adver Int | Checksum |
29  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
30  | IP Address (1) |
31  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32  | . |
33  | . |
34  | . |
35  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36  | IP Address (n) |
37  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38  | Authentication Data (1) |
39  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40  | Authentication Data (2) |
41  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42  */
43 
44  /* VRRPv3 Packet Format
45  0 1 2 3
46  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
47  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48  | IPv4 Fields or IPv6 Fields |
49  ... ...
50  | |
51  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52  |Version| Type | Virtual Rtr ID| Priority |Count IPvX Addr|
53  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54  |(rsvd) | Max Adver Int | Checksum |
55  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
56  | |
57  + +
58  | IPvX Address(es) |
59  + +
60  + +
61  + +
62  + +
63  | |
64  + +
65  | |
66  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
67  */
68 
73  struct vrrp_header
74  {
75 #if (BYTE_ORDER == LITTLE_ENDIAN)
77  uint8_t type : 4;
78 
80  uint8_t version : 4;
81 #else
83  uint8_t version : 4;
84 
86  uint8_t type : 4;
87 #endif
90  uint8_t vrId;
91 
93  uint8_t priority;
94 
96  uint8_t ipAddrCount;
97 
100  uint16_t authTypeAdvInt;
101 
104  uint16_t checksum;
105 
107  uint8_t* ipAddresses[];
108  };
109 
116  class VrrpLayer : public Layer
117  {
118  private:
119  bool addIPAddressesAt(const std::vector<IPAddress>& ipAddresses, int offset);
120 
121  uint8_t getIPAddressLen() const;
122 
123  bool isIPAddressValid(IPAddress& ipAddress) const;
124 
125  uint8_t* getFirstIPAddressPtr() const;
126 
127  uint8_t* getNextIPAddressPtr(uint8_t* ipAddressPtr) const;
128 
129  IPAddress getIPAddressFromData(uint8_t* data) const;
130 
131  void copyIPAddressToData(uint8_t* data, const IPAddress& ipAddress) const;
132 
133  IPAddress::AddressType m_AddressType;
134 
135  protected:
136  VrrpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, ProtocolType vrrpVer,
137  IPAddress::AddressType addressType)
138  : Layer(data, dataLen, prevLayer, packet, vrrpVer), m_AddressType(addressType)
139  {}
140 
141  explicit VrrpLayer(ProtocolType subProtocol, uint8_t virtualRouterId, uint8_t priority);
142 
143  vrrp_header* getVrrpHeader() const
144  {
145  return (vrrp_header*)m_Data;
146  }
147 
148  void setAddressType(IPAddress::AddressType addressType);
149 
150  public:
154  enum VrrpType
155  {
158 
161  };
162 
167  {
175  Other
176  };
177 
178  virtual ~VrrpLayer()
179  {}
180 
185 
193  static ProtocolType getVersionFromData(uint8_t* data, size_t dataLen);
194 
198  uint8_t getVersion() const;
199 
203  VrrpType getType() const;
204 
208  uint8_t getVirtualRouterID() const;
209 
214  void setVirtualRouterID(uint8_t virtualRouterID);
215 
219  uint8_t getPriority() const;
220 
225 
230  void setPriority(uint8_t priority);
231 
235  uint16_t getChecksum() const;
236 
241 
246  virtual uint16_t calculateChecksum() const = 0;
247 
251  bool isChecksumCorrect() const;
252 
256  uint8_t getIPAddressesCount() const;
257 
261  std::vector<IPAddress> getIPAddresses() const;
262 
269  bool addIPAddresses(const std::vector<IPAddress>& ipAddresses);
270 
277  bool addIPAddress(const IPAddress& ipAddress);
278 
286  bool removeIPAddressAtIndex(int index);
287 
294 
295  // implement abstract methods
296 
300  void parseNextLayer() override
301  {}
302 
306  void computeCalculateFields() override;
307 
311  size_t getHeaderLen() const override
312  {
313  return m_DataLen;
314  }
315 
316  std::string toString() const override;
317 
319  {
320  return OsiModelNetworkLayer;
321  }
322  };
323 
329  class VrrpV2Layer : public VrrpLayer
330  {
331  private:
332  struct vrrpv2_auth_adv
333  {
334  uint8_t authType;
335  uint8_t advInt;
336  };
337 
338  public:
342  enum class VrrpAuthType : uint8_t
343  {
345  NoAuthentication = 0,
347  SimpleTextPassword = 1,
351  MD5 = 3,
353  Other = 4
354  };
355 
362  VrrpV2Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
363  : VrrpLayer(data, dataLen, prevLayer, packet, VRRPv2, IPAddress::IPv4AddressType)
364  {}
365 
373  explicit VrrpV2Layer(uint8_t virtualRouterId, uint8_t priority, uint8_t advInt, uint8_t authType = 0);
374 
379  {}
380 
384  uint8_t getAdvInt() const;
385 
390  void setAdvInt(uint8_t advInt);
391 
395  uint8_t getAuthType() const;
396 
401 
406  void setAuthType(uint8_t authType);
407 
408  // implement abstract methods
409 
414  uint16_t calculateChecksum() const override;
415  };
416 
422  class VrrpV3Layer : public VrrpLayer
423  {
424  private:
425  struct vrrpv3_rsvd_adv
426  {
427  uint16_t maxAdvInt;
428  };
429 
430  public:
438  VrrpV3Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, IPAddress::AddressType addressType)
439  : VrrpLayer(data, dataLen, prevLayer, packet, VRRPv3, addressType)
440  {}
441 
449  explicit VrrpV3Layer(IPAddress::AddressType addressType, uint8_t virtualRouterId, uint8_t priority,
450  uint16_t maxAdvInt);
451 
456  {}
457 
461  uint16_t getMaxAdvInt() const;
462 
467  void setMaxAdvInt(uint16_t maxAdvInt);
468 
469  // implement abstract methods
470 
475  uint16_t calculateChecksum() const override;
476  };
477 } // namespace pcpp
Definition: IpAddress.h:358
AddressType
Definition: IpAddress.h:364
Definition: Layer.h:70
Definition: Packet.h:27
Definition: VrrpLayer.h:117
bool isChecksumCorrect() const
void setVirtualRouterID(uint8_t virtualRouterID)
virtual uint16_t calculateChecksum() const =0
static ProtocolType getVersionFromData(uint8_t *data, size_t dataLen)
size_t getHeaderLen() const override
Definition: VrrpLayer.h:311
uint8_t getPriority() const
bool addIPAddresses(const std::vector< IPAddress > &ipAddresses)
IPAddress::AddressType getAddressType() const
void parseNextLayer() override
Definition: VrrpLayer.h:300
std::string toString() const override
void calculateAndSetChecksum()
uint8_t getIPAddressesCount() const
VrrpPriority getPriorityAsEnum() const
VrrpType
Definition: VrrpLayer.h:155
@ VrrpType_Unknown
Definition: VrrpLayer.h:157
@ VrrpType_Advertisement
Definition: VrrpLayer.h:160
void computeCalculateFields() override
uint8_t getVersion() const
bool removeIPAddressAtIndex(int index)
uint8_t getVirtualRouterID() const
void setPriority(uint8_t priority)
OsiModelLayer getOsiModelLayer() const override
Definition: VrrpLayer.h:318
VrrpType getType() const
uint16_t getChecksum() const
std::vector< IPAddress > getIPAddresses() const
bool removeAllIPAddresses()
VrrpPriority
Definition: VrrpLayer.h:167
@ Other
Definition: VrrpLayer.h:175
@ Stop
Definition: VrrpLayer.h:171
@ Default
Definition: VrrpLayer.h:169
@ Owner
Definition: VrrpLayer.h:173
bool addIPAddress(const IPAddress &ipAddress)
Definition: VrrpLayer.h:330
VrrpAuthType
Definition: VrrpLayer.h:343
uint8_t getAdvInt() const
VrrpAuthType getAuthTypeAsEnum() const
VrrpV2Layer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: VrrpLayer.h:362
~VrrpV2Layer()
Definition: VrrpLayer.h:378
uint16_t calculateChecksum() const override
void setAdvInt(uint8_t advInt)
uint8_t getAuthType() const
void setAuthType(uint8_t authType)
VrrpV2Layer(uint8_t virtualRouterId, uint8_t priority, uint8_t advInt, uint8_t authType=0)
Definition: VrrpLayer.h:423
uint16_t calculateChecksum() const override
void setMaxAdvInt(uint16_t maxAdvInt)
VrrpV3Layer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet, IPAddress::AddressType addressType)
Definition: VrrpLayer.h:438
VrrpV3Layer(IPAddress::AddressType addressType, uint8_t virtualRouterId, uint8_t priority, uint16_t maxAdvInt)
~VrrpV3Layer()
Definition: VrrpLayer.h:455
uint16_t getMaxAdvInt() const
The main namespace for the PcapPlusPlus lib.
const ProtocolType VRRPv3
Definition: ProtocolType.h:318
uint8_t ProtocolType
Definition: ProtocolType.h:17
OsiModelLayer
Definition: ProtocolType.h:354
@ OsiModelNetworkLayer
Definition: ProtocolType.h:360
const ProtocolType VRRPv2
Definition: ProtocolType.h:313
Definition: VrrpLayer.h:74
uint8_t vrId
Definition: VrrpLayer.h:90
uint16_t checksum
Definition: VrrpLayer.h:104
uint8_t * ipAddresses[]
Definition: VrrpLayer.h:107
uint8_t ipAddrCount
Definition: VrrpLayer.h:96
uint16_t authTypeAdvInt
Definition: VrrpLayer.h:100
uint8_t type
Definition: VrrpLayer.h:77
uint8_t priority
Definition: VrrpLayer.h:93
uint8_t version
Definition: VrrpLayer.h:80