1 | /* |
1 | /* |
2 | vped.C -- the main file for vped |
2 | vped.C -- the main file for gvpe |
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-2005 Marc Lehmann <gvpe@schmorp.de> |
6 | |
6 | |
|
|
7 | This file is part of GVPE. |
|
|
8 | |
7 | This program is free software; you can redistribute it and/or modify |
9 | GVPE is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by |
10 | 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 |
11 | the Free Software Foundation; either version 2 of the License, or |
10 | (at your option) any later version. |
12 | (at your option) any later version. |
11 | |
13 | |
12 | This program is distributed in the hope that it will be useful, |
14 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | GNU General Public License for more details. |
17 | GNU General Public License for more details. |
16 | |
18 | |
17 | You should have received a copy of the GNU General Public License |
19 | You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software |
20 | along with gvpe; if not, write to the Free Software |
19 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 | */ |
22 | */ |
21 | |
23 | |
22 | #include "config.h" |
24 | #include "config.h" |
23 | |
25 | |
24 | #include <cstdio> |
26 | #include <cstdio> |
25 | #include <cstring> |
27 | #include <cstring> |
|
|
28 | #include <cstdlib> |
|
|
29 | #include <locale.h> |
26 | |
30 | |
27 | #include <errno.h> |
31 | #include <errno.h> |
28 | #include <fcntl.h> |
32 | #include <fcntl.h> |
29 | #include <getopt.h> |
33 | #include <getopt.h> |
30 | #include <signal.h> |
34 | #include <signal.h> |
31 | #include <sys/types.h> |
35 | #include <sys/types.h> |
32 | #include <unistd.h> |
36 | #include <unistd.h> |
33 | #include <signal.h> |
37 | #include <signal.h> |
34 | #include <termios.h> |
38 | #include <termios.h> |
35 | |
39 | |
|
|
40 | #if HAVE_SYS_MMAN_H |
36 | #include <sys/mman.h> |
41 | # include <sys/mman.h> |
|
|
42 | #endif |
37 | |
43 | |
|
|
44 | #include <openssl/err.h> |
38 | #include <openssl/rand.h> |
45 | #include <openssl/rand.h> |
39 | |
46 | |
40 | #include "gettext.h" |
47 | #include "gettext.h" |
41 | #include "pidfile.h" |
48 | #include "pidfile.h" |
42 | |
49 | |
43 | #include "conf.h" |
50 | #include "conf.h" |
44 | #include "slog.h" |
51 | #include "slog.h" |
45 | #include "util.h" |
52 | #include "util.h" |
46 | #include "protocol.h" |
53 | #include "vpn.h" |
47 | #include "iom.h" |
54 | #include "iom.h" |
48 | |
|
|
49 | vpn network; |
|
|
50 | |
55 | |
51 | static loglevel llevel = L_NONE; |
56 | static loglevel llevel = L_NONE; |
52 | |
57 | |
53 | /* If nonzero, display usage information and exit. */ |
58 | /* If nonzero, display usage information and exit. */ |
54 | static int show_help; |
59 | static int show_help; |
55 | |
60 | |
56 | /* If nonzero, print the version on standard output and exit. */ |
61 | /* If nonzero, print the version on standard output and exit. */ |
57 | static int show_version; |
62 | static int show_version; |
58 | |
63 | |
59 | #if HAVE_MLOCKALL |
|
|
60 | /* If nonzero, disable swapping for this process. */ |
64 | /* If nonzero, disable swapping for this process. */ |
61 | static int do_mlock = 0; |
65 | static int do_mlock = 0; |
62 | #endif |
|
|
63 | |
66 | |
64 | /* If zero, don't detach from the terminal. */ |
67 | /* If zero, don't detach from the terminal. */ |
65 | static int do_detach = 1; |
68 | static int do_detach = 1; |
66 | |
69 | |
67 | static struct option const long_options[] = |
70 | static struct option const long_options[] = |
… | |
… | |
69 | {"config", required_argument, NULL, 'c'}, |
72 | {"config", required_argument, NULL, 'c'}, |
70 | {"help", no_argument, &show_help, 1}, |
73 | {"help", no_argument, &show_help, 1}, |
71 | {"version", no_argument, &show_version, 1}, |
74 | {"version", no_argument, &show_version, 1}, |
72 | {"no-detach", no_argument, &do_detach, 0}, |
75 | {"no-detach", no_argument, &do_detach, 0}, |
73 | {"log-level", required_argument, NULL, 'l'}, |
76 | {"log-level", required_argument, NULL, 'l'}, |
74 | #if HAVE_MLOCKALL |
|
|
75 | {"mlock", no_argument, &do_mlock, 1}, |
77 | {"mlock", no_argument, &do_mlock, 1}, |
76 | #endif |
|
|
77 | {NULL, 0, NULL, 0} |
78 | {NULL, 0, NULL, 0} |
78 | }; |
79 | }; |
79 | |
80 | |
80 | static void |
81 | static void |
81 | usage (int status) |
82 | usage (int status) |
… | |
… | |
121 | |
122 | |
122 | case 'D': /* no detach */ |
123 | case 'D': /* no detach */ |
123 | do_detach = 0; |
124 | do_detach = 0; |
124 | break; |
125 | break; |
125 | |
126 | |
126 | #if HAVE_MLOCKALL |
|
|
127 | case 'L': /* lock into memory */ |
127 | case 'L': /* lock into memory */ |
128 | do_mlock = 1; |
128 | do_mlock = 1; |
129 | break; |
129 | break; |
130 | #endif |
|
|
131 | |
130 | |
132 | case 'l': /* inc debug level */ |
131 | case 'l': /* inc debug level */ |
133 | { |
132 | { |
134 | llevel = string_to_loglevel (optarg); |
133 | llevel = string_to_loglevel (optarg); |
135 | |
134 | |
… | |
… | |
152 | */ |
151 | */ |
153 | void cleanup_and_exit(int c) |
152 | void cleanup_and_exit(int c) |
154 | { |
153 | { |
155 | network.shutdown_all (); |
154 | network.shutdown_all (); |
156 | |
155 | |
157 | if (pidfilename) |
156 | if (conf.pidfilename) |
158 | remove_pid (pidfilename); |
157 | remove_pid (conf.pidfilename); |
159 | |
158 | |
160 | slog (L_INFO, _("terminating with exit code %d"), c); |
159 | slog (L_INFO, _("terminating with exit code %d"), c); |
161 | |
160 | |
162 | exit (c); |
161 | exit (c); |
163 | } |
162 | } |
… | |
… | |
180 | } |
179 | } |
181 | |
180 | |
182 | RETSIGTYPE |
181 | RETSIGTYPE |
183 | sigusr1_handler (int a) |
182 | sigusr1_handler (int a) |
184 | { |
183 | { |
|
|
184 | network.dump_status (); |
185 | } |
185 | } |
186 | |
186 | |
187 | RETSIGTYPE |
187 | RETSIGTYPE |
188 | sigusr2_handler (int a) |
188 | sigusr2_handler (int a) |
189 | { |
189 | { |
… | |
… | |
199 | |
199 | |
200 | act.sa_handler = sighup_handler; sigaction (SIGHUP , &act, NULL); |
200 | act.sa_handler = sighup_handler; sigaction (SIGHUP , &act, NULL); |
201 | act.sa_handler = sigusr1_handler; sigaction (SIGUSR1, &act, NULL); |
201 | act.sa_handler = sigusr1_handler; sigaction (SIGUSR1, &act, NULL); |
202 | act.sa_handler = sigusr2_handler; sigaction (SIGUSR2, &act, NULL); |
202 | act.sa_handler = sigusr2_handler; sigaction (SIGUSR2, &act, NULL); |
203 | act.sa_handler = SIG_IGN; sigaction (SIGCHLD, &act, NULL); |
203 | act.sa_handler = SIG_IGN; sigaction (SIGCHLD, &act, NULL); |
|
|
204 | act.sa_handler = SIG_IGN; sigaction (SIGPIPE, &act, NULL); |
204 | act.sa_flags = SA_RESETHAND; |
205 | act.sa_flags = SA_RESETHAND; |
205 | act.sa_handler = sigterm_handler; sigaction (SIGINT , &act, NULL); |
206 | act.sa_handler = sigterm_handler; sigaction (SIGINT , &act, NULL); |
206 | act.sa_handler = sigterm_handler; sigaction (SIGTERM, &act, NULL); |
207 | act.sa_handler = sigterm_handler; sigaction (SIGTERM, &act, NULL); |
207 | } |
208 | } |
208 | |
209 | |
209 | int |
210 | int |
210 | main (int argc, char **argv, char **envp) |
211 | main (int argc, char **argv, char **envp) |
211 | { |
212 | { |
|
|
213 | ERR_load_crypto_strings (); // we have the RAM |
|
|
214 | |
212 | set_loglevel (L_INFO); |
215 | set_loglevel (L_INFO); |
213 | set_identity (argv[0]); |
216 | set_identity (argv[0]); |
214 | log_to (LOGTO_SYSLOG | LOGTO_STDERR); |
217 | log_to (LOGTO_SYSLOG | LOGTO_STDERR); |
215 | |
218 | |
216 | setlocale (LC_ALL, ""); |
219 | setlocale (LC_ALL, ""); |
… | |
… | |
219 | |
222 | |
220 | parse_options (argc, argv, envp); |
223 | parse_options (argc, argv, envp); |
221 | |
224 | |
222 | if (show_version) |
225 | if (show_version) |
223 | { |
226 | { |
224 | printf (_("%s version %s (built %s %s, protocol %d:%d)\n"), get_identity (), |
227 | printf (_("%s version %s (built %s %s, protocol %d.%d)\n"), get_identity (), |
225 | VERSION, __DATE__, __TIME__, PROTOCOL_MAJOR, PROTOCOL_MINOR); |
228 | VERSION, __DATE__, __TIME__, PROTOCOL_MAJOR, PROTOCOL_MINOR); |
|
|
229 | printf (_("Built with kernel interface %s/%s.\n"), IFTYPE, IFSUBTYPE); |
226 | printf (_ |
230 | printf (_ |
227 | ("Copyright (C) 2003 Marc Lehmann <vpe@plan9.de> and others.\n" |
231 | ("Copyright (C) 2003 Marc Lehmann <vpe@plan9.de> and others.\n" |
228 | "See the AUTHORS file for a complete list.\n\n" |
232 | "See the AUTHORS file for a complete list.\n\n" |
229 | "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n" |
233 | "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n" |
230 | "and you are welcome to redistribute it under certain conditions;\n" |
234 | "and you are welcome to redistribute it under certain conditions;\n" |
… | |
… | |
238 | |
242 | |
239 | log_to (LOGTO_SYSLOG | LOGTO_STDERR); |
243 | log_to (LOGTO_SYSLOG | LOGTO_STDERR); |
240 | |
244 | |
241 | /* Lock all pages into memory if requested */ |
245 | /* Lock all pages into memory if requested */ |
242 | |
246 | |
243 | #if HAVE_MLOCKALL |
247 | #if HAVE_MLOCKALL && HAVE_SYS_MMAN_H && _POSIX_MEMLOCK |
244 | if (do_mlock) |
248 | if (do_mlock) |
245 | if (mlockall (MCL_CURRENT | MCL_FUTURE)) |
249 | if (mlockall (MCL_CURRENT | MCL_FUTURE)) |
246 | slog (L_ERR, _("system call `%s' failed: %s"), "mlockall", strerror (errno)); |
250 | slog (L_ERR, _("system call `%s' failed: %s"), "mlockall", strerror (errno)); |
247 | #endif |
251 | #endif |
248 | |
252 | |
249 | make_names (); |
|
|
250 | conf.read_config (true); |
253 | conf.read_config (true); |
251 | |
254 | |
252 | set_loglevel (llevel != L_NONE ? llevel : conf.llevel); |
255 | set_loglevel (llevel != L_NONE ? llevel : conf.llevel); |
253 | |
256 | |
254 | RAND_load_file ("/dev/urandom", 1024); |
257 | RAND_load_file ("/dev/urandom", 1024); |
255 | |
258 | |
256 | if (!THISNODE) |
259 | if (!THISNODE) |
257 | { |
260 | { |
258 | slog (L_ERR, _("current node not set, or node '%s' not found in configfile, use the -n switch when starting vped."), |
261 | slog (L_ERR, _("current node not set, or node '%s' not found in configfile, specify the nodename when starting vped."), |
259 | thisnode ? thisnode : "<unset>"); |
262 | thisnode ? thisnode : "<unset>"); |
260 | exit (1); |
263 | exit (EXIT_FAILURE); |
261 | } |
264 | } |
262 | |
265 | |
263 | if (detach (do_detach)) |
266 | if (detach (do_detach)) |
264 | exit (0); |
267 | exit (EXIT_SUCCESS); |
265 | |
268 | |
266 | setup_signals (); |
269 | setup_signals (); |
267 | |
270 | |
268 | if (!network.setup ()) |
271 | if (!network.setup ()) |
269 | { |
272 | { |
270 | iom.loop (); |
273 | io_manager::loop (); |
271 | cleanup_and_exit (1); |
274 | cleanup_and_exit (EXIT_FAILURE); |
272 | } |
275 | } |
273 | |
276 | |
274 | slog (L_ERR, _("unable to setup network, unrecoverable error, exiting.")); |
277 | slog (L_ERR, _("unable to setup network, unrecoverable error, exiting.")); |
275 | cleanup_and_exit (1); |
278 | cleanup_and_exit (EXIT_FAILURE); |
276 | } |
279 | } |
277 | |
280 | |