diff --git a/include/n2n_define.h b/include/n2n_define.h index 28a851e..a5f1469 100644 --- a/include/n2n_define.h +++ b/include/n2n_define.h @@ -35,6 +35,9 @@ #define IFACE_UPDATE_INTERVAL (30) /* sec. How long it usually takes to get an IP lease. */ #define TRANSOP_TICK_INTERVAL (10) /* sec */ +#define PURGE_REGISTRATION_FREQUENCY 30 +#define REGISTRATION_TIMEOUT 60 + #define ETH_FRAMESIZE 14 #define IP4_SRCOFFSET 12 #define IP4_DSTOFFSET 16 diff --git a/src/n2n.c b/src/n2n.c index 7d57cf6..10f44c4 100644 --- a/src/n2n.c +++ b/src/n2n.c @@ -22,9 +22,6 @@ #include -#define PURGE_REGISTRATION_FREQUENCY 30 -#define REGISTRATION_TIMEOUT 60 - static const uint8_t broadcast_addr[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; static const uint8_t multicast_addr[6] = { 0x01, 0x00, 0x5E, 0x00, 0x00, 0x00 }; /* First 3 bytes are meaningful */ static const uint8_t ipv6_multicast_addr[6] = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 }; /* First 2 bytes are meaningful */ diff --git a/src/sn_utils.c b/src/sn_utils.c index 4ea34f6..b966050 100644 --- a/src/sn_utils.c +++ b/src/sn_utils.c @@ -30,6 +30,10 @@ static int update_edge(n2n_sn_t *sss, const n2n_sock_t *sender_sock, time_t now); +static int purge_expired_communities(n2n_sn_t *sss, + time_t* p_last_purge, + time_t now); + static int process_mgmt(n2n_sn_t *sss, const struct sockaddr_in *sender_sock, const uint8_t *mgmt_buf, @@ -286,6 +290,35 @@ static int update_edge(n2n_sn_t *sss, return 0; } +static int purge_expired_communities(n2n_sn_t *sss, + time_t* p_last_purge, + time_t now) +{ + struct sn_community *comm, *tmp; + size_t num_reg = 0; + + if ((now - (*p_last_purge)) < PURGE_REGISTRATION_FREQUENCY) return 0; + + traceEvent(TRACE_DEBUG, "Purging old communities and edges"); + + HASH_ITER(hh, sss->communities, comm, tmp) { + num_reg += purge_peer_list(&comm->edges, now - REGISTRATION_TIMEOUT); + if ((comm->edges == NULL) && (!sss->lock_communities)) { + traceEvent(TRACE_INFO, "Purging idle community %s", comm->community); + if (NULL != comm->header_encryption_ctx) + /* this should not happen as no 'locked' and thus only communities w/o encrypted header here */ + free(comm->header_encryption_ctx); + HASH_DEL(sss->communities, comm); + free(comm); + } + } + (*p_last_purge) = now; + + traceEvent(TRACE_DEBUG, "Remove %ld edges", num_reg); + + return 0; +} + static int process_mgmt(n2n_sn_t *sss, const struct sockaddr_in *sender_sock, const uint8_t *mgmt_buf, @@ -759,7 +792,6 @@ int run_sn_loop(n2n_sn_t *sss, int *keep_running) { uint8_t pktbuf[N2N_SN_PKTBUF_SIZE]; time_t last_purge_edges = 0; - struct sn_community *comm, *tmp; sss->start_time = time(NULL); @@ -844,20 +876,7 @@ int run_sn_loop(n2n_sn_t *sss, int *keep_running) traceEvent(TRACE_DEBUG, "timeout"); } - HASH_ITER(hh, sss->communities, comm, tmp) - { - purge_expired_registrations(&comm->edges, &last_purge_edges); - - if ((comm->edges == NULL) && (!sss->lock_communities)) - { - traceEvent(TRACE_INFO, "Purging idle community %s", comm->community); - if (NULL != comm->header_encryption_ctx) - /* this should not happen as no 'locked' and thus only communities w/o encrypted header here */ - free (comm->header_encryption_ctx); - HASH_DEL(sss->communities, comm); - free(comm); - } - } + purge_expired_communities(sss, &last_purge_edges, now); } /* while */