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 reinterpret_cast<vrrp_header*>(m_Data);
137  }
138 
139  void setAddressType(IPAddress::AddressType addressType);
140 
141  void serializeLayer(ObjectScope& serializer) const override;
142 
143  virtual void serializeVrrpLayer(ObjectScope& serializer) const = 0;
144 
145  public:
147  enum VrrpType
148  {
151 
154  };
155 
158  {
166  Other
167  };
168 
169  ~VrrpLayer() override = default;
170 
173 
179  static ProtocolType getVersionFromData(uint8_t* data, size_t dataLen);
180 
182  uint8_t getVersion() const;
183 
185  VrrpType getType() const;
186 
188  uint8_t getVirtualRouterID() const;
189 
192  void setVirtualRouterID(uint8_t virtualRouterID);
193 
195 
196  uint8_t getPriority() const;
197 
200 
203  void setPriority(uint8_t priority);
204 
206  uint16_t getChecksum() const;
207 
210 
213  virtual uint16_t calculateChecksum() const = 0;
214 
216  bool isChecksumCorrect() const;
217 
219  uint8_t getIPAddressesCount() const;
220 
222  std::vector<IPAddress> getIPAddresses() const;
223 
228  bool addIPAddresses(const std::vector<IPAddress>& ipAddresses);
229 
234  bool addIPAddress(const IPAddress& ipAddress);
235 
241  bool removeIPAddressAtIndex(int index);
242 
247 
248  // implement abstract methods
249 
251  void parseNextLayer() override
252  {}
253 
255  void computeCalculateFields() override;
256 
259  size_t getHeaderLen() const override
260  {
261  return m_DataLen;
262  }
263 
264  std::string toString() const override;
265 
267  {
268  return OsiModelNetworkLayer;
269  }
270 
275  {
277  static std::vector<FieldDescriptor> all()
278  {
279  auto result = Layer::SerializedFields::all();
280  std::initializer_list<FieldDescriptor> extra{ Type, VirtualRouterID, Priority, Checksum, IPAddresses };
281  std::copy(extra.begin(), extra.end(), std::back_inserter(result));
282  return result;
283  }
284 
286  static const FieldDescriptor Type;
287 
290 
293  static const FieldDescriptor Priority;
294 
297 
299  static const FieldDescriptor Checksum;
300 
303 
306 
308  static constexpr uint16_t MaxID = Layer::SerializedFields::MaxID + 5;
309  };
310  };
311 
315  class VrrpV2Layer : public VrrpLayer
316  {
317  private:
318  struct vrrpv2_auth_adv
319  {
320  uint8_t authType;
321  uint8_t advInt;
322  };
323 
324  public:
326  enum class VrrpAuthType : uint8_t
327  {
329  NoAuthentication = 0,
331  SimpleTextPassword = 1,
335  MD5 = 3,
337  Other = 4
338  };
339 
345  VrrpV2Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
346  : VrrpLayer(data, dataLen, prevLayer, packet, VRRPv2, IPAddress::IPv4AddressType)
347  {}
348 
354  explicit VrrpV2Layer(uint8_t virtualRouterId, uint8_t priority, uint8_t advInt, uint8_t authType = 0);
355 
357  ~VrrpV2Layer() override = default;
358 
360  uint8_t getAdvInt() const;
361 
364  void setAdvInt(uint8_t advInt);
365 
367  uint8_t getAuthType() const;
368 
371 
374  void setAuthType(uint8_t authType);
375 
376  // implement abstract methods
377 
380  uint16_t calculateChecksum() const override;
381 
386  static bool isDataValid(uint8_t const* data, size_t dataLen)
387  {
388  return canReinterpretAs<vrrp_header>(data, dataLen);
389  }
390 
395  {
397  static std::vector<FieldDescriptor> all()
398  {
399  auto result = VrrpLayer::SerializedFields::all();
400  std::initializer_list<FieldDescriptor> extra{ AdvInt, AuthType };
401  std::copy(extra.begin(), extra.end(), std::back_inserter(result));
402  return result;
403  }
404 
406  static const FieldDescriptor AdvInt;
407 
410  static const FieldDescriptor AuthType;
411  };
412 
413  protected:
414  void serializeVrrpLayer(ObjectScope& serializer) const override;
415  };
416 
420  class VrrpV3Layer : public VrrpLayer
421  {
422  private:
423  struct vrrpv3_rsvd_adv
424  {
425  uint16_t maxAdvInt;
426  };
427 
428  public:
435  VrrpV3Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, IPAddress::AddressType addressType)
436  : VrrpLayer(data, dataLen, prevLayer, packet, VRRPv3, addressType)
437  {}
438 
444  explicit VrrpV3Layer(IPAddress::AddressType addressType, uint8_t virtualRouterId, uint8_t priority,
445  uint16_t maxAdvInt);
446 
448  ~VrrpV3Layer() override = default;
449 
451  uint16_t getMaxAdvInt() const;
452 
455  void setMaxAdvInt(uint16_t maxAdvInt);
456 
457  // implement abstract methods
458 
461  uint16_t calculateChecksum() const override;
462 
467  static bool isDataValid(uint8_t const* data, size_t dataLen)
468  {
469  return canReinterpretAs<vrrp_header>(data, dataLen);
470  }
471 
476  {
478  static std::vector<FieldDescriptor> all()
479  {
480  auto result = VrrpLayer::SerializedFields::all();
481  std::initializer_list<FieldDescriptor> extra{ MaxAdvInt };
482  std::copy(extra.begin(), extra.end(), std::back_inserter(result));
483  return result;
484  }
485 
488  };
489 
490  protected:
491  void serializeVrrpLayer(ObjectScope& serializer) const override;
492  };
493 } // namespace pcpp
Definition: IpAddress.h:318
AddressType
An enum representing the address type: IPv4 or IPv6.
Definition: IpAddress.h:322
Definition: Layer.h:117
Definition: Serializers.h:230
Definition: Packet.h:49
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:259
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:251
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:148
@ VrrpType_Unknown
Unknown VRRP message.
Definition: VrrpLayer.h:150
@ VrrpType_Advertisement
VRRP advertisement message.
Definition: VrrpLayer.h:153
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:266
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:158
@ Other
Other priority.
Definition: VrrpLayer.h:166
@ Stop
Current Master has stopped participating in VRRP (value of 0)
Definition: VrrpLayer.h:162
@ Default
Default priority for a backup VRRP router (value of 100)
Definition: VrrpLayer.h:160
@ Owner
This VRRP router owns the virtual router's IP address(es) (value of 255)
Definition: VrrpLayer.h:164
void serializeLayer(ObjectScope &serializer) const override
bool addIPAddress(const IPAddress &ipAddress)
Definition: VrrpLayer.h:316
VrrpAuthType
VRRP v2 authentication types.
Definition: VrrpLayer.h:327
@ 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:345
static bool isDataValid(uint8_t const *data, size_t dataLen)
Definition: VrrpLayer.h:386
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
A destructor for this layer (does nothing)
void setMaxAdvInt(uint16_t maxAdvInt)
static bool isDataValid(uint8_t const *data, size_t dataLen)
Definition: VrrpLayer.h:467
VrrpV3Layer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet, IPAddress::AddressType addressType)
Definition: VrrpLayer.h:435
VrrpV3Layer(IPAddress::AddressType addressType, uint8_t virtualRouterId, uint8_t priority, uint16_t maxAdvInt)
uint16_t getMaxAdvInt() const
const ProtocolType VRRPv3
VRRP version 3 protocol.
Definition: ProtocolType.h:219
uint8_t ProtocolType
Definition: ProtocolType.h:38
const ProtocolType VRRPv2
VRRP version 2 protocol.
Definition: ProtocolType.h:216
The main namespace for the PcapPlusPlus lib.
Definition: AssertionUtils.h:19
OsiModelLayer
An enum representing OSI model layers.
Definition: ProtocolType.h:425
@ OsiModelNetworkLayer
Network layer (layer 3)
Definition: ProtocolType.h:431
Definition: Serializers.h:18
Definition: Layer.h:212
static std::vector< FieldDescriptor > all()
Definition: Layer.h:214
static constexpr uint16_t MaxID
Maximum field ID used by the layer.
Definition: Layer.h:229
Definition: VrrpLayer.h:275
static constexpr uint16_t MaxID
Maximum field ID used by the layer.
Definition: VrrpLayer.h:308
static const FieldDescriptor Checksum
Field descriptor for the VRRP checksum, written as a hex value.
Definition: VrrpLayer.h:299
static const FieldDescriptor Priority
Field descriptor for the priority category, written as a string: "Default", "Stop",...
Definition: VrrpLayer.h:293
static std::vector< FieldDescriptor > all()
Definition: VrrpLayer.h:277
static const FieldDescriptor IPAddresses
Field descriptor for the array of virtual router IP addresses.
Definition: VrrpLayer.h:302
static const FieldDescriptor PriorityValue
Field descriptor for the raw priority value, written as a number.
Definition: VrrpLayer.h:296
static const FieldDescriptor VirtualRouterID
Field descriptor for the virtual router ID (VRID), written as a number.
Definition: VrrpLayer.h:289
static const FieldDescriptor IPAddress
Field descriptor for a single entry of the IPAddresses array, written as an IP address string.
Definition: VrrpLayer.h:305
static const FieldDescriptor Type
Field descriptor for the VRRP message type, written as a string.
Definition: VrrpLayer.h:286
Definition: VrrpLayer.h:395
static std::vector< FieldDescriptor > all()
Definition: VrrpLayer.h:397
static const FieldDescriptor AuthType
Field descriptor for the authentication type, written as a string (see VrrpV2Layer::VrrpAuthType).
Definition: VrrpLayer.h:410
static const FieldDescriptor AdvInt
Field descriptor for the advertisement interval (in seconds), written as a number.
Definition: VrrpLayer.h:406
Definition: VrrpLayer.h:476
static std::vector< FieldDescriptor > all()
Definition: VrrpLayer.h:478
static const FieldDescriptor MaxAdvInt
Field descriptor for the maximum advertisement interval (in centiseconds), written as a number.
Definition: VrrpLayer.h:487
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