blob: 2e53a0f1e374fc43e1702bcc2e06110595c50db4 [file] [log] [blame]
Darin Petkov1c115202012-03-22 15:35:47 +01001// Copyright (c) 2012 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_OPENVPN_MANAGEMENT_SERVER_
6#define SHILL_OPENVPN_MANAGEMENT_SERVER_
7
8#include <base/basictypes.h>
Darin Petkov78f63262012-03-26 01:30:24 +02009#include <base/cancelable_callback.h>
10#include <base/memory/weak_ptr.h>
Darin Petkov271fe522012-03-27 13:47:29 +020011#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkov1c115202012-03-22 15:35:47 +010012
13namespace shill {
14
Darin Petkov78f63262012-03-26 01:30:24 +020015class EventDispatcher;
Darin Petkov683942b2012-03-27 18:00:04 +020016class GLib;
Darin Petkov78f63262012-03-26 01:30:24 +020017class InputData;
18class IOHandler;
Darin Petkov1c115202012-03-22 15:35:47 +010019class OpenVPNDriver;
Darin Petkov78f63262012-03-26 01:30:24 +020020class Sockets;
Darin Petkov1c115202012-03-22 15:35:47 +010021
22class OpenVPNManagementServer {
23 public:
Darin Petkov683942b2012-03-27 18:00:04 +020024 OpenVPNManagementServer(OpenVPNDriver *driver, GLib *glib);
Darin Petkov1c115202012-03-22 15:35:47 +010025 virtual ~OpenVPNManagementServer();
26
27 // Returns true on success, false on failure.
Darin Petkov78f63262012-03-26 01:30:24 +020028 bool Start(EventDispatcher *dispatcher, Sockets *sockets);
29
30 void Stop();
Darin Petkov1c115202012-03-22 15:35:47 +010031
32 private:
Darin Petkov271fe522012-03-27 13:47:29 +020033 friend class OpenVPNManagementServerTest;
34 FRIEND_TEST(OpenVPNManagementServerTest, OnInput);
35 FRIEND_TEST(OpenVPNManagementServerTest, OnReady);
36 FRIEND_TEST(OpenVPNManagementServerTest, OnReadyAcceptFail);
Darin Petkov683942b2012-03-27 18:00:04 +020037 FRIEND_TEST(OpenVPNManagementServerTest, PerformStaticChallenge);
38 FRIEND_TEST(OpenVPNManagementServerTest, PerformStaticChallengeNoCreds);
Darin Petkov271fe522012-03-27 13:47:29 +020039 FRIEND_TEST(OpenVPNManagementServerTest, ProcessInfoMessage);
40 FRIEND_TEST(OpenVPNManagementServerTest, ProcessMessage);
Darin Petkov683942b2012-03-27 18:00:04 +020041 FRIEND_TEST(OpenVPNManagementServerTest, ProcessNeedPasswordMessageAuthSC);
Darin Petkov271fe522012-03-27 13:47:29 +020042 FRIEND_TEST(OpenVPNManagementServerTest, ProcessStateMessage);
43 FRIEND_TEST(OpenVPNManagementServerTest, Send);
Darin Petkov683942b2012-03-27 18:00:04 +020044 FRIEND_TEST(OpenVPNManagementServerTest, SendPassword);
Darin Petkov271fe522012-03-27 13:47:29 +020045 FRIEND_TEST(OpenVPNManagementServerTest, SendState);
Darin Petkov683942b2012-03-27 18:00:04 +020046 FRIEND_TEST(OpenVPNManagementServerTest, SendUsername);
Darin Petkov271fe522012-03-27 13:47:29 +020047 FRIEND_TEST(OpenVPNManagementServerTest, Start);
48 FRIEND_TEST(OpenVPNManagementServerTest, Stop);
49
Darin Petkov78f63262012-03-26 01:30:24 +020050 // IO handler callbacks.
51 void OnReady(int fd);
52 void OnInput(InputData *data);
53
54 void Send(const std::string &data);
55 void SendState(const std::string &state);
Darin Petkov683942b2012-03-27 18:00:04 +020056 void SendUsername(const std::string &tag, const std::string &username);
57 void SendPassword(const std::string &tag, const std::string &password);
Darin Petkov78f63262012-03-26 01:30:24 +020058
59 void ProcessMessage(const std::string &message);
Darin Petkov271fe522012-03-27 13:47:29 +020060 bool ProcessInfoMessage(const std::string &message);
61 bool ProcessNeedPasswordMessage(const std::string &message);
62 bool ProcessFailedPasswordMessage(const std::string &message);
63 bool ProcessStateMessage(const std::string &message);
Darin Petkov78f63262012-03-26 01:30:24 +020064
Darin Petkov683942b2012-03-27 18:00:04 +020065 void PerformStaticChallenge();
66
Darin Petkov1c115202012-03-22 15:35:47 +010067 OpenVPNDriver *driver_;
Darin Petkov683942b2012-03-27 18:00:04 +020068 GLib *glib_;
Darin Petkov78f63262012-03-26 01:30:24 +020069 base::WeakPtrFactory<OpenVPNManagementServer> weak_ptr_factory_;
70 base::Callback<void(int)> ready_callback_;
71 base::Callback<void(InputData *)> input_callback_;
72
73 Sockets *sockets_;
74 int socket_;
75 scoped_ptr<IOHandler> ready_handler_;
76 EventDispatcher *dispatcher_;
77 int connected_socket_;
78 scoped_ptr<IOHandler> input_handler_;
Darin Petkov1c115202012-03-22 15:35:47 +010079
80 DISALLOW_COPY_AND_ASSIGN(OpenVPNManagementServer);
81};
82
83} // namespace shill
84
85#endif // SHILL_OPENVPN_MANAGEMENT_SERVER_