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.35.4.195 2003/07/29 22:59:00 guus Exp $
25 #include <openssl/rand.h>
30 #include "connection.h"
44 bool do_purge = false;
48 /* Purge edges and subnets of unreachable nodes. Use carefully. */
50 static void purge(void)
52 avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext;
59 ifdebug(PROTOCOL) logger(LOG_DEBUG, _("Purging unreachable nodes"));
61 for(nnode = node_tree->head; nnode; nnode = nnext) {
63 n = (node_t *) nnode->data;
65 if(!n->status.reachable) {
66 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Purging node %s (%s)"), n->name,
69 for(snode = n->subnet_tree->head; snode; snode = snext) {
71 s = (subnet_t *) snode->data;
72 send_del_subnet(broadcast, s);
76 for(enode = n->edge_tree->head; enode; enode = enext) {
78 e = (edge_t *) enode->data;
79 send_del_edge(broadcast, e);
89 put all file descriptors in an fd_set array
90 While we're at it, purge stuff that needs to be removed.
92 static int build_fdset(fd_set * fs)
94 avl_node_t *node, *next;
102 for(node = connection_tree->head; node; node = next) {
104 c = (connection_t *) node->data;
106 if(c->status.remove) {
108 if(!connection_tree->head)
111 FD_SET(c->socket, fs);
117 for(i = 0; i < listen_sockets; i++) {
118 FD_SET(listen_socket[i].tcp, fs);
119 if(listen_socket[i].tcp > max)
120 max = listen_socket[i].tcp;
121 FD_SET(listen_socket[i].udp, fs);
122 if(listen_socket[i].udp > max)
123 max = listen_socket[i].udp;
126 FD_SET(device_fd, fs);
134 Terminate a connection:
136 - Remove associated edge and tell other connections about it if report = true
137 - Check if we need to retry making an outgoing connection
138 - Deactivate the host
140 void terminate_connection(connection_t *c, bool report)
147 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Closing connection with %s (%s)"),
148 c->name, c->hostname);
150 c->status.remove = true;
151 c->status.active = false;
154 c->node->connection = NULL;
157 closesocket(c->socket);
161 send_del_edge(broadcast, c->edge);
165 /* Run MST and SSSP algorithms */
170 /* Check if this was our outgoing connection */
173 retry_outgoing(c->outgoing);
179 Check if the other end is active.
180 If we have sent packets, but didn't receive any,
181 then possibly the other end is dead. We send a
182 PING request over the meta connection. If the other
183 end does not reply in time, we consider them dead
184 and close the connection.
186 static void check_dead_connections(void)
188 avl_node_t *node, *next;
193 for(node = connection_tree->head; node; node = next) {
195 c = (connection_t *) node->data;
197 if(c->last_ping_time + pingtimeout < now) {
198 if(c->status.active) {
199 if(c->status.pinged) {
200 ifdebug(CONNECTIONS) logger(LOG_INFO, _("%s (%s) didn't respond to PING"),
201 c->name, c->hostname);
202 c->status.timeout = true;
203 terminate_connection(c, true);
208 if(c->status.remove) {
209 logger(LOG_WARNING, _("Old connection_t for %s (%s) status %04x still lingering, deleting..."),
210 c->name, c->hostname, *(uint32_t *)&c->status);
214 ifdebug(CONNECTIONS) logger(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
215 c->name, c->hostname);
216 terminate_connection(c, false);
223 check all connections to see if anything
224 happened on their sockets
226 static void check_network_activity(fd_set * f)
231 int len = sizeof(result);
236 if(FD_ISSET(device_fd, f)) {
237 if(read_packet(&packet))
238 route_outgoing(&packet);
241 for(node = connection_tree->head; node; node = node->next) {
242 c = (connection_t *) node->data;
247 if(FD_ISSET(c->socket, f)) {
248 if(c->status.connecting) {
249 c->status.connecting = false;
250 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
253 finish_connecting(c);
255 ifdebug(CONNECTIONS) logger(LOG_DEBUG,
256 _("Error while connecting to %s (%s): %s"),
257 c->name, c->hostname, strerror(result));
258 closesocket(c->socket);
259 do_outgoing_connection(c);
264 if(!receive_meta(c)) {
265 terminate_connection(c, c->status.active);
271 for(i = 0; i < listen_sockets; i++) {
272 if(FD_ISSET(listen_socket[i].udp, f))
273 handle_incoming_vpn_data(listen_socket[i].udp);
275 if(FD_ISSET(listen_socket[i].tcp, f))
276 handle_new_meta_connection(listen_socket[i].tcp);
281 this is where it all happens...
288 time_t last_ping_check, last_config_check;
293 last_ping_check = now;
294 last_config_check = now;
300 tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
303 maxfd = build_fdset(&fset);
305 r = select(maxfd + 1, &fset, NULL, NULL, &tv);
308 if(errno != EINTR && errno != EAGAIN) {
309 logger(LOG_ERR, _("Error while waiting for input: %s"),
319 check_network_activity(&fset);
326 /* Let's check if everybody is still alive */
328 if(last_ping_check + pingtimeout < now) {
329 check_dead_connections();
330 last_ping_check = now;
332 if(routing_mode == RMODE_SWITCH)
337 /* Should we regenerate our key? */
339 if(keyexpires < now) {
340 ifdebug(STATUS) logger(LOG_INFO, _("Regenerating symmetric key"));
342 RAND_pseudo_bytes(myself->key, myself->keylength);
344 EVP_DecryptInit_ex(&packet_ctx, myself->cipher, NULL, myself->key, myself->key + myself->cipher->key_len);
345 send_key_changed(broadcast, myself);
346 keyexpires = now + keylifetime;
351 while((event = get_expired_event())) {
352 event->handler(event->data);
357 logger(LOG_INFO, _("Flushing event queue"));
359 while(event_tree->head) {
360 event = (event_t *) event_tree->head->data;
361 event->handler(event->data);
375 /* Reread our own configuration file */
377 exit_configuration(&config_tree);
378 init_configuration(&config_tree);
380 if(!read_server_config()) {
381 logger(LOG_ERR, _("Unable to reread configuration file, exitting."));
385 /* Close connections to hosts that have a changed or deleted host config file */
387 for(node = connection_tree->head; node; node = node->next) {
388 c = (connection_t *) node->data;
391 free(c->outgoing->name);
392 freeaddrinfo(c->outgoing->ai);
397 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
398 if(stat(fname, &s) || s.st_mtime > last_config_check)
399 terminate_connection(c, c->status.active);
403 last_config_check = now;
405 /* Try to make outgoing connections */
407 try_outgoing_connections();