libtins  3.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
snap.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_SNAP_H
31 #define TINS_SNAP_H
32 
33 
34 #include <stdint.h>
35 #include "pdu.h"
36 #include "macros.h"
37 #include "endianness.h"
38 #include "small_uint.h"
39 
40 namespace Tins {
41 
49 class TINS_API SNAP : public PDU {
50 public:
54  static const PDU::PDUType pdu_flag = PDU::SNAP;
55 
61  SNAP();
62 
75  SNAP(const uint8_t* buffer, uint32_t total_sz);
76 
77  /* Setters */
78 
83  void control(uint8_t new_control);
84 
89  void org_code(small_uint<24> new_org);
90 
95  void eth_type(uint16_t new_eth);
96 
97  /* Getters */
98 
103  uint8_t dsap() const {
104  return snap_.dsap;
105  }
106 
111  uint8_t ssap() const {
112  return snap_.ssap;
113  }
114 
119  uint8_t control() const {
120  #if TINS_IS_LITTLE_ENDIAN
121  return (snap_.control_org) & 0xff;
122  #else
123  return (snap_.control_org >> 24) & 0xff;
124  #endif
125  }
126 
132  #if TINS_IS_LITTLE_ENDIAN
133  return Endian::be_to_host<uint32_t>(snap_.control_org & 0xffffff00);
134  #else
135  return snap_.control_org & 0xffffff;
136  #endif
137  }
138 
143  uint16_t eth_type() const {
144  return Endian::be_to_host(snap_.eth_type);
145  }
146 
153  uint32_t header_size() const;
154 
159  PDUType pdu_type() const {
160  return pdu_flag;
161  }
162 
168  SNAP* clone() const {
169  return new SNAP(*this);
170  }
171 private:
172  TINS_BEGIN_PACK
173  struct snap_header {
174  uint8_t dsap;
175  uint8_t ssap;
176  uint32_t control_org;
177  uint16_t eth_type;
178  } TINS_END_PACK;
179 
180  void write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* parent);
181 
182  snap_header snap_;
183 };
184 
185 } // Tins
186 
187 #endif // TINS_SNAP_H
uint8_t ssap() const
Getter for the SSAP field.
Definition: snap.h:111
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:128
small_uint< 24 > org_code() const
Getter for the Organization Code field.
Definition: snap.h:131
uint8_t control() const
Getter for the Control field.
Definition: snap.h:119
PDUType pdu_type() const
Getter for the PDU's type.
Definition: snap.h:159
SNAP * clone() const
Clones this PDU.
Definition: snap.h:168
uint16_t eth_type() const
Getter for the Ethernet Type field.
Definition: snap.h:143
uint8_t dsap() const
Getter for the DSAP field.
Definition: snap.h:103
Represents a SNAP frame.
Definition: snap.h:49
Base class for protocol data units.
Definition: pdu.h:108