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.171 2002/05/01 09:15:58 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"
71 #ifndef HAVE_RAND_PSEUDO_BYTES
72 #define RAND_pseudo_bytes RAND_bytes
81 /* Purge edges and subnets of unreachable nodes. Use carefully. */
85 avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext, *cnode;
91 if(debug_lvl >= DEBUG_PROTOCOL)
92 syslog(LOG_DEBUG, _("Purging unreachable nodes"));
94 for(nnode = node_tree->head; nnode; nnode = nnext)
97 n = (node_t *)nnode->data;
99 if(!n->status.reachable)
101 if(debug_lvl >= DEBUG_SCARY_THINGS)
102 syslog(LOG_DEBUG, _("Purging node %s (%s)"), n->name, n->hostname);
104 for(snode = n->subnet_tree->head; snode; snode = snext)
107 s = (subnet_t *)snode->data;
109 for(cnode = connection_tree->head; cnode; cnode = cnode->next)
111 c = (connection_t *)cnode->data;
113 send_del_subnet(c, s);
119 for(enode = n->edge_tree->head; enode; enode = enext)
122 e = (edge_t *)enode->data;
124 for(cnode = connection_tree->head; cnode; cnode = cnode->next)
126 c = (connection_t *)cnode->data;
141 put all file descriptors in an fd_set array
142 While we're at it, purge stuff that needs to be removed.
144 void build_fdset(fd_set *fs)
146 avl_node_t *node, *next;
152 for(node = connection_tree->head; node; node = next)
155 c = (connection_t *)node->data;
160 if(!connection_tree->head)
164 FD_SET(c->socket, fs);
167 for(i = 0; i < listen_sockets; i++)
169 FD_SET(listen_socket[i].tcp, fs);
170 FD_SET(listen_socket[i].udp, fs);
173 FD_SET(device_fd, fs);
178 Terminate a connection:
180 - Remove associated edge and tell other connections about it if report = 1
181 - Check if we need to retry making an outgoing connection
182 - Deactivate the host
184 void terminate_connection(connection_t *c, int report)
192 if(debug_lvl >= DEBUG_CONNECTIONS)
193 syslog(LOG_NOTICE, _("Closing connection with %s (%s)"),
194 c->name, c->hostname);
196 c->status.remove = 1;
197 c->status.active = 0;
200 c->node->connection = NULL;
209 for(node = connection_tree->head; node; node = node->next)
211 other = (connection_t *)node->data;
212 if(other->status.active && other != c)
213 send_del_edge(other, c->edge);
219 /* Run MST and SSSP algorithms */
224 /* Check if this was our outgoing connection */
228 retry_outgoing(c->outgoing);
235 Check if the other end is active.
236 If we have sent packets, but didn't receive any,
237 then possibly the other end is dead. We send a
238 PING request over the meta connection. If the other
239 end does not reply in time, we consider them dead
240 and close the connection.
242 void check_dead_connections(void)
244 avl_node_t *node, *next;
247 for(node = connection_tree->head; node; node = next)
250 c = (connection_t *)node->data;
251 if(c->last_ping_time + pingtimeout < now)
257 if(debug_lvl >= DEBUG_PROTOCOL)
258 syslog(LOG_INFO, _("%s (%s) didn't respond to PING"),
259 c->name, c->hostname);
260 c->status.timeout = 1;
261 terminate_connection(c, 1);
270 if(debug_lvl >= DEBUG_CONNECTIONS)
271 syslog(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
272 c->name, c->hostname);
273 terminate_connection(c, 0);
281 check all connections to see if anything
282 happened on their sockets
284 void check_network_activity(fd_set *f)
289 int len = sizeof(result);
292 if(FD_ISSET(device_fd, f))
294 if(!read_packet(&packet))
295 route_outgoing(&packet);
298 for(node = connection_tree->head; node; node = node->next)
300 c = (connection_t *)node->data;
305 if(FD_ISSET(c->socket, f))
307 if(c->status.connecting)
309 c->status.connecting = 0;
310 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
312 finish_connecting(c);
315 if(debug_lvl >= DEBUG_CONNECTIONS)
316 syslog(LOG_DEBUG, _("Error while connecting to %s (%s): %s"), c->name, c->hostname, strerror(result));
318 do_outgoing_connection(c);
322 if(receive_meta(c) < 0)
324 terminate_connection(c, c->status.active);
330 for(i = 0; i < listen_sockets; i++)
332 if(FD_ISSET(listen_socket[i].udp, f))
333 handle_incoming_vpn_data(listen_socket[i].udp);
334 if(FD_ISSET(listen_socket[i].tcp, f))
335 handle_new_meta_connection(listen_socket[i].tcp);
341 this is where it all happens...
348 time_t last_ping_check;
351 last_ping_check = now;
359 tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
364 if((r = select(FD_SETSIZE, &fset, NULL, NULL, &tv)) < 0)
366 if(errno != EINTR && errno != EAGAIN)
368 syslog(LOG_ERR, _("Error while waiting for input: %s"), strerror(errno));
377 check_network_activity(&fset);
385 /* Let's check if everybody is still alive */
387 if(last_ping_check + pingtimeout < now)
389 check_dead_connections();
390 last_ping_check = now;
392 if(routing_mode== RMODE_SWITCH)
397 /* Should we regenerate our key? */
401 if(debug_lvl >= DEBUG_STATUS)
402 syslog(LOG_INFO, _("Regenerating symmetric key"));
404 RAND_pseudo_bytes(myself->key, myself->keylength);
405 send_key_changed(myself->connection, myself);
406 keyexpires = now + keylifetime;
411 while((event = get_expired_event()))
413 event->handler(event->data);
419 syslog(LOG_INFO, _("Flushing event queue"));
421 while(event_tree->head)
423 event = (event_t *)event_tree->head->data;
424 event->handler(event->data);
433 close_network_connections();
434 exit_configuration(&config_tree);
436 syslog(LOG_INFO, _("Rereading configuration file and restarting in 5 seconds..."));
439 init_configuration(&config_tree);
441 if(read_server_config())
443 syslog(LOG_ERR, _("Unable to reread configuration file, exitting."));
447 if(setup_network_connections())