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.1 2002/04/28 12:46:26 zarq Exp $
28 #include <netinet/in.h>
30 #include <netinet/ip.h>
31 #include <netinet/tcp.h>
38 #include <sys/types.h>
40 #include <sys/ioctl.h>
41 /* SunOS really wants sys/socket.h BEFORE net/if.h,
42 and FreeBSD wants these lines below the rest. */
43 #include <arpa/inet.h>
44 #include <sys/socket.h>
47 #include <openssl/rand.h>
57 #include "interface.h"
58 #include "connection.h"
82 put all file descriptors in an fd_set array
84 void build_fdset(fd_set *fs)
92 for(node = connection_tree->head; node; node = node->next)
94 c = (connection_t *)node->data;
95 FD_SET(c->socket, fs);
98 for(i = 0; i < listen_sockets; i++)
100 FD_SET(listen_socket[i].tcp, fs);
101 FD_SET(listen_socket[i].udp, fs);
106 /* Purge edges and subnets of unreachable nodes. Use carefully. */
110 avl_node_t *nnode, *nnext, *enode, *enext, *snode, *snext, *cnode;
116 log(DEBUG_PROTOCOL, TLOG_DEBUG,
117 _("Purging unreachable nodes"));
119 for(nnode = node_tree->head; nnode; nnode = nnext)
122 n = (node_t *)nnode->data;
124 if(!n->status.reachable)
126 if(debug_lvl >= DEBUG_SCARY_THINGS)
127 syslog(LOG_DEBUG, _("Purging node %s (%s)"), n->name, n->hostname);
129 for(snode = n->subnet_tree->head; snode; snode = snext)
132 s = (subnet_t *)snode->data;
134 for(cnode = connection_tree->head; cnode; cnode = cnode->next)
136 c = (connection_t *)cnode->data;
138 send_del_subnet(c, s);
144 for(enode = n->edge_tree->head; enode; enode = enext)
147 e = (edge_t *)enode->data;
149 for(cnode = connection_tree->head; cnode; cnode = cnode->next)
151 c = (connection_t *)cnode->data;
166 Terminate a connection:
168 - Remove associated edge and tell other connections about it if report = 1
169 - Check if we need to retry making an outgoing connection
170 - Deactivate the host
172 void terminate_connection(connection_t *c, int report)
180 if(debug_lvl >= DEBUG_CONNECTIONS)
181 syslog(LOG_NOTICE, _("Closing connection with %s (%s)"),
182 c->name, c->hostname);
184 c->status.remove = 1;
193 for(node = connection_tree->head; node; node = node->next)
195 other = (connection_t *)node->data;
196 if(other->status.active && other != c)
197 send_del_edge(other, c->edge);
203 /* Run MST and SSSP algorithms */
208 /* Check if this was our outgoing connection */
212 retry_outgoing(c->outgoing);
218 c->status.active = 0;
220 c->node->connection = NULL;
226 Check if the other end is active.
227 If we have sent packets, but didn't receive any,
228 then possibly the other end is dead. We send a
229 PING request over the meta connection. If the other
230 end does not reply in time, we consider them dead
231 and close the connection.
233 void check_dead_connections(void)
235 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);
282 for(i = 0; i < listen_sockets; i++)
284 if(FD_ISSET(listen_socket[i].tcp, f))
285 handle_new_meta_connection(listen_socket[i].tcp);
288 for(node = connection_tree->head; node; node = node->next)
290 c = (connection_t *)node->data;
295 if(FD_ISSET(c->socket, f))
297 if(c->status.connecting)
299 c->status.connecting = 0;
300 getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
302 finish_connecting(c);
305 if(debug_lvl >= DEBUG_CONNECTIONS)
306 syslog(LOG_DEBUG, _("Error while connecting to %s (%s): %s"), c->name, c->hostname, strerror(result));
308 do_outgoing_connection(c);
312 if(receive_meta(c) < 0)
314 terminate_connection(c, c->status.active);
322 void prune_connections(void)
325 avl_node_t *node, *next;
327 for(node = connection_tree->head; node; node = next)
330 c = (connection_t *)node->data;
336 if(!connection_tree->head)
342 this is where it all happens...
349 time_t last_ping_check;
352 last_ping_check = now;
360 /* tv.tv_sec = 1 + (rand() & 7); /\* Approx. 5 seconds, randomized to prevent global synchronisation effects *\/ */
361 /* tv.tv_usec = 0; */
373 while(gtk_events_pending())
374 if(gtk_main_iteration() == FALSE)
377 if((r = select(FD_SETSIZE, &fset, NULL, NULL, &tv)) < 0)
379 if(errno != EINTR) /* because of a signal */
381 syslog(LOG_ERR, _("Error while waiting for input: %s"), strerror(errno));
387 check_network_activity(&fset);
395 /* Let's check if everybody is still alive */
397 if(last_ping_check + pingtimeout < now)
399 check_dead_connections();
400 last_ping_check = now;
402 if(routing_mode== RMODE_SWITCH)
407 /* Should we regenerate our key? */
411 if(debug_lvl >= DEBUG_STATUS)
412 syslog(LOG_INFO, _("Regenerating symmetric key"));
414 RAND_pseudo_bytes(myself->key, myself->keylength);
415 send_key_changed(myself->connection, myself);
416 keyexpires = now + keylifetime;
421 while((event = get_expired_event()))
423 event->handler(event->data);
429 syslog(LOG_INFO, _("Flushing event queue"));
431 while(event_tree->head)
433 event = (event_t *)event_tree->head->data;
434 event->handler(event->data);
443 close_network_connections();
444 exit_configuration(&config_tree);
446 syslog(LOG_INFO, _("Rereading configuration file and restarting in 5 seconds..."));
449 init_configuration(&config_tree);
451 if(read_server_config())
453 syslog(LOG_ERR, _("Unable to reread configuration file, exitting."));
457 if(setup_network_connections())