Paul Stewart | ac1328e | 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_CLIENT_H_ |
| 6 | #define SHILL_ARP_CLIENT_H_ |
| 7 | |
Ben Chan | cd47732 | 2014-10-17 14:19:30 -0700 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
Ben Chan | cc67c52 | 2014-09-03 07:19:18 -0700 | [diff] [blame] | 10 | #include <base/macros.h> |
Paul Stewart | ac1328e | 2012-07-20 11:55:40 -0700 | [diff] [blame] | 11 | |
| 12 | namespace shill { |
| 13 | |
| 14 | class ArpPacket; |
| 15 | class ByteString; |
| 16 | class Sockets; |
| 17 | class ScopedSocketCloser; |
| 18 | |
| 19 | // ArpClient task of creating ARP-capable sockets, as well as |
| 20 | // transmitting requests on and receiving responses from such |
| 21 | // sockets. |
| 22 | class ArpClient { |
| 23 | public: |
| 24 | explicit ArpClient(int interface_index); |
| 25 | virtual ~ArpClient(); |
| 26 | |
Paul Stewart | 417e5f0 | 2014-10-09 08:52:35 -0700 | [diff] [blame] | 27 | // 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 Stewart | ac1328e | 2012-07-20 11:55:40 -0700 | [diff] [blame] | 34 | |
| 35 | // Destroy the client socket. |
| 36 | virtual void Stop(); |
| 37 | |
Paul Stewart | 417e5f0 | 2014-10-09 08:52:35 -0700 | [diff] [blame] | 38 | // 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 Stewart | ac1328e | 2012-07-20 11:55:40 -0700 | [diff] [blame] | 40 | // MAC address in the ARP response) in |sender|. Returns true on |
| 41 | // succes, false otherwise. |
Paul Stewart | 417e5f0 | 2014-10-09 08:52:35 -0700 | [diff] [blame] | 42 | virtual bool ReceivePacket(ArpPacket *packet, ByteString *sender) const; |
Paul Stewart | ac1328e | 2012-07-20 11:55:40 -0700 | [diff] [blame] | 43 | |
| 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 Stewart | 417e5f0 | 2014-10-09 08:52:35 -0700 | [diff] [blame] | 59 | // 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 Stewart | ac1328e | 2012-07-20 11:55:40 -0700 | [diff] [blame] | 62 | |
| 63 | const int interface_index_; |
Ben Chan | cd47732 | 2014-10-17 14:19:30 -0700 | [diff] [blame] | 64 | std::unique_ptr<Sockets> sockets_; |
| 65 | std::unique_ptr<ScopedSocketCloser> socket_closer_; |
Paul Stewart | ac1328e | 2012-07-20 11:55:40 -0700 | [diff] [blame] | 66 | int socket_; |
| 67 | |
| 68 | DISALLOW_COPY_AND_ASSIGN(ArpClient); |
| 69 | }; |
| 70 | |
| 71 | } // namespace shill |
| 72 | |
| 73 | #endif // SHILL_ARP_CLIENT_H_ |