PcapPlusPlus  Next
IPSecLayer.h
Go to the documentation of this file.
1 #pragma once
2 
4 
5 #include "Layer.h"
6 
11 namespace pcpp
12 {
17 #pragma pack(push, 1)
19  {
21  uint8_t nextHeader;
23  uint8_t payloadLen;
25  uint16_t reserved;
27  uint32_t spi;
29  uint32_t sequenceNumber;
30  };
31 #pragma pack(pop)
32 
37 #pragma pack(push, 1)
38  struct ipsec_esp
39  {
41  uint32_t spi;
43  uint32_t sequenceNumber;
44  };
45 #pragma pack(pop)
46 
52  {
53  public:
60  AuthenticationHeaderLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
61  : Layer(data, dataLen, prevLayer, packet, AuthenticationHeader)
62  {}
63 
70  {
71  return reinterpret_cast<ipsec_authentication_header*>(m_Data);
72  }
73 
77  uint32_t getSPI() const;
78 
82  uint32_t getSequenceNumber() const;
83 
87  size_t getICVLength() const;
88 
92  uint8_t* getICVBytes() const;
93 
97  std::string getICVHexStream() const;
98 
105  static inline bool isDataValid(const uint8_t* data, size_t dataLen);
106 
107  // implement abstract methods
108 
112  size_t getHeaderLen() const override
113  {
114  return static_cast<size_t>(4) * (getAHHeader()->payloadLen + 2);
115  }
116 
121  void parseNextLayer() override;
122 
126  void computeCalculateFields() override
127  {}
128 
129  std::string toString() const override;
130 
132  {
133  return OsiModelNetworkLayer;
134  }
135 
136  private:
137  // this layer supports parsing only
139  {}
140  };
141 
146  class ESPLayer : public Layer
147  {
148  public:
155  ESPLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
156  : Layer(data, dataLen, prevLayer, packet, ESP)
157  {}
158 
159  ipsec_esp* getESPHeader() const
160  {
161  return reinterpret_cast<ipsec_esp*>(m_Data);
162  }
163 
167  uint32_t getSPI() const;
168 
172  uint32_t getSequenceNumber() const;
173 
180  static inline bool isDataValid(const uint8_t* data, size_t dataLen);
181 
182  // implement abstract methods
183 
187  size_t getHeaderLen() const override
188  {
189  return sizeof(ipsec_esp);
190  }
191 
195  void parseNextLayer() override;
196 
200  void computeCalculateFields() override
201  {}
202 
203  std::string toString() const override;
204 
206  {
207  return OsiModelTransportLayer;
208  }
209 
210  private:
211  // this layer supports parsing only
212  ESPLayer()
213  {}
214  };
215 
216  // implementation of inline methods
217 
218  bool AuthenticationHeaderLayer::isDataValid(const uint8_t* data, size_t dataLen)
219  {
220  if (dataLen < sizeof(ipsec_authentication_header))
221  return false;
222 
223  size_t payloadLen = 4 * (data[1] + 2);
224  if (payloadLen < sizeof(ipsec_authentication_header) || payloadLen > dataLen)
225  return false;
226 
227  return true;
228  }
229 
230  bool ESPLayer::isDataValid(const uint8_t* data, size_t dataLen)
231  {
232  return data && dataLen >= sizeof(ipsec_esp);
233  }
234 } // namespace pcpp
Definition: IPSecLayer.h:52
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: IPSecLayer.h:218
size_t getHeaderLen() const override
Definition: IPSecLayer.h:112
std::string toString() const override
void computeCalculateFields() override
Definition: IPSecLayer.h:126
ipsec_authentication_header * getAHHeader() const
Definition: IPSecLayer.h:69
OsiModelLayer getOsiModelLayer() const override
Definition: IPSecLayer.h:131
uint32_t getSequenceNumber() const
std::string getICVHexStream() const
AuthenticationHeaderLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: IPSecLayer.h:60
Definition: IPSecLayer.h:147
std::string toString() const override
ESPLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: IPSecLayer.h:155
uint32_t getSPI() const
OsiModelLayer getOsiModelLayer() const override
Definition: IPSecLayer.h:205
uint32_t getSequenceNumber() const
size_t getHeaderLen() const override
Definition: IPSecLayer.h:187
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: IPSecLayer.h:230
void parseNextLayer() override
void computeCalculateFields() override
Definition: IPSecLayer.h:200
Definition: Layer.h:69
Definition: Packet.h:27
The main namespace for the PcapPlusPlus lib.
const ProtocolType ESP
Definition: ProtocolType.h:248
const ProtocolType AuthenticationHeader
Definition: ProtocolType.h:243
OsiModelLayer
Definition: ProtocolType.h:364
@ OsiModelTransportLayer
Definition: ProtocolType.h:372
@ OsiModelNetworkLayer
Definition: ProtocolType.h:370
Definition: IPSecLayer.h:19
uint32_t sequenceNumber
Definition: IPSecLayer.h:29
uint16_t reserved
Definition: IPSecLayer.h:25
uint8_t nextHeader
Definition: IPSecLayer.h:21
uint8_t payloadLen
Definition: IPSecLayer.h:23
uint32_t spi
Definition: IPSecLayer.h:27
Definition: IPSecLayer.h:39
uint32_t sequenceNumber
Definition: IPSecLayer.h:43
uint32_t spi
Definition: IPSecLayer.h:41