libtins  3.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
ethernetII.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_ETHERNET_II_H
31 #define TINS_ETHERNET_II_H
32 
33 #include <stdint.h>
34 #include "macros.h"
35 #include "pdu.h"
36 #include "config.h"
37 #include "endianness.h"
38 #include "hw_address.h"
39 
40 namespace Tins {
41 
46 class TINS_API EthernetII : public PDU {
47 public:
52 
56  static const PDU::PDUType pdu_flag = PDU::ETHERNET_II;
57 
61  static const address_type BROADCAST;
62 
69  static metadata extract_metadata(const uint8_t *buffer, uint32_t total_sz);
70 
77  EthernetII(const address_type& dst_hw_addr = address_type(),
78  const address_type& src_hw_addr = address_type());
79 
93  EthernetII(const uint8_t* buffer, uint32_t total_sz);
94 
95  /* Getters */
103  return header_.dst_mac;
104  }
105 
112  return header_.src_mac;
113  }
114 
119  uint16_t payload_type() const {
120  return Endian::be_to_host(header_.payload_type);
121  }
122 
123  /* Setters */
124 
130  void dst_addr(const address_type& new_dst_addr);
131 
137  void src_addr(const address_type& new_src_addr);
138 
144  void payload_type(uint16_t new_payload_type);
145 
146  /* Virtual methods */
153  uint32_t header_size() const;
154 
161  uint32_t trailer_size() const;
162 
166  void send(PacketSender& sender, const NetworkInterface& iface);
167 
175  bool matches_response(const uint8_t* ptr, uint32_t total_sz) const;
176 
177  #ifndef _WIN32
178 
183  PDU* recv_response(PacketSender& sender, const NetworkInterface& iface);
184  #endif // _WIN32
185 
190  PDUType pdu_type() const {
191  return pdu_flag;
192  }
193 
197  EthernetII* clone() const {
198  return new EthernetII(*this);
199  }
200 private:
204  TINS_BEGIN_PACK
205  struct ethernet_header {
206  uint8_t dst_mac[address_type::address_size];
207  uint8_t src_mac[address_type::address_size];
208  uint16_t payload_type;
209  } TINS_END_PACK;
210 
211  void write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* parent);
212 
213  ethernet_header header_;
214 };
215 
216 } // Tins
217 
218 #endif // TINS_ETHERNET_II_H
address_type src_addr() const
Getter for the source's hardware address.
Definition: ethernetII.h:111
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:128
Sends packets through a network interface.
Definition: packet_sender.h:117
HWAddress< 6 > address_type
The hardware address type.
Definition: ethernetII.h:51
Type used to store a PDU header's data.
Definition: pdu.h:195
static const address_type BROADCAST
Represents the ethernetII broadcast address.
Definition: ethernetII.h:61
Abstraction of a network interface.
Definition: network_interface.h:47
Represents an Ethernet II PDU.
Definition: ethernetII.h:46
PDUType pdu_type() const
Getter for the PDU's type.
Definition: ethernetII.h:190
address_type dst_addr() const
Getter for the destination's hardware address.
Definition: ethernetII.h:102
EthernetII * clone() const
Definition: ethernetII.h:197
uint16_t payload_type() const
Getter for the payload_type.
Definition: ethernetII.h:119
Base class for protocol data units.
Definition: pdu.h:108