1 |
pcg |
1.1 |
/* |
2 |
|
|
vped.C -- the main file for vped |
3 |
|
|
Copyright (C) 1998-2002 Ivo Timmermans <ivo@o2w.nl> |
4 |
|
|
2000-2002 Guus Sliepen <guus@sliepen.eu.org> |
5 |
pcg |
1.11 |
2003 Marc Lehmann <pcg@goof.com> |
6 |
pcg |
1.1 |
|
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 |
9 |
|
|
the Free Software Foundation; either version 2 of the License, or |
10 |
|
|
(at your option) any later version. |
11 |
|
|
|
12 |
|
|
This program is distributed in the hope that it will be useful, |
13 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
|
|
GNU General Public License for more details. |
16 |
|
|
|
17 |
|
|
You should have received a copy of the GNU General Public License |
18 |
|
|
along with this program; if not, write to the Free Software |
19 |
|
|
Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
|
|
*/ |
21 |
|
|
|
22 |
|
|
#include "config.h" |
23 |
|
|
|
24 |
|
|
#include <cstdio> |
25 |
|
|
#include <cstring> |
26 |
pcg |
1.14 |
#include <clocale> |
27 |
pcg |
1.1 |
|
28 |
|
|
#include <errno.h> |
29 |
|
|
#include <fcntl.h> |
30 |
|
|
#include <getopt.h> |
31 |
|
|
#include <signal.h> |
32 |
|
|
#include <sys/types.h> |
33 |
|
|
#include <unistd.h> |
34 |
|
|
#include <signal.h> |
35 |
|
|
#include <termios.h> |
36 |
|
|
|
37 |
pcg |
1.9 |
#if HAVE_SYS_MMAN_H |
38 |
|
|
# include <sys/mman.h> |
39 |
|
|
#endif |
40 |
pcg |
1.1 |
|
41 |
pcg |
1.8 |
#include <openssl/err.h> |
42 |
pcg |
1.1 |
#include <openssl/rand.h> |
43 |
|
|
|
44 |
|
|
#include "gettext.h" |
45 |
|
|
#include "pidfile.h" |
46 |
|
|
|
47 |
|
|
#include "conf.h" |
48 |
|
|
#include "slog.h" |
49 |
|
|
#include "util.h" |
50 |
pcg |
1.5 |
#include "vpn.h" |
51 |
pcg |
1.3 |
#include "iom.h" |
52 |
pcg |
1.1 |
|
53 |
pcg |
1.2 |
static loglevel llevel = L_NONE; |
54 |
|
|
|
55 |
pcg |
1.1 |
/* If nonzero, display usage information and exit. */ |
56 |
|
|
static int show_help; |
57 |
|
|
|
58 |
|
|
/* If nonzero, print the version on standard output and exit. */ |
59 |
|
|
static int show_version; |
60 |
|
|
|
61 |
|
|
/* If nonzero, disable swapping for this process. */ |
62 |
|
|
static int do_mlock = 0; |
63 |
|
|
|
64 |
|
|
/* If zero, don't detach from the terminal. */ |
65 |
|
|
static int do_detach = 1; |
66 |
|
|
|
67 |
|
|
static struct option const long_options[] = |
68 |
|
|
{ |
69 |
|
|
{"config", required_argument, NULL, 'c'}, |
70 |
|
|
{"help", no_argument, &show_help, 1}, |
71 |
|
|
{"version", no_argument, &show_version, 1}, |
72 |
|
|
{"no-detach", no_argument, &do_detach, 0}, |
73 |
|
|
{"log-level", required_argument, NULL, 'l'}, |
74 |
|
|
{"mlock", no_argument, &do_mlock, 1}, |
75 |
|
|
{NULL, 0, NULL, 0} |
76 |
|
|
}; |
77 |
|
|
|
78 |
|
|
static void |
79 |
|
|
usage (int status) |
80 |
|
|
{ |
81 |
|
|
if (status != 0) |
82 |
|
|
fprintf (stderr, _("Try `%s --help\' for more information.\n"), get_identity ()); |
83 |
|
|
else |
84 |
|
|
{ |
85 |
|
|
printf (_("Usage: %s [option]... NODENAME\n\n"), get_identity ()); |
86 |
|
|
printf (_ |
87 |
|
|
(" -c, --config=DIR Read configuration options from DIR.\n" |
88 |
|
|
" -D, --no-detach Don't fork and detach.\n" |
89 |
|
|
" -l, --log-level=LEVEL Set logging level (info, notice, warn are common).\n" |
90 |
|
|
" -L, --mlock Lock tinc into main memory.\n" |
91 |
|
|
" --help Display this help and exit.\n" |
92 |
|
|
" --version Output version information and exit.\n\n")); |
93 |
|
|
printf (_("Report bugs to <vpe@plan9.de>.\n")); |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
exit (status); |
97 |
|
|
} |
98 |
|
|
|
99 |
|
|
void |
100 |
|
|
parse_options (int argc, char **argv, char **envp) |
101 |
|
|
{ |
102 |
|
|
int r; |
103 |
|
|
int option_index = 0; |
104 |
|
|
|
105 |
|
|
while ((r = getopt_long (argc, argv, "-c:DLl:", long_options, &option_index)) != EOF) |
106 |
|
|
{ |
107 |
|
|
switch (r) |
108 |
|
|
{ |
109 |
|
|
case 0: /* long option */ |
110 |
|
|
break; |
111 |
|
|
|
112 |
|
|
case 1: /* this node name */ |
113 |
|
|
thisnode = strdup (optarg); |
114 |
|
|
break; |
115 |
|
|
|
116 |
|
|
case 'c': /* config file */ |
117 |
|
|
confbase = strdup (optarg); |
118 |
|
|
break; |
119 |
|
|
|
120 |
|
|
case 'D': /* no detach */ |
121 |
|
|
do_detach = 0; |
122 |
|
|
break; |
123 |
|
|
|
124 |
|
|
case 'L': /* lock into memory */ |
125 |
|
|
do_mlock = 1; |
126 |
|
|
break; |
127 |
|
|
|
128 |
|
|
case 'l': /* inc debug level */ |
129 |
|
|
{ |
130 |
pcg |
1.2 |
llevel = string_to_loglevel (optarg); |
131 |
pcg |
1.1 |
|
132 |
pcg |
1.2 |
if (llevel == L_NONE) |
133 |
pcg |
1.1 |
slog (L_WARN, "'%s': %s", optarg, UNKNOWN_LOGLEVEL); |
134 |
|
|
} |
135 |
|
|
break; |
136 |
|
|
|
137 |
|
|
case '?': |
138 |
|
|
usage (1); |
139 |
|
|
|
140 |
|
|
default: |
141 |
|
|
break; |
142 |
|
|
} |
143 |
|
|
} |
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
/* |
147 |
|
|
Close network connections, and terminate neatly |
148 |
|
|
*/ |
149 |
|
|
void cleanup_and_exit(int c) |
150 |
|
|
{ |
151 |
|
|
network.shutdown_all (); |
152 |
|
|
|
153 |
|
|
if (pidfilename) |
154 |
|
|
remove_pid (pidfilename); |
155 |
|
|
|
156 |
|
|
slog (L_INFO, _("terminating with exit code %d"), c); |
157 |
|
|
|
158 |
|
|
exit (c); |
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
/* |
162 |
|
|
Signal handlers. |
163 |
|
|
*/ |
164 |
|
|
RETSIGTYPE |
165 |
|
|
sigterm_handler (int a) |
166 |
|
|
{ |
167 |
|
|
network.events |= vpn::EVENT_SHUTDOWN; |
168 |
pcg |
1.3 |
network.event.start (0); |
169 |
pcg |
1.1 |
} |
170 |
|
|
|
171 |
|
|
RETSIGTYPE |
172 |
|
|
sighup_handler (int a) |
173 |
|
|
{ |
174 |
|
|
network.events |= vpn::EVENT_RECONNECT; |
175 |
pcg |
1.3 |
network.event.start (0); |
176 |
pcg |
1.1 |
} |
177 |
|
|
|
178 |
|
|
RETSIGTYPE |
179 |
|
|
sigusr1_handler (int a) |
180 |
|
|
{ |
181 |
pcg |
1.4 |
network.dump_status (); |
182 |
pcg |
1.1 |
} |
183 |
|
|
|
184 |
|
|
RETSIGTYPE |
185 |
|
|
sigusr2_handler (int a) |
186 |
|
|
{ |
187 |
|
|
} |
188 |
|
|
|
189 |
|
|
void |
190 |
|
|
setup_signals (void) |
191 |
|
|
{ |
192 |
|
|
struct sigaction act; |
193 |
|
|
|
194 |
|
|
sigfillset (&act.sa_mask); |
195 |
|
|
act.sa_flags = 0; |
196 |
|
|
|
197 |
|
|
act.sa_handler = sighup_handler; sigaction (SIGHUP , &act, NULL); |
198 |
|
|
act.sa_handler = sigusr1_handler; sigaction (SIGUSR1, &act, NULL); |
199 |
|
|
act.sa_handler = sigusr2_handler; sigaction (SIGUSR2, &act, NULL); |
200 |
|
|
act.sa_handler = SIG_IGN; sigaction (SIGCHLD, &act, NULL); |
201 |
pcg |
1.6 |
act.sa_handler = SIG_IGN; sigaction (SIGPIPE, &act, NULL); |
202 |
pcg |
1.1 |
act.sa_flags = SA_RESETHAND; |
203 |
|
|
act.sa_handler = sigterm_handler; sigaction (SIGINT , &act, NULL); |
204 |
|
|
act.sa_handler = sigterm_handler; sigaction (SIGTERM, &act, NULL); |
205 |
|
|
} |
206 |
|
|
|
207 |
|
|
int |
208 |
|
|
main (int argc, char **argv, char **envp) |
209 |
|
|
{ |
210 |
pcg |
1.8 |
ERR_load_crypto_strings (); // we have the RAM |
211 |
|
|
|
212 |
pcg |
1.1 |
set_loglevel (L_INFO); |
213 |
|
|
set_identity (argv[0]); |
214 |
|
|
log_to (LOGTO_SYSLOG | LOGTO_STDERR); |
215 |
|
|
|
216 |
|
|
setlocale (LC_ALL, ""); |
217 |
|
|
bindtextdomain (PACKAGE, LOCALEDIR); |
218 |
|
|
textdomain (PACKAGE); |
219 |
|
|
|
220 |
|
|
parse_options (argc, argv, envp); |
221 |
|
|
|
222 |
|
|
if (show_version) |
223 |
|
|
{ |
224 |
pcg |
1.7 |
printf (_("%s version %s (built %s %s, protocol %d.%d)\n"), get_identity (), |
225 |
pcg |
1.1 |
VERSION, __DATE__, __TIME__, PROTOCOL_MAJOR, PROTOCOL_MINOR); |
226 |
pcg |
1.13 |
printf (_("Built with kernel interface %s/%s.\n"), IFTYPE, IFSUBTYPE); |
227 |
pcg |
1.1 |
printf (_ |
228 |
|
|
("Copyright (C) 2003 Marc Lehmann <vpe@plan9.de> and others.\n" |
229 |
|
|
"See the AUTHORS file for a complete list.\n\n" |
230 |
|
|
"tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n" |
231 |
|
|
"and you are welcome to redistribute it under certain conditions;\n" |
232 |
|
|
"see the file COPYING for details.\n")); |
233 |
|
|
|
234 |
|
|
return 0; |
235 |
|
|
} |
236 |
|
|
|
237 |
|
|
if (show_help) |
238 |
|
|
usage (0); |
239 |
|
|
|
240 |
|
|
log_to (LOGTO_SYSLOG | LOGTO_STDERR); |
241 |
|
|
|
242 |
|
|
/* Lock all pages into memory if requested */ |
243 |
|
|
|
244 |
pcg |
1.10 |
#if HAVE_MLOCKALL && HAVE_SYS_MMAN_H && _POSIX_MEMLOCK |
245 |
pcg |
1.1 |
if (do_mlock) |
246 |
|
|
if (mlockall (MCL_CURRENT | MCL_FUTURE)) |
247 |
|
|
slog (L_ERR, _("system call `%s' failed: %s"), "mlockall", strerror (errno)); |
248 |
|
|
#endif |
249 |
|
|
|
250 |
|
|
make_names (); |
251 |
|
|
conf.read_config (true); |
252 |
|
|
|
253 |
pcg |
1.2 |
set_loglevel (llevel != L_NONE ? llevel : conf.llevel); |
254 |
|
|
|
255 |
pcg |
1.1 |
RAND_load_file ("/dev/urandom", 1024); |
256 |
|
|
|
257 |
pcg |
1.3 |
if (!THISNODE) |
258 |
pcg |
1.1 |
{ |
259 |
|
|
slog (L_ERR, _("current node not set, or node '%s' not found in configfile, use the -n switch when starting vped."), |
260 |
|
|
thisnode ? thisnode : "<unset>"); |
261 |
|
|
exit (1); |
262 |
|
|
} |
263 |
|
|
|
264 |
|
|
if (detach (do_detach)) |
265 |
|
|
exit (0); |
266 |
|
|
|
267 |
|
|
setup_signals (); |
268 |
|
|
|
269 |
|
|
if (!network.setup ()) |
270 |
|
|
{ |
271 |
pcg |
1.3 |
iom.loop (); |
272 |
pcg |
1.1 |
cleanup_and_exit (1); |
273 |
|
|
} |
274 |
|
|
|
275 |
|
|
slog (L_ERR, _("unable to setup network, unrecoverable error, exiting.")); |
276 |
|
|
cleanup_and_exit (1); |
277 |
|
|
} |
278 |
|
|
|