1 | /* |
1 | /* |
2 | vped.C -- the main file for vped |
2 | vped.C -- the main file for vped |
3 | Copyright (C) 1998-2002 Ivo Timmermans <ivo@o2w.nl> |
3 | Copyright (C) 1998-2002 Ivo Timmermans <ivo@o2w.nl> |
4 | 2000-2002 Guus Sliepen <guus@sliepen.eu.org> |
4 | 2000-2002 Guus Sliepen <guus@sliepen.eu.org> |
5 | 2003 Marc Lehmannn <pcg@goof.com> |
5 | 2003 Marc Lehmann <pcg@goof.com> |
6 | |
6 | |
7 | This program is free software; you can redistribute it and/or modify |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by |
8 | it under the terms of the GNU General Public License as published by |
9 | the Free Software Foundation; either version 2 of the License, or |
9 | the Free Software Foundation; either version 2 of the License, or |
10 | (at your option) any later version. |
10 | (at your option) any later version. |
… | |
… | |
21 | |
21 | |
22 | #include "config.h" |
22 | #include "config.h" |
23 | |
23 | |
24 | #include <cstdio> |
24 | #include <cstdio> |
25 | #include <cstring> |
25 | #include <cstring> |
|
|
26 | #include <cstdlib> |
|
|
27 | #include <clocale> |
26 | |
28 | |
27 | #include <errno.h> |
29 | #include <errno.h> |
28 | #include <fcntl.h> |
30 | #include <fcntl.h> |
29 | #include <getopt.h> |
31 | #include <getopt.h> |
30 | #include <signal.h> |
32 | #include <signal.h> |
31 | #include <sys/types.h> |
33 | #include <sys/types.h> |
32 | #include <unistd.h> |
34 | #include <unistd.h> |
33 | #include <signal.h> |
35 | #include <signal.h> |
34 | #include <termios.h> |
36 | #include <termios.h> |
35 | |
37 | |
|
|
38 | #if HAVE_SYS_MMAN_H |
36 | #include <sys/mman.h> |
39 | # include <sys/mman.h> |
|
|
40 | #endif |
37 | |
41 | |
38 | #include <openssl/err.h> |
42 | #include <openssl/err.h> |
39 | #include <openssl/rand.h> |
43 | #include <openssl/rand.h> |
40 | |
44 | |
41 | #include "gettext.h" |
45 | #include "gettext.h" |
… | |
… | |
45 | #include "slog.h" |
49 | #include "slog.h" |
46 | #include "util.h" |
50 | #include "util.h" |
47 | #include "vpn.h" |
51 | #include "vpn.h" |
48 | #include "iom.h" |
52 | #include "iom.h" |
49 | |
53 | |
50 | vpn network; |
|
|
51 | |
|
|
52 | static loglevel llevel = L_NONE; |
54 | static loglevel llevel = L_NONE; |
53 | |
55 | |
54 | /* If nonzero, display usage information and exit. */ |
56 | /* If nonzero, display usage information and exit. */ |
55 | static int show_help; |
57 | static int show_help; |
56 | |
58 | |
57 | /* If nonzero, print the version on standard output and exit. */ |
59 | /* If nonzero, print the version on standard output and exit. */ |
58 | static int show_version; |
60 | static int show_version; |
59 | |
61 | |
60 | #if HAVE_MLOCKALL |
|
|
61 | /* If nonzero, disable swapping for this process. */ |
62 | /* If nonzero, disable swapping for this process. */ |
62 | static int do_mlock = 0; |
63 | static int do_mlock = 0; |
63 | #endif |
|
|
64 | |
64 | |
65 | /* If zero, don't detach from the terminal. */ |
65 | /* If zero, don't detach from the terminal. */ |
66 | static int do_detach = 1; |
66 | static int do_detach = 1; |
67 | |
67 | |
68 | static struct option const long_options[] = |
68 | static struct option const long_options[] = |
… | |
… | |
70 | {"config", required_argument, NULL, 'c'}, |
70 | {"config", required_argument, NULL, 'c'}, |
71 | {"help", no_argument, &show_help, 1}, |
71 | {"help", no_argument, &show_help, 1}, |
72 | {"version", no_argument, &show_version, 1}, |
72 | {"version", no_argument, &show_version, 1}, |
73 | {"no-detach", no_argument, &do_detach, 0}, |
73 | {"no-detach", no_argument, &do_detach, 0}, |
74 | {"log-level", required_argument, NULL, 'l'}, |
74 | {"log-level", required_argument, NULL, 'l'}, |
75 | #if HAVE_MLOCKALL |
|
|
76 | {"mlock", no_argument, &do_mlock, 1}, |
75 | {"mlock", no_argument, &do_mlock, 1}, |
77 | #endif |
|
|
78 | {NULL, 0, NULL, 0} |
76 | {NULL, 0, NULL, 0} |
79 | }; |
77 | }; |
80 | |
78 | |
81 | static void |
79 | static void |
82 | usage (int status) |
80 | usage (int status) |
… | |
… | |
122 | |
120 | |
123 | case 'D': /* no detach */ |
121 | case 'D': /* no detach */ |
124 | do_detach = 0; |
122 | do_detach = 0; |
125 | break; |
123 | break; |
126 | |
124 | |
127 | #if HAVE_MLOCKALL |
|
|
128 | case 'L': /* lock into memory */ |
125 | case 'L': /* lock into memory */ |
129 | do_mlock = 1; |
126 | do_mlock = 1; |
130 | break; |
127 | break; |
131 | #endif |
|
|
132 | |
128 | |
133 | case 'l': /* inc debug level */ |
129 | case 'l': /* inc debug level */ |
134 | { |
130 | { |
135 | llevel = string_to_loglevel (optarg); |
131 | llevel = string_to_loglevel (optarg); |
136 | |
132 | |
… | |
… | |
153 | */ |
149 | */ |
154 | void cleanup_and_exit(int c) |
150 | void cleanup_and_exit(int c) |
155 | { |
151 | { |
156 | network.shutdown_all (); |
152 | network.shutdown_all (); |
157 | |
153 | |
158 | if (pidfilename) |
154 | if (conf.pidfilename) |
159 | remove_pid (pidfilename); |
155 | remove_pid (conf.pidfilename); |
160 | |
156 | |
161 | slog (L_INFO, _("terminating with exit code %d"), c); |
157 | slog (L_INFO, _("terminating with exit code %d"), c); |
162 | |
158 | |
163 | exit (c); |
159 | exit (c); |
164 | } |
160 | } |
… | |
… | |
226 | |
222 | |
227 | if (show_version) |
223 | if (show_version) |
228 | { |
224 | { |
229 | printf (_("%s version %s (built %s %s, protocol %d.%d)\n"), get_identity (), |
225 | printf (_("%s version %s (built %s %s, protocol %d.%d)\n"), get_identity (), |
230 | VERSION, __DATE__, __TIME__, PROTOCOL_MAJOR, PROTOCOL_MINOR); |
226 | VERSION, __DATE__, __TIME__, PROTOCOL_MAJOR, PROTOCOL_MINOR); |
|
|
227 | printf (_("Built with kernel interface %s/%s.\n"), IFTYPE, IFSUBTYPE); |
231 | printf (_ |
228 | printf (_ |
232 | ("Copyright (C) 2003 Marc Lehmann <vpe@plan9.de> and others.\n" |
229 | ("Copyright (C) 2003 Marc Lehmann <vpe@plan9.de> and others.\n" |
233 | "See the AUTHORS file for a complete list.\n\n" |
230 | "See the AUTHORS file for a complete list.\n\n" |
234 | "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n" |
231 | "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n" |
235 | "and you are welcome to redistribute it under certain conditions;\n" |
232 | "and you are welcome to redistribute it under certain conditions;\n" |
… | |
… | |
243 | |
240 | |
244 | log_to (LOGTO_SYSLOG | LOGTO_STDERR); |
241 | log_to (LOGTO_SYSLOG | LOGTO_STDERR); |
245 | |
242 | |
246 | /* Lock all pages into memory if requested */ |
243 | /* Lock all pages into memory if requested */ |
247 | |
244 | |
248 | #if HAVE_MLOCKALL |
245 | #if HAVE_MLOCKALL && HAVE_SYS_MMAN_H && _POSIX_MEMLOCK |
249 | if (do_mlock) |
246 | if (do_mlock) |
250 | if (mlockall (MCL_CURRENT | MCL_FUTURE)) |
247 | if (mlockall (MCL_CURRENT | MCL_FUTURE)) |
251 | slog (L_ERR, _("system call `%s' failed: %s"), "mlockall", strerror (errno)); |
248 | slog (L_ERR, _("system call `%s' failed: %s"), "mlockall", strerror (errno)); |
252 | #endif |
249 | #endif |
253 | |
250 | |
254 | make_names (); |
|
|
255 | conf.read_config (true); |
251 | conf.read_config (true); |
256 | |
252 | |
257 | set_loglevel (llevel != L_NONE ? llevel : conf.llevel); |
253 | set_loglevel (llevel != L_NONE ? llevel : conf.llevel); |
258 | |
254 | |
259 | RAND_load_file ("/dev/urandom", 1024); |
255 | RAND_load_file ("/dev/urandom", 1024); |
260 | |
256 | |
261 | if (!THISNODE) |
257 | if (!THISNODE) |
262 | { |
258 | { |
263 | slog (L_ERR, _("current node not set, or node '%s' not found in configfile, use the -n switch when starting vped."), |
259 | slog (L_ERR, _("current node not set, or node '%s' not found in configfile, specify the nodename when starting vped."), |
264 | thisnode ? thisnode : "<unset>"); |
260 | thisnode ? thisnode : "<unset>"); |
265 | exit (1); |
261 | exit (EXIT_FAILURE); |
266 | } |
262 | } |
267 | |
263 | |
268 | if (detach (do_detach)) |
264 | if (detach (do_detach)) |
269 | exit (0); |
265 | exit (EXIT_SUCCESS); |
270 | |
266 | |
271 | setup_signals (); |
267 | setup_signals (); |
272 | |
268 | |
273 | if (!network.setup ()) |
269 | if (!network.setup ()) |
274 | { |
270 | { |
275 | iom.loop (); |
271 | iom.loop (); |
276 | cleanup_and_exit (1); |
272 | cleanup_and_exit (EXIT_FAILURE); |
277 | } |
273 | } |
278 | |
274 | |
279 | slog (L_ERR, _("unable to setup network, unrecoverable error, exiting.")); |
275 | slog (L_ERR, _("unable to setup network, unrecoverable error, exiting.")); |
280 | cleanup_and_exit (1); |
276 | cleanup_and_exit (EXIT_FAILURE); |
281 | } |
277 | } |
282 | |
278 | |