libtins  4.0
stp.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_STP_H
31 #define TINS_STP_H
32 
33 #include <tins/pdu.h>
34 #include <tins/macros.h>
35 #include <tins/endianness.h>
36 #include <tins/hw_address.h>
37 #include <tins/small_uint.h>
38 
39 namespace Tins {
44 class TINS_API STP : public PDU {
45 public:
49  static const PDU::PDUType pdu_flag = PDU::STP;
50 
55 
59  struct bpdu_id_type {
60  small_uint<4> priority;
61  small_uint<12> ext_id;
62  address_type id;
63 
64  bpdu_id_type(small_uint<4> priority=0, small_uint<12> ext_id=0,
65  const address_type& id=address_type())
66  : priority(priority), ext_id(ext_id), id(id) { }
67  };
68 
72  STP();
73 
83  STP(const uint8_t* buffer, uint32_t total_sz);
84 
85  // Getters
86 
91  uint16_t proto_id() const {
92  return Endian::be_to_host(header_.proto_id);
93  }
94 
99  uint8_t proto_version() const {
100  return header_.proto_version;
101  }
102 
107  uint8_t bpdu_type() const {
108  return header_.bpdu_type;
109  }
110 
115  uint8_t bpdu_flags() const {
116  return header_.bpdu_flags;
117  }
118 
123  uint32_t root_path_cost() const {
124  return Endian::be_to_host(header_.root_path_cost);
125  }
126 
131  uint16_t port_id() const {
132  return Endian::be_to_host(header_.port_id);
133  }
134 
139  uint16_t msg_age() const {
140  return Endian::be_to_host(header_.msg_age) / 256;
141  }
142 
147  uint16_t max_age() const {
148  return Endian::be_to_host(header_.max_age) / 256;
149  }
150 
155  uint16_t hello_time() const {
156  return Endian::be_to_host(header_.hello_time) / 256;
157  }
158 
163  uint16_t fwd_delay() const {
164  return Endian::be_to_host(header_.fwd_delay) / 256;
165  }
166 
171  bpdu_id_type root_id() const;
172 
177  bpdu_id_type bridge_id() const;
178 
183  PDUType pdu_type() const {
184  return pdu_flag;
185  }
186 
190  STP* clone() const {
191  return new STP(*this);
192  }
193 
199  uint32_t header_size() const;
200 
201  // Setters
202 
207  void proto_id(uint16_t new_proto_id);
208 
213  void proto_version(uint8_t new_proto_version);
214 
219  void bpdu_type(uint8_t new_bpdu_type);
220 
225  void bpdu_flags(uint8_t new_bpdu_flags);
226 
231  void root_path_cost(uint32_t new_root_path_cost);
232 
237  void port_id(uint16_t new_port_id);
238 
243  void msg_age(uint16_t new_msg_age);
244 
249  void max_age(uint16_t new_max_age);
250 
255  void hello_time(uint16_t new_hello_time);
256 
261  void fwd_delay(uint16_t new_fwd_delay);
262 
267  void root_id(const bpdu_id_type& id);
268 
273  void bridge_id(const bpdu_id_type& id);
274 private:
275  TINS_BEGIN_PACK
276  struct pvt_bpdu_id {
277  #if TINS_IS_LITTLE_ENDIAN
278  // fixme
279  uint16_t ext_id:4,
280  priority:4,
281  ext_idL:8;
282  #else
283  uint16_t priority:4,
284  ext_id:12;
285  #endif
286  uint8_t id[6];
287  } TINS_END_PACK;
288 
289  TINS_BEGIN_PACK
290  struct stp_header {
291  uint16_t proto_id;
292  uint8_t proto_version;
293  uint8_t bpdu_type;
294  uint8_t bpdu_flags;
295  pvt_bpdu_id root_id;
296  uint32_t root_path_cost;
297  pvt_bpdu_id bridge_id;
298  uint16_t port_id;
299  uint16_t msg_age;
300  uint16_t max_age;
301  uint16_t hello_time;
302  uint16_t fwd_delay;
303  } TINS_END_PACK;
304 
305  static bpdu_id_type convert(const pvt_bpdu_id& id);
306  static pvt_bpdu_id convert(const bpdu_id_type& id);
307 
308  void write_serialization(uint8_t* buffer, uint32_t total_sz);
309 
310  stp_header header_;
311 };
312 }
313 
314 #endif // TINS_STP_H
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:127
uint8_t bpdu_type() const
Getter for the BDU Type field.
Definition: stp.h:107
uint16_t fwd_delay() const
Getter for the Forward Delay field.
Definition: stp.h:163
uint16_t hello_time() const
Getter for the Hello Time field.
Definition: stp.h:155
uint16_t port_id() const
Getter for the Port ID field.
Definition: stp.h:131
uint16_t proto_id() const
Getter for the Protocol ID field.
Definition: stp.h:91
Represents a Spanning Tree Protocol PDU.
Definition: stp.h:44
The Tins namespace.
Definition: address_range.h:38
uint32_t root_path_cost() const
Getter for the Root Path Cost field.
Definition: stp.h:123
Definition: stp.h:59
uint16_t msg_age() const
Getter for the Message Age field.
Definition: stp.h:139
PDUType pdu_type() const
Getter for the PDU&#39;s type.
Definition: stp.h:183
uint8_t bpdu_flags() const
Getter for the BDU Flags field.
Definition: stp.h:115
uint16_t max_age() const
Getter for the Maximum Age field.
Definition: stp.h:147
uint8_t proto_version() const
Getter for the Protocol Version field.
Definition: stp.h:99
STP * clone() const
Definition: stp.h:190
Base class for protocol data units.
Definition: pdu.h:107
HWAddress< 6 > address_type
Definition: stp.h:54