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 |
|
|
2003 Marc Lehmannn <pcg@goof.com> |
6 |
|
|
|
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 |
|
|
|
27 |
|
|
#include <errno.h> |
28 |
|
|
#include <fcntl.h> |
29 |
|
|
#include <getopt.h> |
30 |
|
|
#include <signal.h> |
31 |
|
|
#include <sys/types.h> |
32 |
|
|
#include <unistd.h> |
33 |
|
|
#include <signal.h> |
34 |
|
|
#include <termios.h> |
35 |
|
|
|
36 |
|
|
#include <sys/mman.h> |
37 |
|
|
|
38 |
|
|
#include <openssl/rand.h> |
39 |
|
|
|
40 |
|
|
#include "gettext.h" |
41 |
|
|
#include "pidfile.h" |
42 |
|
|
|
43 |
|
|
#include "conf.h" |
44 |
|
|
#include "slog.h" |
45 |
|
|
#include "util.h" |
46 |
|
|
#include "protocol.h" |
47 |
|
|
|
48 |
|
|
vpn network; |
49 |
|
|
|
50 |
pcg |
1.2 |
static loglevel llevel = L_NONE; |
51 |
|
|
|
52 |
pcg |
1.1 |
/* If nonzero, display usage information and exit. */ |
53 |
|
|
static int show_help; |
54 |
|
|
|
55 |
|
|
/* If nonzero, print the version on standard output and exit. */ |
56 |
|
|
static int show_version; |
57 |
|
|
|
58 |
|
|
#if HAVE_MLOCKALL |
59 |
|
|
/* If nonzero, disable swapping for this process. */ |
60 |
|
|
static int do_mlock = 0; |
61 |
|
|
#endif |
62 |
|
|
|
63 |
|
|
/* If zero, don't detach from the terminal. */ |
64 |
|
|
static int do_detach = 1; |
65 |
|
|
|
66 |
|
|
static struct option const long_options[] = |
67 |
|
|
{ |
68 |
|
|
{"config", required_argument, NULL, 'c'}, |
69 |
|
|
{"help", no_argument, &show_help, 1}, |
70 |
|
|
{"version", no_argument, &show_version, 1}, |
71 |
|
|
{"no-detach", no_argument, &do_detach, 0}, |
72 |
|
|
{"log-level", required_argument, NULL, 'l'}, |
73 |
|
|
#if HAVE_MLOCKALL |
74 |
|
|
{"mlock", no_argument, &do_mlock, 1}, |
75 |
|
|
#endif |
76 |
|
|
{NULL, 0, NULL, 0} |
77 |
|
|
}; |
78 |
|
|
|
79 |
|
|
static void |
80 |
|
|
usage (int status) |
81 |
|
|
{ |
82 |
|
|
if (status != 0) |
83 |
|
|
fprintf (stderr, _("Try `%s --help\' for more information.\n"), get_identity ()); |
84 |
|
|
else |
85 |
|
|
{ |
86 |
|
|
printf (_("Usage: %s [option]... NODENAME\n\n"), get_identity ()); |
87 |
|
|
printf (_ |
88 |
|
|
(" -c, --config=DIR Read configuration options from DIR.\n" |
89 |
|
|
" -D, --no-detach Don't fork and detach.\n" |
90 |
|
|
" -l, --log-level=LEVEL Set logging level (info, notice, warn are common).\n" |
91 |
|
|
" -L, --mlock Lock tinc into main memory.\n" |
92 |
|
|
" --help Display this help and exit.\n" |
93 |
|
|
" --version Output version information and exit.\n\n")); |
94 |
|
|
printf (_("Report bugs to <vpe@plan9.de>.\n")); |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
exit (status); |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
void |
101 |
|
|
parse_options (int argc, char **argv, char **envp) |
102 |
|
|
{ |
103 |
|
|
int r; |
104 |
|
|
int option_index = 0; |
105 |
|
|
|
106 |
|
|
while ((r = getopt_long (argc, argv, "-c:DLl:", long_options, &option_index)) != EOF) |
107 |
|
|
{ |
108 |
|
|
switch (r) |
109 |
|
|
{ |
110 |
|
|
case 0: /* long option */ |
111 |
|
|
break; |
112 |
|
|
|
113 |
|
|
case 1: /* this node name */ |
114 |
|
|
thisnode = strdup (optarg); |
115 |
|
|
break; |
116 |
|
|
|
117 |
|
|
case 'c': /* config file */ |
118 |
|
|
confbase = strdup (optarg); |
119 |
|
|
break; |
120 |
|
|
|
121 |
|
|
case 'D': /* no detach */ |
122 |
|
|
do_detach = 0; |
123 |
|
|
break; |
124 |
|
|
|
125 |
|
|
#if HAVE_MLOCKALL |
126 |
|
|
case 'L': /* lock into memory */ |
127 |
|
|
do_mlock = 1; |
128 |
|
|
break; |
129 |
|
|
#endif |
130 |
|
|
|
131 |
|
|
case 'l': /* inc debug level */ |
132 |
|
|
{ |
133 |
pcg |
1.2 |
llevel = string_to_loglevel (optarg); |
134 |
pcg |
1.1 |
|
135 |
pcg |
1.2 |
if (llevel == L_NONE) |
136 |
pcg |
1.1 |
slog (L_WARN, "'%s': %s", optarg, UNKNOWN_LOGLEVEL); |
137 |
|
|
} |
138 |
|
|
break; |
139 |
|
|
|
140 |
|
|
case '?': |
141 |
|
|
usage (1); |
142 |
|
|
|
143 |
|
|
default: |
144 |
|
|
break; |
145 |
|
|
} |
146 |
|
|
} |
147 |
|
|
} |
148 |
|
|
|
149 |
|
|
/* |
150 |
|
|
Close network connections, and terminate neatly |
151 |
|
|
*/ |
152 |
|
|
void cleanup_and_exit(int c) |
153 |
|
|
{ |
154 |
|
|
network.shutdown_all (); |
155 |
|
|
|
156 |
|
|
if (pidfilename) |
157 |
|
|
remove_pid (pidfilename); |
158 |
|
|
|
159 |
|
|
slog (L_INFO, _("terminating with exit code %d"), c); |
160 |
|
|
|
161 |
|
|
exit (c); |
162 |
|
|
} |
163 |
|
|
|
164 |
|
|
/* |
165 |
|
|
Signal handlers. |
166 |
|
|
*/ |
167 |
|
|
RETSIGTYPE |
168 |
|
|
sigterm_handler (int a) |
169 |
|
|
{ |
170 |
|
|
network.events |= vpn::EVENT_SHUTDOWN; |
171 |
|
|
} |
172 |
|
|
|
173 |
|
|
RETSIGTYPE |
174 |
|
|
sighup_handler (int a) |
175 |
|
|
{ |
176 |
|
|
network.events |= vpn::EVENT_RECONNECT; |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
RETSIGTYPE |
180 |
|
|
sigusr1_handler (int a) |
181 |
|
|
{ |
182 |
|
|
} |
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 |
|
|
act.sa_flags = SA_RESETHAND; |
202 |
|
|
act.sa_handler = sigterm_handler; sigaction (SIGINT , &act, NULL); |
203 |
|
|
act.sa_handler = sigterm_handler; sigaction (SIGTERM, &act, NULL); |
204 |
|
|
} |
205 |
|
|
|
206 |
|
|
int |
207 |
|
|
main (int argc, char **argv, char **envp) |
208 |
|
|
{ |
209 |
|
|
set_loglevel (L_INFO); |
210 |
|
|
set_identity (argv[0]); |
211 |
|
|
log_to (LOGTO_SYSLOG | LOGTO_STDERR); |
212 |
|
|
|
213 |
|
|
setlocale (LC_ALL, ""); |
214 |
|
|
bindtextdomain (PACKAGE, LOCALEDIR); |
215 |
|
|
textdomain (PACKAGE); |
216 |
|
|
|
217 |
|
|
parse_options (argc, argv, envp); |
218 |
|
|
|
219 |
|
|
if (show_version) |
220 |
|
|
{ |
221 |
|
|
printf (_("%s version %s (built %s %s, protocol %d:%d)\n"), get_identity (), |
222 |
|
|
VERSION, __DATE__, __TIME__, PROTOCOL_MAJOR, PROTOCOL_MINOR); |
223 |
|
|
printf (_ |
224 |
|
|
("Copyright (C) 2003 Marc Lehmann <vpe@plan9.de> and others.\n" |
225 |
|
|
"See the AUTHORS file for a complete list.\n\n" |
226 |
|
|
"tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n" |
227 |
|
|
"and you are welcome to redistribute it under certain conditions;\n" |
228 |
|
|
"see the file COPYING for details.\n")); |
229 |
|
|
|
230 |
|
|
return 0; |
231 |
|
|
} |
232 |
|
|
|
233 |
|
|
if (show_help) |
234 |
|
|
usage (0); |
235 |
|
|
|
236 |
|
|
log_to (LOGTO_SYSLOG | LOGTO_STDERR); |
237 |
|
|
|
238 |
|
|
/* Lock all pages into memory if requested */ |
239 |
|
|
|
240 |
|
|
#if HAVE_MLOCKALL |
241 |
|
|
if (do_mlock) |
242 |
|
|
if (mlockall (MCL_CURRENT | MCL_FUTURE)) |
243 |
|
|
slog (L_ERR, _("system call `%s' failed: %s"), "mlockall", strerror (errno)); |
244 |
|
|
#endif |
245 |
|
|
|
246 |
|
|
make_names (); |
247 |
|
|
conf.read_config (true); |
248 |
|
|
|
249 |
pcg |
1.2 |
set_loglevel (llevel != L_NONE ? llevel : conf.llevel); |
250 |
|
|
|
251 |
pcg |
1.1 |
RAND_load_file ("/dev/urandom", 1024); |
252 |
|
|
|
253 |
pcg |
1.2 |
if (!thisnode) |
254 |
pcg |
1.1 |
{ |
255 |
|
|
slog (L_ERR, _("current node not set, or node '%s' not found in configfile, use the -n switch when starting vped."), |
256 |
|
|
thisnode ? thisnode : "<unset>"); |
257 |
|
|
exit (1); |
258 |
|
|
} |
259 |
|
|
|
260 |
|
|
if (detach (do_detach)) |
261 |
|
|
exit (0); |
262 |
|
|
|
263 |
|
|
setup_signals (); |
264 |
|
|
|
265 |
|
|
if (!network.setup ()) |
266 |
|
|
{ |
267 |
|
|
network.main_loop (); |
268 |
|
|
cleanup_and_exit (1); |
269 |
|
|
} |
270 |
|
|
|
271 |
|
|
slog (L_ERR, _("unable to setup network, unrecoverable error, exiting.")); |
272 |
|
|
cleanup_and_exit (1); |
273 |
|
|
} |
274 |
|
|
|