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.200 2003/08/28 21:05:10 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) {
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) {
75 send_del_subnet(broadcast, s);
79 for(enode = n->edge_tree->head; enode; enode = enext) {
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) {
94 if(!n->status.reachable) {
95 for(enode = edge_weight_tree->head; enode; enode = enext) {
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) {
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 */
190 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
192 if(report && !c->node->status.reachable) {
194 e = lookup_edge(c->node, myself);
196 send_del_edge(broadcast, e);
202 /* Check if this was our outgoing connection */
205 retry_outgoing(c->outgoing);
211 Check if the other end is active.
212 If we have sent packets, but didn't receive any,
213 then possibly the other end is dead. We send a
214 PING request over the meta connection. If the other
215 end does not reply in time, we consider them dead
216 and close the connection.
218 static void check_dead_connections(void)
220 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"),
233 c->name, c->hostname);
234 c->status.timeout = true;
235 terminate_connection(c, true);
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, *(uint32_t *)&c->status);
246 ifdebug(CONNECTIONS) logger(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
247 c->name, c->hostname);
248 terminate_connection(c, false);
255 check all connections to see if anything
256 happened on their sockets
258 static void check_network_activity(fd_set * f)
263 int len = sizeof(result);
268 if(FD_ISSET(device_fd, f)) {
269 if(read_packet(&packet))
270 route_outgoing(&packet);
273 for(node = connection_tree->head; node; node = node->next) {
279 if(FD_ISSET(c->socket, f)) {
280 if(c->status.connecting) {
281 c->status.connecting = false;
282 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
285 finish_connecting(c);
287 ifdebug(CONNECTIONS) logger(LOG_DEBUG,
288 _("Error while connecting to %s (%s): %s"),
289 c->name, c->hostname, strerror(result));
290 closesocket(c->socket);
291 do_outgoing_connection(c);
296 if(!receive_meta(c)) {
297 terminate_connection(c, c->status.active);
303 for(i = 0; i < listen_sockets; i++) {
304 if(FD_ISSET(listen_socket[i].udp, f))
305 handle_incoming_vpn_data(listen_socket[i].udp);
307 if(FD_ISSET(listen_socket[i].tcp, f))
308 handle_new_meta_connection(listen_socket[i].tcp);
313 this is where it all happens...
320 time_t last_ping_check, last_config_check;
325 last_ping_check = now;
326 last_config_check = now;
334 tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
337 maxfd = build_fdset(&fset);
339 r = select(maxfd + 1, &fset, NULL, NULL, &tv);
342 if(errno != EINTR && errno != EAGAIN) {
343 logger(LOG_ERR, _("Error while waiting for input: %s"),
353 check_network_activity(&fset);
360 /* Let's check if everybody is still alive */
362 if(last_ping_check + pingtimeout < now) {
363 check_dead_connections();
364 last_ping_check = now;
366 if(routing_mode == RMODE_SWITCH)
371 /* Should we regenerate our key? */
373 if(keyexpires < now) {
374 ifdebug(STATUS) logger(LOG_INFO, _("Regenerating symmetric key"));
376 RAND_pseudo_bytes(myself->key, myself->keylength);
378 EVP_DecryptInit_ex(&packet_ctx, myself->cipher, NULL, myself->key, myself->key + myself->cipher->key_len);
379 send_key_changed(broadcast, myself);
380 keyexpires = now + keylifetime;
385 while((event = get_expired_event())) {
386 event->handler(event->data);
391 logger(LOG_INFO, _("Flushing event queue"));
393 while(event_tree->head) {
394 event = event_tree->head->data;
395 event->handler(event->data);
409 /* Reread our own configuration file */
411 exit_configuration(&config_tree);
412 init_configuration(&config_tree);
414 if(!read_server_config()) {
415 logger(LOG_ERR, _("Unable to reread configuration file, exitting."));
419 /* Close connections to hosts that have a changed or deleted host config file */
421 for(node = connection_tree->head; node; node = node->next) {
425 free(c->outgoing->name);
426 freeaddrinfo(c->outgoing->ai);
431 asprintf(&fname, "%s/hosts/%s", confbase, c->name);
432 if(stat(fname, &s) || s.st_mtime > last_config_check)
433 terminate_connection(c, c->status.active);
437 last_config_check = now;
439 /* Try to make outgoing connections */
441 try_outgoing_connections();