From 14979f835df4214a7c2510852f7ffedc9e08c2c0 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Fri, 1 Mar 2002 14:09:31 +0000 Subject: [PATCH] - Global time_t now, so that we don't have to call time() too often. - MAC addresses expire after a time configurable by MACExpire (default 600 seconds) --- src/event.c | 5 +++-- src/meta.c | 4 ++-- src/net.c | 26 ++++++++++++++------------ src/net.h | 3 ++- src/net_packet.c | 7 +++---- src/net_setup.c | 9 +++++++-- src/net_socket.c | 10 +++++----- src/protocol_misc.c | 4 ++-- src/route.c | 28 +++++++++++++++++++++++++++- src/route.h | 4 +++- src/subnet.h | 3 ++- 11 files changed, 70 insertions(+), 33 deletions(-) diff --git a/src/event.c b/src/event.c index d11f91c..028483b 100644 --- a/src/event.c +++ b/src/event.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: event.c,v 1.1.4.1 2002/02/11 10:05:58 guus Exp $ + $Id: event.c,v 1.1.4.2 2002/03/01 14:09:30 guus Exp $ */ #include "config.h" @@ -34,6 +34,7 @@ #include "system.h" avl_tree_t *event_tree; +extern time_t now; int id; @@ -98,7 +99,7 @@ cp if(event_tree->head) { event = (event_t *)event_tree->head->data; - if(event->time < time(NULL)) + if(event->time < now) { avl_delete(event_tree, event); return event; diff --git a/src/meta.c b/src/meta.c index f2326d1..b51c64d 100644 --- a/src/meta.c +++ b/src/meta.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: meta.c,v 1.1.2.24 2002/02/18 16:25:16 guus Exp $ + $Id: meta.c,v 1.1.2.25 2002/03/01 14:09:31 guus Exp $ */ #include "config.h" @@ -204,7 +204,7 @@ cp return -1; } - c->last_ping_time = time(NULL); + c->last_ping_time = now; cp return 0; } diff --git a/src/net.c b/src/net.c index 5632727..a1b1705 100644 --- a/src/net.c +++ b/src/net.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: net.c,v 1.35.4.159 2002/03/01 13:18:54 guus Exp $ + $Id: net.c,v 1.35.4.160 2002/03/01 14:09:31 guus Exp $ */ #include "config.h" @@ -73,6 +73,8 @@ int do_purge = 0; int sighup = 0; int sigalrm = 0; +time_t now = 0; + /* put all file descriptors in an fd_set array */ @@ -229,12 +231,9 @@ cp */ void check_dead_connections(void) { - time_t now; avl_node_t *node, *next; connection_t *c; cp - now = time(NULL); - for(node = connection_tree->head; node; node = next) { next = node->next; @@ -359,12 +358,14 @@ void main_loop(void) int t; event_t *event; cp - last_ping_check = time(NULL); + last_ping_check = now; - srand(time(NULL)); + srand(now); for(;;) { + now = time(NULL); + tv.tv_sec = 1 + (rand() & 7); /* Approx. 5 seconds, randomized to prevent global synchronisation effects */ tv.tv_usec = 0; @@ -394,25 +395,26 @@ cp do_purge = 0; } - t = time(NULL); - /* Let's check if everybody is still alive */ - if(last_ping_check + pingtimeout < t) + if(last_ping_check + pingtimeout < now) { check_dead_connections(); - last_ping_check = time(NULL); + last_ping_check = now; + + if(routing_mode != RMODE_ROUTER) + age_mac(); /* Should we regenerate our key? */ - if(keyexpires < t) + if(keyexpires < now) { if(debug_lvl >= DEBUG_STATUS) syslog(LOG_INFO, _("Regenerating symmetric key")); RAND_pseudo_bytes(myself->key, myself->keylength); send_key_changed(myself->connection, myself); - keyexpires = time(NULL) + keylifetime; + keyexpires = now + keylifetime; } } diff --git a/src/net.h b/src/net.h index 6a98118..07e589a 100644 --- a/src/net.h +++ b/src/net.h @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: net.h,v 1.9.4.45 2002/03/01 13:18:54 guus Exp $ + $Id: net.h,v 1.9.4.46 2002/03/01 14:09:31 guus Exp $ */ #ifndef __TINC_NET_H__ @@ -115,6 +115,7 @@ extern int keylifetime; extern int do_prune; extern int do_purge; extern char *myport; +extern time_t now; extern void retry_outgoing(outgoing_t *); extern void handle_incoming_vpn_data(int); diff --git a/src/net_packet.c b/src/net_packet.c index 81d759a..bffe4a7 100644 --- a/src/net_packet.c +++ b/src/net_packet.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: net_packet.c,v 1.1.2.6 2002/03/01 12:26:56 guus Exp $ + $Id: net_packet.c,v 1.1.2.7 2002/03/01 14:09:31 guus Exp $ */ #include "config.h" @@ -399,10 +399,9 @@ cp return; } -/* if(n->connection) - n->connection->last_ping_time = time(NULL); -*/ + n->connection->last_ping_time = now; + receive_udppacket(n, &pkt); cp } diff --git a/src/net_setup.c b/src/net_setup.c index 6887fe3..f8a8b99 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: net_setup.c,v 1.1.2.7 2002/03/01 13:18:54 guus Exp $ + $Id: net_setup.c,v 1.1.2.8 2002/03/01 14:09:31 guus Exp $ */ #include "config.h" @@ -328,6 +328,9 @@ cp get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance); + if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire)) + macexpire= 600; + if(get_config_int(lookup_config(myself->connection->config_tree, "MaxTimeout"), &maxtimeout)) { if(maxtimeout <= 0) @@ -392,7 +395,7 @@ cp if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime)) keylifetime = 3600; - keyexpires = time(NULL) + keylifetime; + keyexpires = now + keylifetime; /* Check if we want to use message authentication codes... */ @@ -514,6 +517,8 @@ cp int setup_network_connections(void) { cp + now = time(NULL); + init_connections(); init_subnets(); init_nodes(); diff --git a/src/net_socket.c b/src/net_socket.c index 39bbbaa..63760af 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: net_socket.c,v 1.1.2.6 2002/03/01 13:18:54 guus Exp $ + $Id: net_socket.c,v 1.1.2.7 2002/03/01 14:09:31 guus Exp $ */ #include "config.h" @@ -204,7 +204,7 @@ cp event = new_event(); event->handler = (event_handler_t)setup_outgoing_connection; - event->time = time(NULL) + outgoing->timeout; + event->time = now + outgoing->timeout; event->data = outgoing; event_add(event); @@ -260,7 +260,7 @@ cp if(debug_lvl >= DEBUG_CONNECTIONS) syslog(LOG_INFO, _("Connected to %s (%s)"), c->name, c->hostname); - c->last_ping_time = time(NULL); + c->last_ping_time = now; send_id(c); cp @@ -407,7 +407,7 @@ cp } c->outgoing = outgoing; - c->last_ping_time = time(NULL); + c->last_ping_time = now; connection_add(c); @@ -439,7 +439,7 @@ cp c->address = sa; c->hostname = sockaddr2hostname(&sa); c->socket = fd; - c->last_ping_time = time(NULL); + c->last_ping_time = now; if(debug_lvl >= DEBUG_CONNECTIONS) syslog(LOG_NOTICE, _("Connection from %s"), c->hostname); diff --git a/src/protocol_misc.c b/src/protocol_misc.c index 5863f9e..0e4a86d 100644 --- a/src/protocol_misc.c +++ b/src/protocol_misc.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: protocol_misc.c,v 1.1.4.1 2002/02/11 10:05:58 guus Exp $ + $Id: protocol_misc.c,v 1.1.4.2 2002/03/01 14:09:31 guus Exp $ */ #include "config.h" @@ -122,7 +122,7 @@ int send_ping(connection_t *c) { cp c->status.pinged = 1; - c->last_ping_time = time(NULL); + c->last_ping_time = now; cp return send_request(c, "%d", PING); } diff --git a/src/route.c b/src/route.c index a6790c9..d76bd9b 100644 --- a/src/route.c +++ b/src/route.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: route.c,v 1.1.2.25 2002/03/01 12:26:56 guus Exp $ + $Id: route.c,v 1.1.2.26 2002/03/01 14:09:31 guus Exp $ */ #include "config.h" @@ -52,6 +52,7 @@ int routing_mode = RMODE_ROUTER; int priorityinheritance = 0; +int macexpire = 600; subnet_t mymac; void learn_mac(mac_t *address) @@ -84,6 +85,31 @@ cp send_add_subnet(c, subnet); } } + + subnet->net.mac.lastseen = now; +} + +void age_mac(void) +{ + subnet_t *s; + connection_t *c; + avl_node_t *node, *next, *node2; +cp + for(node = myself->subnet_tree->head; node; node = next) + { + s = (subnet_t *)node->data; + if(s->type == SUBNET_MAC && s->net.mac.lastseen && s->net.mac.lastseen + macexpire < now) + { + for(node2 = connection_tree->head; node2; node2 = node2->next) + { + c = (connection_t *)node2->data; + if(c->status.active) + send_del_subnet(c, s); + } + subnet_del(myself, s); + } + } +cp } node_t *route_mac(vpn_packet_t *packet) diff --git a/src/route.h b/src/route.h index 1f9e7a5..d188af2 100644 --- a/src/route.h +++ b/src/route.h @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: route.h,v 1.1.2.6 2002/03/01 12:26:56 guus Exp $ + $Id: route.h,v 1.1.2.7 2002/03/01 14:09:31 guus Exp $ */ #ifndef __TINC_ROUTE_H__ @@ -32,7 +32,9 @@ enum extern int routing_mode; extern int priorityinheritance; +extern int macexpire; +extern void age_mac(void); extern void route_incoming(node_t *, vpn_packet_t *); extern void route_outgoing(vpn_packet_t *); diff --git a/src/subnet.h b/src/subnet.h index 04e5c28..7e926e4 100644 --- a/src/subnet.h +++ b/src/subnet.h @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: subnet.h,v 1.1.2.16 2002/02/18 16:25:19 guus Exp $ + $Id: subnet.h,v 1.1.2.17 2002/03/01 14:09:31 guus Exp $ */ #ifndef __TINC_SUBNET_H__ @@ -36,6 +36,7 @@ enum typedef struct subnet_mac_t { mac_t address; + time_t lastseen; } subnet_mac_t; typedef struct subnet_ipv4_t -- 2.25.1