tets
[anytun.git] / openvpn / openvpn.c
blob47701f9c069e47b34aaf85c0a3957e02a4440601
1 /*
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
6 * packet compression.
8 * Copyright (C) 2002-2005 OpenVPN Solutions LLC <info@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
25 #ifdef WIN32
26 #include "config-win32.h"
27 #else
28 #include "config.h"
29 #endif
31 #include "syshead.h"
33 #include "init.h"
34 #include "forward.h"
35 #include "multi.h"
37 #include "memdbg.h"
39 #include "forward-inline.h"
41 #define P2P_CHECK_SIG() EVENT_LOOP_CHECK_SIGNAL (c, process_signal_p2p, c);
43 static bool
44 process_signal_p2p (struct context *c)
46 remap_signal (c);
47 return process_signal (c);
50 static void
51 tunnel_point_to_point (struct context *c)
53 context_clear_2 (c);
55 /* set point-to-point mode */
56 c->mode = CM_P2P;
58 /* initialize tunnel instance */
59 init_instance_handle_signals (c, c->es, CC_HARD_USR1_TO_HUP);
60 if (IS_SIG (c))
61 return;
63 init_management_callback_p2p (c);
65 /* main event loop */
66 while (true)
68 perf_push (PERF_EVENT_LOOP);
70 /* process timers, TLS, etc. */
71 pre_select (c);
72 P2P_CHECK_SIG();
74 /* set up and do the I/O wait */
75 io_wait (c, p2p_iow_flags (c));
76 P2P_CHECK_SIG();
78 /* timeout? */
79 if (c->c2.event_set_status == ES_TIMEOUT)
81 perf_pop ();
82 continue;
85 /* process the I/O which triggered select */
86 process_io (c);
87 P2P_CHECK_SIG();
89 perf_pop ();
92 uninit_management_callback ();
94 /* tear down tunnel instance (unless --persist-tun) */
95 close_instance (c);
98 #undef PROCESS_SIGNAL_P2P
101 main (int argc, char *argv[])
103 struct context c;
105 #if PEDANTIC
106 fprintf (stderr, "Sorry, I was built with --enable-pedantic and I am incapable of doing any real work!\n");
107 return 1;
108 #endif
110 CLEAR (c);
112 /* signify first time for components which can
113 only be initialized once per program instantiation. */
114 c.first_time = true;
116 /* initialize program-wide statics */
117 if (init_static ())
120 * This loop is initially executed on startup and then
121 * once per SIGHUP.
125 /* zero context struct but leave first_time member alone */
126 context_clear_all_except_first_time (&c);
128 /* static signal info object */
129 CLEAR (siginfo_static);
130 c.sig = &siginfo_static;
132 /* initialize garbage collector scoped to context object */
133 gc_init (&c.gc);
135 /* initialize environmental variable store */
136 c.es = env_set_create (&c.gc);
138 #ifdef ENABLE_MANAGEMENT
139 /* initialize management subsystem */
140 init_management (&c);
141 #endif
143 /* initialize options to default state */
144 init_options (&c.options);
146 /* parse command line options, and read configuration file */
147 parse_argv (&c.options, argc, argv, M_USAGE, OPT_P_DEFAULT, NULL, c.es);
149 /* init verbosity and mute levels */
150 init_verb_mute (&c, IVM_LEVEL_1);
152 /* set dev options */
153 init_options_dev (&c.options);
155 /* openssl print info? */
156 if (print_openssl_info (&c.options))
157 break;
159 /* --genkey mode? */
160 if (do_genkey (&c.options))
161 break;
163 /* tun/tap persist command? */
164 if (do_persist_tuntap (&c.options))
165 break;
167 /* sanity check on options */
168 options_postprocess (&c.options, c.first_time);
170 /* show all option settings */
171 show_settings (&c.options);
173 /* print version number */
174 msg (M_INFO, "%s", title_string);
176 /* misc stuff */
177 pre_setup (&c.options);
179 /* test crypto? */
180 if (do_test_crypto (&c.options))
181 break;
183 #ifdef ENABLE_MANAGEMENT
184 /* open management subsystem */
185 if (!open_management (&c))
186 break;
187 #endif
189 /* set certain options as environmental variables */
190 setenv_settings (c.es, &c.options);
192 /* finish context init */
193 context_init_1 (&c);
197 /* run tunnel depending on mode */
198 switch (c.options.mode)
200 case MODE_POINT_TO_POINT:
201 tunnel_point_to_point (&c);
202 break;
203 #if P2MP_SERVER
204 case MODE_SERVER:
205 tunnel_server (&c);
206 break;
207 #endif
208 default:
209 ASSERT (0);
212 /* indicates first iteration -- has program-wide scope */
213 c.first_time = false;
215 /* any signals received? */
216 if (IS_SIG (&c))
217 print_signal (c.sig, NULL, M_INFO);
219 /* pass restart status to management subsystem */
220 signal_restart_status (c.sig);
222 while (c.sig->signal_received == SIGUSR1);
224 uninit_options (&c.options);
225 gc_reset (&c.gc);
227 while (c.sig->signal_received == SIGHUP);
230 context_gc_free (&c);
232 #ifdef ENABLE_MANAGEMENT
233 /* close management interface */
234 close_management ();
235 #endif
237 /* uninitialize program-wide statics */
238 uninit_static ();
240 openvpn_exit (OPENVPN_EXIT_STATUS_GOOD); /* exit point */
241 return 0; /* NOTREACHED */