2 net.c -- most of the network code
3 Copyright (C) 1998-2002 Ivo Timmermans <itimmermans@bigfoot.com>,
4 2000-2002 Guus Sliepen <guus@sliepen.warande.net>
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.158 2002/02/26 23:26:41 guus Exp $
28 #include <netinet/in.h>
30 #include <netinet/ip.h>
31 #include <netinet/tcp.h>
38 #include <sys/types.h>
41 #include <sys/ioctl.h>
42 /* SunOS really wants sys/socket.h BEFORE net/if.h,
43 and FreeBSD wants these lines below the rest. */
44 #include <arpa/inet.h>
45 #include <sys/socket.h>
48 #include <openssl/rand.h>
56 #include "connection.h"
77 put all file descriptors in an fd_set array
79 void build_fdset(fd_set *fs)
87 for(node = connection_tree->head; node; node = node->next)
89 c = (connection_t *)node->data;
90 FD_SET(c->socket, fs);
93 for(i = 0; i < tcp_sockets; i++)
94 FD_SET(tcp_socket[i], fs);
96 for(i = 0; i < udp_sockets; i++)
97 FD_SET(udp_socket[i], fs);
99 FD_SET(device_fd, fs);
103 /* Purge edges and subnets of unreachable nodes. Use carefully. */
107 avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext, *cnode;
113 if(debug_lvl >= DEBUG_PROTOCOL)
114 syslog(LOG_DEBUG, _("Purging unreachable nodes"));
116 for(nnode = node_tree->head; nnode; nnode = nnext)
119 n = (node_t *)nnode->data;
121 if(!n->status.reachable)
123 if(debug_lvl >= DEBUG_SCARY_THINGS)
124 syslog(LOG_DEBUG, _("Purging node %s (%s)"), n->name, n->hostname);
126 for(snode = n->subnet_tree->head; snode; snode = snext)
129 s = (subnet_t *)snode->data;
131 for(cnode = connection_tree->head; cnode; cnode = cnode->next)
133 c = (connection_t *)cnode->data;
135 send_del_subnet(c, s);
141 for(enode = n->edge_tree->head; enode; enode = enext)
144 e = (edge_t *)enode->data;
146 for(cnode = connection_tree->head; cnode; cnode = cnode->next)
148 c = (connection_t *)cnode->data;
163 Terminate a connection:
165 - Remove associated edge and tell other connections about it if report = 1
166 - Check if we need to retry making an outgoing connection
167 - Deactivate the host
169 void terminate_connection(connection_t *c, int report)
177 if(debug_lvl >= DEBUG_CONNECTIONS)
178 syslog(LOG_NOTICE, _("Closing connection with %s (%s)"),
179 c->name, c->hostname);
181 c->status.remove = 1;
190 for(node = connection_tree->head; node; node = node->next)
192 other = (connection_t *)node->data;
193 if(other->status.active && other != c)
194 send_del_edge(other, c->edge);
201 /* Run MST and SSSP algorithms */
205 /* Check if this was our outgoing connection */
209 retry_outgoing(c->outgoing);
215 c->status.active = 0;
217 c->node->connection = NULL;
223 Check if the other end is active.
224 If we have sent packets, but didn't receive any,
225 then possibly the other end is dead. We send a
226 PING request over the meta connection. If the other
227 end does not reply in time, we consider them dead
228 and close the connection.
230 void check_dead_connections(void)
233 avl_node_t *node, *next;
238 for(node = connection_tree->head; node; node = next)
241 c = (connection_t *)node->data;
242 if(c->last_ping_time + pingtimeout < now)
248 if(debug_lvl >= DEBUG_PROTOCOL)
249 syslog(LOG_INFO, _("%s (%s) didn't respond to PING"),
250 c->name, c->hostname);
251 c->status.timeout = 1;
252 terminate_connection(c, 1);
261 if(debug_lvl >= DEBUG_CONNECTIONS)
262 syslog(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
263 c->name, c->hostname);
264 terminate_connection(c, 0);
272 check all connections to see if anything
273 happened on their sockets
275 void check_network_activity(fd_set *f)
280 int len = sizeof(result);
283 if(FD_ISSET(device_fd, f))
285 if(!read_packet(&packet))
286 route_outgoing(&packet);
289 for(i = 0; i < udp_sockets; i++)
290 if(FD_ISSET(udp_socket[i], f))
291 handle_incoming_vpn_data(udp_socket[i]);
293 for(node = connection_tree->head; node; node = node->next)
295 c = (connection_t *)node->data;
300 if(FD_ISSET(c->socket, f))
302 if(c->status.connecting)
304 c->status.connecting = 0;
305 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
307 finish_connecting(c);
310 if(debug_lvl >= DEBUG_CONNECTIONS)
311 syslog(LOG_DEBUG, _("Error while connecting to %s (%s): %s"), c->name, c->hostname, strerror(result));
313 do_outgoing_connection(c);
317 if(receive_meta(c) < 0)
319 terminate_connection(c, c->status.active);
325 for(i = 0; i < tcp_sockets; i++)
326 if(FD_ISSET(tcp_socket[i], f))
327 handle_new_meta_connection(tcp_socket[i]);
331 void prune_connections(void)
334 avl_node_t *node, *next;
336 for(node = connection_tree->head; node; node = next)
339 c = (connection_t *)node->data;
345 if(!connection_tree->head)
351 this is where it all happens...
358 time_t last_ping_check;
362 last_ping_check = time(NULL);
368 tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
373 if((r = select(FD_SETSIZE, &fset, NULL, NULL, &tv)) < 0)
375 if(errno != EINTR) /* because of a signal */
377 syslog(LOG_ERR, _("Error while waiting for input: %s"), strerror(errno));
383 check_network_activity(&fset);
399 /* Let's check if everybody is still alive */
401 if(last_ping_check + pingtimeout < t)
403 check_dead_connections();
404 last_ping_check = time(NULL);
406 /* Should we regenerate our key? */
410 if(debug_lvl >= DEBUG_STATUS)
411 syslog(LOG_INFO, _("Regenerating symmetric key"));
413 RAND_pseudo_bytes(myself->key, myself->keylength);
414 send_key_changed(myself->connection, myself);
415 keyexpires = time(NULL) + keylifetime;
420 while((event = get_expired_event()))
422 event->handler(event->data);
428 syslog(LOG_INFO, _("Flushing event queue"));
430 while(event_tree->head)
432 event = (event_t *)event_tree->head->data;
433 event->handler(event->data);
442 close_network_connections();
443 exit_configuration(&config_tree);
445 syslog(LOG_INFO, _("Rereading configuration file and restarting in 5 seconds"));
448 init_configuration(&config_tree);
450 if(read_server_config())
452 syslog(LOG_ERR, _("Unable to reread configuration file, exiting"));
456 if(setup_network_connections())