blob: 9b2ae48339ac06be1b65f4b5f3085d9a187dc04c [file] [log] [blame]
Paul Stewartac802ac2013-04-02 15:45:24 -07001// 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 Chancd477322014-10-17 14:19:30 -07008#include <memory>
9
Paul Stewartac802ac2013-04-02 15:45:24 -070010#include <base/callback.h>
Ben Chancc67c522014-09-03 07:19:18 -070011#include <base/macros.h>
Paul Stewartac802ac2013-04-02 15:45:24 -070012
13namespace shill {
14
15class EventDispatcher;
16class IOHandler;
17class ScopedSocketCloser;
18class Sockets;
19
20// Listens for EAP packets on |interface_index| and invokes a
21// callback when a request frame arrives.
22class EapListener {
23 public:
24 typedef base::Callback<void()> EapRequestReceivedCallback;
25
Paul Stewarta794cd62015-06-16 13:13:10 -070026 explicit EapListener(EventDispatcher* event_dispatcher,
Paul Stewartac802ac2013-04-02 15:45:24 -070027 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 Stewarta794cd62015-06-16 13:13:10 -070039 const EapRequestReceivedCallback& callback) {
Paul Stewartac802ac2013-04-02 15:45:24 -070040 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 Stewarta794cd62015-06-16 13:13:10 -070057 EventDispatcher* dispatcher_;
Paul Stewartac802ac2013-04-02 15:45:24 -070058
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 Chancd477322014-10-17 14:19:30 -070066 std::unique_ptr<Sockets> sockets_;
Paul Stewartac802ac2013-04-02 15:45:24 -070067
68 // Receive socket configured to receive PAE (Port Access Entity) packets.
69 int socket_;
70
71 // Scoped socket closer for the receive |socket_|.
Ben Chancd477322014-10-17 14:19:30 -070072 std::unique_ptr<ScopedSocketCloser> socket_closer_;
Paul Stewartac802ac2013-04-02 15:45:24 -070073
74 // Input handler for |socket_|. Calls ReceiveRequest().
Ben Chancd477322014-10-17 14:19:30 -070075 std::unique_ptr<IOHandler> receive_request_handler_;
Paul Stewartac802ac2013-04-02 15:45:24 -070076
77 DISALLOW_COPY_AND_ASSIGN(EapListener);
78};
79
80} // namespace shill
81
82#endif // SHILL_EAP_LISTENER_H_