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.164 2002/03/18 22:47:20 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"
79 put all file descriptors in an fd_set array
81 void build_fdset(fd_set *fs)
89 for(node = connection_tree->head; node; node = node->next)
91 c = (connection_t *)node->data;
92 FD_SET(c->socket, fs);
95 for(i = 0; i < listen_sockets; i++)
97 FD_SET(listen_socket[i].tcp, fs);
98 FD_SET(listen_socket[i].udp, fs);
101 FD_SET(device_fd, fs);
105 /* Purge edges and subnets of unreachable nodes. Use carefully. */
109 avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext, *cnode;
115 if(debug_lvl >= DEBUG_PROTOCOL)
116 syslog(LOG_DEBUG, _("Purging unreachable nodes"));
118 for(nnode = node_tree->head; nnode; nnode = nnext)
121 n = (node_t *)nnode->data;
123 if(!n->status.reachable)
125 if(debug_lvl >= DEBUG_SCARY_THINGS)
126 syslog(LOG_DEBUG, _("Purging node %s (%s)"), n->name, n->hostname);
128 for(snode = n->subnet_tree->head; snode; snode = snext)
131 s = (subnet_t *)snode->data;
133 for(cnode = connection_tree->head; cnode; cnode = cnode->next)
135 c = (connection_t *)cnode->data;
137 send_del_subnet(c, s);
143 for(enode = n->edge_tree->head; enode; enode = enext)
146 e = (edge_t *)enode->data;
148 for(cnode = connection_tree->head; cnode; cnode = cnode->next)
150 c = (connection_t *)cnode->data;
165 Terminate a connection:
167 - Remove associated edge and tell other connections about it if report = 1
168 - Check if we need to retry making an outgoing connection
169 - Deactivate the host
171 void terminate_connection(connection_t *c, int report)
179 if(debug_lvl >= DEBUG_CONNECTIONS)
180 syslog(LOG_NOTICE, _("Closing connection with %s (%s)"),
181 c->name, c->hostname);
183 c->status.remove = 1;
192 for(node = connection_tree->head; node; node = node->next)
194 other = (connection_t *)node->data;
195 if(other->status.active && other != c)
196 send_del_edge(other, c->edge);
203 /* Run MST and SSSP algorithms */
207 /* Check if this was our outgoing connection */
211 retry_outgoing(c->outgoing);
217 c->status.active = 0;
219 c->node->connection = NULL;
225 Check if the other end is active.
226 If we have sent packets, but didn't receive any,
227 then possibly the other end is dead. We send a
228 PING request over the meta connection. If the other
229 end does not reply in time, we consider them dead
230 and close the connection.
232 void check_dead_connections(void)
234 avl_node_t *node, *next;
237 for(node = connection_tree->head; node; node = next)
240 c = (connection_t *)node->data;
241 if(c->last_ping_time + pingtimeout < now)
247 if(debug_lvl >= DEBUG_PROTOCOL)
248 syslog(LOG_INFO, _("%s (%s) didn't respond to PING"),
249 c->name, c->hostname);
250 c->status.timeout = 1;
251 terminate_connection(c, 1);
260 if(debug_lvl >= DEBUG_CONNECTIONS)
261 syslog(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
262 c->name, c->hostname);
263 terminate_connection(c, 0);
271 check all connections to see if anything
272 happened on their sockets
274 void check_network_activity(fd_set *f)
279 int len = sizeof(result);
282 if(FD_ISSET(device_fd, f))
284 if(!read_packet(&packet))
285 route_outgoing(&packet);
288 for(i = 0; i < listen_sockets; i++)
290 if(FD_ISSET(listen_socket[i].udp, f))
291 handle_incoming_vpn_data(listen_socket[i].udp);
292 if(FD_ISSET(listen_socket[i].tcp, f))
293 handle_new_meta_connection(listen_socket[i].tcp);
296 for(node = connection_tree->head; node; node = node->next)
298 c = (connection_t *)node->data;
303 if(FD_ISSET(c->socket, f))
305 if(c->status.connecting)
307 c->status.connecting = 0;
308 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
310 finish_connecting(c);
313 if(debug_lvl >= DEBUG_CONNECTIONS)
314 syslog(LOG_DEBUG, _("Error while connecting to %s (%s): %s"), c->name, c->hostname, strerror(result));
316 do_outgoing_connection(c);
320 if(receive_meta(c) < 0)
322 terminate_connection(c, c->status.active);
330 void prune_connections(void)
333 avl_node_t *node, *next;
335 for(node = connection_tree->head; node; node = next)
338 c = (connection_t *)node->data;
344 if(!connection_tree->head)
350 this is where it all happens...
357 time_t last_ping_check;
361 last_ping_check = now;
369 tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */
380 if((r = select(FD_SETSIZE, &fset, NULL, NULL, &tv)) < 0)
382 if(errno != EINTR) /* because of a signal */
384 syslog(LOG_ERR, _("Error while waiting for input: %s"), strerror(errno));
390 check_network_activity(&fset);
398 /* Let's check if everybody is still alive */
400 if(last_ping_check + pingtimeout < now)
402 check_dead_connections();
403 last_ping_check = now;
405 if(routing_mode== RMODE_SWITCH)
408 /* Should we regenerate our key? */
412 if(debug_lvl >= DEBUG_STATUS)
413 syslog(LOG_INFO, _("Regenerating symmetric key"));
415 RAND_pseudo_bytes(myself->key, myself->keylength);
416 send_key_changed(myself->connection, myself);
417 keyexpires = now + keylifetime;
422 while((event = get_expired_event()))
424 event->handler(event->data);
430 syslog(LOG_INFO, _("Flushing event queue"));
432 while(event_tree->head)
434 event = (event_t *)event_tree->head->data;
435 event->handler(event->data);
444 close_network_connections();
445 exit_configuration(&config_tree);
447 syslog(LOG_INFO, _("Rereading configuration file and restarting in 5 seconds..."));
450 init_configuration(&config_tree);
452 if(read_server_config())
454 syslog(LOG_ERR, _("Unable to reread configuration file, exitting."));
458 if(setup_network_connections())