From 8852d4407d87cf5dcf2c212d352279015aa050c0 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Thu, 17 May 2007 23:24:40 +0000 Subject: [PATCH] Use libevent to age learned MAC addresses. --- src/net.c | 4 --- src/route.c | 71 +++++++++++++++++++++++++++++++---------------------- src/route.h | 1 - 3 files changed, 42 insertions(+), 34 deletions(-) diff --git a/src/net.c b/src/net.c index e09734cf..57ac8242 100644 --- a/src/net.c +++ b/src/net.c @@ -36,7 +36,6 @@ #include "netutl.h" #include "process.h" #include "protocol.h" -#include "route.h" #include "subnet.h" #include "xalloc.h" @@ -482,9 +481,6 @@ int main_loop(void) check_dead_connections(); last_ping_check = now; - if(routing_mode == RMODE_SWITCH) - age_subnets(); - /* Should we regenerate our key? */ if(keyexpires < now) { diff --git a/src/route.c b/src/route.c index 0123107e..32afa0d6 100644 --- a/src/route.c +++ b/src/route.c @@ -51,6 +51,8 @@ static const size_t icmp6_size = sizeof(struct icmp6_hdr); static const size_t ns_size = sizeof(struct nd_neighbor_solicit); static const size_t opt_size = sizeof(struct nd_opt_hdr); +static struct event age_subnets_event; + /* RFC 1071 */ static uint16_t inet_checksum(void *data, int len, uint16_t prevsum) @@ -95,6 +97,42 @@ static bool checklength(node_t *source, vpn_packet_t *packet, length_t length) { return true; } +static void age_subnets(int fd, short events, void *data) +{ + subnet_t *s; + connection_t *c; + avl_node_t *node, *next, *node2; + bool left = false; + + cp(); + + for(node = myself->subnet_tree->head; node; node = next) { + next = node->next; + s = node->data; + if(s->expires && s->expires < now) { + ifdebug(TRAFFIC) { + char netstr[MAXNETSTR]; + if(net2str(netstr, sizeof netstr, s)) + logger(LOG_INFO, _("Subnet %s expired"), netstr); + } + + for(node2 = connection_tree->head; node2; node2 = node2->next) { + c = node2->data; + if(c->status.active) + send_del_subnet(c, s); + } + + subnet_del(myself, s); + } else { + if(s->expires) + left = true; + } + } + + if(left) + event_add(&age_subnets_event, &(struct timeval){10, 0}); +} + static void learn_mac(mac_t *address) { subnet_t *subnet; @@ -125,41 +163,16 @@ static void learn_mac(mac_t *address) if(c->status.active) send_add_subnet(c, subnet); } + + if(!timeout_initialized(&age_subnets_event)) + timeout_set(&age_subnets_event, age_subnets, NULL); + event_add(&age_subnets_event, &(struct timeval){10, 0}); } if(subnet->expires) subnet->expires = now + macexpire; } -void age_subnets(void) -{ - subnet_t *s; - connection_t *c; - avl_node_t *node, *next, *node2; - - cp(); - - for(node = myself->subnet_tree->head; node; node = next) { - next = node->next; - s = node->data; - if(s->expires && s->expires < now) { - ifdebug(TRAFFIC) { - char netstr[MAXNETSTR]; - if(net2str(netstr, sizeof netstr, s)) - logger(LOG_INFO, _("Subnet %s expired"), netstr); - } - - for(node2 = connection_tree->head; node2; node2 = node2->next) { - c = node2->data; - if(c->status.active) - send_del_subnet(c, s); - } - - subnet_del(myself, s); - } - } -} - static void route_mac(node_t *source, vpn_packet_t *packet) { subnet_t *subnet; diff --git a/src/route.h b/src/route.h index 807b707a..0021506c 100644 --- a/src/route.h +++ b/src/route.h @@ -39,7 +39,6 @@ extern int macexpire; extern mac_t mymac; -extern void age_subnets(void); extern void route(struct node_t *, struct vpn_packet_t *); #endif /* __TINC_ROUTE_H__ */ -- 2.39.5