libtins  4.0
ipsec.h
1 /*
2  * Copyright (c) 2017, Matias Fontanini
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #ifndef TINS_IPSEC_H
31 #define TINS_IPSEC_H
32 
33 #include <tins/pdu.h>
34 #include <tins/macros.h>
35 #include <tins/endianness.h>
36 #include <tins/small_uint.h>
37 
38 namespace Tins {
39 
44 class TINS_API IPSecAH : public PDU {
45 public:
49  static const PDU::PDUType pdu_flag = PDU::IPSEC_AH;
50 
57  IPSecAH();
58 
70  IPSecAH(const uint8_t* buffer, uint32_t total_sz);
71 
72  // Getters
73 
78  uint8_t next_header() const {
79  return header_.next_header;
80  }
81 
86  uint8_t length() const {
87  return header_.length;
88  }
89 
94  uint32_t spi() const {
95  return Endian::be_to_host(header_.spi);
96  }
97 
102  uint32_t seq_number() const {
103  return Endian::be_to_host(header_.seq_number);
104  }
105 
110  const byte_array& icv() const {
111  return icv_;
112  }
113 
114  // Setters
115 
120  void next_header(uint8_t new_next_header);
121 
126  void length(uint8_t new_length);
127 
132  void spi(uint32_t new_spi);
133 
138  void seq_number(uint32_t new_seq_number);
139 
144  void icv(const byte_array& newicv_);
145 
151  uint32_t header_size() const;
152 
157  PDUType pdu_type() const { return pdu_flag; }
158 
162  IPSecAH* clone() const {
163  return new IPSecAH(*this);
164  }
165 private:
166  struct ipsec_header {
167  uint8_t next_header, length;
168  uint32_t spi, seq_number;
169  };
170 
171  void write_serialization(uint8_t* buffer, uint32_t total_sz);
172 
173  ipsec_header header_;
174  byte_array icv_;
175 };
176 
180 class TINS_API IPSecESP : public PDU {
181 public:
185  static const PDU::PDUType pdu_flag = PDU::IPSEC_ESP;
186 
190  IPSecESP();
191 
203  IPSecESP(const uint8_t* buffer, uint32_t total_sz);
204 
205  // Getters
206 
211  uint32_t spi() const {
212  return Endian::be_to_host(header_.spi);
213  }
214 
219  uint32_t seq_number() const {
220  return Endian::be_to_host(header_.seq_number);
221  }
222 
223  // Setters
224 
229  void spi(uint32_t new_spi);
230 
235  void seq_number(uint32_t new_seq_number);
236 
242  uint32_t header_size() const;
243 
248  PDUType pdu_type() const { return pdu_flag; }
249 
253  IPSecESP* clone() const {
254  return new IPSecESP(*this);
255  }
256 private:
257  struct ipsec_header {
258  uint32_t spi, seq_number;
259  };
260 
261  void write_serialization(uint8_t* buffer, uint32_t total_sz);
262 
263  ipsec_header header_;
264 };
265 }
266 
267 #endif // TINS_IPSEC_H
std::vector< uint8_t > byte_array
Definition: pdu.h:45
uint8_t next_header() const
Getter for the Next header field.
Definition: ipsec.h:78
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:127
const byte_array & icv() const
Getter for the ICV field.
Definition: ipsec.h:110
uint32_t seq_number() const
Getter for the Sequence number field.
Definition: ipsec.h:102
uint32_t seq_number() const
Getter for the Sequence number field.
Definition: ipsec.h:219
uint32_t spi() const
Getter for the Security Parameters Index field.
Definition: ipsec.h:94
The Tins namespace.
Definition: address_range.h:38
IPSecAH * clone() const
Definition: ipsec.h:162
PDUType pdu_type() const
Getter for the PDU&#39;s type.
Definition: ipsec.h:248
Represents an IPSec Authentication Header.
Definition: ipsec.h:44
PDUType pdu_type() const
Getter for the PDU&#39;s type.
Definition: ipsec.h:157
uint32_t spi() const
Getter for the Security Parameters Index field.
Definition: ipsec.h:211
uint8_t length() const
Getter for the Length field.
Definition: ipsec.h:86
Base class for protocol data units.
Definition: pdu.h:107
Represents an IPSec Authentication Header.
Definition: ipsec.h:180
IPSecESP * clone() const
Definition: ipsec.h:253