Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 PSK"); | |
| 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 bool s_terminate_request = false; | |
|
petkov
2011/03/04 18:42:56
static?
kmixter1
2011/03/05 02:48:59
Done.
| |
| 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 int main(int argc, char* argv[]) { | |
| 58 CommandLine::Init(argc, argv); | |
| 59 google::ParseCommandLineFlags(&argc, &argv, true); | |
| 60 int log_flags = chromeos::kLogToSyslog; | |
| 61 if (isatty(STDOUT_FILENO)) log_flags |= chromeos::kLogToStderr; | |
| 62 chromeos::InitLog(log_flags); | |
| 63 chromeos::OpenLog("l2tpipsec_vpn", true); | |
| 64 IpsecManager ipsec; | |
| 65 L2tpManager l2tp; | |
| 66 | |
| 67 LockDownUmask(); | |
| 68 | |
| 69 ServiceManager::Initialize(); | |
| 70 | |
| 71 if (!ipsec.Initialize(1, | |
| 72 FLAGS_remote, | |
| 73 FLAGS_psk_file, | |
| 74 FLAGS_server_ca_file, | |
| 75 FLAGS_client_key_file, | |
| 76 FLAGS_client_cert_file)) { | |
| 77 return 1; | |
| 78 } | |
| 79 if (!l2tp.Initialize(FLAGS_remote)) { | |
| 80 return 1; | |
| 81 } | |
| 82 ServiceManager::SetLayerOrder(&ipsec, &l2tp); | |
| 83 | |
| 84 InstallSignalHandlers(); | |
| 85 CHECK(ipsec.Start()) << "Unable to start IPsec layer"; | |
| 86 | |
| 87 // Event loop. Events are: | |
|
petkov
2011/03/04 18:42:56
move this event loop into a separate function
kmixter1
2011/03/05 02:48:59
Done.
| |
| 88 // 1) timeout from poll | |
| 89 // 2) caught signal | |
| 90 // 3) stdout/err of child process ready | |
| 91 // 4) child process dies | |
| 92 do { | |
| 93 int status; | |
| 94 int ipsec_poll_timeout = ipsec.Poll(); | |
| 95 int l2tp_poll_timeout = l2tp.Poll(); | |
| 96 int poll_timeout = (ipsec_poll_timeout > l2tp_poll_timeout) ? | |
| 97 ipsec_poll_timeout : l2tp_poll_timeout; | |
| 98 | |
| 99 const int poll_input_count = 2; | |
| 100 struct pollfd poll_inputs[poll_input_count] = { | |
| 101 { ipsec.output_fd(), POLLIN }, // ipsec output | |
| 102 { l2tp.output_fd(), POLLIN } // l2tp output | |
| 103 }; | |
| 104 int poll_result = poll(poll_inputs, poll_input_count, poll_timeout); | |
| 105 CHECK(poll_result >= 0 || errno == EINTR) << "Unexpected poll result"; | |
| 106 | |
| 107 pid_t pid = waitpid(-1, &status, WNOHANG); | |
|
petkov
2011/03/04 18:42:56
add a comment what this does...
kmixter1
2011/03/05 02:48:59
Done.
| |
| 108 if (pid > 0) { | |
| 109 ipsec.HandleChild(pid, status); | |
| 110 l2tp.HandleChild(pid, status); | |
| 111 } | |
| 112 | |
| 113 if (poll_inputs[0].revents & POLLIN) { | |
| 114 ServiceManager::WriteFdToSyslog(ipsec.output_fd(), "IPsec: "); | |
| 115 } | |
| 116 if (poll_inputs[1].revents & POLLIN) { | |
| 117 ServiceManager::WriteFdToSyslog(l2tp.output_fd(), "L2TP: "); | |
| 118 } | |
| 119 | |
|
petkov
2011/03/04 18:42:56
remove blank line
kmixter1
2011/03/05 02:48:59
Done.
| |
| 120 } while(!ipsec.was_stopped() && !s_terminate_request); | |
| 121 LOG(INFO) << "Shutting down..."; | |
| 122 l2tp.Stop(); | |
| 123 // TODO: use error codes to distinguish login failed vs connect | |
| 124 // failed. | |
| 125 return 0; | |
| 126 } | |
| OLD | NEW |