libtins  3.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
ipv6.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_IPV6_h
31 #define TINS_IPV6_h
32 
33 #include <list>
34 #include <stdexcept>
35 #include "macros.h"
36 #include "pdu.h"
37 #include "endianness.h"
38 #include "small_uint.h"
39 #include "pdu_option.h"
40 #include "ipv6_address.h"
41 
42 namespace Tins {
43 namespace Memory {
44 
45 class OutputMemoryStream;
46 
47 } // Memory
48 
49 class PacketSender;
50 
55 class TINS_API IPv6 : public PDU {
56 public:
60  static const PDU::PDUType pdu_flag = PDU::IPv6;
61 
66 
71 
75  typedef std::list<ext_header> headers_type;
76 
81  HOP_BY_HOP = 0,
82  DESTINATION_ROUTING_OPTIONS = 60,
83  ROUTING = 43,
84  FRAGMENT = 44,
85  AUTHENTICATION = 51,
86  SECURITY_ENCAPSULATION = 50,
87  DESTINATION_OPTIONS = 60,
88  MOBILITY = 135,
89  NO_NEXT_HEADER = 59
90  };
91 
98  static metadata extract_metadata(const uint8_t *buffer, uint32_t total_sz);
99 
108  IPv6(address_type ip_dst = address_type(),
109  address_type ip_src = address_type(),
110  PDU* child = 0);
111 
122  IPv6(const uint8_t* buffer, uint32_t total_sz);
123 
124  // Getters
125 
131  return header_.version;
132  }
133 
138  uint8_t traffic_class() const {
139  #if TINS_IS_LITTLE_ENDIAN
140  return ((header_.traffic_class << 4) & 0xf0) |
141  ((header_.flow_label[0] >> 4) & 0x0f);
142  #else
143  return header_.traffic_class;
144  #endif
145  }
146 
152  #if TINS_IS_LITTLE_ENDIAN
153  return ((header_.flow_label[0] & 0x0f) << 16)
154  | (header_.flow_label[1] << 8)
155  | (header_.flow_label[2]);
156  #else
157  return header_.flow_label;
158  #endif
159  }
160 
165  uint16_t payload_length() const {
166  return Endian::be_to_host(header_.payload_length);
167  }
168 
173  uint8_t next_header() const {
174  return header_.next_header;
175  }
176 
181  uint8_t hop_limit() const {
182  return header_.hop_limit;
183  }
184 
190  return header_.src_addr;
191  }
192 
198  return header_.dst_addr;
199  }
200 
205  const headers_type& headers() const {
206  return ext_headers_;
207  }
208 
209  // Setters
210 
215  void version(small_uint<4> new_version);
216 
221  void traffic_class(uint8_t new_traffic_class);
222 
227  void flow_label(small_uint<20> new_flow_label);
228 
233  void payload_length(uint16_t new_payload_length);
234 
239  void next_header(uint8_t new_next_header);
240 
245  void hop_limit(uint8_t new_hop_limit);
246 
251  void src_addr(const address_type& new_src_addr);
252 
257  void dst_addr(const address_type& new_dst_addr);
258 
264  uint32_t header_size() const;
265 
273  bool matches_response(const uint8_t* ptr, uint32_t total_sz) const;
274 
278  IPv6* clone() const {
279  return new IPv6(*this);
280  }
281 
286  PDUType pdu_type() const { return pdu_flag; }
287 
288  #ifndef BSD
289 
292  void send(PacketSender& sender, const NetworkInterface &);
293  #endif
294 
300  void add_ext_header(const ext_header& header);
301 
312  const ext_header* search_header(ExtensionHeader id) const;
313 private:
314  void write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* parent);
315  void set_last_next_header(uint8_t value);
316  static void write_header(const ext_header& header, Memory::OutputMemoryStream& stream);
317  static bool is_extension_header(uint8_t header_id);
318 
319  TINS_BEGIN_PACK
320  struct ipv6_header {
321  #if TINS_IS_BIG_ENDIAN
322  uint32_t version:4,
323  traffic_class:8,
324  flow_label:20;
325  uint32_t payload_length:16,
326  next_header:8,
327  hop_limit:8;
328  #else
329  uint8_t traffic_class:4,
330  version:4;
331  uint8_t flow_label[3];
332  uint16_t payload_length;
333  uint8_t next_header;
334  uint8_t hop_limit;
335  #endif
336  uint8_t src_addr[16], dst_addr[16];
337  } TINS_END_PACK;
338 
339  ipv6_header header_;
340  headers_type ext_headers_;
341  uint32_t headers_size_;
342 };
343 }
344 
345 #endif // TINS_IPV6_h
small_uint< 20 > flow_label() const
Getter for the flow_label field.
Definition: ipv6.h:151
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:128
uint8_t hop_limit() const
Getter for the hop_limit field.
Definition: ipv6.h:181
std::list< ext_header > headers_type
Definition: ipv6.h:75
address_type src_addr() const
Getter for the src_addr field.
Definition: ipv6.h:189
uint16_t payload_length() const
Getter for the payload_length field.
Definition: ipv6.h:165
uint8_t traffic_class() const
Getter for the traffic_class field.
Definition: ipv6.h:138
IPv6Address address_type
Definition: ipv6.h:65
Sends packets through a network interface.
Definition: packet_sender.h:117
Represents a PDU option field.
Definition: pdu_option.h:320
PDUOption< uint8_t, IPv6 > ext_header
Definition: ipv6.h:70
address_type dst_addr() const
Getter for the dst_addr field.
Definition: ipv6.h:197
small_uint< 4 > version() const
Getter for the version field.
Definition: ipv6.h:130
Definition: ipv6.h:55
const headers_type & headers() const
Getter for the IPv6 extension headers.
Definition: ipv6.h:205
ExtensionHeader
Definition: ipv6.h:80
PDUType pdu_type() const
Getter for the PDU's type.
Definition: ipv6.h:286
Abstraction of a network interface.
Definition: network_interface.h:47
uint8_t next_header() const
Getter for the next_header field.
Definition: ipv6.h:173
IPv6 * clone() const
Definition: ipv6.h:278
Definition: ipv6_address.h:44
Base class for protocol data units.
Definition: pdu.h:108