1 //
2 // Copyright (C) 2012 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef SHILL_ARP_PACKET_H_
18 #define SHILL_ARP_PACKET_H_
19 
20 #include "shill/net/byte_string.h"
21 #include "shill/net/ip_address.h"
22 
23 namespace shill {
24 
25 // ArpPacket encapsulates the task of creating and parsing
26 // Address Resolution Protocol (ARP) packets for IP and
27 // IPv6 protocols on Ethernet (or Ethernet-like) networks.
28 class ArpPacket {
29  public:
30   ArpPacket();
31   ArpPacket(const IPAddress& local_ip, const IPAddress& remote_ip,
32             const ByteString& local_mac, const ByteString& remote_mac);
33   virtual ~ArpPacket();
34 
35   // Parse a payload and save to local parameters.
36   bool Parse(const ByteString& packet);
37 
38   // Output a payload from local parameters.
39   bool FormatRequest(ByteString* packet) const;
40 
41   // Returns true if this packet is an ARP response.
42   bool IsReply() const;
43 
44   // Getters and seters.
local_ip_address()45   const IPAddress& local_ip_address() const { return local_ip_address_; }
set_local_ip_address(const IPAddress & address)46   void set_local_ip_address(const IPAddress& address) {
47     local_ip_address_ = address;
48   }
49 
remote_ip_address()50   const IPAddress& remote_ip_address() const { return remote_ip_address_; }
set_remote_ip_address(const IPAddress & address)51   void set_remote_ip_address(const IPAddress& address) {
52     remote_ip_address_ = address;
53   }
54 
local_mac_address()55   const ByteString& local_mac_address() const { return local_mac_address_; }
set_local_mac_address(const ByteString & address)56   void set_local_mac_address(const ByteString& address) {
57     local_mac_address_ = address;
58   }
59 
remote_mac_address()60   const ByteString& remote_mac_address() const { return remote_mac_address_; }
set_remote_mac_address(const ByteString & address)61   void set_remote_mac_address(const ByteString& address) {
62     remote_mac_address_ = address;
63   }
64 
operation()65   uint16_t operation() const { return operation_; }
set_operation(uint16_t operation)66   void set_operation(uint16_t operation) {
67     operation_ = operation;
68   }
69 
70  private:
71   friend class ArpPacketTest;
72 
73   // The minimum number of bytes of ARP payload which will produce the
74   // smallest valid Ethernet frame.
75   static const size_t kMinPayloadSize;
76 
77   uint16_t operation_;
78   IPAddress local_ip_address_;
79   IPAddress remote_ip_address_;
80   ByteString local_mac_address_;
81   ByteString remote_mac_address_;
82 
83   DISALLOW_COPY_AND_ASSIGN(ArpPacket);
84 };
85 
86 }  // namespace shill
87 
88 #endif  // SHILL_ARP_PACKET_H_
89