libtins  4.0
pppoe.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_PPPoE_H
31 #define TINS_PPPoE_H
32 
33 #include <string>
34 #include <vector>
35 #include <tins/pdu.h>
36 #include <tins/macros.h>
37 #include <tins/endianness.h>
38 #include <tins/small_uint.h>
39 #include <tins/pdu_option.h>
40 #include <tins/cxxstd.h>
41 
42 namespace Tins {
47 class TINS_API PPPoE : public PDU {
48 public:
52  enum TagTypes {
53  END_OF_LIST = 0,
54  SERVICE_NAME = 0x101,
55  #if TINS_IS_LITTLE_ENDIAN
56  AC_NAME = 0x201,
57  HOST_UNIQ = 0x301,
58  AC_COOKIE = 0x401,
59  VENDOR_SPECIFIC = 0x501,
60  RELAY_SESSION_ID = 0x101,
61  SERVICE_NAME_ERROR = 0x102,
62  AC_SYSTEM_ERROR = 0x202,
63  GENERIC_ERROR = 0x302
64  #else
65  AC_NAME = 0x102,
66  HOST_UNIQ = 0x103,
67  AC_COOKIE = 0x104,
68  VENDOR_SPECIFIC = 0x105,
69  RELAY_SESSION_ID = 0x110,
70  SERVICE_NAME_ERROR = 0x201,
71  AC_SYSTEM_ERROR = 0x202,
72  GENERIC_ERROR = 0x203
73  #endif
74  };
75 
80 
84  typedef std::vector<tag> tags_type;
85 
90  typedef std::vector<uint8_t> data_type;
91 
92  uint32_t vendor_id;
93  data_type data;
94 
95  vendor_spec_type(uint32_t vendor_id = 0, const data_type& data = data_type())
96  : vendor_id(vendor_id), data(data) { }
97 
98  static vendor_spec_type from_option(const tag& opt);
99  };
100 
104  static const PDU::PDUType pdu_flag = PDU::PPPOE;
105 
111  PPPoE();
112 
122  PPPoE(const uint8_t* buffer, uint32_t total_sz);
123 
124  // Getters
125 
131  return header_.version;
132  }
133 
138  small_uint<4> type() const {
139  return header_.type;
140  }
141 
146  uint8_t code() const {
147  return header_.code;
148  }
149 
154  uint16_t session_id() const {
155  return Endian::be_to_host(header_.session_id);
156  }
157 
162  uint16_t payload_length() const {
163  return Endian::be_to_host(header_.payload_length);
164  }
165 
171  uint32_t header_size() const;
172 
176  const tags_type& tags() const {
177  return tags_;
178  }
179 
183  PPPoE* clone() const {
184  return new PPPoE(*this);
185  }
186 
187  const tag* search_tag(TagTypes identifier) const;
188 
193  PDUType pdu_type() const { return pdu_flag; }
194 
195  // Setters
196 
201  void version(small_uint<4> new_version);
202 
207  void type(small_uint<4> new_type);
208 
213  void code(uint8_t new_code);
214 
219  void session_id(uint16_t new_session_id);
220 
225  void payload_length(uint16_t new_payload_length);
226 
232  void add_tag(const tag& option);
233 
234  #if TINS_IS_CXX11
235 
242  void add_tag(tag &&option) {
243  tags_size_ += static_cast<uint16_t>(option.data_size() + sizeof(uint16_t) * 2);
244  tags_.push_back(std::move(option));
245  }
246  #endif
247 
248  // Option setters
249 
253  void end_of_list();
254 
260  void service_name(const std::string& value);
261 
267  void ac_name(const std::string& value);
268 
274  void host_uniq(const byte_array& value);
275 
281  void ac_cookie(const byte_array& value);
282 
288  void vendor_specific(const vendor_spec_type& value);
289 
295  void relay_session_id(const byte_array& value);
296 
302  void service_name_error(const std::string& value);
303 
309  void ac_system_error(const std::string& value);
310 
316  void generic_error(const std::string& value);
317 
318  // Option getters
319 
326  std::string service_name() const;
327 
334  std::string ac_name() const;
335 
342  byte_array host_uniq() const;
343 
350  byte_array ac_cookie() const;
351 
358  vendor_spec_type vendor_specific() const;
359 
366  byte_array relay_session_id() const;
367 
374  std::string service_name_error() const;
375 
382  std::string ac_system_error() const;
383 
390  std::string generic_error() const;
391 private:
392  void write_serialization(uint8_t* buffer, uint32_t total_sz);
393 
394  template<typename T>
395  void add_tag_iterable(TagTypes id, const T& data) {
396  add_tag(
397  tag(
398  id,
399  data.begin(),
400  data.end()
401  )
402  );
403  }
404 
405  template<typename T>
406  T search_and_convert(TagTypes id) const {
407  const tag* t = search_tag(id);
408  if (!t) {
409  throw option_not_found();
410  }
411  return t->to<T>();
412  }
413 
414  TINS_BEGIN_PACK
415  struct pppoe_header {
416  #if TINS_IS_LITTLE_ENDIAN
417  uint8_t version:4,
418  type:4;
419  uint8_t code;
420  #else
421  uint16_t version:4,
422  type:4,
423  code:8;
424  #endif
425  uint16_t session_id;
426  uint16_t payload_length;
427  } TINS_END_PACK;
428 
429  pppoe_header header_;
430  tags_type tags_;
431  uint16_t tags_size_;
432 };
433 }
434 
435 #endif // TINS_PPPoE_H
TagTypes
Definition: pppoe.h:52
std::vector< uint8_t > byte_array
Definition: pdu.h:45
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:127
small_uint< 4 > type() const
Getter for the type field.
Definition: pppoe.h:138
small_uint< 4 > version() const
Getter for the version field.
Definition: pppoe.h:130
Definition: pppoe.h:89
Represents a PDU option field.
Definition: pdu_option.h:201
PPPoE * clone() const
Definition: pppoe.h:183
void add_tag(tag &&option)
Adds a PPPoE tag.
Definition: pppoe.h:242
PDUOption< TagTypes, PPPoE > tag
Definition: pppoe.h:79
uint16_t session_id() const
Getter for the session_id field.
Definition: pppoe.h:154
The Tins namespace.
Definition: address_range.h:38
const tags_type & tags() const
Returns the list of tags.
Definition: pppoe.h:176
T to() const
Constructs a T from this PDUOption.
Definition: pdu_option.h:389
uint16_t payload_length() const
Getter for the payload_length field.
Definition: pppoe.h:162
uint8_t code() const
Getter for the code field.
Definition: pppoe.h:146
std::vector< tag > tags_type
Definition: pppoe.h:84
PDUType pdu_type() const
Getter for the PDU&#39;s type.
Definition: pppoe.h:193
size_t data_size() const
Retrieves the length of this option&#39;s data.
Definition: pdu_option.h:361
Base class for protocol data units.
Definition: pdu.h:107
Exception thrown when an option is not found.
Definition: exceptions.h:56
Represents a Point-to-point protocol over Ethernet PDU.
Definition: pppoe.h:47