libtins  4.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
dot11_data.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 #include <tins/config.h>
31 
32 #if !defined(TINS_DOT11_DOT11_DATA_H) && defined(TINS_HAVE_DOT11)
33 #define TINS_DOT11_DOT11_DATA_H
34 
35 #include <tins/dot11/dot11_base.h>
36 #include <tins/macros.h>
37 
38 namespace Tins {
39 
43 class TINS_API Dot11Data : public Dot11 {
44 public:
48  static const PDU::PDUType pdu_flag = PDU::DOT11_DATA;
49 
59  Dot11Data(const address_type& dst_hw_addr = address_type(),
60  const address_type& src_hw_addr = address_type());
61 
76  Dot11Data(const uint8_t* buffer, uint32_t total_sz);
77 
83  address_type addr2() const {
84  return ext_header_.addr2;
85  }
86 
92  address_type addr3() const {
93  return ext_header_.addr3;
94  }
95 
101  small_uint<4> frag_num() const {
102  #if TINS_IS_LITTLE_ENDIAN
103  return ext_header_.frag_seq & 0xf;
104  #else
105  return (ext_header_.frag_seq >> 8) & 0xf;
106  #endif
107  }
108 
114  small_uint<12> seq_num() const {
115  #if TINS_IS_LITTLE_ENDIAN
116  return (ext_header_.frag_seq >> 4) & 0xfff;
117  #else
118  return (Endian::le_to_host<uint16_t>(ext_header_.frag_seq) >> 4) & 0xfff;
119  #endif
120  }
121 
127  address_type addr4() const {
128  return addr4_;
129  }
130 
136  void addr2(const address_type& new_addr2);
137 
143  void addr3(const address_type& new_addr3);
144 
150  void frag_num(small_uint<4> new_frag_num);
151 
157  void seq_num(small_uint<12> new_seq_num);
158 
164  void addr4(const address_type& new_addr4);
165 
174  address_type src_addr() const {
175  if (!from_ds() && !to_ds()) {
176  return addr2();
177  }
178  if (!from_ds() && to_ds()) {
179  return addr2();
180  }
181  return addr3();
182  }
183 
192  address_type dst_addr() const {
193  if (!from_ds() && !to_ds()) {
194  return addr1();
195  }
196  if (!from_ds() && to_ds()) {
197  return addr3();
198  }
199  return addr1();
200  }
201 
210  address_type bssid_addr() const {
211  if (!from_ds() && !to_ds()) {
212  return addr3();
213  }
214  if (!from_ds() && to_ds()) {
215  return addr1();
216  }
217  return addr2();
218  }
219 
226  uint32_t header_size() const;
227 
232  PDUType pdu_type() const {
233  return pdu_flag;
234  }
235 
241  bool matches_flag(PDUType flag) const {
242  return flag == pdu_flag || Dot11::matches_flag(flag);
243  }
244 
250  Dot11Data* clone() const {
251  return new Dot11Data(*this);
252  }
253 protected:
254  TINS_BEGIN_PACK
255  struct dot11_extended_header {
256  uint8_t addr2[address_type::address_size];
257  uint8_t addr3[address_type::address_size];
258  uint16_t frag_seq;
259  } TINS_END_PACK;
260 
261  struct no_inner_pdu { };
262  Dot11Data(const uint8_t* buffer, uint32_t total_sz, no_inner_pdu);
263 
264  uint32_t init(const uint8_t* buffer, uint32_t total_sz);
265  void write_ext_header(Memory::OutputMemoryStream& stream);
266 private:
267  dot11_extended_header ext_header_;
268  address_type addr4_;
269 };
270 
271 class TINS_API Dot11QoSData : public Dot11Data {
272 public:
276  static const PDU::PDUType pdu_flag = PDU::DOT11_QOS_DATA;
277 
287  Dot11QoSData(const address_type& dst_hw_addr = address_type(),
288  const address_type& src_hw_addr = address_type());
289 
304  Dot11QoSData(const uint8_t* buffer, uint32_t total_sz);
305 
311  uint16_t qos_control() const {
312  return Endian::le_to_host(qos_control_);
313  }
314 
320  void qos_control(uint16_t new_qos_control);
321 
328  uint32_t header_size() const;
329 
335  Dot11QoSData* clone() const {
336  return new Dot11QoSData(*this);
337  }
338 
343  PDUType pdu_type() const {
344  return pdu_flag;
345  }
346 
352  bool matches_flag(PDUType flag) const {
353  return flag == pdu_flag || Dot11Data::matches_flag(flag);
354  }
355 private:
356  void write_fixed_parameters(Memory::OutputMemoryStream& stream);
357 
358  uint16_t qos_control_;
359 };
360 }
361 
362 #endif // TINS_DOT11_DOT11_DATA_H
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:127