libtins  3.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
icmp_extension.h
1 /*
2  * Copyright (c) 2016, 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 <list>
35 #include <stdint.h>
36 #include "macros.h"
37 #include "small_uint.h"
38 #include "endianness.h"
39 
40 namespace Tins {
41 
42 class MPLS;
43 
47 class TINS_API ICMPExtension {
48 public:
52  typedef std::vector<uint8_t> payload_type;
53 
58  typedef std::vector<uint8_t> serialization_type;
59 
63  ICMPExtension();
64 
71  ICMPExtension(uint8_t ext_class, uint8_t ext_type);
72 
79  ICMPExtension(const uint8_t* buffer, uint32_t total_sz);
80 
86  void extension_class(uint8_t value);
87 
93  void extension_type(uint8_t value);
94 
100  void payload(const payload_type& value);
101 
107  uint8_t extension_class() const {
108  return extension_class_;
109  }
110 
116  uint8_t extension_type() const {
117  return extension_type_;
118  }
119 
125  const payload_type& payload() const {
126  return payload_;
127  }
128 
136  uint32_t size() const;
137 
144  void serialize(uint8_t* buffer, uint32_t buffer_size) const;
145 
151  serialization_type serialize() const;
152 private:
153  static const uint32_t BASE_HEADER_SIZE;
154 
155  payload_type payload_;
156  uint8_t extension_class_, extension_type_;
157 };
158 
162 class TINS_API ICMPExtensionsStructure {
163 public:
168  static const uint32_t MINIMUM_ICMP_PAYLOAD;
169 
175 
179  typedef std::list<ICMPExtension> extensions_type;
180 
187 
194  ICMPExtensionsStructure(const uint8_t* buffer, uint32_t total_sz);
195 
201  void version(small_uint<4> value);
202 
208  void reserved(small_uint<12> value);
209 
216  uint16_t value = Endian::be_to_host(version_and_reserved_);
217  return (value >> 12) & 0xf;
218  }
219 
226  uint16_t value = Endian::be_to_host(version_and_reserved_);
227  return value & 0xfff;
228  }
229 
235  uint16_t checksum() const {
236  return Endian::be_to_host(checksum_);
237  }
238 
244  const extensions_type& extensions() const {
245  return extensions_;
246  }
247 
253  void add_extension(const ICMPExtension& extension);
254 
263  void add_extension(MPLS& mpls);
264 
270  uint32_t size() const;
271 
278  void serialize(uint8_t* buffer, uint32_t buffer_size);
279 
285  serialization_type serialize();
286 
297  static bool validate_extensions(const uint8_t* buffer, uint32_t total_sz);
298 private:
299  static const uint32_t BASE_HEADER_SIZE;
300 
301  uint16_t version_and_reserved_;
302  uint16_t checksum_;
303  extensions_type extensions_;
304 };
305 
306 } // Tins
307 
308 #endif // TINS_ICMP_EXTENSION_H
uint8_t extension_class() const
Getter for the extension class field.
Definition: icmp_extension.h:107
Represents an MPLS PDU.
Definition: mpls.h:46
std::vector< uint8_t > serialization_type
Definition: icmp_extension.h:58
Class that represents an ICMP extension object.
Definition: icmp_extension.h:47
ICMPExtension::serialization_type serialization_type
Definition: icmp_extension.h:174
uint8_t extension_type() const
Getter for the extension sub-type field.
Definition: icmp_extension.h:116
small_uint< 12 > reserved() const
Getter for the reserved field.
Definition: icmp_extension.h:225
small_uint< 4 > version() const
Getter for the version field.
Definition: icmp_extension.h:215
const extensions_type & extensions() const
Getter for the extensions stored by this structure.
Definition: icmp_extension.h:244
std::vector< uint8_t > payload_type
Definition: icmp_extension.h:52
uint16_t checksum() const
Getter for the checksum field.
Definition: icmp_extension.h:235
Class that represents an ICMP extensions structure.
Definition: icmp_extension.h:162
const payload_type & payload() const
Getter for the extension payload field.
Definition: icmp_extension.h:125
std::list< ICMPExtension > extensions_type
Definition: icmp_extension.h:179
static const uint32_t MINIMUM_ICMP_PAYLOAD
Definition: icmp_extension.h:168