PcapPlusPlus  23.09
VrrpLayer.h
Go to the documentation of this file.
1 #ifndef PACKETPP_VRRP_LAYER
2 #define PACKETPP_VRRP_LAYER
3 
4 #include "Layer.h"
5 #include "IpAddress.h"
6 #include <vector>
7 
9 
14 namespace pcpp
15 {
23  /* VRRPv2 Packet Format
24  0 1 2 3
25  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
26  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
27  |Version| Type | Virtual Rtr ID| Priority | Count IP Addrs|
28  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
29  | Auth Type | Adver Int | Checksum |
30  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
31  | IP Address (1) |
32  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33  | . |
34  | . |
35  | . |
36  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37  | IP Address (n) |
38  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39  | Authentication Data (1) |
40  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41  | Authentication Data (2) |
42  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43  */
44 
45  /* VRRPv3 Packet Format
46  0 1 2 3
47  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
48  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49  | IPv4 Fields or IPv6 Fields |
50  ... ...
51  | |
52  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53  |Version| Type | Virtual Rtr ID| Priority |Count IPvX Addr|
54  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55  |(rsvd) | Max Adver Int | Checksum |
56  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57  | |
58  + +
59  | IPvX Address(es) |
60  + +
61  + +
62  + +
63  + +
64  | |
65  + +
66  | |
67  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
68  */
69 
74  struct vrrp_header
75  {
76 #if (BYTE_ORDER == LITTLE_ENDIAN)
77 
78  uint8_t type: 4,
79 
81  version: 4;
82 #else
83 
84  uint8_t version:4,
85 
87  type: 4;
88 #endif
89 
90  uint8_t vrId;
91 
93  uint8_t priority;
94 
96  uint8_t ipAddrCount;
97 
99  uint16_t authTypeAdvInt;
100 
103  uint16_t checksum;
104 
106  uint8_t *ipAddresses[];
107  };
108 
115  class VrrpLayer : public Layer
116  {
117  private:
118  bool addIPAddressesAt(const std::vector<IPAddress> &ipAddresses, int offset);
119 
120  uint8_t getIPAddressLen() const;
121 
122  bool isIPAddressValid(IPAddress &ipAddress) const;
123 
124  uint8_t* getFirstIPAddressPtr() const;
125 
126  uint8_t* getNextIPAddressPtr(uint8_t* ipAddressPtr) const;
127 
128  IPAddress getIPAddressFromData(uint8_t *data) const;
129 
130  void copyIPAddressToData(uint8_t *data, const IPAddress &ipAddress) const;
131 
132  IPAddress::AddressType m_AddressType;
133 
134  protected:
135  VrrpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet, ProtocolType vrrpVer,
136  IPAddress::AddressType addressType)
137  : Layer(data, dataLen, prevLayer, packet), m_AddressType(addressType)
138  {
139  m_Protocol = vrrpVer;
140  }
141 
142  explicit VrrpLayer(ProtocolType subProtocol, uint8_t virtualRouterId, uint8_t priority);
143 
144  vrrp_header *getVrrpHeader() const { return (vrrp_header *) m_Data; }
145 
146  void setAddressType(IPAddress::AddressType addressType);
147 
148  public:
152  enum VrrpType
153  {
155  VrrpType_Unknown = 0,
156 
158  VrrpType_Advertisement = 1
159  };
160 
165  {
173  Other
174  };
175 
176  virtual ~VrrpLayer() {}
177 
181  IPAddress::AddressType getAddressType() const;
182 
190  static ProtocolType getVersionFromData(uint8_t *data, size_t dataLen);
191 
195  uint8_t getVersion() const;
196 
200  VrrpType getType() const;
201 
205  uint8_t getVirtualRouterID() const;
206 
211  void setVirtualRouterID(uint8_t virtualRouterID);
212 
216  uint8_t getPriority() const;
217 
221  VrrpPriority getPriorityAsEnum() const;
222 
227  void setPriority(uint8_t priority);
228 
232  uint16_t getChecksum() const;
233 
237  void calculateAndSetChecksum();
238 
243  virtual uint16_t calculateChecksum() const = 0;
244 
248  bool isChecksumCorrect() const;
249 
253  uint8_t getIPAddressesCount() const;
254 
258  std::vector<IPAddress> getIPAddresses() const;
259 
266  bool addIPAddresses(const std::vector<IPAddress> &ipAddresses);
267 
274  bool addIPAddress(const IPAddress &ipAddress);
275 
282  bool removeIPAddressAtIndex(int index);
283 
289  bool removeAllIPAddresses();
290 
291  // implement abstract methods
292 
296  void parseNextLayer() override {}
297 
301  void computeCalculateFields() override;
302 
306  size_t getHeaderLen() const override { return m_DataLen; }
307 
308  std::string toString() const override;
309 
311  };
312 
317  class VrrpV2Layer : public VrrpLayer
318  {
319  private:
320  struct vrrpv2_auth_adv
321  {
322  uint8_t authType;
323  uint8_t advInt;
324  };
325 
326  public:
330  enum class VrrpAuthType : uint8_t
331  {
333  NoAuthentication = 0,
335  SimpleTextPassword = 1,
337  IPAuthenticationHeader = 2,
339  MD5 = 3,
341  Other = 4
342  };
343 
350  VrrpV2Layer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
351  : VrrpLayer(data, dataLen, prevLayer, packet, VRRPv2, IPAddress::IPv4AddressType) {}
352 
360  explicit VrrpV2Layer(uint8_t virtualRouterId, uint8_t priority, uint8_t advInt, uint8_t authType = 0);
361 
366 
370  uint8_t getAdvInt() const;
371 
376  void setAdvInt(uint8_t advInt);
377 
381  uint8_t getAuthType() const;
382 
386  VrrpAuthType getAuthTypeAsEnum() const;
387 
392  void setAuthType(uint8_t authType);
393 
394  // implement abstract methods
395 
400  uint16_t calculateChecksum() const override;
401  };
402 
407  class VrrpV3Layer : public VrrpLayer
408  {
409  private:
410  struct vrrpv3_rsvd_adv
411  {
412  uint16_t maxAdvInt;
413  };
414 
415  public:
423  VrrpV3Layer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet, IPAddress::AddressType addressType)
424  : VrrpLayer(data, dataLen, prevLayer, packet, VRRPv3, addressType) {}
425 
433  explicit VrrpV3Layer(IPAddress::AddressType addressType, uint8_t virtualRouterId, uint8_t priority, uint16_t maxAdvInt);
434 
439 
443  uint16_t getMaxAdvInt() const;
444 
449  void setMaxAdvInt(uint16_t maxAdvInt);
450 
451  // implement abstract methods
452 
457  uint16_t calculateChecksum() const override;
458  };
459 }
460 
461 #endif // PACKETPP_VRRP_LAYER
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:333
Definition: VrrpLayer.h:115
VrrpPriority
Definition: VrrpLayer.h:164
void parseNextLayer() override
Definition: VrrpLayer.h:296
AddressType
Definition: IpAddress.h:327
~VrrpV2Layer()
Definition: VrrpLayer.h:365
uint16_t authTypeAdvInt
Definition: VrrpLayer.h:99
size_t getHeaderLen() const override
Definition: VrrpLayer.h:306
uint16_t checksum
Definition: VrrpLayer.h:103
Definition: Layer.h:70
uint8_t type
Definition: VrrpLayer.h:78
uint8_t ipAddrCount
Definition: VrrpLayer.h:96
uint8_t version
Definition: VrrpLayer.h:78
Definition: Packet.h:26
Definition: VrrpLayer.h:169
Definition: VrrpLayer.h:167
Definition: ProtocolType.h:340
uint64_t ProtocolType
Definition: ProtocolType.h:18
VrrpV2Layer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: VrrpLayer.h:350
OsiModelLayer getOsiModelLayer() const override
Definition: VrrpLayer.h:310
~VrrpV3Layer()
Definition: VrrpLayer.h:438
Definition: VrrpLayer.h:317
const ProtocolType VRRPv3
Definition: ProtocolType.h:318
Definition: VrrpLayer.h:407
Definition: VrrpLayer.h:171
VrrpType
Definition: VrrpLayer.h:152
const ProtocolType VRRPv2
Definition: ProtocolType.h:313
VrrpAuthType
Definition: VrrpLayer.h:330
uint8_t vrId
Definition: VrrpLayer.h:90
Definition: VrrpLayer.h:74
Definition: IpAddress.h:321
VrrpV3Layer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet, IPAddress::AddressType addressType)
Definition: VrrpLayer.h:423
uint8_t priority
Definition: VrrpLayer.h:93
uint8_t * ipAddresses[]
Definition: VrrpLayer.h:106