blob: 3cb66e3dbb8c77186a87dfd5e62d23b76fd6393c [file] [log] [blame]
Paul Stewart91a5aac2012-07-20 11:55:40 -07001// 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 Qiu8d6b5972014-10-28 15:33:34 -07008#include "shill/net/byte_string.h"
9#include "shill/net/ip_address.h"
Paul Stewart91a5aac2012-07-20 11:55:40 -070010
11namespace 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.
16class ArpPacket {
17 public:
18 ArpPacket();
Paul Stewarta794cd62015-06-16 13:13:10 -070019 ArpPacket(const IPAddress& local_ip, const IPAddress& remote_ip,
20 const ByteString& local_mac, const ByteString& remote_mac);
Paul Stewart91a5aac2012-07-20 11:55:40 -070021 virtual ~ArpPacket();
22
23 // Parse a payload and save to local parameters.
Paul Stewarta794cd62015-06-16 13:13:10 -070024 bool Parse(const ByteString& packet);
Paul Stewart91a5aac2012-07-20 11:55:40 -070025
26 // Output a payload from local parameters.
Paul Stewarta794cd62015-06-16 13:13:10 -070027 bool FormatRequest(ByteString* packet) const;
Paul Stewart91a5aac2012-07-20 11:55:40 -070028
Paul Stewart417e5f02014-10-09 08:52:35 -070029 // Returns true if this packet is an ARP response.
30 bool IsReply() const;
31
Paul Stewart91a5aac2012-07-20 11:55:40 -070032 // Getters and seters.
Paul Stewarta794cd62015-06-16 13:13:10 -070033 const IPAddress& local_ip_address() const { return local_ip_address_; }
34 void set_local_ip_address(const IPAddress& address) {
Paul Stewart91a5aac2012-07-20 11:55:40 -070035 local_ip_address_ = address;
36 }
37
Paul Stewarta794cd62015-06-16 13:13:10 -070038 const IPAddress& remote_ip_address() const { return remote_ip_address_; }
39 void set_remote_ip_address(const IPAddress& address) {
Paul Stewart91a5aac2012-07-20 11:55:40 -070040 remote_ip_address_ = address;
41 }
42
Paul Stewarta794cd62015-06-16 13:13:10 -070043 const ByteString& local_mac_address() const { return local_mac_address_; }
44 void set_local_mac_address(const ByteString& address) {
Paul Stewart91a5aac2012-07-20 11:55:40 -070045 local_mac_address_ = address;
46 }
47
Paul Stewarta794cd62015-06-16 13:13:10 -070048 const ByteString& remote_mac_address() const { return remote_mac_address_; }
49 void set_remote_mac_address(const ByteString& address) {
Paul Stewart91a5aac2012-07-20 11:55:40 -070050 remote_mac_address_ = address;
51 }
52
Paul Stewart417e5f02014-10-09 08:52:35 -070053 uint16_t operation() const { return operation_; }
54 void set_operation(uint16_t operation) {
55 operation_ = operation;
56 }
57
Paul Stewart91a5aac2012-07-20 11:55:40 -070058 private:
59 friend class ArpPacketTest;
60
Paul Stewartac1328e2012-07-20 11:55:40 -070061 // 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 Stewart417e5f02014-10-09 08:52:35 -070065 uint16_t operation_;
Paul Stewart91a5aac2012-07-20 11:55:40 -070066 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_