libtins  4.0
icmp_extension.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_ICMP_EXTENSION_H
31 #define TINS_ICMP_EXTENSION_H
32 
33 #include <vector>
34 #include <stdint.h>
35 #include <tins/macros.h>
36 #include <tins/small_uint.h>
37 #include <tins/endianness.h>
38 
39 namespace Tins {
40 
41 class MPLS;
42 
46 class TINS_API ICMPExtension {
47 public:
51  typedef std::vector<uint8_t> payload_type;
52 
57  typedef std::vector<uint8_t> serialization_type;
58 
62  ICMPExtension();
63 
70  ICMPExtension(uint8_t ext_class, uint8_t ext_type);
71 
78  ICMPExtension(const uint8_t* buffer, uint32_t total_sz);
79 
85  void extension_class(uint8_t value);
86 
92  void extension_type(uint8_t value);
93 
99  void payload(const payload_type& value);
100 
106  uint8_t extension_class() const {
107  return extension_class_;
108  }
109 
115  uint8_t extension_type() const {
116  return extension_type_;
117  }
118 
124  const payload_type& payload() const {
125  return payload_;
126  }
127 
135  uint32_t size() const;
136 
143  void serialize(uint8_t* buffer, uint32_t buffer_size) const;
144 
150  serialization_type serialize() const;
151 private:
152  static const uint32_t BASE_HEADER_SIZE;
153 
154  payload_type payload_;
155  uint8_t extension_class_, extension_type_;
156 };
157 
161 class TINS_API ICMPExtensionsStructure {
162 public:
167  static const uint32_t MINIMUM_ICMP_PAYLOAD;
168 
174 
178  typedef std::vector<ICMPExtension> extensions_type;
179 
186 
193  ICMPExtensionsStructure(const uint8_t* buffer, uint32_t total_sz);
194 
200  void version(small_uint<4> value);
201 
207  void reserved(small_uint<12> value);
208 
215  uint16_t value = Endian::be_to_host(version_and_reserved_);
216  return (value >> 12) & 0xf;
217  }
218 
225  uint16_t value = Endian::be_to_host(version_and_reserved_);
226  return value & 0xfff;
227  }
228 
234  uint16_t checksum() const {
235  return Endian::be_to_host(checksum_);
236  }
237 
243  const extensions_type& extensions() const {
244  return extensions_;
245  }
246 
252  void add_extension(const ICMPExtension& extension);
253 
262  void add_extension(MPLS& mpls);
263 
269  uint32_t size() const;
270 
277  void serialize(uint8_t* buffer, uint32_t buffer_size);
278 
284  serialization_type serialize();
285 
296  static bool validate_extensions(const uint8_t* buffer, uint32_t total_sz);
297 private:
298  static const uint32_t BASE_HEADER_SIZE;
299 
300  uint16_t version_and_reserved_;
301  uint16_t checksum_;
302  extensions_type extensions_;
303 };
304 
305 } // Tins
306 
307 #endif // TINS_ICMP_EXTENSION_H
uint8_t extension_class() const
Getter for the extension class field.
Definition: icmp_extension.h:106
Represents an MPLS PDU.
Definition: mpls.h:46
std::vector< uint8_t > serialization_type
Definition: icmp_extension.h:57
std::vector< ICMPExtension > extensions_type
Definition: icmp_extension.h:178
Class that represents an ICMP extension object.
Definition: icmp_extension.h:46
ICMPExtension::serialization_type serialization_type
Definition: icmp_extension.h:173
uint8_t extension_type() const
Getter for the extension sub-type field.
Definition: icmp_extension.h:115
small_uint< 12 > reserved() const
Getter for the reserved field.
Definition: icmp_extension.h:224
The Tins namespace.
Definition: address_range.h:38
small_uint< 4 > version() const
Getter for the version field.
Definition: icmp_extension.h:214
const extensions_type & extensions() const
Getter for the extensions stored by this structure.
Definition: icmp_extension.h:243
std::vector< uint8_t > payload_type
Definition: icmp_extension.h:51
uint16_t checksum() const
Getter for the checksum field.
Definition: icmp_extension.h:234
Class that represents an ICMP extensions structure.
Definition: icmp_extension.h:161
const payload_type & payload() const
Getter for the extension payload field.
Definition: icmp_extension.h:124
static const uint32_t MINIMUM_ICMP_PAYLOAD
Definition: icmp_extension.h:167