libtins  4.0
dot3.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_DOT3_H
31 #define TINS_DOT3_H
32 
33 #include <stdint.h>
34 #include <tins/macros.h>
35 #include <tins/pdu.h>
36 #include <tins/config.h>
37 #include <tins/endianness.h>
38 #include <tins/hw_address.h>
39 
40 namespace Tins {
41 
46 class TINS_API Dot3 : public PDU {
47 public:
52 
56  static const PDU::PDUType pdu_flag = PDU::IEEE802_3;
57 
61  static const address_type BROADCAST;
62 
69  static metadata extract_metadata(const uint8_t *buffer, uint32_t total_sz);
70 
81  Dot3(const address_type& dst_hw_addr = address_type(),
82  const address_type& src_hw_addr = address_type());
83 
94  Dot3(const uint8_t* buffer, uint32_t total_sz);
95 
96  /* Getters */
102  address_type dst_addr() const {
103  return header_.dst_mac;
104  }
105 
111  address_type src_addr() const {
112  return header_.src_mac;
113  }
114 
119  uint16_t length() const {
120  return Endian::be_to_host(header_.length);
121  }
122 
123  /* Setters */
124 
130  void dst_addr(const address_type& address);
131 
137  void src_addr(const address_type& address);
138 
144  void length(uint16_t value);
145 
146  // Virtual methods
147 
154  uint32_t header_size() const;
155 
156  #if !defined(_WIN32) || defined(TINS_HAVE_PACKET_SENDER_PCAP_SENDPACKET)
157 
160  void send(PacketSender& sender, const NetworkInterface& iface);
161  #endif // !_WIN32 || TINS_HAVE_PACKET_SENDER_PCAP_SENDPACKET
162 
170  bool matches_response(const uint8_t* ptr, uint32_t total_sz) const;
171 
172  #ifndef _WIN32
173 
176  PDU* recv_response(PacketSender& sender, const NetworkInterface& iface);
177  #endif // _WIN32
178 
183  PDUType pdu_type() const { return pdu_flag; }
184 
188  Dot3* clone() const {
189  return new Dot3(*this);
190  }
191 private:
195  TINS_BEGIN_PACK
196  struct dot3_header {
197  uint8_t dst_mac[address_type::address_size];
198  uint8_t src_mac[address_type::address_size];
199  uint16_t length;
200  } TINS_END_PACK;
201 
202  void write_serialization(uint8_t* buffer, uint32_t total_sz);
203 
204  dot3_header header_;
205 };
206 
207 } // Tins
208 
209 #endif // TINS_DOT3_H
uint16_t length() const
Getter for the length field.
Definition: dot3.h:119
HWAddress< 6 > address_type
The address type.
Definition: dot3.h:51
static const address_type BROADCAST
Represents the Dot3 broadcast address.
Definition: dot3.h:61
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:127
Sends packets through a network interface.
Definition: packet_sender.h:116
address_type src_addr() const
Getter for the source hardware address.
Definition: dot3.h:111
address_type dst_addr() const
Getter for the destination hardware address.
Definition: dot3.h:102
Class representing an IEEE 802.3 PDU.
Definition: dot3.h:46
The Tins namespace.
Definition: address_range.h:38
Type used to store a PDU header&#39;s data.
Definition: pdu.h:194
Abstraction of a network interface.
Definition: network_interface.h:47
Dot3 * clone() const
Definition: dot3.h:188
PDUType pdu_type() const
Getter for the PDU&#39;s type.
Definition: dot3.h:183
Base class for protocol data units.
Definition: pdu.h:107