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>
30 #include "connection.h"
43 bool do_purge = false;
44 volatile bool running = false;
48 /* Purge edges and subnets of unreachable nodes. Use carefully. */
50 static void purge(void)
52 avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
59 ifdebug(PROTOCOL) logger(LOG_DEBUG, _("Purging unreachable nodes"));
61 /* Remove all edges and subnets owned by unreachable nodes. */
63 for(nnode = node_tree->head; nnode; nnode = nnext) {
67 if(!n->status.reachable) {
68 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Purging node %s (%s)"), n->name,
71 for(snode = n->subnet_tree->head; snode; snode = snext) {
75 send_del_subnet(broadcast, s);
79 for(enode = n->edge_tree->head; enode; enode = enext) {
83 send_del_edge(broadcast, e);
89 /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
91 for(nnode = node_tree->head; nnode; nnode = nnext) {
95 if(!n->status.reachable) {
96 for(enode = edge_weight_tree->head; enode; enode = enext) {
111 put all file descriptors into events
112 While we're at it, purge stuf that needs to be removed.
114 static int build_fdset(void)
116 avl_node_t *node, *next;
122 for(node = connection_tree->head; node; node = next) {
126 if(c->status.remove) {
128 if(!connection_tree->head)
137 Terminate a connection:
139 - Remove associated edge and tell other connections about it if report = true
140 - Check if we need to retry making an outgoing connection
141 - Deactivate the host
143 void terminate_connection(connection_t *c, bool report)
150 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Closing connection with %s (%s)"),
151 c->name, c->hostname);
153 c->status.remove = true;
154 c->status.active = false;
157 c->node->connection = NULL;
160 closesocket(c->socket);
165 if(report && !tunnelserver)
166 send_del_edge(broadcast, c->edge);
170 /* Run MST and SSSP algorithms */
174 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
176 if(report && !c->node->status.reachable) {
178 e = lookup_edge(c->node, myself);
181 send_del_edge(broadcast, e);
187 /* Check if this was our outgoing connection */
190 retry_outgoing(c->outgoing);
202 Check if the other end is active.
203 If we have sent packets, but didn't receive any,
204 then possibly the other end is dead. We send a
205 PING request over the meta connection. If the other
206 end does not reply in time, we consider them dead
207 and close the connection.
209 static void check_dead_connections(void)
211 avl_node_t *node, *next;
216 for(node = connection_tree->head; node; node = next) {
220 if(c->last_ping_time + pingtimeout < now) {
221 if(c->status.active) {
222 if(c->status.pinged) {
223 ifdebug(CONNECTIONS) logger(LOG_INFO, _("%s (%s) didn't respond to PING in %ld seconds"),
224 c->name, c->hostname, now - c->last_ping_time);
225 c->status.timeout = true;
226 terminate_connection(c, true);
227 } else if(c->last_ping_time + pinginterval < now) {
231 if(c->status.remove) {
232 logger(LOG_WARNING, _("Old connection_t for %s (%s) status %04x still lingering, deleting..."),
233 c->name, c->hostname, c->status.value);
237 ifdebug(CONNECTIONS) logger(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
238 c->name, c->hostname);
239 if(c->status.connecting) {
240 c->status.connecting = false;
241 closesocket(c->socket);
242 do_outgoing_connection(c);
244 terminate_connection(c, false);
249 if(c->outbuflen > 0 && c->last_flushed_time + pingtimeout < now) {
250 if(c->status.active) {
251 ifdebug(CONNECTIONS) logger(LOG_INFO,
252 _("%s (%s) could not flush for %ld seconds (%d bytes remaining)"),
253 c->name, c->hostname, now - c->last_flushed_time, c->outbuflen);
254 c->status.timeout = true;
255 terminate_connection(c, true);
261 void handle_meta_connection_data(int fd, short events, void *data)
263 connection_t *c = data;
265 socklen_t len = sizeof(result);
267 if (c->status.remove)
270 if(c->status.connecting) {
271 c->status.connecting = false;
272 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
275 finish_connecting(c);
277 ifdebug(CONNECTIONS) logger(LOG_DEBUG,
278 _("Error while connecting to %s (%s): %s"),
279 c->name, c->hostname, strerror(result));
280 closesocket(c->socket);
281 do_outgoing_connection(c);
286 if (!receive_meta(c)) {
287 terminate_connection(c, c->status.active);
292 static void dummy(int a, short b, void *c)
296 void sighup_handler(int signal, short events, void *data) {
301 static time_t last_config_check = 0;
303 /* Reread our own configuration file */
305 exit_configuration(&config_tree);
306 init_configuration(&config_tree);
308 if(!read_server_config()) {
309 logger(LOG_ERR, _("Unable to reread configuration file, exitting."));
310 event_loopexit(NULL);
314 /* Close connections to hosts that have a changed or deleted host config file */
316 for(node = connection_tree->head; node; node = node->next) {
320 free(c->outgoing->name);
322 freeaddrinfo(c->outgoing->ai);
327 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
328 if(stat(fname, &s) || s.st_mtime > last_config_check)
329 terminate_connection(c, c->status.active);
333 last_config_check = time(NULL);
335 /* Try to make outgoing connections */
337 try_outgoing_connections();
341 this is where it all happens...
347 time_t last_ping_check;
348 struct event timeout;
349 struct event sighup_event;
353 signal_set(&sighup_event, SIGHUP, sighup_handler, NULL);
354 signal_add(&sighup_event, NULL);
356 last_ping_check = now;
365 // tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
369 /* XXX: libevent transition: old timeout code in this loop */
370 timeout_set(&timeout, dummy, NULL);
371 timeout_add(&timeout, &tv);
375 logger(LOG_ERR, _("Error building fdset: %s"), strerror(errno));
381 r = event_loop(EVLOOP_ONCE);
384 logger(LOG_ERR, _("Error while waiting for input: %s"),
391 /* XXX: more libevent transition */
392 timeout_del(&timeout);
399 /* Let's check if everybody is still alive */
401 if(last_ping_check + pingtimeout < now) {
402 check_dead_connections();
403 last_ping_check = now;
405 if(routing_mode == RMODE_SWITCH)
410 /* Should we regenerate our key? */
412 if(keyexpires < now) {
413 ifdebug(STATUS) logger(LOG_INFO, _("Regenerating symmetric key"));
415 RAND_pseudo_bytes((unsigned char *)myself->key, myself->keylength);
417 EVP_DecryptInit_ex(&packet_ctx, myself->cipher, NULL, (unsigned char *)myself->key, (unsigned char *)myself->key + myself->cipher->key_len);
418 send_key_changed(broadcast, myself);
419 keyexpires = now + keylifetime;
424 logger(LOG_INFO, _("Flushing event queue"));
425 // TODO: do this another way
430 signal_del(&sighup_event);