libtins  3.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
mpls.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_MPLS_H
31 #define TINS_MPLS_H
32 
33 #include "pdu.h"
34 #include "endianness.h"
35 #include "macros.h"
36 #include "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_and_bottom >> 4) & 0xf);
78  }
79 
84  return header_.label_low_and_bottom & 0x1;
85  }
86 
90  uint8_t ttl() const {
91  return header_.ttl;
92  }
93 
99  void label(small_uint<20> value);
100 
111  void bottom_of_stack(small_uint<1> value);
112 
118  void ttl(uint8_t value);
119 
126  uint32_t header_size() const;
127 
132  PDUType pdu_type() const {
133  return pdu_flag;
134  }
135 
139  MPLS* clone() const {
140  return new MPLS(*this);
141  }
142 private:
143  TINS_BEGIN_PACK
144  struct mpls_header {
145  uint16_t label_high;
146  uint8_t label_low_and_bottom;
147  uint8_t ttl;
148  } TINS_END_PACK;
149 
150  void write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* parent);
151 
152  mpls_header header_;
153 };
154 
155 } // Tins
156 
157 #endif // TINS_MPLS_H
Represents an MPLS PDU.
Definition: mpls.h:46
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:128
MPLS * clone() const
Definition: mpls.h:139
Class that represents an ICMP extension object.
Definition: icmp_extension.h:47
small_uint< 20 > label() const
Getter for the label field.
Definition: mpls.h:75
Represents a field of n bits.
Definition: small_uint.h:52
uint8_t ttl() const
Getter for the ttl field.
Definition: mpls.h:90
PDUType pdu_type() const
Getter for the PDU's type.
Definition: mpls.h:132
small_uint< 1 > bottom_of_stack() const
Getter for the bottom of the stack field.
Definition: mpls.h:83
Base class for protocol data units.
Definition: pdu.h:108