2 net.c -- most of the network code
3 Copyright (C) 1998-2002 Ivo Timmermans <ivo@o2w.nl>,
4 2000-2002 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.176 2002/09/04 13:48:51 guus Exp $
28 #include <netinet/in.h>
29 #ifdef HAVE_NETINET_IN_SYSTM_H
30 #include <netinet/in_systm.h>
32 #ifdef HAVE_NETINET_IP_H
33 #include <netinet/ip.h>
35 #ifdef HAVE_NETINET_TCP_H
36 #include <netinet/tcp.h>
43 #include <sys/types.h>
46 #include <sys/ioctl.h>
47 /* SunOS really wants sys/socket.h BEFORE net/if.h,
48 and FreeBSD wants these lines below the rest. */
49 #include <arpa/inet.h>
50 #include <sys/socket.h>
53 #include <openssl/rand.h>
61 #include "connection.h"
76 #ifndef HAVE_RAND_PSEUDO_BYTES
77 #define RAND_pseudo_bytes RAND_bytes
86 /* Purge edges and subnets of unreachable nodes. Use carefully. */
90 avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext, *cnode;
96 if(debug_lvl >= DEBUG_PROTOCOL)
97 syslog(LOG_DEBUG, _("Purging unreachable nodes"));
99 for(nnode = node_tree->head; nnode; nnode = nnext)
102 n = (node_t *)nnode->data;
104 if(!n->status.reachable)
106 if(debug_lvl >= DEBUG_SCARY_THINGS)
107 syslog(LOG_DEBUG, _("Purging node %s (%s)"), n->name, n->hostname);
109 for(snode = n->subnet_tree->head; snode; snode = snext)
112 s = (subnet_t *)snode->data;
114 for(cnode = connection_tree->head; cnode; cnode = cnode->next)
116 c = (connection_t *)cnode->data;
118 send_del_subnet(c, s);
124 for(enode = n->edge_tree->head; enode; enode = enext)
127 e = (edge_t *)enode->data;
129 for(cnode = connection_tree->head; cnode; cnode = cnode->next)
131 c = (connection_t *)cnode->data;
146 put all file descriptors in an fd_set array
147 While we're at it, purge stuff that needs to be removed.
149 void build_fdset(fd_set *fs)
151 avl_node_t *node, *next;
157 for(node = connection_tree->head; node; node = next)
160 c = (connection_t *)node->data;
165 if(!connection_tree->head)
169 FD_SET(c->socket, fs);
172 for(i = 0; i < listen_sockets; i++)
174 FD_SET(listen_socket[i].tcp, fs);
175 FD_SET(listen_socket[i].udp, fs);
178 FD_SET(device_fd, fs);
183 Terminate a connection:
185 - Remove associated edge and tell other connections about it if report = 1
186 - Check if we need to retry making an outgoing connection
187 - Deactivate the host
189 void terminate_connection(connection_t *c, int report)
197 if(debug_lvl >= DEBUG_CONNECTIONS)
198 syslog(LOG_NOTICE, _("Closing connection with %s (%s)"),
199 c->name, c->hostname);
201 c->status.remove = 1;
202 c->status.active = 0;
205 c->node->connection = NULL;
214 for(node = connection_tree->head; node; node = node->next)
216 other = (connection_t *)node->data;
217 if(other->status.active && other != c)
218 send_del_edge(other, c->edge);
224 /* Run MST and SSSP algorithms */
229 /* Check if this was our outgoing connection */
233 retry_outgoing(c->outgoing);
240 Check if the other end is active.
241 If we have sent packets, but didn't receive any,
242 then possibly the other end is dead. We send a
243 PING request over the meta connection. If the other
244 end does not reply in time, we consider them dead
245 and close the connection.
247 void check_dead_connections(void)
249 avl_node_t *node, *next;
252 for(node = connection_tree->head; node; node = next)
255 c = (connection_t *)node->data;
256 if(c->last_ping_time + pingtimeout < now)
262 if(debug_lvl >= DEBUG_PROTOCOL)
263 syslog(LOG_INFO, _("%s (%s) didn't respond to PING"),
264 c->name, c->hostname);
265 c->status.timeout = 1;
266 terminate_connection(c, 1);
275 if(debug_lvl >= DEBUG_CONNECTIONS)
276 syslog(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
277 c->name, c->hostname);
278 terminate_connection(c, 0);
286 check all connections to see if anything
287 happened on their sockets
289 void check_network_activity(fd_set *f)
294 int len = sizeof(result);
297 if(FD_ISSET(device_fd, f))
299 if(!read_packet(&packet))
300 route_outgoing(&packet);
303 for(node = connection_tree->head; node; node = node->next)
305 c = (connection_t *)node->data;
310 if(FD_ISSET(c->socket, f))
312 if(c->status.connecting)
314 c->status.connecting = 0;
315 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
317 finish_connecting(c);
320 if(debug_lvl >= DEBUG_CONNECTIONS)
321 syslog(LOG_DEBUG, _("Error while connecting to %s (%s): %s"), c->name, c->hostname, strerror(result));
323 do_outgoing_connection(c);
327 if(receive_meta(c) < 0)
329 terminate_connection(c, c->status.active);
335 for(i = 0; i < listen_sockets; i++)
337 if(FD_ISSET(listen_socket[i].udp, f))
338 handle_incoming_vpn_data(listen_socket[i].udp);
339 if(FD_ISSET(listen_socket[i].tcp, f))
340 handle_new_meta_connection(listen_socket[i].tcp);
346 this is where it all happens...
353 time_t last_ping_check;
356 last_ping_check = now;
364 tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
369 if((r = select(FD_SETSIZE, &fset, NULL, NULL, &tv)) < 0)
371 if(errno != EINTR && errno != EAGAIN)
373 syslog(LOG_ERR, _("Error while waiting for input: %s"), strerror(errno));
382 check_network_activity(&fset);
390 /* Let's check if everybody is still alive */
392 if(last_ping_check + pingtimeout < now)
394 check_dead_connections();
395 last_ping_check = now;
397 if(routing_mode== RMODE_SWITCH)
402 /* Should we regenerate our key? */
406 if(debug_lvl >= DEBUG_STATUS)
407 syslog(LOG_INFO, _("Regenerating symmetric key"));
409 RAND_pseudo_bytes(myself->key, myself->keylength);
410 send_key_changed(myself->connection, myself);
411 keyexpires = now + keylifetime;
416 while((event = get_expired_event()))
418 event->handler(event->data);
424 syslog(LOG_INFO, _("Flushing event queue"));
426 while(event_tree->head)
428 event = (event_t *)event_tree->head->data;
429 event->handler(event->data);
438 close_network_connections();
439 exit_configuration(&config_tree);
441 syslog(LOG_INFO, _("Rereading configuration file and restarting in 5 seconds..."));
444 init_configuration(&config_tree);
446 if(read_server_config())
448 syslog(LOG_ERR, _("Unable to reread configuration file, exitting."));
452 if(setup_network_connections())