libtins  4.0
arp.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 
31 #ifndef TINS_ARP_H
32 #define TINS_ARP_H
33 
34 #include <tins/macros.h>
35 #include <tins/pdu.h>
36 #include <tins/endianness.h>
37 #include <tins/hw_address.h>
38 #include <tins/ip_address.h>
39 
40 namespace Tins {
41 
42 class NetworkInterface;
43 class EthernetII;
44 
50 class TINS_API ARP : public PDU {
51 public:
56 
61 
65  static const PDU::PDUType pdu_flag = PDU::ARP;
66 
70  enum Flags {
71  REQUEST = 0x0001,
72  REPLY = 0x0002
73  };
74 
81  static metadata extract_metadata(const uint8_t *buffer, uint32_t total_sz);
82 
97  ARP(ipaddress_type target_ip = ipaddress_type(),
98  ipaddress_type sender_ip = ipaddress_type(),
99  const hwaddress_type& target_hw = hwaddress_type(),
100  const hwaddress_type& sender_hw = hwaddress_type());
101 
114  ARP(const uint8_t* buffer, uint32_t total_sz);
115 
116  /* Getters */
122  hwaddress_type sender_hw_addr() const {
123  return header_.sender_hw_address;
124  }
125 
131  ipaddress_type sender_ip_addr() const {
132  return ipaddress_type(header_.sender_ip_address);
133  }
134 
140  hwaddress_type target_hw_addr() const {
141  return header_.target_hw_address;
142  }
143 
149  ipaddress_type target_ip_addr() const {
150  return ipaddress_type(header_.target_ip_address);
151  }
152 
158  uint16_t hw_addr_format() const {
159  return Endian::be_to_host(header_.hw_address_format);
160  }
161 
167  uint16_t prot_addr_format() const {
168  return Endian::be_to_host(header_.proto_address_format);
169  }
170 
176  uint8_t hw_addr_length() const {
177  return header_.hw_address_length;
178  }
179 
185  uint8_t prot_addr_length() const {
186  return header_.proto_address_length;
187  }
188 
194  uint16_t opcode() const {
195  return Endian::be_to_host(header_.opcode);
196  }
197 
203  uint32_t header_size() const;
204 
205  /* Setters */
206 
212  void sender_hw_addr(const hwaddress_type& address);
213 
219  void sender_ip_addr(ipaddress_type address);
220 
226  void target_hw_addr(const hwaddress_type& address);
227 
233  void target_ip_addr(ipaddress_type address);
234 
240  void hw_addr_format(uint16_t format);
241 
247  void prot_addr_format(uint16_t format);
248 
254  void hw_addr_length(uint8_t length);
255 
261  void prot_addr_length(uint8_t length);
262 
268  void opcode(Flags code);
269 
274  PDUType pdu_type() const { return pdu_flag; }
275 
287  static EthernetII make_arp_request(ipaddress_type target,
288  ipaddress_type sender,
289  const hwaddress_type& hw_snd = hwaddress_type());
290 
303  static EthernetII make_arp_reply(ipaddress_type target,
304  ipaddress_type sender,
305  const hwaddress_type& hw_tgt = hwaddress_type(),
306  const hwaddress_type& hw_snd = hwaddress_type());
307 
315  bool matches_response(const uint8_t* ptr, uint32_t total_sz) const;
316 
320  ARP* clone() const {
321  return new ARP(*this);
322  }
323 private:
324  TINS_BEGIN_PACK
325  struct arp_header {
326  uint16_t hw_address_format;
327  uint16_t proto_address_format;
328  uint8_t hw_address_length;
329  uint8_t proto_address_length;
330  uint16_t opcode;
331  uint8_t sender_hw_address[hwaddress_type::address_size];
332  uint32_t sender_ip_address;
333  uint8_t target_hw_address[hwaddress_type::address_size];
334  uint32_t target_ip_address;
335  } TINS_END_PACK;
336 
337  void write_serialization(uint8_t* buffer, uint32_t total_sz);
338 
339  arp_header header_;
340 };
341 
342 } // Tins
343 
344 #endif // TINS_ARP_H
Flags
Enum which indicates the type of ARP packet.
Definition: arp.h:70
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:127
uint16_t prot_addr_format() const
Getter for the protocol address format field.
Definition: arp.h:167
ARP * clone() const
Definition: arp.h:320
uint8_t prot_addr_length() const
Getter for the protocol address length field.
Definition: arp.h:185
uint8_t hw_addr_length() const
Getter for the hardware address length field.
Definition: arp.h:176
Represents an ARP PDU.
Definition: arp.h:50
HWAddress< 6 > hwaddress_type
Definition: arp.h:55
ipaddress_type target_ip_addr() const
Getter for the target&#39;s IP address.
Definition: arp.h:149
hwaddress_type sender_hw_addr() const
Getter for the sender&#39;s hardware address.
Definition: arp.h:122
uint16_t opcode() const
Getter for the ARP opcode field.
Definition: arp.h:194
The Tins namespace.
Definition: address_range.h:38
Type used to store a PDU header&#39;s data.
Definition: pdu.h:194
Represents an Ethernet II PDU.
Definition: ethernetII.h:46
Abstraction of an IPv4 address.
Definition: ip_address.h:45
uint16_t hw_addr_format() const
Getter for the hardware address format field.
Definition: arp.h:158
ipaddress_type sender_ip_addr() const
Getter for the sender&#39;s IP address.
Definition: arp.h:131
IPv4Address ipaddress_type
Definition: arp.h:60
hwaddress_type target_hw_addr() const
Getter for the target&#39;s hardware address.
Definition: arp.h:140
Base class for protocol data units.
Definition: pdu.h:107
PDUType pdu_type() const
Getter for the PDU&#39;s type.
Definition: arp.h:274