libtins  4.0
dot1q.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_DOT1Q_H
31 #define TINS_DOT1Q_H
32 
33 #include <tins/pdu.h>
34 #include <tins/macros.h>
35 #include <tins/endianness.h>
36 #include <tins/small_uint.h>
37 
38 namespace Tins {
39 
44 class TINS_API Dot1Q : public PDU {
45 public:
49  static const PDU::PDUType pdu_flag = PDU::DOT1Q;
50 
57  static metadata extract_metadata(const uint8_t *buffer, uint32_t total_sz);
58 
62  Dot1Q(small_uint<12> tag_id = 0, bool append_pad = true);
63 
77  Dot1Q(const uint8_t* buffer, uint32_t total_sz);
78 
79  // Getters
80 
86  uint32_t header_size() const;
87 
92  uint32_t trailer_size() const;
93 
99  return header_.priority;
100  }
101 
106  small_uint<1> cfi() const {
107  return header_.cfi;
108  }
109 
114  small_uint<12> id() const {
115  #if TINS_IS_LITTLE_ENDIAN
116  return header_.idL | (header_.idH << 8);
117  #else
118  return header_.id;
119  #endif
120  }
121 
126  uint16_t payload_type() const {
127  return Endian::be_to_host(header_.type);
128  }
129 
134  PDUType pdu_type() const {
135  return pdu_flag;
136  }
137 
141  Dot1Q* clone() const {
142  return new Dot1Q(*this);
143  }
144 
149  bool append_padding() const {
150  return append_padding_;
151  }
152 
153  // Setters
154 
159  void priority(small_uint<3> new_priority);
160 
165  void cfi(small_uint<1> new_cfi);
166 
171  void id(small_uint<12> new_id);
172 
177  void payload_type(uint16_t new_type);
178 
189  void append_padding(bool value);
190 
198  bool matches_response(const uint8_t* ptr, uint32_t total_sz) const;
199 private:
200  void write_serialization(uint8_t* buffer, uint32_t total_sz);
201 
202  TINS_BEGIN_PACK
203  struct dot1q_header {
204  #if TINS_IS_BIG_ENDIAN
205  uint16_t priority:3,
206  cfi:1,
207  id:12;
208  uint16_t type;
209  #else
210  uint16_t idH:4,
211  cfi:1,
212  priority:3,
213  idL:8;
214  uint16_t type;
215  #endif
216  } TINS_END_PACK;
217 
218  static uint16_t get_id(const dot1q_header* hdr);
219 
220  dot1q_header header_;
221  bool append_padding_;
222 };
223 }
224 
225 #endif // TINS_DOT1Q_H
small_uint< 1 > cfi() const
Getter for the Canonical Format Identifier field.
Definition: dot1q.h:106
Definition: dot1q.h:44
Dot1Q * clone() const
Definition: dot1q.h:141
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:127
bool append_padding() const
Retrieves the flag indicating whether padding will be appended at the end of this packet...
Definition: dot1q.h:149
small_uint< 3 > priority() const
Getter for the priority field.
Definition: dot1q.h:98
The Tins namespace.
Definition: address_range.h:38
Type used to store a PDU header&#39;s data.
Definition: pdu.h:194
small_uint< 12 > id() const
Getter for the VLAN ID field.
Definition: dot1q.h:114
PDUType pdu_type() const
Getter for the PDU&#39;s type.
Definition: dot1q.h:134
uint16_t payload_type() const
Getter for the payload type field.
Definition: dot1q.h:126
Base class for protocol data units.
Definition: pdu.h:107