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) {
71 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) {
101 if(!enode && (!strictsubnets || !n->subnet_tree->head))
102 /* in strictsubnets mode do not delete nodes with subnets */
109 put all file descriptors in an fd_set array
110 While we're at it, purge stuff that needs to be removed.
112 static int build_fdset(fd_set *readset, fd_set *writeset) {
113 avl_node_t *node, *next;
120 for(node = connection_tree->head; node; node = next) {
124 if(c->status.remove) {
126 if(!connection_tree->head)
129 FD_SET(c->socket, readset);
131 FD_SET(c->socket, writeset);
137 for(i = 0; i < listen_sockets; i++) {
138 FD_SET(listen_socket[i].tcp, readset);
139 if(listen_socket[i].tcp > max)
140 max = listen_socket[i].tcp;
141 FD_SET(listen_socket[i].udp, readset);
142 if(listen_socket[i].udp > max)
143 max = listen_socket[i].udp;
147 FD_SET(device_fd, readset);
155 Terminate a connection:
157 - Remove associated edge and tell other connections about it if report = true
158 - Check if we need to retry making an outgoing connection
159 - Deactivate the host
161 void terminate_connection(connection_t *c, bool report) {
165 ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Closing connection with %s (%s)",
166 c->name, c->hostname);
168 c->status.remove = true;
169 c->status.active = false;
172 c->node->connection = NULL;
175 closesocket(c->socket);
178 if(report && !tunnelserver)
179 send_del_edge(broadcast, c->edge);
183 /* Run MST and SSSP algorithms */
187 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
189 if(report && !c->node->status.reachable) {
191 e = lookup_edge(c->node, myself);
194 send_del_edge(broadcast, e);
200 /* Check if this was our outgoing connection */
203 retry_outgoing(c->outgoing);
215 Check if the other end is active.
216 If we have sent packets, but didn't receive any,
217 then possibly the other end is dead. We send a
218 PING request over the meta connection. If the other
219 end does not reply in time, we consider them dead
220 and close the connection.
222 static void check_dead_connections(void) {
223 avl_node_t *node, *next;
226 for(node = connection_tree->head; node; node = next) {
230 if(c->last_ping_time + pingtimeout < now) {
231 if(c->status.active) {
232 if(c->status.pinged) {
233 ifdebug(CONNECTIONS) logger(LOG_INFO, "%s (%s) didn't respond to PING in %ld seconds",
234 c->name, c->hostname, now - c->last_ping_time);
235 c->status.timeout = true;
236 terminate_connection(c, true);
237 } else if(c->last_ping_time + pinginterval < now) {
241 if(c->status.remove) {
242 logger(LOG_WARNING, "Old connection_t for %s (%s) status %04x still lingering, deleting...",
243 c->name, c->hostname, bitfield_to_int(&c->status, sizeof c->status));
247 ifdebug(CONNECTIONS) logger(LOG_WARNING, "Timeout from %s (%s) during authentication",
248 c->name, c->hostname);
249 if(c->status.connecting) {
250 c->status.connecting = false;
251 closesocket(c->socket);
252 do_outgoing_connection(c);
254 terminate_connection(c, false);
259 if(c->outbuflen > 0 && c->last_flushed_time + pingtimeout < now) {
260 if(c->status.active) {
261 ifdebug(CONNECTIONS) logger(LOG_INFO,
262 "%s (%s) could not flush for %ld seconds (%d bytes remaining)",
263 c->name, c->hostname, now - c->last_flushed_time, c->outbuflen);
264 c->status.timeout = true;
265 terminate_connection(c, true);
272 check all connections to see if anything
273 happened on their sockets
275 static void check_network_activity(fd_set * readset, fd_set * writeset) {
279 socklen_t len = sizeof(result);
282 /* check input from kernel */
283 if(device_fd >= 0 && FD_ISSET(device_fd, readset)) {
284 if(read_packet(&packet)) {
286 route(myself, &packet);
290 /* check meta connections */
291 for(node = connection_tree->head; node; node = node->next) {
297 if(FD_ISSET(c->socket, readset)) {
298 if(c->status.connecting) {
299 c->status.connecting = false;
300 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
303 finish_connecting(c);
305 ifdebug(CONNECTIONS) logger(LOG_DEBUG,
306 "Error while connecting to %s (%s): %s",
307 c->name, c->hostname, sockstrerror(result));
308 closesocket(c->socket);
309 do_outgoing_connection(c);
314 if(!receive_meta(c)) {
315 terminate_connection(c, c->status.active);
320 if(FD_ISSET(c->socket, writeset)) {
322 terminate_connection(c, c->status.active);
328 for(i = 0; i < listen_sockets; i++) {
329 if(FD_ISSET(listen_socket[i].udp, readset))
330 handle_incoming_vpn_data(listen_socket[i].udp);
332 if(FD_ISSET(listen_socket[i].tcp, readset))
333 handle_new_meta_connection(listen_socket[i].tcp);
338 this is where it all happens...
340 int main_loop(void) {
341 fd_set readset, writeset;
344 time_t last_ping_check, last_config_check, last_graph_dump;
347 last_ping_check = now;
348 last_config_check = now;
349 last_graph_dump = now;
358 // tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
362 maxfd = build_fdset(&readset, &writeset);
365 LeaveCriticalSection(&mutex);
367 r = select(maxfd + 1, &readset, &writeset, NULL, &tv);
369 EnterCriticalSection(&mutex);
373 if(!sockwouldblock(sockerrno)) {
374 logger(LOG_ERR, "Error while waiting for input: %s", sockstrerror(sockerrno));
381 check_network_activity(&readset, &writeset);
388 /* Let's check if everybody is still alive */
390 if(last_ping_check + pingtimeout < now) {
391 check_dead_connections();
392 last_ping_check = now;
394 if(routing_mode == RMODE_SWITCH)
399 /* Should we regenerate our key? */
401 if(keyexpires < now) {
405 ifdebug(STATUS) logger(LOG_INFO, "Expiring symmetric keys");
407 for(node = node_tree->head; node; node = node->next) {
415 send_key_changed(broadcast, myself);
416 keyexpires = now + keylifetime;
422 logger(LOG_INFO, "Flushing event queue");
424 for(node = connection_tree->head; node; node = node->next) {
425 connection_t *c = node->data;
431 while((event = get_expired_event())) {
432 event->handler(event->data);
438 avl_node_t *node, *next;
444 /* Reread our own configuration file */
446 exit_configuration(&config_tree);
447 init_configuration(&config_tree);
449 if(!read_server_config()) {
450 logger(LOG_ERR, "Unable to reread configuration file, exitting.");
454 /* Cancel non-active outgoing connections */
456 for(node = connection_tree->head; node; node = next) {
462 if(c->status.connecting) {
463 terminate_connection(c, false);
468 /* Wipe list of outgoing connections */
470 for(list_node_t *node = outgoing_list->head; node; node = node->next) {
471 outgoing_t *outgoing = node->data;
474 event_del(outgoing->event);
477 list_delete_list(outgoing_list);
479 /* Close connections to hosts that have a changed or deleted host config file */
481 for(node = connection_tree->head; node; node = node->next) {
484 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
485 if(stat(fname, &s) || s.st_mtime > last_config_check)
486 terminate_connection(c, c->status.active);
490 last_config_check = now;
492 /* Try to make outgoing connections */
494 try_outgoing_connections();
497 /* Dump graph if wanted every 60 seconds*/
499 if(last_graph_dump + 60 < now) {
501 last_graph_dump = now;