Paul Stewart | 91a5aac | 2012-07-20 11:55:40 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef SHILL_ARP_PACKET_H_ |
| 6 | #define SHILL_ARP_PACKET_H_ |
| 7 | |
Peter Qiu | 8d6b597 | 2014-10-28 15:33:34 -0700 | [diff] [blame] | 8 | #include "shill/net/byte_string.h" |
| 9 | #include "shill/net/ip_address.h" |
Paul Stewart | 91a5aac | 2012-07-20 11:55:40 -0700 | [diff] [blame] | 10 | |
| 11 | namespace shill { |
| 12 | |
| 13 | // ArpPacket encapsulates the task of creating and parsing |
| 14 | // Address Resolution Protocol (ARP) packets for IP and |
| 15 | // IPv6 protocols on Ethernet (or Ethernet-like) networks. |
| 16 | class ArpPacket { |
| 17 | public: |
| 18 | ArpPacket(); |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 19 | ArpPacket(const IPAddress& local_ip, const IPAddress& remote_ip, |
| 20 | const ByteString& local_mac, const ByteString& remote_mac); |
Paul Stewart | 91a5aac | 2012-07-20 11:55:40 -0700 | [diff] [blame] | 21 | virtual ~ArpPacket(); |
| 22 | |
| 23 | // Parse a payload and save to local parameters. |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 24 | bool Parse(const ByteString& packet); |
Paul Stewart | 91a5aac | 2012-07-20 11:55:40 -0700 | [diff] [blame] | 25 | |
| 26 | // Output a payload from local parameters. |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 27 | bool FormatRequest(ByteString* packet) const; |
Paul Stewart | 91a5aac | 2012-07-20 11:55:40 -0700 | [diff] [blame] | 28 | |
Paul Stewart | 417e5f0 | 2014-10-09 08:52:35 -0700 | [diff] [blame] | 29 | // Returns true if this packet is an ARP response. |
| 30 | bool IsReply() const; |
| 31 | |
Paul Stewart | 91a5aac | 2012-07-20 11:55:40 -0700 | [diff] [blame] | 32 | // Getters and seters. |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 33 | const IPAddress& local_ip_address() const { return local_ip_address_; } |
| 34 | void set_local_ip_address(const IPAddress& address) { |
Paul Stewart | 91a5aac | 2012-07-20 11:55:40 -0700 | [diff] [blame] | 35 | local_ip_address_ = address; |
| 36 | } |
| 37 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 38 | const IPAddress& remote_ip_address() const { return remote_ip_address_; } |
| 39 | void set_remote_ip_address(const IPAddress& address) { |
Paul Stewart | 91a5aac | 2012-07-20 11:55:40 -0700 | [diff] [blame] | 40 | remote_ip_address_ = address; |
| 41 | } |
| 42 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 43 | const ByteString& local_mac_address() const { return local_mac_address_; } |
| 44 | void set_local_mac_address(const ByteString& address) { |
Paul Stewart | 91a5aac | 2012-07-20 11:55:40 -0700 | [diff] [blame] | 45 | local_mac_address_ = address; |
| 46 | } |
| 47 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 48 | const ByteString& remote_mac_address() const { return remote_mac_address_; } |
| 49 | void set_remote_mac_address(const ByteString& address) { |
Paul Stewart | 91a5aac | 2012-07-20 11:55:40 -0700 | [diff] [blame] | 50 | remote_mac_address_ = address; |
| 51 | } |
| 52 | |
Paul Stewart | 417e5f0 | 2014-10-09 08:52:35 -0700 | [diff] [blame] | 53 | uint16_t operation() const { return operation_; } |
| 54 | void set_operation(uint16_t operation) { |
| 55 | operation_ = operation; |
| 56 | } |
| 57 | |
Paul Stewart | 91a5aac | 2012-07-20 11:55:40 -0700 | [diff] [blame] | 58 | private: |
| 59 | friend class ArpPacketTest; |
| 60 | |
Paul Stewart | ac1328e | 2012-07-20 11:55:40 -0700 | [diff] [blame] | 61 | // The minimum number of bytes of ARP payload which will produce the |
| 62 | // smallest valid Ethernet frame. |
| 63 | static const size_t kMinPayloadSize; |
| 64 | |
Paul Stewart | 417e5f0 | 2014-10-09 08:52:35 -0700 | [diff] [blame] | 65 | uint16_t operation_; |
Paul Stewart | 91a5aac | 2012-07-20 11:55:40 -0700 | [diff] [blame] | 66 | IPAddress local_ip_address_; |
| 67 | IPAddress remote_ip_address_; |
| 68 | ByteString local_mac_address_; |
| 69 | ByteString remote_mac_address_; |
| 70 | |
| 71 | DISALLOW_COPY_AND_ASSIGN(ArpPacket); |
| 72 | }; |
| 73 | |
| 74 | } // namespace shill |
| 75 | |
| 76 | #endif // SHILL_ARP_PACKET_H_ |