2 net.c -- most of the network code
3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2009 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 in an fd_set array
113 While we're at it, purge stuff that needs to be removed.
115 static int build_fdset(fd_set *readset, fd_set *writeset)
117 avl_node_t *node, *next;
126 for(node = connection_tree->head; node; node = next) {
130 if(c->status.remove) {
132 if(!connection_tree->head)
135 FD_SET(c->socket, readset);
137 FD_SET(c->socket, writeset);
143 for(i = 0; i < listen_sockets; i++) {
144 FD_SET(listen_socket[i].tcp, readset);
145 if(listen_socket[i].tcp > max)
146 max = listen_socket[i].tcp;
147 FD_SET(listen_socket[i].udp, readset);
148 if(listen_socket[i].udp > max)
149 max = listen_socket[i].udp;
152 FD_SET(device_fd, readset);
160 Terminate a connection:
162 - Remove associated edge and tell other connections about it if report = true
163 - Check if we need to retry making an outgoing connection
164 - Deactivate the host
166 void terminate_connection(connection_t *c, bool report)
173 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Closing connection with %s (%s)"),
174 c->name, c->hostname);
176 c->status.remove = true;
177 c->status.active = false;
180 c->node->connection = NULL;
183 closesocket(c->socket);
186 if(report && !tunnelserver)
187 send_del_edge(broadcast, c->edge);
191 /* Run MST and SSSP algorithms */
195 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
197 if(report && !c->node->status.reachable) {
199 e = lookup_edge(c->node, myself);
202 send_del_edge(broadcast, e);
208 /* Check if this was our outgoing connection */
211 retry_outgoing(c->outgoing);
223 Check if the other end is active.
224 If we have sent packets, but didn't receive any,
225 then possibly the other end is dead. We send a
226 PING request over the meta connection. If the other
227 end does not reply in time, we consider them dead
228 and close the connection.
230 static void check_dead_connections(void)
232 avl_node_t *node, *next;
237 for(node = connection_tree->head; node; node = next) {
241 if(c->last_ping_time + pingtimeout < now) {
242 if(c->status.active) {
243 if(c->status.pinged) {
244 ifdebug(CONNECTIONS) logger(LOG_INFO, _("%s (%s) didn't respond to PING in %ld seconds"),
245 c->name, c->hostname, now - c->last_ping_time);
246 c->status.timeout = true;
247 terminate_connection(c, true);
248 } else if(c->last_ping_time + pinginterval < now) {
252 if(c->status.remove) {
253 logger(LOG_WARNING, _("Old connection_t for %s (%s) status %04x still lingering, deleting..."),
254 c->name, c->hostname, bitfield_to_int(&c->status, sizeof c->status));
258 ifdebug(CONNECTIONS) logger(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
259 c->name, c->hostname);
260 if(c->status.connecting) {
261 c->status.connecting = false;
262 closesocket(c->socket);
263 do_outgoing_connection(c);
265 terminate_connection(c, false);
270 if(c->outbuflen > 0 && c->last_flushed_time + pingtimeout < now) {
271 if(c->status.active) {
272 ifdebug(CONNECTIONS) logger(LOG_INFO,
273 _("%s (%s) could not flush for %ld seconds (%d bytes remaining)"),
274 c->name, c->hostname, now - c->last_flushed_time, c->outbuflen);
275 c->status.timeout = true;
276 terminate_connection(c, true);
283 check all connections to see if anything
284 happened on their sockets
286 static void check_network_activity(fd_set * readset, fd_set * writeset)
291 socklen_t len = sizeof(result);
296 /* check input from kernel */
297 if(FD_ISSET(device_fd, readset)) {
298 if(read_packet(&packet)) {
300 route(myself, &packet);
304 /* check meta connections */
305 for(node = connection_tree->head; node; node = node->next) {
311 if(FD_ISSET(c->socket, readset)) {
312 if(c->status.connecting) {
313 c->status.connecting = false;
314 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
317 finish_connecting(c);
319 ifdebug(CONNECTIONS) logger(LOG_DEBUG,
320 _("Error while connecting to %s (%s): %s"),
321 c->name, c->hostname, strerror(result));
322 closesocket(c->socket);
323 do_outgoing_connection(c);
328 if(!receive_meta(c)) {
329 terminate_connection(c, c->status.active);
334 if(FD_ISSET(c->socket, writeset)) {
336 terminate_connection(c, c->status.active);
342 for(i = 0; i < listen_sockets; i++) {
343 if(FD_ISSET(listen_socket[i].udp, readset))
344 handle_incoming_vpn_data(listen_socket[i].udp);
346 if(FD_ISSET(listen_socket[i].tcp, readset))
347 handle_new_meta_connection(listen_socket[i].tcp);
352 this is where it all happens...
356 fd_set readset, writeset;
359 time_t last_ping_check, last_config_check, last_graph_dump;
364 last_ping_check = now;
365 last_config_check = now;
366 last_graph_dump = now;
376 // tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
380 maxfd = build_fdset(&readset, &writeset);
382 r = select(maxfd + 1, &readset, &writeset, NULL, &tv);
385 if(errno != EINTR && errno != EAGAIN) {
386 logger(LOG_ERR, _("Error while waiting for input: %s"),
396 check_network_activity(&readset, &writeset);
403 /* Let's check if everybody is still alive */
405 if(last_ping_check + pingtimeout < now) {
406 check_dead_connections();
407 last_ping_check = now;
409 if(routing_mode == RMODE_SWITCH)
414 /* Should we regenerate our key? */
416 if(keyexpires < now) {
420 ifdebug(STATUS) logger(LOG_INFO, _("Expiring symmetric keys"));
422 for(node = node_tree->head; node; node = node->next) {
430 send_key_changed(broadcast, myself);
431 keyexpires = now + keylifetime;
436 logger(LOG_INFO, _("Flushing event queue"));
441 while((event = get_expired_event())) {
442 event->handler(event->data);
454 /* Reread our own configuration file */
456 exit_configuration(&config_tree);
457 init_configuration(&config_tree);
459 if(!read_server_config()) {
460 logger(LOG_ERR, _("Unable to reread configuration file, exitting."));
464 /* Close connections to hosts that have a changed or deleted host config file */
466 for(node = connection_tree->head; node; node = node->next) {
469 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
470 if(stat(fname, &s) || s.st_mtime > last_config_check)
471 terminate_connection(c, c->status.active);
475 last_config_check = now;
477 /* Try to make outgoing connections */
479 try_outgoing_connections();
482 /* Dump graph if wanted every 60 seconds*/
484 if(last_graph_dump + 60 < now) {
486 last_graph_dump = now;