PcapPlusPlus  Next
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  ~VrrpLayer() override = default;
179 
184 
192  static ProtocolType getVersionFromData(uint8_t* data, size_t dataLen);
193 
197  uint8_t getVersion() const;
198 
202  VrrpType getType() const;
203 
207  uint8_t getVirtualRouterID() const;
208 
213  void setVirtualRouterID(uint8_t virtualRouterID);
214 
218  uint8_t getPriority() const;
219 
224 
229  void setPriority(uint8_t priority);
230 
234  uint16_t getChecksum() const;
235 
240 
245  virtual uint16_t calculateChecksum() const = 0;
246 
250  bool isChecksumCorrect() const;
251 
255  uint8_t getIPAddressesCount() const;
256 
260  std::vector<IPAddress> getIPAddresses() const;
261 
268  bool addIPAddresses(const std::vector<IPAddress>& ipAddresses);
269 
276  bool addIPAddress(const IPAddress& ipAddress);
277 
285  bool removeIPAddressAtIndex(int index);
286 
293 
294  // implement abstract methods
295 
299  void parseNextLayer() override
300  {}
301 
305  void computeCalculateFields() override;
306 
310  size_t getHeaderLen() const override
311  {
312  return m_DataLen;
313  }
314 
315  std::string toString() const override;
316 
318  {
319  return OsiModelNetworkLayer;
320  }
321  };
322 
328  class VrrpV2Layer : public VrrpLayer
329  {
330  private:
331  struct vrrpv2_auth_adv
332  {
333  uint8_t authType;
334  uint8_t advInt;
335  };
336 
337  public:
341  enum class VrrpAuthType : uint8_t
342  {
344  NoAuthentication = 0,
346  SimpleTextPassword = 1,
350  MD5 = 3,
352  Other = 4
353  };
354 
361  VrrpV2Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
362  : VrrpLayer(data, dataLen, prevLayer, packet, VRRPv2, IPAddress::IPv4AddressType)
363  {}
364 
372  explicit VrrpV2Layer(uint8_t virtualRouterId, uint8_t priority, uint8_t advInt, uint8_t authType = 0);
373 
377  ~VrrpV2Layer() override = default;
378 
382  uint8_t getAdvInt() const;
383 
388  void setAdvInt(uint8_t advInt);
389 
393  uint8_t getAuthType() const;
394 
399 
404  void setAuthType(uint8_t authType);
405 
406  // implement abstract methods
407 
412  uint16_t calculateChecksum() const override;
413  };
414 
420  class VrrpV3Layer : public VrrpLayer
421  {
422  private:
423  struct vrrpv3_rsvd_adv
424  {
425  uint16_t maxAdvInt;
426  };
427 
428  public:
436  VrrpV3Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, IPAddress::AddressType addressType)
437  : VrrpLayer(data, dataLen, prevLayer, packet, VRRPv3, addressType)
438  {}
439 
447  explicit VrrpV3Layer(IPAddress::AddressType addressType, uint8_t virtualRouterId, uint8_t priority,
448  uint16_t maxAdvInt);
449 
453  ~VrrpV3Layer() override = default;
454 
458  uint16_t getMaxAdvInt() const;
459 
464  void setMaxAdvInt(uint16_t maxAdvInt);
465 
466  // implement abstract methods
467 
472  uint16_t calculateChecksum() const override;
473  };
474 } // namespace pcpp
Definition: IpAddress.h:358
AddressType
Definition: IpAddress.h:364
Definition: Layer.h:69
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:310
uint8_t getPriority() const
bool addIPAddresses(const std::vector< IPAddress > &ipAddresses)
IPAddress::AddressType getAddressType() const
void parseNextLayer() override
Definition: VrrpLayer.h:299
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:317
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:329
VrrpAuthType
Definition: VrrpLayer.h:342
uint8_t getAdvInt() const
~VrrpV2Layer() override=default
VrrpAuthType getAuthTypeAsEnum() const
VrrpV2Layer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: VrrpLayer.h:361
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:421
uint16_t calculateChecksum() const override
~VrrpV3Layer() override=default
void setMaxAdvInt(uint16_t maxAdvInt)
VrrpV3Layer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet, IPAddress::AddressType addressType)
Definition: VrrpLayer.h:436
VrrpV3Layer(IPAddress::AddressType addressType, uint8_t virtualRouterId, uint8_t priority, uint16_t maxAdvInt)
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:364
@ OsiModelNetworkLayer
Definition: ProtocolType.h:370
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