2 net.c -- most of the network code
3 Copyright (C) 1998-2003 Ivo Timmermans <ivo@o2w.nl>,
4 2000-2003 Guus Sliepen <guus@sliepen.eu.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.
20 $Id: net.c,v 1.40 2003/08/24 20:38:24 guus Exp $
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) {
66 n = (node_t *) nnode->data;
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) {
74 s = (subnet_t *) snode->data;
75 send_del_subnet(broadcast, s);
79 for(enode = n->edge_tree->head; enode; enode = enext) {
81 e = (edge_t *) enode->data;
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) {
92 n = (node_t *) nnode->data;
94 if(!n->status.reachable) {
95 for(enode = edge_weight_tree->head; enode; enode = enext) {
97 e = (edge_t *) enode->data;
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 * fs)
115 avl_node_t *node, *next;
123 for(node = connection_tree->head; node; node = next) {
125 c = (connection_t *) node->data;
127 if(c->status.remove) {
129 if(!connection_tree->head)
132 FD_SET(c->socket, fs);
138 for(i = 0; i < listen_sockets; i++) {
139 FD_SET(listen_socket[i].tcp, fs);
140 if(listen_socket[i].tcp > max)
141 max = listen_socket[i].tcp;
142 FD_SET(listen_socket[i].udp, fs);
143 if(listen_socket[i].udp > max)
144 max = listen_socket[i].udp;
147 FD_SET(device_fd, fs);
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)
168 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Closing connection with %s (%s)"),
169 c->name, c->hostname);
171 c->status.remove = true;
172 c->status.active = false;
175 c->node->connection = NULL;
178 closesocket(c->socket);
182 send_del_edge(broadcast, c->edge);
186 /* Run MST and SSSP algorithms */
191 /* Check if this was our outgoing connection */
194 retry_outgoing(c->outgoing);
200 Check if the other end is active.
201 If we have sent packets, but didn't receive any,
202 then possibly the other end is dead. We send a
203 PING request over the meta connection. If the other
204 end does not reply in time, we consider them dead
205 and close the connection.
207 static void check_dead_connections(void)
209 avl_node_t *node, *next;
214 for(node = connection_tree->head; node; node = next) {
216 c = (connection_t *) node->data;
218 if(c->last_ping_time + pingtimeout < now) {
219 if(c->status.active) {
220 if(c->status.pinged) {
221 ifdebug(CONNECTIONS) logger(LOG_INFO, _("%s (%s) didn't respond to PING"),
222 c->name, c->hostname);
223 c->status.timeout = true;
224 terminate_connection(c, true);
229 if(c->status.remove) {
230 logger(LOG_WARNING, _("Old connection_t for %s (%s) status %04x still lingering, deleting..."),
231 c->name, c->hostname, *(uint32_t *)&c->status);
235 ifdebug(CONNECTIONS) logger(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
236 c->name, c->hostname);
237 terminate_connection(c, false);
244 check all connections to see if anything
245 happened on their sockets
247 static void check_network_activity(fd_set * f)
252 int len = sizeof(result);
257 if(FD_ISSET(device_fd, f)) {
258 if(read_packet(&packet))
259 route_outgoing(&packet);
262 for(node = connection_tree->head; node; node = node->next) {
263 c = (connection_t *) node->data;
268 if(FD_ISSET(c->socket, f)) {
269 if(c->status.connecting) {
270 c->status.connecting = false;
271 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
274 finish_connecting(c);
276 ifdebug(CONNECTIONS) logger(LOG_DEBUG,
277 _("Error while connecting to %s (%s): %s"),
278 c->name, c->hostname, strerror(result));
279 closesocket(c->socket);
280 do_outgoing_connection(c);
285 if(!receive_meta(c)) {
286 terminate_connection(c, c->status.active);
292 for(i = 0; i < listen_sockets; i++) {
293 if(FD_ISSET(listen_socket[i].udp, f))
294 handle_incoming_vpn_data(listen_socket[i].udp);
296 if(FD_ISSET(listen_socket[i].tcp, f))
297 handle_new_meta_connection(listen_socket[i].tcp);
302 this is where it all happens...
309 time_t last_ping_check, last_config_check;
314 last_ping_check = now;
315 last_config_check = now;
323 tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
326 maxfd = build_fdset(&fset);
328 r = select(maxfd + 1, &fset, NULL, NULL, &tv);
331 if(errno != EINTR && errno != EAGAIN) {
332 logger(LOG_ERR, _("Error while waiting for input: %s"),
342 check_network_activity(&fset);
349 /* Let's check if everybody is still alive */
351 if(last_ping_check + pingtimeout < now) {
352 check_dead_connections();
353 last_ping_check = now;
355 if(routing_mode == RMODE_SWITCH)
360 /* Should we regenerate our key? */
362 if(keyexpires < now) {
363 ifdebug(STATUS) logger(LOG_INFO, _("Regenerating symmetric key"));
365 RAND_pseudo_bytes(myself->key, myself->keylength);
367 EVP_DecryptInit_ex(&packet_ctx, myself->cipher, NULL, myself->key, myself->key + myself->cipher->key_len);
368 send_key_changed(broadcast, myself);
369 keyexpires = now + keylifetime;
374 while((event = get_expired_event())) {
375 event->handler(event->data);
380 logger(LOG_INFO, _("Flushing event queue"));
382 while(event_tree->head) {
383 event = (event_t *) event_tree->head->data;
384 event->handler(event->data);
398 /* Reread our own configuration file */
400 exit_configuration(&config_tree);
401 init_configuration(&config_tree);
403 if(!read_server_config()) {
404 logger(LOG_ERR, _("Unable to reread configuration file, exitting."));
408 /* Close connections to hosts that have a changed or deleted host config file */
410 for(node = connection_tree->head; node; node = node->next) {
411 c = (connection_t *) node->data;
414 free(c->outgoing->name);
415 freeaddrinfo(c->outgoing->ai);
420 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
421 if(stat(fname, &s) || s.st_mtime > last_config_check)
422 terminate_connection(c, c->status.active);
426 last_config_check = now;
428 /* Try to make outgoing connections */
430 try_outgoing_connections();