libtins  3.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
llc.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_IEEE8022_H
31 #define TINS_IEEE8022_H
32 
33 #include <list>
34 #include <vector>
35 #include <stdint.h>
36 #include "macros.h"
37 #include "pdu.h"
38 #include "macros.h"
39 #include "endianness.h"
40 
41 namespace Tins {
42 
49 class TINS_API LLC : public PDU {
50 public:
54  static const PDU::PDUType pdu_flag = PDU::LLC;
55 
59  static const uint8_t GLOBAL_DSAP_ADDR;
60 
64  static const uint8_t NULL_ADDR;
65 
69  enum Format {
70  INFORMATION = 0,
71  SUPERVISORY = 1,
72  UNNUMBERED = 3
73  };
74 
79  UI = 0x00,
80  XID = 0x1D,
81  TEST = 0x07,
82  SABME = 0x1E,
83  DISC = 0x02,
84  UA = 0x06,
85  DM = 0x18,
86  FRMR = 0x11
87  };
88 
93  RECEIVE_READY = 0,
94  REJECT = 2,
95  RECEIVE_NOT_READY = 1
96  };
97 
101  LLC();
102 
109  LLC(uint8_t dsap, uint8_t ssap);
110 
121  LLC(const uint8_t* buffer, uint32_t total_sz);
122 
123  /* Setters */
124 
129  void group(bool value);
130 
135  void dsap(uint8_t new_dsap);
136 
141  void response(bool value);
142 
147  void ssap(uint8_t new_ssap);
148 
153  void type(Format type);
154 
160  void send_seq_number(uint8_t seq_number);
161 
167  void receive_seq_number(uint8_t seq_number);
168 
173  void poll_final(bool value);
174 
180  void supervisory_function(SupervisoryFunctions new_func);
181 
187  void modifier_function(ModifierFunctions mod_func);
188 
196  void add_xid_information(uint8_t xid_id,
197  uint8_t llc_type_class,
198  uint8_t receive_window);
199 
200  //TODO: Add Acknowledged connectionless information
201 
202  /* Getters */
203 
208  bool group() {
209  return header_.dsap & 0x01;
210  }
211 
216  uint8_t dsap() {
217  return header_.dsap;
218  }
219 
224  bool response() {
225  return (header_.ssap & 0x01);
226  }
227 
232  uint8_t ssap() {
233  return header_.ssap;
234  }
235 
240  uint8_t type() {
241  return type_;
242  }
243 
249  uint8_t send_seq_number() {
250  return (type() == INFORMATION) ? (control_field.info.send_seq_num) : 0;
251  }
252 
259  uint8_t receive_seq_number() {
260  switch (type()) {
261  case INFORMATION:
262  return control_field.info.recv_seq_num;
263  case SUPERVISORY:
264  return control_field.super.recv_seq_num;
265  case UNNUMBERED:
266  return 0;
267  default:
268  return 0;
269  }
270  }
271 
276  bool poll_final() {
277  switch (type()) {
278  case UNNUMBERED:
279  return control_field.unnumbered.poll_final_bit;
280  case INFORMATION:
281  return control_field.info.poll_final_bit;
282  case SUPERVISORY:
283  return control_field.super.poll_final_bit;
284  default:
285  return false;
286  }
287  }
288 
295  if (type() == SUPERVISORY) {
296  return control_field.super.supervisory_func;
297  }
298  return 0;
299  }
300 
306  uint8_t modifier_function() {
307  if (type() == UNNUMBERED) {
308  return (control_field.unnumbered.mod_func1 << 3) + control_field.unnumbered.mod_func2;
309  }
310  return 0;
311  }
312 
319  uint32_t header_size() const;
320 
325  PDUType pdu_type() const {
326  return pdu_flag;
327  }
328 
332  void clear_information_fields();
333 
339  LLC* clone() const {
340  return new LLC(*this);
341  }
342 private:
343  TINS_BEGIN_PACK
344  struct llchdr {
345  uint8_t dsap;
346  uint8_t ssap;
347  } TINS_END_PACK;
348 
349  #if TINS_IS_LITTLE_ENDIAN
350  TINS_BEGIN_PACK
351  struct info_control_field {
352  uint16_t
353  type_bit:1,
354  send_seq_num:7,
355  poll_final_bit:1,
356  recv_seq_num:7;
357  } TINS_END_PACK;
358 
359  TINS_BEGIN_PACK
360  struct super_control_field {
361  uint16_t type_bit:2,
362  supervisory_func:2,
363  unused:4,
364  poll_final_bit:1,
365  recv_seq_num:7;
366  } TINS_END_PACK;
367 
368  TINS_BEGIN_PACK
369  struct un_control_field {
370  uint8_t type_bits:2,
371  mod_func1:2,
372  poll_final_bit:1,
373  mod_func2:3;
374  } TINS_END_PACK;
375  #elif TINS_IS_BIG_ENDIAN
376  TINS_BEGIN_PACK
377  struct info_control_field {
378  uint16_t send_seq_num:7,
379  type_bit:1,
380  recv_seq_num:7,
381  poll_final_bit:1;
382  } TINS_END_PACK;
383 
384  TINS_BEGIN_PACK
385  struct super_control_field {
386  uint16_t unused:4,
387  supervisory_func:2,
388  type_bit:2,
389  recv_seq_num:7,
390  poll_final_bit:1;
391  } TINS_END_PACK;
392 
393  TINS_BEGIN_PACK
394  struct un_control_field {
395  uint8_t mod_func2:3,
396  poll_final_bit:1,
397  mod_func1:2,
398  type_bits:2;
399  } TINS_END_PACK;
400  #endif
401 
402  typedef std::vector<uint8_t> field_type;
403  typedef std::list<field_type> field_list;
404 
405  void write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* parent);
406 
407  llchdr header_;
408  uint8_t control_field_length_;
409  union {
410  info_control_field info;
411  super_control_field super;
412  un_control_field unnumbered;
413  } control_field;
414  Format type_;
415  uint8_t information_field_length_;
416  field_list information_fields_;
417 };
418 
419 } // Tins
420 
421 #endif // TINS_IEEE8022_H
SupervisoryFunctions
LLC Supervisory functions.
Definition: llc.h:92
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:128
static const uint8_t GLOBAL_DSAP_ADDR
Represents the LLC global DSAP address.
Definition: llc.h:59
uint8_t send_seq_number()
Getter for sender send sequence number.
Definition: llc.h:249
PDUType pdu_type() const
Getter for the PDU's type.
Definition: llc.h:325
uint8_t type()
Getter for the LLC frame format type.
Definition: llc.h:240
uint8_t receive_seq_number()
Getter for sender receive sequence number.
Definition: llc.h:259
uint8_t dsap()
Getter for the dsap field.
Definition: llc.h:216
uint8_t modifier_function()
Getter for the modifier function field.
Definition: llc.h:306
uint8_t supervisory_function()
Getter for the supervisory function.
Definition: llc.h:294
bool poll_final()
Getter for the poll/final flag.
Definition: llc.h:276
Representing a LLC frame.
Definition: llc.h:49
LLC * clone() const
Clones this PDU.
Definition: llc.h:339
bool response()
Getter for the response bit.
Definition: llc.h:224
ModifierFunctions
LLC Modifier functions.
Definition: llc.h:78
static const uint8_t NULL_ADDR
Represents the LLC NULL address.
Definition: llc.h:64
uint8_t ssap()
Getter for the ssap field.
Definition: llc.h:232
Base class for protocol data units.
Definition: pdu.h:108
Format
LLC Format flags.
Definition: llc.h:69
bool group()
Getter for the group destination bit.
Definition: llc.h:208