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"
44 bool do_purge = false;
45 volatile bool running = false;
49 /* Purge edges and subnets of unreachable nodes. Use carefully. */
51 static void purge(void)
53 avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
60 ifdebug(PROTOCOL) logger(LOG_DEBUG, _("Purging unreachable nodes"));
62 /* Remove all edges and subnets owned by unreachable nodes. */
64 for(nnode = node_tree->head; nnode; nnode = nnext) {
68 if(!n->status.reachable) {
69 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Purging node %s (%s)"), n->name,
72 for(snode = n->subnet_tree->head; snode; snode = snext) {
76 send_del_subnet(broadcast, s);
80 for(enode = n->edge_tree->head; enode; enode = enext) {
84 send_del_edge(broadcast, e);
90 /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
92 for(nnode = node_tree->head; nnode; nnode = nnext) {
96 if(!n->status.reachable) {
97 for(enode = edge_weight_tree->head; enode; enode = enext) {
112 put all file descriptors into events
113 While we're at it, purge stuf that needs to be removed.
115 static int build_fdset(void)
117 avl_node_t *node, *next;
123 for(node = connection_tree->head; node; node = next) {
127 if(c->status.remove) {
129 if(!connection_tree->head)
138 Terminate a connection:
140 - Remove associated edge and tell other connections about it if report = true
141 - Check if we need to retry making an outgoing connection
142 - Deactivate the host
144 void terminate_connection(connection_t *c, bool report)
151 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Closing connection with %s (%s)"),
152 c->name, c->hostname);
154 c->status.remove = true;
155 c->status.active = false;
158 c->node->connection = NULL;
161 closesocket(c->socket);
166 if(report && !tunnelserver)
167 send_del_edge(broadcast, c->edge);
171 /* Run MST and SSSP algorithms */
175 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
177 if(report && !c->node->status.reachable) {
179 e = lookup_edge(c->node, myself);
182 send_del_edge(broadcast, e);
188 /* Check if this was our outgoing connection */
191 retry_outgoing(c->outgoing);
203 Check if the other end is active.
204 If we have sent packets, but didn't receive any,
205 then possibly the other end is dead. We send a
206 PING request over the meta connection. If the other
207 end does not reply in time, we consider them dead
208 and close the connection.
210 static void check_dead_connections(void)
212 avl_node_t *node, *next;
217 for(node = connection_tree->head; node; node = next) {
221 if(c->last_ping_time + pingtimeout < now) {
222 if(c->status.active) {
223 if(c->status.pinged) {
224 ifdebug(CONNECTIONS) logger(LOG_INFO, _("%s (%s) didn't respond to PING in %ld seconds"),
225 c->name, c->hostname, now - c->last_ping_time);
226 c->status.timeout = true;
227 terminate_connection(c, true);
228 } else if(c->last_ping_time + pinginterval < now) {
232 if(c->status.remove) {
233 logger(LOG_WARNING, _("Old connection_t for %s (%s) status %04x still lingering, deleting..."),
234 c->name, c->hostname, c->status.value);
238 ifdebug(CONNECTIONS) logger(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
239 c->name, c->hostname);
240 if(c->status.connecting) {
241 c->status.connecting = false;
242 closesocket(c->socket);
243 do_outgoing_connection(c);
245 terminate_connection(c, false);
250 if(c->outbuflen > 0 && c->last_flushed_time + pingtimeout < now) {
251 if(c->status.active) {
252 ifdebug(CONNECTIONS) logger(LOG_INFO,
253 _("%s (%s) could not flush for %ld seconds (%d bytes remaining)"),
254 c->name, c->hostname, now - c->last_flushed_time, c->outbuflen);
255 c->status.timeout = true;
256 terminate_connection(c, true);
262 void handle_meta_connection_data(int fd, short events, void *data)
264 connection_t *c = data;
266 socklen_t len = sizeof(result);
268 if (c->status.remove)
271 if(c->status.connecting) {
272 c->status.connecting = false;
273 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
276 finish_connecting(c);
278 ifdebug(CONNECTIONS) logger(LOG_DEBUG,
279 _("Error while connecting to %s (%s): %s"),
280 c->name, c->hostname, strerror(result));
281 closesocket(c->socket);
282 do_outgoing_connection(c);
287 if (!receive_meta(c)) {
288 terminate_connection(c, c->status.active);
293 static void dummy(int a, short b, void *c)
297 void sighup_handler(int signal, short events, void *data) {
302 static time_t last_config_check = 0;
304 /* Reread our own configuration file */
306 exit_configuration(&config_tree);
307 init_configuration(&config_tree);
309 if(!read_server_config()) {
310 logger(LOG_ERR, _("Unable to reread configuration file, exitting."));
311 event_loopexit(NULL);
315 /* Close connections to hosts that have a changed or deleted host config file */
317 for(node = connection_tree->head; node; node = node->next) {
321 free(c->outgoing->name);
323 freeaddrinfo(c->outgoing->ai);
328 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
329 if(stat(fname, &s) || s.st_mtime > last_config_check)
330 terminate_connection(c, c->status.active);
334 last_config_check = time(NULL);
336 /* Try to make outgoing connections */
338 try_outgoing_connections();
342 this is where it all happens...
348 time_t last_ping_check;
350 struct event timeout;
351 struct event sighup_event;
355 signal_set(&sighup_event, SIGHUP, sighup_handler, NULL);
356 signal_add(&sighup_event, NULL);
358 last_ping_check = now;
367 // tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
371 /* XXX: libevent transition: old timeout code in this loop */
372 timeout_set(&timeout, dummy, NULL);
373 timeout_add(&timeout, &tv);
377 logger(LOG_ERR, _("Error building fdset: %s"), strerror(errno));
383 r = event_loop(EVLOOP_ONCE);
386 logger(LOG_ERR, _("Error while waiting for input: %s"),
393 /* XXX: more libevent transition */
394 timeout_del(&timeout);
401 /* Let's check if everybody is still alive */
403 if(last_ping_check + pingtimeout < now) {
404 check_dead_connections();
405 last_ping_check = now;
407 if(routing_mode == RMODE_SWITCH)
412 /* Should we regenerate our key? */
414 if(keyexpires < now) {
415 ifdebug(STATUS) logger(LOG_INFO, _("Regenerating symmetric key"));
417 RAND_pseudo_bytes((unsigned char *)myself->key, myself->keylength);
419 EVP_DecryptInit_ex(&packet_ctx, myself->cipher, NULL, (unsigned char *)myself->key, (unsigned char *)myself->key + myself->cipher->key_len);
420 send_key_changed(broadcast, myself);
421 keyexpires = now + keylifetime;
426 while((event = get_expired_tevent())) {
427 event->handler(event->data);
432 logger(LOG_INFO, _("Flushing event queue"));
439 signal_del(&sighup_event);