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.
23 #include <openssl/rand.h>
28 #include "connection.h"
42 bool do_purge = false;
43 volatile bool running = false;
47 /* Purge edges and subnets of unreachable nodes. Use carefully. */
49 static void purge(void)
51 avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
58 ifdebug(PROTOCOL) logger(LOG_DEBUG, _("Purging unreachable nodes"));
60 /* Remove all edges and subnets owned by unreachable nodes. */
62 for(nnode = node_tree->head; nnode; nnode = nnext) {
66 if(!n->status.reachable) {
67 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Purging node %s (%s)"), n->name,
70 for(snode = n->subnet_tree->head; snode; snode = snext) {
74 send_del_subnet(broadcast, s);
78 for(enode = n->edge_tree->head; enode; enode = enext) {
82 send_del_edge(broadcast, e);
88 /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
90 for(nnode = node_tree->head; nnode; nnode = nnext) {
94 if(!n->status.reachable) {
95 for(enode = edge_weight_tree->head; enode; enode = enext) {
110 put all file descriptors in an fd_set array
111 While we're at it, purge stuff that needs to be removed.
113 static int build_fdset(fd_set *readset, fd_set *writeset)
115 avl_node_t *node, *next;
124 for(node = connection_tree->head; node; node = next) {
128 if(c->status.remove) {
130 if(!connection_tree->head)
133 FD_SET(c->socket, readset);
135 FD_SET(c->socket, writeset);
141 for(i = 0; i < listen_sockets; i++) {
142 FD_SET(listen_socket[i].tcp, readset);
143 if(listen_socket[i].tcp > max)
144 max = listen_socket[i].tcp;
145 FD_SET(listen_socket[i].udp, readset);
146 if(listen_socket[i].udp > max)
147 max = listen_socket[i].udp;
151 FD_SET(device_fd, readset);
159 Terminate a connection:
161 - Remove associated edge and tell other connections about it if report = true
162 - Check if we need to retry making an outgoing connection
163 - Deactivate the host
165 void terminate_connection(connection_t *c, bool report)
172 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Closing connection with %s (%s)"),
173 c->name, c->hostname);
175 c->status.remove = true;
176 c->status.active = false;
179 c->node->connection = NULL;
182 closesocket(c->socket);
185 if(report && !tunnelserver)
186 send_del_edge(broadcast, c->edge);
190 /* Run MST and SSSP algorithms */
194 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
196 if(report && !c->node->status.reachable) {
198 e = lookup_edge(c->node, myself);
201 send_del_edge(broadcast, e);
207 /* Check if this was our outgoing connection */
210 retry_outgoing(c->outgoing);
222 Check if the other end is active.
223 If we have sent packets, but didn't receive any,
224 then possibly the other end is dead. We send a
225 PING request over the meta connection. If the other
226 end does not reply in time, we consider them dead
227 and close the connection.
229 static void check_dead_connections(void)
231 avl_node_t *node, *next;
236 for(node = connection_tree->head; node; node = next) {
240 if(c->last_ping_time + pingtimeout < now) {
241 if(c->status.active) {
242 if(c->status.pinged) {
243 ifdebug(CONNECTIONS) logger(LOG_INFO, _("%s (%s) didn't respond to PING in %ld seconds"),
244 c->name, c->hostname, now - c->last_ping_time);
245 c->status.timeout = true;
246 terminate_connection(c, true);
247 } else if(c->last_ping_time + pinginterval < now) {
251 if(c->status.remove) {
252 logger(LOG_WARNING, _("Old connection_t for %s (%s) status %04x still lingering, deleting..."),
253 c->name, c->hostname, bitfield_to_int(&c->status, sizeof c->status));
257 ifdebug(CONNECTIONS) logger(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
258 c->name, c->hostname);
259 if(c->status.connecting) {
260 c->status.connecting = false;
261 closesocket(c->socket);
262 do_outgoing_connection(c);
264 terminate_connection(c, false);
269 if(c->outbuflen > 0 && c->last_flushed_time + pingtimeout < now) {
270 if(c->status.active) {
271 ifdebug(CONNECTIONS) logger(LOG_INFO,
272 _("%s (%s) could not flush for %ld seconds (%d bytes remaining)"),
273 c->name, c->hostname, now - c->last_flushed_time, c->outbuflen);
274 c->status.timeout = true;
275 terminate_connection(c, true);
282 check all connections to see if anything
283 happened on their sockets
285 static void check_network_activity(fd_set * readset, fd_set * writeset)
290 socklen_t len = sizeof(result);
295 /* check input from kernel */
296 if(device_fd >= 0 && FD_ISSET(device_fd, readset)) {
297 if(read_packet(&packet)) {
299 route(myself, &packet);
303 /* check meta connections */
304 for(node = connection_tree->head; node; node = node->next) {
310 if(FD_ISSET(c->socket, readset)) {
311 if(c->status.connecting) {
312 c->status.connecting = false;
313 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
316 finish_connecting(c);
318 ifdebug(CONNECTIONS) logger(LOG_DEBUG,
319 _("Error while connecting to %s (%s): %s"),
320 c->name, c->hostname, strerror(result));
321 closesocket(c->socket);
322 do_outgoing_connection(c);
327 if(!receive_meta(c)) {
328 terminate_connection(c, c->status.active);
333 if(FD_ISSET(c->socket, writeset)) {
335 terminate_connection(c, c->status.active);
341 for(i = 0; i < listen_sockets; i++) {
342 if(FD_ISSET(listen_socket[i].udp, readset))
343 handle_incoming_vpn_data(listen_socket[i].udp);
345 if(FD_ISSET(listen_socket[i].tcp, readset))
346 handle_new_meta_connection(listen_socket[i].tcp);
351 this is where it all happens...
355 fd_set readset, writeset;
358 time_t last_ping_check, last_config_check, last_graph_dump;
363 last_ping_check = now;
364 last_config_check = now;
365 last_graph_dump = now;
374 // tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
378 maxfd = build_fdset(&readset, &writeset);
381 LeaveCriticalSection(&mutex);
383 r = select(maxfd + 1, &readset, &writeset, NULL, &tv);
385 EnterCriticalSection(&mutex);
389 if(errno != EINTR && errno != EAGAIN) {
390 logger(LOG_ERR, _("Error while waiting for input: %s"),
400 check_network_activity(&readset, &writeset);
407 /* Let's check if everybody is still alive */
409 if(last_ping_check + pingtimeout < now) {
410 check_dead_connections();
411 last_ping_check = now;
413 if(routing_mode == RMODE_SWITCH)
418 /* Should we regenerate our key? */
420 if(keyexpires < now) {
424 ifdebug(STATUS) logger(LOG_INFO, _("Expiring symmetric keys"));
426 for(node = node_tree->head; node; node = node->next) {
434 send_key_changed(broadcast, myself);
435 keyexpires = now + keylifetime;
440 logger(LOG_INFO, _("Flushing event queue"));
445 while((event = get_expired_event())) {
446 event->handler(event->data);
458 /* Reread our own configuration file */
460 exit_configuration(&config_tree);
461 init_configuration(&config_tree);
463 if(!read_server_config()) {
464 logger(LOG_ERR, _("Unable to reread configuration file, exitting."));
468 /* Close connections to hosts that have a changed or deleted host config file */
470 for(node = connection_tree->head; node; node = node->next) {
473 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
474 if(stat(fname, &s) || s.st_mtime > last_config_check)
475 terminate_connection(c, c->status.active);
479 last_config_check = now;
481 /* Try to make outgoing connections */
483 try_outgoing_connections();
486 /* Dump graph if wanted every 60 seconds*/
488 if(last_graph_dump + 60 < now) {
490 last_graph_dump = now;