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)
298 this is where it all happens...
304 time_t last_ping_check, last_config_check;
306 struct event timeout;
310 last_ping_check = now;
311 last_config_check = now;
320 // tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
324 /* XXX: libevent transition: old timeout code in this loop */
325 timeout_set(&timeout, dummy, NULL);
326 timeout_add(&timeout, &tv);
330 logger(LOG_ERR, _("Error building fdset: %s"), strerror(errno));
336 r = event_loop(EVLOOP_ONCE);
339 logger(LOG_ERR, _("Error while waiting for input: %s"),
346 /* XXX: more libevent transition */
347 timeout_del(&timeout);
354 /* Let's check if everybody is still alive */
356 if(last_ping_check + pingtimeout < now) {
357 check_dead_connections();
358 last_ping_check = now;
360 if(routing_mode == RMODE_SWITCH)
365 /* Should we regenerate our key? */
367 if(keyexpires < now) {
368 ifdebug(STATUS) logger(LOG_INFO, _("Regenerating symmetric key"));
370 RAND_pseudo_bytes((unsigned char *)myself->key, myself->keylength);
372 EVP_DecryptInit_ex(&packet_ctx, myself->cipher, NULL, (unsigned char *)myself->key, (unsigned char *)myself->key + myself->cipher->key_len);
373 send_key_changed(broadcast, myself);
374 keyexpires = now + keylifetime;
379 while((event = get_expired_tevent())) {
380 event->handler(event->data);
385 logger(LOG_INFO, _("Flushing event queue"));
398 /* Reread our own configuration file */
400 exit_configuration(&config_tree);
401 init_configuration(&config_tree);
403 if(!read_server_config()) {
404 logger(LOG_ERR, _("Unable to reread configuration file, exitting."));
408 /* Close connections to hosts that have a changed or deleted host config file */
410 for(node = connection_tree->head; node; node = node->next) {
414 free(c->outgoing->name);
416 freeaddrinfo(c->outgoing->ai);
421 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
422 if(stat(fname, &s) || s.st_mtime > last_config_check)
423 terminate_connection(c, c->status.active);
427 last_config_check = now;
429 /* Try to make outgoing connections */
431 try_outgoing_connections();