2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
8 * Copyright (C) 2002-2009 OpenVPN Technologies, Inc. <sales@openvpn.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program (see the file COPYING included with this
21 * distribution); if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #include "forward-inline.h"
36 #define P2P_CHECK_SIG() EVENT_LOOP_CHECK_SIGNAL (c, process_signal_p2p, c);
39 process_signal_p2p (struct context
*c
)
42 return process_signal (c
);
46 tunnel_point_to_point (struct context
*c
)
50 /* set point-to-point mode */
53 /* initialize tunnel instance */
54 init_instance_handle_signals (c
, c
->es
, CC_HARD_USR1_TO_HUP
);
58 init_management_callback_p2p (c
);
63 perf_push (PERF_EVENT_LOOP
);
65 /* process timers, TLS, etc. */
69 /* set up and do the I/O wait */
70 io_wait (c
, p2p_iow_flags (c
));
74 if (c
->c2
.event_set_status
== ES_TIMEOUT
)
80 /* process the I/O which triggered select */
87 uninit_management_callback ();
89 /* tear down tunnel instance (unless --persist-tun) */
93 #undef PROCESS_SIGNAL_P2P
96 main (int argc
, char *argv
[])
101 fprintf (stderr
, "Sorry, I was built with --enable-pedantic and I am incapable of doing any real work!\n");
107 /* signify first time for components which can
108 only be initialized once per program instantiation. */
111 /* initialize program-wide statics */
115 * This loop is initially executed on startup and then
120 /* enter pre-initialization mode with regard to signal handling */
121 pre_init_signal_catch ();
123 /* zero context struct but leave first_time member alone */
124 context_clear_all_except_first_time (&c
);
126 /* static signal info object */
127 CLEAR (siginfo_static
);
128 c
.sig
= &siginfo_static
;
130 /* initialize garbage collector scoped to context object */
133 /* initialize environmental variable store */
134 c
.es
= env_set_create (NULL
);
136 env_set_add_win32 (c
.es
);
139 #ifdef ENABLE_MANAGEMENT
140 /* initialize management subsystem */
141 init_management (&c
);
144 /* initialize options to default state */
145 init_options (&c
.options
, true);
147 /* parse command line options, and read configuration file */
148 parse_argv (&c
.options
, argc
, argv
, M_USAGE
, OPT_P_DEFAULT
, NULL
, c
.es
);
151 /* plugins may contribute options configuration */
152 init_verb_mute (&c
, IVM_LEVEL_1
);
154 open_plugins (&c
, true, OPENVPN_PLUGIN_INIT_PRE_CONFIG_PARSE
);
157 /* init verbosity and mute levels */
158 init_verb_mute (&c
, IVM_LEVEL_1
);
160 /* set dev options */
161 init_options_dev (&c
.options
);
163 /* openssl print info? */
164 if (print_openssl_info (&c
.options
))
168 if (do_genkey (&c
.options
))
171 /* tun/tap persist command? */
172 if (do_persist_tuntap (&c
.options
))
175 /* sanity check on options */
176 options_postprocess (&c
.options
);
178 /* show all option settings */
179 show_settings (&c
.options
);
181 /* print version number */
182 msg (M_INFO
, "%s", title_string
);
185 pre_setup (&c
.options
);
188 if (do_test_crypto (&c
.options
))
191 #ifdef ENABLE_MANAGEMENT
192 /* open management subsystem */
193 if (!open_management (&c
))
197 /* set certain options as environmental variables */
198 setenv_settings (c
.es
, &c
.options
);
200 /* finish context init */
205 /* run tunnel depending on mode */
206 switch (c
.options
.mode
)
208 case MODE_POINT_TO_POINT
:
209 tunnel_point_to_point (&c
);
220 /* indicates first iteration -- has program-wide scope */
221 c
.first_time
= false;
223 /* any signals received? */
225 print_signal (c
.sig
, NULL
, M_INFO
);
227 /* pass restart status to management subsystem */
228 signal_restart_status (c
.sig
);
230 while (c
.sig
->signal_received
== SIGUSR1
);
232 uninit_options (&c
.options
);
235 while (c
.sig
->signal_received
== SIGHUP
);
238 context_gc_free (&c
);
240 env_set_destroy (c
.es
);
242 #ifdef ENABLE_MANAGEMENT
243 /* close management interface */
247 /* uninitialize program-wide statics */
250 openvpn_exit (OPENVPN_EXIT_STATUS_GOOD
); /* exit point */
251 return 0; /* NOTREACHED */