Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: l2tpipsec_vpn.cc

Issue 6508016: vpn-manager: Add l2tp/ipsec vpn manager (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/vpn-manager.git@master
Patch Set: respond to petkov Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 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 #include <poll.h>
6 #include <signal.h>
7 #include <stddef.h>
8 #include <sys/wait.h>
9
10 #include <string>
11
12 #include "base/command_line.h"
13 #include "base/logging.h"
14 #include "chromeos/process.h"
15 #include "chromeos/syslog_logging.h"
16 #include "gflags/gflags.h"
17 #include "vpn-manager/ipsec_manager.h"
18 #include "vpn-manager/l2tp_manager.h"
19
20 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
21 DEFINE_string(client_cert_file, "", "File with IPsec client certificate");
22 DEFINE_string(client_key_file, "", "File with IPsec client private key");
23 DEFINE_string(psk_file, "", "File with IPsec pre-shared key");
24 DEFINE_string(remote, "", "VPN server address");
25 DEFINE_string(server_ca_file, "", "File with IPsec server CA");
26 #pragma GCC diagnostic error "-Wstrict-aliasing"
27
28 // True if a signal has requested termination.
29 static bool s_terminate_request = false;
30
31 void HandleSignal(int sig_num) {
32 LOG(INFO) << "Caught signal " << sig_num;
33 switch(sig_num) {
34 case SIGTERM:
35 case SIGINT:
36 s_terminate_request = true;
37 break;
38 case SIGALRM:
39 break;
40 }
41 }
42
43 static void InstallSignalHandlers() {
44 struct sigaction sa;
45 memset(&sa, 0, sizeof(sa));
46 sa.sa_handler = HandleSignal;
47 sigaction(SIGTERM, &sa, NULL);
48 sigaction(SIGINT, &sa, NULL);
49 sigaction(SIGALRM, &sa, NULL);
50 }
51
52 static void LockDownUmask() {
53 // Only user and group may access configuration files we create.
54 umask(S_IWGRP | S_IROTH | S_IWOTH);
55 }
56
57 // Run the main event loop. The events to handle are:
58 // 1) timeout from poll
59 // 2) caught signal
60 // 3) stdout/err of child process ready
61 // 4) child process dies
62 static void RunEventLoop(IpsecManager* ipsec, L2tpManager* l2tp) {
Will Drewry 2011/03/05 04:06:39 not necessarily important now, but it seems like t
kmixter1 2011/03/11 01:34:27 Good point. Yeah, I'm not sure how much this fram
63 do {
64 int status;
65 int ipsec_poll_timeout = ipsec->Poll();
66 int l2tp_poll_timeout = l2tp->Poll();
67 int poll_timeout = (ipsec_poll_timeout > l2tp_poll_timeout) ?
68 ipsec_poll_timeout : l2tp_poll_timeout;
69
70 const int poll_input_count = 2;
71 struct pollfd poll_inputs[poll_input_count] = {
72 { ipsec->output_fd(), POLLIN }, // ipsec output
73 { l2tp->output_fd(), POLLIN } // l2tp output
74 };
75 DLOG(INFO) << "Polling with timeout " << poll_timeout << "ms";
76 int poll_result = poll(poll_inputs, poll_input_count, poll_timeout);
77 CHECK(poll_result >= 0 || errno == EINTR) << "Unexpected poll result";
Will Drewry 2011/03/05 04:06:39 Do you want this to hard-terminate or should it tr
kmixter1 2011/03/11 01:34:27 Good point again - changing to behave more gracefu
78
79 // Check if there are any child processes to be reaped without
80 // blocking.
81 pid_t pid = waitpid(-1, &status, WNOHANG);
82 if (pid > 0 && (ipsec->IsChild(pid) || l2tp->IsChild(pid))) {
83 LOG(WARNING) << "Child process " << pid << " stopped early";
84 s_terminate_request = true;
85 }
86
87 if (poll_inputs[0].revents & POLLIN)
88 ipsec->ProcessOutput();
89
90 if (poll_inputs[1].revents & POLLIN)
91 l2tp->ProcessOutput();
92 } while(!ipsec->was_stopped() && !s_terminate_request);
93 }
94
95 int main(int argc, char* argv[]) {
96 CommandLine::Init(argc, argv);
97 google::ParseCommandLineFlags(&argc, &argv, true);
98 int log_flags = chromeos::kLogToSyslog;
99 if (isatty(STDOUT_FILENO)) log_flags |= chromeos::kLogToStderr;
100 chromeos::InitLog(log_flags);
101 chromeos::OpenLog("l2tpipsec_vpn", true);
102 IpsecManager ipsec;
103 L2tpManager l2tp;
104
105 LockDownUmask();
106
107 ServiceManager::InitializeDirectories();
108
109 if (!ipsec.Initialize(1,
110 FLAGS_remote,
111 FLAGS_psk_file,
112 FLAGS_server_ca_file,
113 FLAGS_client_key_file,
114 FLAGS_client_cert_file)) {
115 return 1;
116 }
117 if (!l2tp.Initialize(FLAGS_remote)) {
118 return 1;
119 }
120 ServiceManager::SetLayerOrder(&ipsec, &l2tp);
121
122 InstallSignalHandlers();
123 CHECK(ipsec.Start()) << "Unable to start IPsec layer";
124
125 RunEventLoop(&ipsec, &l2tp);
126
127 LOG(INFO) << "Shutting down...";
128 l2tp.Stop();
129 // TODO(kmixter): use error codes to distinguish login failed vs
130 // connect failed.
131 return 0;
132 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698