Home | History | Annotate | Download | only in shill
      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_CLIENT_H_
     18 #define SHILL_ARP_CLIENT_H_
     19 
     20 #include <memory>
     21 
     22 #include <base/macros.h>
     23 
     24 namespace shill {
     25 
     26 class ArpPacket;
     27 class ByteString;
     28 class Sockets;
     29 class ScopedSocketCloser;
     30 
     31 // ArpClient task of creating ARP-capable sockets, as well as
     32 // transmitting requests on and receiving responses from such
     33 // sockets.
     34 class ArpClient {
     35  public:
     36   explicit ArpClient(int interface_index);
     37   virtual ~ArpClient();
     38 
     39   // Create a socket for reception of ARP replies, and packet trasmission.
     40   // Returns true if successful, false otherwise.
     41   virtual bool StartReplyListener();
     42 
     43   // Create a socket for reception of ARP requests, and packet trasmission.
     44   // Returns true if successful, false otherwise.
     45   virtual bool StartRequestListener();
     46 
     47   // Destroy the client socket.
     48   virtual void Stop();
     49 
     50   // Receive an ARP request or reply and parse its contents into |packet|.
     51   // Also return the sender's MAC address (which may be different from the
     52   // MAC address in the ARP response) in |sender|.  Returns true on
     53   // succes, false otherwise.
     54   virtual bool ReceivePacket(ArpPacket* packet, ByteString* sender) const;
     55 
     56   // Send a formatted ARP request from |packet|.  Returns true on
     57   // success, false otherwise.
     58   virtual bool TransmitRequest(const ArpPacket& packet) const;
     59 
     60   virtual int socket() const { return socket_; }
     61 
     62   bool IsStarted() { return socket_closer_.get(); }
     63 
     64  private:
     65   friend class ArpClientTest;
     66 
     67   // Offset of the ARP OpCode within a captured ARP packet.
     68   static const size_t kArpOpOffset;
     69 
     70   // The largest packet we expect to receive as an ARP client.
     71   static const size_t kMaxArpPacketLength;
     72 
     73   // Start an ARP listener that listens for |arp_opcode| ARP packets.
     74   bool Start(uint16_t arp_opcode);
     75   bool CreateSocket(uint16_t arp_opcode);
     76 
     77   const int interface_index_;
     78   std::unique_ptr<Sockets> sockets_;
     79   std::unique_ptr<ScopedSocketCloser> socket_closer_;
     80   int socket_;
     81 
     82   DISALLOW_COPY_AND_ASSIGN(ArpClient);
     83 };
     84 
     85 }  // namespace shill
     86 
     87 #endif  // SHILL_ARP_CLIENT_H_
     88