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 | |
|
|
42 | #include <openssl/err.h> |
38 | #include <openssl/rand.h> |
43 | #include <openssl/rand.h> |
39 | |
44 | |
40 | #include "gettext.h" |
45 | #include "gettext.h" |
41 | #include "pidfile.h" |
46 | #include "pidfile.h" |
42 | |
47 | |
43 | #include "conf.h" |
48 | #include "conf.h" |
44 | #include "slog.h" |
49 | #include "slog.h" |
45 | #include "util.h" |
50 | #include "util.h" |
46 | #include "protocol.h" |
51 | #include "vpn.h" |
47 | |
52 | #include "iom.h" |
48 | vpn network; |
|
|
49 | |
53 | |
50 | static loglevel llevel = L_NONE; |
54 | static loglevel llevel = L_NONE; |
51 | |
55 | |
52 | /* If nonzero, display usage information and exit. */ |
56 | /* If nonzero, display usage information and exit. */ |
53 | static int show_help; |
57 | static int show_help; |
54 | |
58 | |
55 | /* If nonzero, print the version on standard output and exit. */ |
59 | /* If nonzero, print the version on standard output and exit. */ |
56 | static int show_version; |
60 | static int show_version; |
57 | |
61 | |
58 | #if HAVE_MLOCKALL |
|
|
59 | /* If nonzero, disable swapping for this process. */ |
62 | /* If nonzero, disable swapping for this process. */ |
60 | static int do_mlock = 0; |
63 | static int do_mlock = 0; |
61 | #endif |
|
|
62 | |
64 | |
63 | /* If zero, don't detach from the terminal. */ |
65 | /* If zero, don't detach from the terminal. */ |
64 | static int do_detach = 1; |
66 | static int do_detach = 1; |
65 | |
67 | |
66 | static struct option const long_options[] = |
68 | static struct option const long_options[] = |
… | |
… | |
68 | {"config", required_argument, NULL, 'c'}, |
70 | {"config", required_argument, NULL, 'c'}, |
69 | {"help", no_argument, &show_help, 1}, |
71 | {"help", no_argument, &show_help, 1}, |
70 | {"version", no_argument, &show_version, 1}, |
72 | {"version", no_argument, &show_version, 1}, |
71 | {"no-detach", no_argument, &do_detach, 0}, |
73 | {"no-detach", no_argument, &do_detach, 0}, |
72 | {"log-level", required_argument, NULL, 'l'}, |
74 | {"log-level", required_argument, NULL, 'l'}, |
73 | #if HAVE_MLOCKALL |
|
|
74 | {"mlock", no_argument, &do_mlock, 1}, |
75 | {"mlock", no_argument, &do_mlock, 1}, |
75 | #endif |
|
|
76 | {NULL, 0, NULL, 0} |
76 | {NULL, 0, NULL, 0} |
77 | }; |
77 | }; |
78 | |
78 | |
79 | static void |
79 | static void |
80 | usage (int status) |
80 | usage (int status) |
… | |
… | |
120 | |
120 | |
121 | case 'D': /* no detach */ |
121 | case 'D': /* no detach */ |
122 | do_detach = 0; |
122 | do_detach = 0; |
123 | break; |
123 | break; |
124 | |
124 | |
125 | #if HAVE_MLOCKALL |
|
|
126 | case 'L': /* lock into memory */ |
125 | case 'L': /* lock into memory */ |
127 | do_mlock = 1; |
126 | do_mlock = 1; |
128 | break; |
127 | break; |
129 | #endif |
|
|
130 | |
128 | |
131 | case 'l': /* inc debug level */ |
129 | case 'l': /* inc debug level */ |
132 | { |
130 | { |
133 | llevel = string_to_loglevel (optarg); |
131 | llevel = string_to_loglevel (optarg); |
134 | |
132 | |
… | |
… | |
151 | */ |
149 | */ |
152 | void cleanup_and_exit(int c) |
150 | void cleanup_and_exit(int c) |
153 | { |
151 | { |
154 | network.shutdown_all (); |
152 | network.shutdown_all (); |
155 | |
153 | |
156 | if (pidfilename) |
154 | if (conf.pidfilename) |
157 | remove_pid (pidfilename); |
155 | remove_pid (conf.pidfilename); |
158 | |
156 | |
159 | slog (L_INFO, _("terminating with exit code %d"), c); |
157 | slog (L_INFO, _("terminating with exit code %d"), c); |
160 | |
158 | |
161 | exit (c); |
159 | exit (c); |
162 | } |
160 | } |
… | |
… | |
166 | */ |
164 | */ |
167 | RETSIGTYPE |
165 | RETSIGTYPE |
168 | sigterm_handler (int a) |
166 | sigterm_handler (int a) |
169 | { |
167 | { |
170 | network.events |= vpn::EVENT_SHUTDOWN; |
168 | network.events |= vpn::EVENT_SHUTDOWN; |
|
|
169 | network.event.start (0); |
171 | } |
170 | } |
172 | |
171 | |
173 | RETSIGTYPE |
172 | RETSIGTYPE |
174 | sighup_handler (int a) |
173 | sighup_handler (int a) |
175 | { |
174 | { |
176 | network.events |= vpn::EVENT_RECONNECT; |
175 | network.events |= vpn::EVENT_RECONNECT; |
|
|
176 | network.event.start (0); |
177 | } |
177 | } |
178 | |
178 | |
179 | RETSIGTYPE |
179 | RETSIGTYPE |
180 | sigusr1_handler (int a) |
180 | sigusr1_handler (int a) |
181 | { |
181 | { |
|
|
182 | network.dump_status (); |
182 | } |
183 | } |
183 | |
184 | |
184 | RETSIGTYPE |
185 | RETSIGTYPE |
185 | sigusr2_handler (int a) |
186 | sigusr2_handler (int a) |
186 | { |
187 | { |
… | |
… | |
196 | |
197 | |
197 | act.sa_handler = sighup_handler; sigaction (SIGHUP , &act, NULL); |
198 | act.sa_handler = sighup_handler; sigaction (SIGHUP , &act, NULL); |
198 | act.sa_handler = sigusr1_handler; sigaction (SIGUSR1, &act, NULL); |
199 | act.sa_handler = sigusr1_handler; sigaction (SIGUSR1, &act, NULL); |
199 | act.sa_handler = sigusr2_handler; sigaction (SIGUSR2, &act, NULL); |
200 | act.sa_handler = sigusr2_handler; sigaction (SIGUSR2, &act, NULL); |
200 | act.sa_handler = SIG_IGN; sigaction (SIGCHLD, &act, NULL); |
201 | act.sa_handler = SIG_IGN; sigaction (SIGCHLD, &act, NULL); |
|
|
202 | act.sa_handler = SIG_IGN; sigaction (SIGPIPE, &act, NULL); |
201 | act.sa_flags = SA_RESETHAND; |
203 | act.sa_flags = SA_RESETHAND; |
202 | act.sa_handler = sigterm_handler; sigaction (SIGINT , &act, NULL); |
204 | act.sa_handler = sigterm_handler; sigaction (SIGINT , &act, NULL); |
203 | act.sa_handler = sigterm_handler; sigaction (SIGTERM, &act, NULL); |
205 | act.sa_handler = sigterm_handler; sigaction (SIGTERM, &act, NULL); |
204 | } |
206 | } |
205 | |
207 | |
206 | int |
208 | int |
207 | main (int argc, char **argv, char **envp) |
209 | main (int argc, char **argv, char **envp) |
208 | { |
210 | { |
|
|
211 | ERR_load_crypto_strings (); // we have the RAM |
|
|
212 | |
209 | set_loglevel (L_INFO); |
213 | set_loglevel (L_INFO); |
210 | set_identity (argv[0]); |
214 | set_identity (argv[0]); |
211 | log_to (LOGTO_SYSLOG | LOGTO_STDERR); |
215 | log_to (LOGTO_SYSLOG | LOGTO_STDERR); |
212 | |
216 | |
213 | setlocale (LC_ALL, ""); |
217 | setlocale (LC_ALL, ""); |
… | |
… | |
216 | |
220 | |
217 | parse_options (argc, argv, envp); |
221 | parse_options (argc, argv, envp); |
218 | |
222 | |
219 | if (show_version) |
223 | if (show_version) |
220 | { |
224 | { |
221 | 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 (), |
222 | 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); |
223 | printf (_ |
228 | printf (_ |
224 | ("Copyright (C) 2003 Marc Lehmann <vpe@plan9.de> and others.\n" |
229 | ("Copyright (C) 2003 Marc Lehmann <vpe@plan9.de> and others.\n" |
225 | "See the AUTHORS file for a complete list.\n\n" |
230 | "See the AUTHORS file for a complete list.\n\n" |
226 | "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n" |
231 | "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n" |
227 | "and you are welcome to redistribute it under certain conditions;\n" |
232 | "and you are welcome to redistribute it under certain conditions;\n" |
… | |
… | |
235 | |
240 | |
236 | log_to (LOGTO_SYSLOG | LOGTO_STDERR); |
241 | log_to (LOGTO_SYSLOG | LOGTO_STDERR); |
237 | |
242 | |
238 | /* Lock all pages into memory if requested */ |
243 | /* Lock all pages into memory if requested */ |
239 | |
244 | |
240 | #if HAVE_MLOCKALL |
245 | #if HAVE_MLOCKALL && HAVE_SYS_MMAN_H && _POSIX_MEMLOCK |
241 | if (do_mlock) |
246 | if (do_mlock) |
242 | if (mlockall (MCL_CURRENT | MCL_FUTURE)) |
247 | if (mlockall (MCL_CURRENT | MCL_FUTURE)) |
243 | slog (L_ERR, _("system call `%s' failed: %s"), "mlockall", strerror (errno)); |
248 | slog (L_ERR, _("system call `%s' failed: %s"), "mlockall", strerror (errno)); |
244 | #endif |
249 | #endif |
245 | |
250 | |
|
|
251 | conf.read_config (true); |
246 | make_names (); |
252 | make_names (); |
247 | conf.read_config (true); |
|
|
248 | |
253 | |
249 | set_loglevel (llevel != L_NONE ? llevel : conf.llevel); |
254 | set_loglevel (llevel != L_NONE ? llevel : conf.llevel); |
250 | |
255 | |
251 | RAND_load_file ("/dev/urandom", 1024); |
256 | RAND_load_file ("/dev/urandom", 1024); |
252 | |
257 | |
253 | if (!thisnode) |
258 | if (!THISNODE) |
254 | { |
259 | { |
255 | slog (L_ERR, _("current node not set, or node '%s' not found in configfile, use the -n switch when starting vped."), |
260 | slog (L_ERR, _("current node not set, or node '%s' not found in configfile, specify the nodename when starting vped."), |
256 | thisnode ? thisnode : "<unset>"); |
261 | thisnode ? thisnode : "<unset>"); |
257 | exit (1); |
262 | exit (EXIT_FAILURE); |
258 | } |
263 | } |
259 | |
264 | |
260 | if (detach (do_detach)) |
265 | if (detach (do_detach)) |
261 | exit (0); |
266 | exit (EXIT_SUCCESS); |
262 | |
267 | |
263 | setup_signals (); |
268 | setup_signals (); |
264 | |
269 | |
265 | if (!network.setup ()) |
270 | if (!network.setup ()) |
266 | { |
271 | { |
267 | network.main_loop (); |
272 | iom.loop (); |
268 | cleanup_and_exit (1); |
273 | cleanup_and_exit (EXIT_FAILURE); |
269 | } |
274 | } |
270 | |
275 | |
271 | slog (L_ERR, _("unable to setup network, unrecoverable error, exiting.")); |
276 | slog (L_ERR, _("unable to setup network, unrecoverable error, exiting.")); |
272 | cleanup_and_exit (1); |
277 | cleanup_and_exit (EXIT_FAILURE); |
273 | } |
278 | } |
274 | |
279 | |