2 net.c -- most of the network code
3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2006 Guus Sliepen <guus@tinc-vpn.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <openssl/rand.h>
28 #include "splay_tree.h"
30 #include "connection.h"
42 /* Purge edges and subnets of unreachable nodes. Use carefully. */
44 static void purge(void) {
45 splay_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
52 ifdebug(PROTOCOL) logger(LOG_DEBUG, _("Purging unreachable nodes"));
54 /* Remove all edges and subnets owned by unreachable nodes. */
56 for(nnode = node_tree->head; nnode; nnode = nnext) {
60 if(!n->status.reachable) {
61 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Purging node %s (%s)"), n->name,
64 for(snode = n->subnet_tree->head; snode; snode = snext) {
68 send_del_subnet(broadcast, s);
72 for(enode = n->edge_tree->head; enode; enode = enext) {
76 send_del_edge(broadcast, e);
82 /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
84 for(nnode = node_tree->head; nnode; nnode = nnext) {
88 if(!n->status.reachable) {
89 for(enode = edge_weight_tree->head; enode; enode = enext) {
104 Terminate a connection:
106 - Remove associated edge and tell other connections about it if report = true
107 - Check if we need to retry making an outgoing connection
108 - Deactivate the host
110 void terminate_connection(connection_t *c, bool report) {
113 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Closing connection with %s (%s)"),
114 c->name, c->hostname);
116 c->status.active = false;
119 c->node->connection = NULL;
122 closesocket(c->socket);
127 if(report && !tunnelserver)
128 send_del_edge(broadcast, c->edge);
132 /* Run MST and SSSP algorithms */
136 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
138 if(report && !c->node->status.reachable) {
140 e = lookup_edge(c->node, myself);
143 send_del_edge(broadcast, e);
155 /* Check if this was our outgoing connection */
158 retry_outgoing(c->outgoing);
166 Check if the other end is active.
167 If we have sent packets, but didn't receive any,
168 then possibly the other end is dead. We send a
169 PING request over the meta connection. If the other
170 end does not reply in time, we consider them dead
171 and close the connection.
173 static void timeout_handler(int fd, short events, void *event) {
174 splay_node_t *node, *next;
176 time_t now = time(NULL);
180 for(node = connection_tree->head; node; node = next) {
184 if(c->last_ping_time + pingtimeout < now) {
185 if(c->status.active) {
186 if(c->status.pinged) {
187 ifdebug(CONNECTIONS) logger(LOG_INFO, _("%s (%s) didn't respond to PING in %ld seconds"),
188 c->name, c->hostname, now - c->last_ping_time);
189 c->status.timeout = true;
190 terminate_connection(c, true);
192 } else if(c->last_ping_time + pinginterval < now) {
196 ifdebug(CONNECTIONS) logger(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
197 c->name, c->hostname);
198 if(c->status.connecting) {
199 c->status.connecting = false;
200 closesocket(c->socket);
201 do_outgoing_connection(c);
203 terminate_connection(c, false);
210 event_add(event, &(struct timeval){pingtimeout, 0});
213 void handle_meta_connection_data(int fd, short events, void *data) {
214 connection_t *c = data;
216 socklen_t len = sizeof(result);
218 if(c->status.connecting) {
219 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
222 finish_connecting(c);
224 ifdebug(CONNECTIONS) logger(LOG_DEBUG,
225 _("Error while connecting to %s (%s): %s"),
226 c->name, c->hostname, strerror(result));
227 c->status.connecting = false;
228 closesocket(c->socket);
229 do_outgoing_connection(c);
234 if (!receive_meta(c)) {
235 terminate_connection(c, c->status.active);
240 static void sigterm_handler(int signal, short events, void *data) {
241 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
242 event_loopexit(NULL);
245 static void sigint_handler(int signal, short events, void *data) {
246 static int saved_debug_level = -1;
248 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
250 if(saved_debug_level != -1) {
251 logger(LOG_NOTICE, _("Reverting to old debug level (%d)"),
253 debug_level = saved_debug_level;
254 saved_debug_level = -1;
257 _("Temporarily setting debug level to 5. Kill me with SIGINT again to go back to level %d."),
259 saved_debug_level = debug_level;
264 static void sigusr1_handler(int signal, short events, void *data) {
265 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
269 static void sigusr2_handler(int signal, short events, void *data) {
270 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
277 static void sigwinch_handler(int signal, short events, void *data) {
278 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
282 static void sighup_handler(int signal, short events, void *data) {
287 static time_t last_config_check = 0;
289 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
291 /* Reread our own configuration file */
293 exit_configuration(&config_tree);
294 init_configuration(&config_tree);
296 if(!read_server_config()) {
297 logger(LOG_ERR, _("Unable to reread configuration file, exitting."));
298 event_loopexit(NULL);
302 /* Close connections to hosts that have a changed or deleted host config file */
304 for(node = connection_tree->head; node; node = node->next) {
308 free(c->outgoing->name);
310 freeaddrinfo(c->outgoing->ai);
315 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
316 if(stat(fname, &s) || s.st_mtime > last_config_check)
317 terminate_connection(c, c->status.active);
321 last_config_check = time(NULL);
323 /* Try to make outgoing connections */
325 try_outgoing_connections();
328 static void sigalrm_handler(int signal, short events, void *data) {
329 logger(LOG_NOTICE, _("Got %s signal"), strsignal(signal));
334 for(node = connection_tree->head; node; node = node->next) {
337 if(c->outgoing && !c->node) {
338 if(timeout_initialized(&c->outgoing->ev))
339 event_del(&c->outgoing->ev);
340 if(c->status.connecting)
342 c->outgoing->timeout = 0;
343 do_outgoing_connection(c);
349 this is where it all happens...
351 int main_loop(void) {
352 struct event timeout_event;
353 struct event sighup_event;
354 struct event sigint_event;
355 struct event sigterm_event;
356 struct event sigquit_event;
357 struct event sigusr1_event;
358 struct event sigusr2_event;
359 struct event sigwinch_event;
360 struct event sigalrm_event;
364 timeout_set(&timeout_event, timeout_handler, &timeout_event);
365 event_add(&timeout_event, &(struct timeval){pingtimeout, 0});
366 signal_set(&sighup_event, SIGHUP, sighup_handler, NULL);
367 signal_add(&sighup_event, NULL);
368 signal_set(&sigint_event, SIGINT, sigint_handler, NULL);
369 signal_add(&sigint_event, NULL);
370 signal_set(&sigterm_event, SIGTERM, sigterm_handler, NULL);
371 signal_add(&sigterm_event, NULL);
372 signal_set(&sigquit_event, SIGQUIT, sigterm_handler, NULL);
373 signal_add(&sigquit_event, NULL);
374 signal_set(&sigusr1_event, SIGUSR1, sigusr1_handler, NULL);
375 signal_add(&sigusr1_event, NULL);
376 signal_set(&sigusr2_event, SIGUSR2, sigusr2_handler, NULL);
377 signal_add(&sigusr2_event, NULL);
378 signal_set(&sigwinch_event, SIGWINCH, sigwinch_handler, NULL);
379 signal_add(&sigwinch_event, NULL);
380 signal_set(&sigalrm_event, SIGALRM, sigalrm_handler, NULL);
381 signal_add(&sigalrm_event, NULL);
383 if(event_loop(0) < 0) {
384 logger(LOG_ERR, _("Error while waiting for input: %s"), strerror(errno));
388 signal_del(&sighup_event);
389 signal_del(&sigint_event);
390 signal_del(&sigterm_event);
391 signal_del(&sigquit_event);
392 signal_del(&sigusr1_event);
393 signal_del(&sigusr2_event);
394 signal_del(&sigwinch_event);
395 signal_del(&sigalrm_event);
396 event_del(&timeout_event);