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 | |
| 8 | #include <base/basictypes.h> |
| 9 | #include <base/memory/scoped_ptr.h> |
| 10 | |
| 11 | namespace shill { |
| 12 | |
| 13 | class ArpPacket; |
| 14 | class ByteString; |
| 15 | class Sockets; |
| 16 | class ScopedSocketCloser; |
| 17 | |
| 18 | // ArpClient task of creating ARP-capable sockets, as well as |
| 19 | // transmitting requests on and receiving responses from such |
| 20 | // sockets. |
| 21 | class ArpClient { |
| 22 | public: |
| 23 | explicit ArpClient(int interface_index); |
| 24 | virtual ~ArpClient(); |
| 25 | |
| 26 | // Create a socket for tranmission and reception. Returns true |
| 27 | // if successful, false otherwise. |
| 28 | virtual bool Start(); |
| 29 | |
| 30 | // Destroy the client socket. |
| 31 | virtual void Stop(); |
| 32 | |
| 33 | // Receive an ARP reply and parse its contents into |packet|. Also |
| 34 | // return the sender's MAC address (which may be different from the |
| 35 | // MAC address in the ARP response) in |sender|. Returns true on |
| 36 | // succes, false otherwise. |
| 37 | virtual bool ReceiveReply(ArpPacket *packet, ByteString *sender) const; |
| 38 | |
| 39 | // Send a formatted ARP request from |packet|. Returns true on |
| 40 | // success, false otherwise. |
| 41 | virtual bool TransmitRequest(const ArpPacket &packet) const; |
| 42 | |
| 43 | virtual int socket() const { return socket_; } |
| 44 | |
| 45 | private: |
| 46 | friend class ArpClientTest; |
| 47 | |
| 48 | // Offset of the ARP OpCode within a captured ARP packet. |
| 49 | static const size_t kArpOpOffset; |
| 50 | |
| 51 | // The largest packet we expect to receive as an ARP client. |
| 52 | static const size_t kMaxArpPacketLength; |
| 53 | |
| 54 | bool CreateSocket(); |
| 55 | |
| 56 | const int interface_index_; |
| 57 | scoped_ptr<Sockets> sockets_; |
| 58 | scoped_ptr<ScopedSocketCloser> socket_closer_; |
| 59 | int socket_; |
| 60 | |
| 61 | DISALLOW_COPY_AND_ASSIGN(ArpClient); |
| 62 | }; |
| 63 | |
| 64 | } // namespace shill |
| 65 | |
| 66 | #endif // SHILL_ARP_CLIENT_H_ |