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