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 along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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) {
50 avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
55 ifdebug(PROTOCOL) logger(LOG_DEBUG, _("Purging unreachable nodes"));
57 /* Remove all edges and subnets owned by unreachable nodes. */
59 for(nnode = node_tree->head; nnode; nnode = nnext) {
63 if(!n->status.reachable) {
64 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Purging node %s (%s)"), n->name,
67 for(snode = n->subnet_tree->head; snode; snode = snext) {
71 send_del_subnet(broadcast, s);
75 for(enode = n->edge_tree->head; enode; enode = enext) {
79 send_del_edge(broadcast, e);
85 /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
87 for(nnode = node_tree->head; nnode; nnode = nnext) {
91 if(!n->status.reachable) {
92 for(enode = edge_weight_tree->head; enode; enode = enext) {
107 put all file descriptors in an fd_set array
108 While we're at it, purge stuff that needs to be removed.
110 static int build_fdset(fd_set *readset, fd_set *writeset) {
111 avl_node_t *node, *next;
118 for(node = connection_tree->head; node; node = next) {
122 if(c->status.remove) {
124 if(!connection_tree->head)
127 FD_SET(c->socket, readset);
129 FD_SET(c->socket, writeset);
135 for(i = 0; i < listen_sockets; i++) {
136 FD_SET(listen_socket[i].tcp, readset);
137 if(listen_socket[i].tcp > max)
138 max = listen_socket[i].tcp;
139 FD_SET(listen_socket[i].udp, readset);
140 if(listen_socket[i].udp > max)
141 max = listen_socket[i].udp;
145 FD_SET(device_fd, readset);
153 Terminate a connection:
155 - Remove associated edge and tell other connections about it if report = true
156 - Check if we need to retry making an outgoing connection
157 - Deactivate the host
159 void terminate_connection(connection_t *c, bool report) {
163 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Closing connection with %s (%s)"),
164 c->name, c->hostname);
166 c->status.remove = true;
167 c->status.active = false;
170 c->node->connection = NULL;
173 closesocket(c->socket);
176 if(report && !tunnelserver)
177 send_del_edge(broadcast, c->edge);
181 /* Run MST and SSSP algorithms */
185 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
187 if(report && !c->node->status.reachable) {
189 e = lookup_edge(c->node, myself);
192 send_del_edge(broadcast, e);
198 /* Check if this was our outgoing connection */
201 retry_outgoing(c->outgoing);
213 Check if the other end is active.
214 If we have sent packets, but didn't receive any,
215 then possibly the other end is dead. We send a
216 PING request over the meta connection. If the other
217 end does not reply in time, we consider them dead
218 and close the connection.
220 static void check_dead_connections(void) {
221 avl_node_t *node, *next;
224 for(node = connection_tree->head; node; node = next) {
228 if(c->last_ping_time + pingtimeout < now) {
229 if(c->status.active) {
230 if(c->status.pinged) {
231 ifdebug(CONNECTIONS) logger(LOG_INFO, _("%s (%s) didn't respond to PING in %ld seconds"),
232 c->name, c->hostname, now - c->last_ping_time);
233 c->status.timeout = true;
234 terminate_connection(c, true);
235 } else if(c->last_ping_time + pinginterval < now) {
239 if(c->status.remove) {
240 logger(LOG_WARNING, _("Old connection_t for %s (%s) status %04x still lingering, deleting..."),
241 c->name, c->hostname, bitfield_to_int(&c->status, sizeof c->status));
245 ifdebug(CONNECTIONS) logger(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
246 c->name, c->hostname);
247 if(c->status.connecting) {
248 c->status.connecting = false;
249 closesocket(c->socket);
250 do_outgoing_connection(c);
252 terminate_connection(c, false);
257 if(c->outbuflen > 0 && c->last_flushed_time + pingtimeout < now) {
258 if(c->status.active) {
259 ifdebug(CONNECTIONS) logger(LOG_INFO,
260 _("%s (%s) could not flush for %ld seconds (%d bytes remaining)"),
261 c->name, c->hostname, now - c->last_flushed_time, c->outbuflen);
262 c->status.timeout = true;
263 terminate_connection(c, true);
270 check all connections to see if anything
271 happened on their sockets
273 static void check_network_activity(fd_set * readset, fd_set * writeset) {
277 socklen_t len = sizeof(result);
280 /* check input from kernel */
281 if(device_fd >= 0 && FD_ISSET(device_fd, readset)) {
282 if(read_packet(&packet)) {
284 route(myself, &packet);
288 /* check meta connections */
289 for(node = connection_tree->head; node; node = node->next) {
295 if(FD_ISSET(c->socket, readset)) {
296 if(c->status.connecting) {
297 c->status.connecting = false;
298 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
301 finish_connecting(c);
303 ifdebug(CONNECTIONS) logger(LOG_DEBUG,
304 _("Error while connecting to %s (%s): %s"),
305 c->name, c->hostname, strerror(result));
306 closesocket(c->socket);
307 do_outgoing_connection(c);
312 if(!receive_meta(c)) {
313 terminate_connection(c, c->status.active);
318 if(FD_ISSET(c->socket, writeset)) {
320 terminate_connection(c, c->status.active);
326 for(i = 0; i < listen_sockets; i++) {
327 if(FD_ISSET(listen_socket[i].udp, readset))
328 handle_incoming_vpn_data(listen_socket[i].udp);
330 if(FD_ISSET(listen_socket[i].tcp, readset))
331 handle_new_meta_connection(listen_socket[i].tcp);
336 this is where it all happens...
338 int main_loop(void) {
339 fd_set readset, writeset;
342 time_t last_ping_check, last_config_check, last_graph_dump;
345 last_ping_check = now;
346 last_config_check = now;
347 last_graph_dump = now;
356 // tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
360 maxfd = build_fdset(&readset, &writeset);
363 LeaveCriticalSection(&mutex);
365 r = select(maxfd + 1, &readset, &writeset, NULL, &tv);
367 EnterCriticalSection(&mutex);
371 if(errno != EINTR && errno != EAGAIN) {
372 logger(LOG_ERR, _("Error while waiting for input: %s"),
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;
421 logger(LOG_INFO, _("Flushing event queue"));
426 while((event = get_expired_event())) {
427 event->handler(event->data);
439 /* Reread our own configuration file */
441 exit_configuration(&config_tree);
442 init_configuration(&config_tree);
444 if(!read_server_config()) {
445 logger(LOG_ERR, _("Unable to reread configuration file, exitting."));
449 /* Close connections to hosts that have a changed or deleted host config file */
451 for(node = connection_tree->head; node; node = node->next) {
454 xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
455 if(stat(fname, &s) || s.st_mtime > last_config_check)
456 terminate_connection(c, c->status.active);
460 last_config_check = now;
462 /* Try to make outgoing connections */
464 try_outgoing_connections();
467 /* Dump graph if wanted every 60 seconds*/
469 if(last_graph_dump + 60 < now) {
471 last_graph_dump = now;