libtins  4.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
mpls.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_MPLS_H
31 #define TINS_MPLS_H
32 
33 #include <tins/pdu.h>
34 #include <tins/endianness.h>
35 #include <tins/macros.h>
36 #include <tins/small_uint.h>
37 
38 namespace Tins {
39 
40 class ICMPExtension;
41 
46 class TINS_API MPLS : public PDU {
47 public:
51  static const PDU::PDUType pdu_flag = PDU::MPLS;
52 
56  MPLS();
57 
65  MPLS(const ICMPExtension& extension);
66 
70  MPLS(const uint8_t* buffer, uint32_t total_sz);
71 
76  return (Endian::be_to_host(header_.label_high) << 4) |
77  ((header_.label_low_exp_and_bottom >> 4) & 0xf);
78  }
79 
84  return (header_.label_low_exp_and_bottom >> 1) & 0x7;
85  }
86 
91  return header_.label_low_exp_and_bottom & 0x1;
92  }
93 
97  uint8_t ttl() const {
98  return header_.ttl;
99  }
100 
106  void label(small_uint<20> value);
107 
113  void experimental(small_uint<3> value);
114 
125  void bottom_of_stack(small_uint<1> value);
126 
132  void ttl(uint8_t value);
133 
140  uint32_t header_size() const;
141 
146  PDUType pdu_type() const {
147  return pdu_flag;
148  }
149 
153  MPLS* clone() const {
154  return new MPLS(*this);
155  }
156 private:
157  TINS_BEGIN_PACK
158  struct mpls_header {
159  uint16_t label_high;
160  uint8_t label_low_exp_and_bottom;
161  uint8_t ttl;
162  } TINS_END_PACK;
163 
164  void write_serialization(uint8_t* buffer, uint32_t total_sz);
165 
166  mpls_header header_;
167 };
168 
169 } // Tins
170 
171 #endif // TINS_MPLS_H
Represents an MPLS PDU.
Definition: mpls.h:46
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:127
MPLS * clone() const
Definition: mpls.h:153
Class that represents an ICMP extension object.
Definition: icmp_extension.h:46
small_uint< 20 > label() const
Getter for the label field.
Definition: mpls.h:75
small_uint< 3 > experimental() const
Getter for the experimental field.
Definition: mpls.h:83
Represents a field of n bits.
Definition: small_uint.h:52
uint8_t ttl() const
Getter for the ttl field.
Definition: mpls.h:97
PDUType pdu_type() const
Getter for the PDU's type.
Definition: mpls.h:146
small_uint< 1 > bottom_of_stack() const
Getter for the bottom of the stack field.
Definition: mpls.h:90
Base class for protocol data units.
Definition: pdu.h:107