PcapPlusPlus  24.09
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 (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
113  {
114  return 4 * (getAHHeader()->payloadLen + 2);
115  }
116 
122 
127  {}
128 
129  std::string toString() const;
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
188  {
189  return sizeof(ipsec_esp);
190  }
191 
196 
201  {}
202 
203  std::string toString() const;
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
void computeCalculateFields()
Definition: IPSecLayer.h:126
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: IPSecLayer.h:218
std::string toString() const
ipsec_authentication_header * getAHHeader() const
Definition: IPSecLayer.h:69
OsiModelLayer getOsiModelLayer() const
Definition: IPSecLayer.h:131
size_t getHeaderLen() const
Definition: IPSecLayer.h:112
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
ESPLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: IPSecLayer.h:155
uint32_t getSPI() const
std::string toString() const
void computeCalculateFields()
Definition: IPSecLayer.h:200
size_t getHeaderLen() const
Definition: IPSecLayer.h:187
uint32_t getSequenceNumber() const
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: IPSecLayer.h:230
void parseNextLayer()
OsiModelLayer getOsiModelLayer() const
Definition: IPSecLayer.h:205
Definition: Layer.h:70
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:354
@ OsiModelTransportLayer
Definition: ProtocolType.h:362
@ OsiModelNetworkLayer
Definition: ProtocolType.h:360
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