~ubuntu-branches/ubuntu/oneiric/openvpn/oneiric

1 by Alberto Gonzalez Iniesta
Import upstream version 1.6.0
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.
7
 *
1.3.5 by Alberto Gonzalez Iniesta
Import upstream version 2.1.3
8
 *  Copyright (C) 2002-2010 OpenVPN Technologies, Inc. <sales@openvpn.net>
1 by Alberto Gonzalez Iniesta
Import upstream version 1.6.0
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
1.1.2 by Alberto Gonzalez Iniesta
Import upstream version 2.0.2
11
 *  it under the terms of the GNU General Public License version 2
12
 *  as published by the Free Software Foundation.
1 by Alberto Gonzalez Iniesta
Import upstream version 1.6.0
13
 *
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.
18
 *
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
23
 */
24
25
#include "syshead.h"
26
1.1.1 by Alberto Gonzalez Iniesta
Import upstream version 1.99+2.rc6
27
#include "init.h"
28
#include "forward.h"
29
#include "multi.h"
1.1.10 by Alberto Gonzalez Iniesta
Import upstream version 2.1~rc9
30
#include "win32.h"
1 by Alberto Gonzalez Iniesta
Import upstream version 1.6.0
31
32
#include "memdbg.h"
33
1.1.1 by Alberto Gonzalez Iniesta
Import upstream version 1.99+2.rc6
34
#include "forward-inline.h"
35
36
#define P2P_CHECK_SIG() EVENT_LOOP_CHECK_SIGNAL (c, process_signal_p2p, c);
37
38
static bool
39
process_signal_p2p (struct context *c)
40
{
41
  remap_signal (c);
42
  return process_signal (c);
43
}
44
45
static void
46
tunnel_point_to_point (struct context *c)
47
{
48
  context_clear_2 (c);
49
50
  /* set point-to-point mode */
51
  c->mode = CM_P2P;
52
53
  /* initialize tunnel instance */
1.1.2 by Alberto Gonzalez Iniesta
Import upstream version 2.0.2
54
  init_instance_handle_signals (c, c->es, CC_HARD_USR1_TO_HUP);
1.1.1 by Alberto Gonzalez Iniesta
Import upstream version 1.99+2.rc6
55
  if (IS_SIG (c))
56
    return;
57
58
  /* main event loop */
1 by Alberto Gonzalez Iniesta
Import upstream version 1.6.0
59
  while (true)
60
    {
1.1.1 by Alberto Gonzalez Iniesta
Import upstream version 1.99+2.rc6
61
      perf_push (PERF_EVENT_LOOP);
62
63
      /* process timers, TLS, etc. */
64
      pre_select (c);
65
      P2P_CHECK_SIG();
66
67
      /* set up and do the I/O wait */
68
      io_wait (c, p2p_iow_flags (c));
69
      P2P_CHECK_SIG();
70
71
      /* timeout? */
72
      if (c->c2.event_set_status == ES_TIMEOUT)
73
	{
74
	  perf_pop ();
75
	  continue;
76
	}
77
78
      /* process the I/O which triggered select */
79
      process_io (c);
80
      P2P_CHECK_SIG();
81
82
      perf_pop ();
83
    }
84
85
  uninit_management_callback ();
86
87
  /* tear down tunnel instance (unless --persist-tun) */
88
  close_instance (c);
89
}
90
91
#undef PROCESS_SIGNAL_P2P
92
93
int
94
main (int argc, char *argv[])
95
{
96
  struct context c;
97
1.1.2 by Alberto Gonzalez Iniesta
Import upstream version 2.0.2
98
#if PEDANTIC
99
  fprintf (stderr, "Sorry, I was built with --enable-pedantic and I am incapable of doing any real work!\n");
100
  return 1;
101
#endif
102
1.1.1 by Alberto Gonzalez Iniesta
Import upstream version 1.99+2.rc6
103
  CLEAR (c);
104
105
  /* signify first time for components which can
106
     only be initialized once per program instantiation. */
107
  c.first_time = true;
108
109
  /* initialize program-wide statics */
110
  if (init_static ())
111
    {
112
      /*
113
       * This loop is initially executed on startup and then
114
       * once per SIGHUP.
115
       */
116
      do
117
	{
1.1.7 by Alberto Gonzalez Iniesta
Import upstream version 2.1~rc4
118
	  /* enter pre-initialization mode with regard to signal handling */
119
	  pre_init_signal_catch ();
120
1.1.1 by Alberto Gonzalez Iniesta
Import upstream version 1.99+2.rc6
121
	  /* zero context struct but leave first_time member alone */
122
	  context_clear_all_except_first_time (&c);
123
124
	  /* static signal info object */
125
	  CLEAR (siginfo_static);
126
	  c.sig = &siginfo_static;
127
128
	  /* initialize garbage collector scoped to context object */
129
	  gc_init (&c.gc);
130
131
	  /* initialize environmental variable store */
1.1.9 by Alberto Gonzalez Iniesta
Import upstream version 2.1~rc8
132
	  c.es = env_set_create (NULL);
1.1.10 by Alberto Gonzalez Iniesta
Import upstream version 2.1~rc9
133
#ifdef WIN32
134
	  env_set_add_win32 (c.es);
135
#endif
1.1.1 by Alberto Gonzalez Iniesta
Import upstream version 1.99+2.rc6
136
137
#ifdef ENABLE_MANAGEMENT
138
	  /* initialize management subsystem */
139
	  init_management (&c);
140
#endif
141
142
	  /* initialize options to default state */
1.1.9 by Alberto Gonzalez Iniesta
Import upstream version 2.1~rc8
143
	  init_options (&c.options, true);
1.1.1 by Alberto Gonzalez Iniesta
Import upstream version 1.99+2.rc6
144
145
	  /* parse command line options, and read configuration file */
146
	  parse_argv (&c.options, argc, argv, M_USAGE, OPT_P_DEFAULT, NULL, c.es);
147
1.1.7 by Alberto Gonzalez Iniesta
Import upstream version 2.1~rc4
148
#ifdef ENABLE_PLUGIN
149
	  /* plugins may contribute options configuration */
150
	  init_verb_mute (&c, IVM_LEVEL_1);
151
	  init_plugins (&c);
152
	  open_plugins (&c, true, OPENVPN_PLUGIN_INIT_PRE_CONFIG_PARSE);
153
#endif
154
1.1.1 by Alberto Gonzalez Iniesta
Import upstream version 1.99+2.rc6
155
	  /* init verbosity and mute levels */
156
	  init_verb_mute (&c, IVM_LEVEL_1);
157
158
	  /* set dev options */
159
	  init_options_dev (&c.options);
160
161
	  /* openssl print info? */
162
	  if (print_openssl_info (&c.options))
163
	    break;
164
165
	  /* --genkey mode? */
166
	  if (do_genkey (&c.options))
167
	    break;
168
169
	  /* tun/tap persist command? */
170
	  if (do_persist_tuntap (&c.options))
171
	    break;
172
173
	  /* sanity check on options */
1.1.9 by Alberto Gonzalez Iniesta
Import upstream version 2.1~rc8
174
	  options_postprocess (&c.options);
1.1.1 by Alberto Gonzalez Iniesta
Import upstream version 1.99+2.rc6
175
176
	  /* show all option settings */
177
	  show_settings (&c.options);
178
179
	  /* print version number */
180
	  msg (M_INFO, "%s", title_string);
181
182
	  /* misc stuff */
183
	  pre_setup (&c.options);
184
185
	  /* test crypto? */
186
	  if (do_test_crypto (&c.options))
187
	    break;
188
	  
189
#ifdef ENABLE_MANAGEMENT
190
	  /* open management subsystem */
191
	  if (!open_management (&c))
192
	    break;
193
#endif
1.1.7 by Alberto Gonzalez Iniesta
Import upstream version 2.1~rc4
194
1.1.1 by Alberto Gonzalez Iniesta
Import upstream version 1.99+2.rc6
195
	  /* set certain options as environmental variables */
196
	  setenv_settings (c.es, &c.options);
197
198
	  /* finish context init */
199
	  context_init_1 (&c);
200
201
	  do
202
	    {
203
	      /* run tunnel depending on mode */
204
	      switch (c.options.mode)
205
		{
206
		case MODE_POINT_TO_POINT:
207
		  tunnel_point_to_point (&c);
208
		  break;
209
#if P2MP_SERVER
210
		case MODE_SERVER:
211
		  tunnel_server (&c);
212
		  break;
213
#endif
1 by Alberto Gonzalez Iniesta
Import upstream version 1.6.0
214
		default:
215
		  ASSERT (0);
216
		}
1.1.1 by Alberto Gonzalez Iniesta
Import upstream version 1.99+2.rc6
217
218
	      /* indicates first iteration -- has program-wide scope */
219
	      c.first_time = false;
220
221
	      /* any signals received? */
222
	      if (IS_SIG (&c))
223
		print_signal (c.sig, NULL, M_INFO);
224
225
	      /* pass restart status to management subsystem */
226
	      signal_restart_status (c.sig);
227
	    }
228
	  while (c.sig->signal_received == SIGUSR1);
229
230
	  uninit_options (&c.options);
231
	  gc_reset (&c.gc);
232
	}
233
      while (c.sig->signal_received == SIGHUP);
234
    }
235
236
  context_gc_free (&c);
237
1.1.9 by Alberto Gonzalez Iniesta
Import upstream version 2.1~rc8
238
  env_set_destroy (c.es);
239
1.1.1 by Alberto Gonzalez Iniesta
Import upstream version 1.99+2.rc6
240
#ifdef ENABLE_MANAGEMENT
241
  /* close management interface */
242
  close_management ();
243
#endif
244
245
  /* uninitialize program-wide statics */
246
  uninit_static ();
247
248
  openvpn_exit (OPENVPN_EXIT_STATUS_GOOD);  /* exit point */
249
  return 0;			            /* NOTREACHED */
250
}