2 net.c -- most of the network code
3 Copyright (C) 1998-2005 Ivo Timmermans,
4 2000-2010 Guus Sliepen <guus@tinc-vpn.org>
5 2006 Scott Lamb <slamb@slamb.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <openssl/rand.h>
29 #include "connection.h"
43 bool do_purge = false;
44 volatile bool running = false;
48 /* Purge edges and subnets of unreachable nodes. Use carefully. */
50 static void purge(void) {
51 avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
56 ifdebug(PROTOCOL) logger(LOG_DEBUG, "Purging unreachable nodes");
58 /* Remove all edges and subnets owned by unreachable nodes. */
60 for(nnode = node_tree->head; nnode; nnode = nnext) {
64 if(!n->status.reachable) {
65 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Purging node %s (%s)", n->name,
68 for(snode = n->subnet_tree->head; snode; snode = snext) {
72 send_del_subnet(broadcast, s);
76 for(enode = n->edge_tree->head; enode; enode = enext) {
80 send_del_edge(broadcast, e);
86 /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
88 for(nnode = node_tree->head; nnode; nnode = nnext) {
92 if(!n->status.reachable) {
93 for(enode = edge_weight_tree->head; enode; enode = enext) {
108 put all file descriptors in an fd_set array
109 While we're at it, purge stuff that needs to be removed.
111 static int build_fdset(fd_set *readset, fd_set *writeset) {
112 avl_node_t *node, *next;
119 for(node = connection_tree->head; node; node = next) {
123 if(c->status.remove) {
125 if(!connection_tree->head)
128 FD_SET(c->socket, readset);
130 FD_SET(c->socket, writeset);
136 for(i = 0; i < listen_sockets; i++) {
137 FD_SET(listen_socket[i].tcp, readset);
138 if(listen_socket[i].tcp > max)
139 max = listen_socket[i].tcp;
140 FD_SET(listen_socket[i].udp, readset);
141 if(listen_socket[i].udp > max)
142 max = listen_socket[i].udp;
146 FD_SET(device_fd, readset);
154 Terminate a connection:
156 - Remove associated edge and tell other connections about it if report = true
157 - Check if we need to retry making an outgoing connection
158 - Deactivate the host
160 void terminate_connection(connection_t *c, bool report) {
164 ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Closing connection with %s (%s)",
165 c->name, c->hostname);
167 c->status.remove = true;
168 c->status.active = false;
171 c->node->connection = NULL;
174 closesocket(c->socket);
177 if(report && !tunnelserver)
178 send_del_edge(broadcast, c->edge);
182 /* Run MST and SSSP algorithms */
186 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
188 if(report && !c->node->status.reachable) {
190 e = lookup_edge(c->node, myself);
193 send_del_edge(broadcast, e);
199 /* Check if this was our outgoing connection */
202 retry_outgoing(c->outgoing);
214 Check if the other end is active.
215 If we have sent packets, but didn't receive any,
216 then possibly the other end is dead. We send a
217 PING request over the meta connection. If the other
218 end does not reply in time, we consider them dead
219 and close the connection.
221 static void check_dead_connections(void) {
222 avl_node_t *node, *next;
225 for(node = connection_tree->head; node; node = next) {
229 if(c->last_ping_time + pingtimeout < now) {
230 if(c->status.active) {
231 if(c->status.pinged) {
232 ifdebug(CONNECTIONS) logger(LOG_INFO, "%s (%s) didn't respond to PING in %ld seconds",
233 c->name, c->hostname, now - c->last_ping_time);
234 c->status.timeout = true;
235 terminate_connection(c, true);
236 } else if(c->last_ping_time + pinginterval < now) {
240 if(c->status.remove) {
241 logger(LOG_WARNING, "Old connection_t for %s (%s) status %04x still lingering, deleting...",
242 c->name, c->hostname, bitfield_to_int(&c->status, sizeof c->status));
246 ifdebug(CONNECTIONS) logger(LOG_WARNING, "Timeout from %s (%s) during authentication",
247 c->name, c->hostname);
248 if(c->status.connecting) {
249 c->status.connecting = false;
250 closesocket(c->socket);
251 do_outgoing_connection(c);
253 terminate_connection(c, false);
258 if(c->outbuflen > 0 && c->last_flushed_time + pingtimeout < now) {
259 if(c->status.active) {
260 ifdebug(CONNECTIONS) logger(LOG_INFO,
261 "%s (%s) could not flush for %ld seconds (%d bytes remaining)",
262 c->name, c->hostname, now - c->last_flushed_time, c->outbuflen);
263 c->status.timeout = true;
264 terminate_connection(c, true);
271 check all connections to see if anything
272 happened on their sockets
274 static void check_network_activity(fd_set * readset, fd_set * writeset) {
278 socklen_t len = sizeof(result);
281 /* check input from kernel */
282 if(device_fd >= 0 && FD_ISSET(device_fd, readset)) {
283 if(read_packet(&packet)) {
285 route(myself, &packet);
289 /* check meta connections */
290 for(node = connection_tree->head; node; node = node->next) {
296 if(FD_ISSET(c->socket, readset)) {
297 if(c->status.connecting) {
298 c->status.connecting = false;
299 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
302 finish_connecting(c);
304 ifdebug(CONNECTIONS) logger(LOG_DEBUG,
305 "Error while connecting to %s (%s): %s",
306 c->name, c->hostname, sockstrerror(result));
307 closesocket(c->socket);
308 do_outgoing_connection(c);
313 if(!receive_meta(c)) {
314 terminate_connection(c, c->status.active);
319 if(FD_ISSET(c->socket, writeset)) {
321 terminate_connection(c, c->status.active);
327 for(i = 0; i < listen_sockets; i++) {
328 if(FD_ISSET(listen_socket[i].udp, readset))
329 handle_incoming_vpn_data(listen_socket[i].udp);
331 if(FD_ISSET(listen_socket[i].tcp, readset))
332 handle_new_meta_connection(listen_socket[i].tcp);
337 this is where it all happens...
339 int main_loop(void) {
340 fd_set readset, writeset;
343 time_t last_ping_check, last_config_check, last_graph_dump;
346 last_ping_check = now;
347 last_config_check = now;
348 last_graph_dump = now;
357 // tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
361 maxfd = build_fdset(&readset, &writeset);
364 LeaveCriticalSection(&mutex);
366 r = select(maxfd + 1, &readset, &writeset, NULL, &tv);
368 EnterCriticalSection(&mutex);
372 if(!sockwouldblock(sockerrno)) {
373 logger(LOG_ERR, "Error while waiting for input: %s", sockstrerror(sockerrno));
380 check_network_activity(&readset, &writeset);
387 /* Let's check if everybody is still alive */
389 if(last_ping_check + pingtimeout < now) {
390 check_dead_connections();
391 last_ping_check = now;
393 if(routing_mode == RMODE_SWITCH)
398 /* Should we regenerate our key? */
400 if(keyexpires < now) {
404 ifdebug(STATUS) logger(LOG_INFO, "Expiring symmetric keys");
406 for(node = node_tree->head; node; node = node->next) {
414 send_key_changed(broadcast, myself);
415 keyexpires = now + keylifetime;
421 logger(LOG_INFO, "Flushing event queue");
423 for(node = connection_tree->head; node; node = node->next) {
424 connection_t *c = node->data;
430 while((event = get_expired_event())) {
431 event->handler(event->data);
437 avl_node_t *node, *next;
443 /* Reread our own configuration file */
445 exit_configuration(&config_tree);
446 init_configuration(&config_tree);
448 if(!read_server_config()) {
449 logger(LOG_ERR, "Unable to reread configuration file, exitting.");
453 /* Cancel non-active outgoing connections */
455 for(node = connection_tree->head; node; node = next) {
461 if(c->status.connecting) {
462 terminate_connection(c, false);
467 /* Wipe list of outgoing connections */
469 for(list_node_t *node = outgoing_list->head; node; node = node->next) {
470 outgoing_t *outgoing = node->data;
473 event_del(outgoing->event);
476 list_delete_list(outgoing_list);
478 /* Close connections to hosts that have a changed or deleted host config file */
480 for(node = connection_tree->head; node; node = node->next) {
483 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
484 if(stat(fname, &s) || s.st_mtime > last_config_check)
485 terminate_connection(c, c->status.active);
489 last_config_check = now;
491 /* Try to make outgoing connections */
493 try_outgoing_connections();
496 /* Dump graph if wanted every 60 seconds*/
498 if(last_graph_dump + 60 < now) {
500 last_graph_dump = now;