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 
11 namespace pcpp
12 {
17 
38 
62 
65  struct vrrp_header
66  {
67 #if (BYTE_ORDER == LITTLE_ENDIAN)
69  uint8_t type : 4;
70 
72  uint8_t version : 4;
73 #else
75  uint8_t version : 4;
76 
78  uint8_t type : 4;
79 #endif
82  uint8_t vrId;
83 
85  uint8_t priority;
86 
88  uint8_t ipAddrCount;
89 
92  uint16_t authTypeAdvInt;
93 
96  uint16_t checksum;
97 
99  uint8_t* ipAddresses[];
100  };
101  static_assert(sizeof(vrrp_header) == 8, "vrrp_header size is not 8 bytes");
102 
107  class VrrpLayer : public Layer
108  {
109  private:
110  bool addIPAddressesAt(const std::vector<IPAddress>& ipAddresses, int offset);
111 
112  uint8_t getIPAddressLen() const;
113 
114  bool isIPAddressValid(IPAddress& ipAddress) const;
115 
116  uint8_t* getFirstIPAddressPtr() const;
117 
118  uint8_t* getNextIPAddressPtr(uint8_t* ipAddressPtr) const;
119 
120  IPAddress getIPAddressFromData(uint8_t* data) const;
121 
122  void copyIPAddressToData(uint8_t* data, const IPAddress& ipAddress) const;
123 
124  IPAddress::AddressType m_AddressType;
125 
126  protected:
127  VrrpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, ProtocolType vrrpVer,
128  IPAddress::AddressType addressType)
129  : Layer(data, dataLen, prevLayer, packet, vrrpVer), m_AddressType(addressType)
130  {}
131 
132  explicit VrrpLayer(ProtocolType subProtocol, uint8_t virtualRouterId, uint8_t priority);
133 
134  vrrp_header* getVrrpHeader() const
135  {
136  return (vrrp_header*)m_Data;
137  }
138 
139  void setAddressType(IPAddress::AddressType addressType);
140 
141  public:
143  enum VrrpType
144  {
147 
150  };
151 
154  {
162  Other
163  };
164 
165  ~VrrpLayer() override = default;
166 
169 
175  static ProtocolType getVersionFromData(uint8_t* data, size_t dataLen);
176 
178  uint8_t getVersion() const;
179 
181  VrrpType getType() const;
182 
184  uint8_t getVirtualRouterID() const;
185 
188  void setVirtualRouterID(uint8_t virtualRouterID);
189 
191 
192  uint8_t getPriority() const;
193 
196 
199  void setPriority(uint8_t priority);
200 
202  uint16_t getChecksum() const;
203 
206 
209  virtual uint16_t calculateChecksum() const = 0;
210 
212  bool isChecksumCorrect() const;
213 
215  uint8_t getIPAddressesCount() const;
216 
218  std::vector<IPAddress> getIPAddresses() const;
219 
224  bool addIPAddresses(const std::vector<IPAddress>& ipAddresses);
225 
230  bool addIPAddress(const IPAddress& ipAddress);
231 
237  bool removeIPAddressAtIndex(int index);
238 
243 
244  // implement abstract methods
245 
247  void parseNextLayer() override
248  {}
249 
251  void computeCalculateFields() override;
252 
255  size_t getHeaderLen() const override
256  {
257  return m_DataLen;
258  }
259 
260  std::string toString() const override;
261 
263  {
264  return OsiModelNetworkLayer;
265  }
266  };
267 
271  class VrrpV2Layer : public VrrpLayer
272  {
273  private:
274  struct vrrpv2_auth_adv
275  {
276  uint8_t authType;
277  uint8_t advInt;
278  };
279 
280  public:
282  enum class VrrpAuthType : uint8_t
283  {
285  NoAuthentication = 0,
287  SimpleTextPassword = 1,
291  MD5 = 3,
293  Other = 4
294  };
295 
301  VrrpV2Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
302  : VrrpLayer(data, dataLen, prevLayer, packet, VRRPv2, IPAddress::IPv4AddressType)
303  {}
304 
310  explicit VrrpV2Layer(uint8_t virtualRouterId, uint8_t priority, uint8_t advInt, uint8_t authType = 0);
311 
313  ~VrrpV2Layer() override = default;
314 
316  uint8_t getAdvInt() const;
317 
320  void setAdvInt(uint8_t advInt);
321 
323  uint8_t getAuthType() const;
324 
327 
330  void setAuthType(uint8_t authType);
331 
332  // implement abstract methods
333 
336  uint16_t calculateChecksum() const override;
337  };
338 
342  class VrrpV3Layer : public VrrpLayer
343  {
344  private:
345  struct vrrpv3_rsvd_adv
346  {
347  uint16_t maxAdvInt;
348  };
349 
350  public:
357  VrrpV3Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, IPAddress::AddressType addressType)
358  : VrrpLayer(data, dataLen, prevLayer, packet, VRRPv3, addressType)
359  {}
360 
366  explicit VrrpV3Layer(IPAddress::AddressType addressType, uint8_t virtualRouterId, uint8_t priority,
367  uint16_t maxAdvInt);
368 
370  ~VrrpV3Layer() override = default;
371 
373  uint16_t getMaxAdvInt() const;
374 
377  void setMaxAdvInt(uint16_t maxAdvInt);
378 
379  // implement abstract methods
380 
383  uint16_t calculateChecksum() const override;
384  };
385 } // namespace pcpp
Definition: IpAddress.h:276
AddressType
An enum representing the address type: IPv4 or IPv6.
Definition: IpAddress.h:280
Definition: Layer.h:60
Definition: Packet.h:22
Definition: VrrpLayer.h:108
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:255
uint8_t getPriority() const
bool addIPAddresses(const std::vector< IPAddress > &ipAddresses)
IPAddress::AddressType getAddressType() const
void parseNextLayer() override
Does nothing for this layer (VRRP layer is always last)
Definition: VrrpLayer.h:247
std::string toString() const override
void calculateAndSetChecksum()
Fill the checksum from header and data and write the result to vrrp_header::checksum.
uint8_t getIPAddressesCount() const
VrrpPriority getPriorityAsEnum() const
VrrpType
VRRP message types.
Definition: VrrpLayer.h:144
@ VrrpType_Unknown
Unknown VRRP message.
Definition: VrrpLayer.h:146
@ VrrpType_Advertisement
VRRP advertisement message.
Definition: VrrpLayer.h:149
void computeCalculateFields() override
Calculate the VRRP checksum.
uint8_t getVersion() const
bool removeIPAddressAtIndex(int index)
uint8_t getVirtualRouterID() const
void setPriority(uint8_t priority)
OsiModelLayer getOsiModelLayer() const override
Definition: VrrpLayer.h:262
VrrpType getType() const
uint16_t getChecksum() const
std::vector< IPAddress > getIPAddresses() const
bool removeAllIPAddresses()
VrrpPriority
An enum describing VRRP special priority values.
Definition: VrrpLayer.h:154
@ Other
Other priority.
Definition: VrrpLayer.h:162
@ Stop
Current Master has stopped participating in VRRP (value of 0)
Definition: VrrpLayer.h:158
@ Default
Default priority for a backup VRRP router (value of 100)
Definition: VrrpLayer.h:156
@ Owner
This VRRP router owns the virtual router's IP address(es) (value of 255)
Definition: VrrpLayer.h:160
bool addIPAddress(const IPAddress &ipAddress)
Definition: VrrpLayer.h:272
VrrpAuthType
VRRP v2 authentication types.
Definition: VrrpLayer.h:283
@ Other
Other/Unknown Authentication Type.
@ MD5
Cisco VRRP MD5 Authentication.
@ IPAuthenticationHeader
IP Authentication Header.
@ NoAuthentication
No Authentication.
@ SimpleTextPassword
Simple Text Password.
uint8_t getAdvInt() const
~VrrpV2Layer() override=default
A destructor for this layer (does nothing)
VrrpAuthType getAuthTypeAsEnum() const
VrrpV2Layer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: VrrpLayer.h:301
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:343
uint16_t calculateChecksum() const override
~VrrpV3Layer() override=default
A destructor for this layer (does nothing)
void setMaxAdvInt(uint16_t maxAdvInt)
VrrpV3Layer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet, IPAddress::AddressType addressType)
Definition: VrrpLayer.h:357
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
VRRP version 3 protocol.
Definition: ProtocolType.h:194
uint8_t ProtocolType
Definition: ProtocolType.h:13
OsiModelLayer
An enum representing OSI model layers.
Definition: ProtocolType.h:225
@ OsiModelNetworkLayer
Network layer (layer 3)
Definition: ProtocolType.h:231
const ProtocolType VRRPv2
VRRP version 2 protocol.
Definition: ProtocolType.h:191
Definition: VrrpLayer.h:66
uint8_t vrId
Definition: VrrpLayer.h:82
uint16_t checksum
Definition: VrrpLayer.h:96
uint8_t * ipAddresses[]
This specifies one or more IPvX addresses that are associated with the virtual router.
Definition: VrrpLayer.h:99
uint8_t ipAddrCount
Specifies how many IPvX addresses are present in this Packet.
Definition: VrrpLayer.h:88
uint16_t authTypeAdvInt
Definition: VrrpLayer.h:92
uint8_t type
Type.
Definition: VrrpLayer.h:69
uint8_t priority
This specifies the sending VRRP router's priority for the virtual router.
Definition: VrrpLayer.h:85
uint8_t version
Version bits.
Definition: VrrpLayer.h:72