blob: e402f224365ea43526f8dc2048b76f102783b6f5 [file] [log] [blame]
Paul Stewartac1328e2012-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_CLIENT_H_
6#define SHILL_ARP_CLIENT_H_
7
Ben Chancd477322014-10-17 14:19:30 -07008#include <memory>
9
Ben Chancc67c522014-09-03 07:19:18 -070010#include <base/macros.h>
Paul Stewartac1328e2012-07-20 11:55:40 -070011
12namespace shill {
13
14class ArpPacket;
15class ByteString;
16class Sockets;
17class ScopedSocketCloser;
18
19// ArpClient task of creating ARP-capable sockets, as well as
20// transmitting requests on and receiving responses from such
21// sockets.
22class ArpClient {
23 public:
24 explicit ArpClient(int interface_index);
25 virtual ~ArpClient();
26
Paul Stewart417e5f02014-10-09 08:52:35 -070027 // Create a socket for reception of ARP replies, and packet trasmission.
28 // Returns true if successful, false otherwise.
29 virtual bool StartReplyListener();
30
31 // Create a socket for reception of ARP requests, and packet trasmission.
32 // Returns true if successful, false otherwise.
33 virtual bool StartRequestListener();
Paul Stewartac1328e2012-07-20 11:55:40 -070034
35 // Destroy the client socket.
36 virtual void Stop();
37
Paul Stewart417e5f02014-10-09 08:52:35 -070038 // Receive an ARP request or reply and parse its contents into |packet|.
39 // Also return the sender's MAC address (which may be different from the
Paul Stewartac1328e2012-07-20 11:55:40 -070040 // MAC address in the ARP response) in |sender|. Returns true on
41 // succes, false otherwise.
Paul Stewart417e5f02014-10-09 08:52:35 -070042 virtual bool ReceivePacket(ArpPacket *packet, ByteString *sender) const;
Paul Stewartac1328e2012-07-20 11:55:40 -070043
44 // Send a formatted ARP request from |packet|. Returns true on
45 // success, false otherwise.
46 virtual bool TransmitRequest(const ArpPacket &packet) const;
47
48 virtual int socket() const { return socket_; }
49
50 private:
51 friend class ArpClientTest;
52
53 // Offset of the ARP OpCode within a captured ARP packet.
54 static const size_t kArpOpOffset;
55
56 // The largest packet we expect to receive as an ARP client.
57 static const size_t kMaxArpPacketLength;
58
Paul Stewart417e5f02014-10-09 08:52:35 -070059 // Start an ARP listener that listens for |arp_opcode| ARP packets.
60 bool Start(uint16_t arp_opcode);
61 bool CreateSocket(uint16_t arp_opcode);
Paul Stewartac1328e2012-07-20 11:55:40 -070062
63 const int interface_index_;
Ben Chancd477322014-10-17 14:19:30 -070064 std::unique_ptr<Sockets> sockets_;
65 std::unique_ptr<ScopedSocketCloser> socket_closer_;
Paul Stewartac1328e2012-07-20 11:55:40 -070066 int socket_;
67
68 DISALLOW_COPY_AND_ASSIGN(ArpClient);
69};
70
71} // namespace shill
72
73#endif // SHILL_ARP_CLIENT_H_