1 //
2 // Copyright (C) 2013 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_EAP_LISTENER_H_
18 #define SHILL_EAP_LISTENER_H_
19 
20 #include <memory>
21 
22 #include <base/callback.h>
23 #include <base/macros.h>
24 
25 namespace shill {
26 
27 class EventDispatcher;
28 class IOHandler;
29 class ScopedSocketCloser;
30 class Sockets;
31 
32 // Listens for EAP packets on |interface_index| and invokes a
33 // callback when a request frame arrives.
34 class EapListener {
35  public:
36   typedef base::Callback<void()> EapRequestReceivedCallback;
37 
38   explicit EapListener(EventDispatcher* event_dispatcher,
39                        int interface_index);
40   virtual ~EapListener();
41 
42   // Create a socket for tranmission and reception.  Returns true
43   // if successful, false otherwise.
44   virtual bool Start();
45 
46   // Destroy the client socket.
47   virtual void Stop();
48 
49   // Setter for |request_received_callback_|.
set_request_received_callback(const EapRequestReceivedCallback & callback)50   virtual void set_request_received_callback(
51       const EapRequestReceivedCallback& callback) {
52     request_received_callback_ = callback;
53   }
54 
55  private:
56   friend class EapListenerTest;
57 
58   // The largest EAP packet we expect to receive.
59   static const size_t kMaxEapPacketLength;
60 
61   // Creates |socket_|.  Returns true on succes, false on failure.
62   bool CreateSocket();
63 
64   // Retrieves an EAP packet from |socket_|.  This is the callback method
65   // configured on |receive_request_handler_|.
66   void ReceiveRequest(int fd);
67 
68   // Event dispatcher to use for creating an input handler.
69   EventDispatcher* dispatcher_;
70 
71   // The interface index fo the device to monitor.
72   const int interface_index_;
73 
74   // Callback handle to invoke when an EAP request is received.
75   EapRequestReceivedCallback request_received_callback_;
76 
77   // Sockets instance to perform socket calls on.
78   std::unique_ptr<Sockets> sockets_;
79 
80   // Receive socket configured to receive PAE (Port Access Entity) packets.
81   int socket_;
82 
83   // Scoped socket closer for the receive |socket_|.
84   std::unique_ptr<ScopedSocketCloser> socket_closer_;
85 
86   // Input handler for |socket_|.  Calls ReceiveRequest().
87   std::unique_ptr<IOHandler> receive_request_handler_;
88 
89   DISALLOW_COPY_AND_ASSIGN(EapListener);
90 };
91 
92 }  // namespace shill
93 
94 #endif  // SHILL_EAP_LISTENER_H_
95