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)
132 short events = EV_READ;
136 event_set(&c->ev, c->socket, events,
137 handle_meta_connection_data, c);
138 if (event_add(&c->ev, NULL) < 0)
146 Terminate a connection:
148 - Remove associated edge and tell other connections about it if report = true
149 - Check if we need to retry making an outgoing connection
150 - Deactivate the host
152 void terminate_connection(connection_t *c, bool report)
159 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Closing connection with %s (%s)"),
160 c->name, c->hostname);
162 c->status.remove = true;
163 c->status.active = false;
166 c->node->connection = NULL;
169 closesocket(c->socket);
172 if(report && !tunnelserver)
173 send_del_edge(broadcast, c->edge);
177 /* Run MST and SSSP algorithms */
181 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
183 if(report && !c->node->status.reachable) {
185 e = lookup_edge(c->node, myself);
188 send_del_edge(broadcast, e);
194 /* Check if this was our outgoing connection */
197 retry_outgoing(c->outgoing);
209 Check if the other end is active.
210 If we have sent packets, but didn't receive any,
211 then possibly the other end is dead. We send a
212 PING request over the meta connection. If the other
213 end does not reply in time, we consider them dead
214 and close the connection.
216 static void check_dead_connections(void)
218 avl_node_t *node, *next;
223 for(node = connection_tree->head; node; node = next) {
227 if(c->last_ping_time + pingtimeout < now) {
228 if(c->status.active) {
229 if(c->status.pinged) {
230 ifdebug(CONNECTIONS) logger(LOG_INFO, _("%s (%s) didn't respond to PING in %ld seconds"),
231 c->name, c->hostname, now - c->last_ping_time);
232 c->status.timeout = true;
233 terminate_connection(c, true);
234 } else if(c->last_ping_time + pinginterval < now) {
238 if(c->status.remove) {
239 logger(LOG_WARNING, _("Old connection_t for %s (%s) status %04x still lingering, deleting..."),
240 c->name, c->hostname, c->status.value);
244 ifdebug(CONNECTIONS) logger(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
245 c->name, c->hostname);
246 if(c->status.connecting) {
247 c->status.connecting = false;
248 closesocket(c->socket);
249 do_outgoing_connection(c);
251 terminate_connection(c, false);
256 if(c->outbuflen > 0 && c->last_flushed_time + pingtimeout < now) {
257 if(c->status.active) {
258 ifdebug(CONNECTIONS) logger(LOG_INFO,
259 _("%s (%s) could not flush for %ld seconds (%d bytes remaining)"),
260 c->name, c->hostname, now - c->last_flushed_time, c->outbuflen);
261 c->status.timeout = true;
262 terminate_connection(c, true);
268 void handle_meta_connection_data(int fd, short events, void *data)
270 connection_t *c = data;
272 socklen_t len = sizeof(result);
274 if (c->status.remove)
277 if (events & EV_READ) {
278 if(c->status.connecting) {
279 c->status.connecting = false;
280 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
283 finish_connecting(c);
285 ifdebug(CONNECTIONS) logger(LOG_DEBUG,
286 _("Error while connecting to %s (%s): %s"),
287 c->name, c->hostname, strerror(result));
288 closesocket(c->socket);
289 do_outgoing_connection(c);
294 if (!receive_meta(c)) {
295 terminate_connection(c, c->status.active);
300 if (events & EV_WRITE) {
302 terminate_connection(c, c->status.active);
307 static void dummy(int a, short b, void *c)
312 this is where it all happens...
318 time_t last_ping_check, last_config_check, last_graph_dump;
320 struct event timeout;
324 last_ping_check = now;
325 last_config_check = now;
326 last_graph_dump = now;
335 // tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
339 /* XXX: libevent transition: old timeout code in this loop */
340 timeout_set(&timeout, dummy, NULL);
341 timeout_add(&timeout, &tv);
345 logger(LOG_ERR, _("Error building fdset: %s"), strerror(errno));
351 r = event_loop(EVLOOP_ONCE);
354 logger(LOG_ERR, _("Error while waiting for input: %s"),
361 /* XXX: more libevent transition */
362 timeout_del(&timeout);
369 /* Let's check if everybody is still alive */
371 if(last_ping_check + pingtimeout < now) {
372 check_dead_connections();
373 last_ping_check = now;
375 if(routing_mode == RMODE_SWITCH)
380 /* Should we regenerate our key? */
382 if(keyexpires < now) {
383 ifdebug(STATUS) logger(LOG_INFO, _("Regenerating symmetric key"));
385 RAND_pseudo_bytes((unsigned char *)myself->key, myself->keylength);
387 EVP_DecryptInit_ex(&packet_ctx, myself->cipher, NULL, (unsigned char *)myself->key, (unsigned char *)myself->key + myself->cipher->key_len);
388 send_key_changed(broadcast, myself);
389 keyexpires = now + keylifetime;
394 while((event = get_expired_tevent())) {
395 event->handler(event->data);
400 logger(LOG_INFO, _("Flushing event queue"));
413 /* Reread our own configuration file */
415 exit_configuration(&config_tree);
416 init_configuration(&config_tree);
418 if(!read_server_config()) {
419 logger(LOG_ERR, _("Unable to reread configuration file, exitting."));
423 /* Close connections to hosts that have a changed or deleted host config file */
425 for(node = connection_tree->head; node; node = node->next) {
429 free(c->outgoing->name);
431 freeaddrinfo(c->outgoing->ai);
436 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
437 if(stat(fname, &s) || s.st_mtime > last_config_check)
438 terminate_connection(c, c->status.active);
442 last_config_check = now;
444 /* Try to make outgoing connections */
446 try_outgoing_connections();
449 /* Dump graph if wanted every 60 seconds*/
451 if(last_graph_dump + 60 < now) {
453 last_graph_dump = now;