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