From 75317acaf3753fab07a4fb7e01b92be251243cf2 Mon Sep 17 00:00:00 2001 From: Logan007 Date: Tue, 28 Jul 2020 00:55:07 +0545 Subject: [PATCH] added sorting of encrypted communities --- include/n2n.h | 3 ++- include/n2n_define.h | 2 ++ src/sn_utils.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/include/n2n.h b/include/n2n.h index 0f13eba..f536f2e 100644 --- a/include/n2n.h +++ b/include/n2n.h @@ -351,8 +351,9 @@ struct sn_community char community[N2N_COMMUNITY_SIZE]; uint8_t header_encryption; /* Header encryption indicator. */ he_context_t *header_encryption_ctx; /* Header encryption cipher context. */ - he_context_t *header_iv_ctx; /* Header IV ecnryption cipher context, REMOVE as soon as seperte fileds for checksum and replay protection available */ + he_context_t *header_iv_ctx; /* Header IV ecnryption cipher context, REMOVE as soon as seperate fields for checksum and replay protection available */ struct peer_info *edges; /* Link list of registered edges. */ + int64_t number_enc_packets; /* Number of encrypted packets handled so far, required for sorting from time to time */ UT_hash_handle hh; /* makes this structure hashable */ }; diff --git a/include/n2n_define.h b/include/n2n_define.h index a5f1469..4372372 100644 --- a/include/n2n_define.h +++ b/include/n2n_define.h @@ -38,6 +38,8 @@ #define PURGE_REGISTRATION_FREQUENCY 30 #define REGISTRATION_TIMEOUT 60 +#define SORT_COMMUNITIES_INTERVAL 90 /* sec. until supernode sorts communities' hash list again */ + #define ETH_FRAMESIZE 14 #define IP4_SRCOFFSET 12 #define IP4_DSTOFFSET 16 diff --git a/src/sn_utils.c b/src/sn_utils.c index 56def7d..352ccd7 100644 --- a/src/sn_utils.c +++ b/src/sn_utils.c @@ -56,6 +56,10 @@ static int purge_expired_communities(n2n_sn_t *sss, time_t* p_last_purge, time_t now); +static int sort_communities (n2n_sn_t *sss, + time_t* p_last_sort, + time_t now); + static int process_mgmt(n2n_sn_t *sss, const struct sockaddr_in *sender_sock, const uint8_t *mgmt_buf, @@ -371,6 +375,36 @@ static int purge_expired_communities(n2n_sn_t *sss, } +static int number_enc_packets_sort (struct sn_community *a, struct sn_community *b) { + // comparison function for sorting communities in descending order of their + // number_enc_packets-fields + return (b->number_enc_packets - a->number_enc_packets); +} + +static int sort_communities (n2n_sn_t *sss, + time_t* p_last_sort, + time_t now) +{ + struct sn_community *comm, *tmp; + + if ((now - (*p_last_sort)) < SORT_COMMUNITIES_INTERVAL) return 0; + + // this routine gets periodically called as defined in SORT_COMMUNITIES_INTERVAL + // it sorts the communities in descending order of their number_enc_packets-fields... + HASH_SORT(sss->communities, number_enc_packets_sort); + + // ... and afterward resets the number_enc__packets-fields to zero + // (other models could reset it to half of their value to respect history) + HASH_ITER(hh, sss->communities, comm, tmp) { + comm->number_enc_packets = 0; + } + + (*p_last_sort) = now; + + return 0; +} + + static int process_mgmt(n2n_sn_t *sss, const struct sockaddr_in *sender_sock, const uint8_t *mgmt_buf, @@ -555,6 +589,9 @@ static int process_udp(n2n_sn_t * sss, /* set 'encrypted' in case it is not set yet */ comm->header_encryption = HEADER_ENCRYPTION_ENABLED; } + // count the number of encrypted packets for sorting the communities from time to time + // for the HASH_ITER a few lines above gets faster for the more busy communities + (comm->number_enc_packets)++; // no need to test further communities break; } @@ -784,7 +821,7 @@ static int process_udp(n2n_sn_t * sss, /* new communities introduced by REGISTERs could not have had encrypted header */ comm->header_encryption = HEADER_ENCRYPTION_NONE; comm->header_encryption_ctx = NULL; - + comm->number_enc_packets = 0; HASH_ADD_STR(sss->communities, community, comm); traceEvent(TRACE_INFO, "New community: %s", comm->community); @@ -904,6 +941,7 @@ int run_sn_loop(n2n_sn_t *sss, int *keep_running) { uint8_t pktbuf[N2N_SN_PKTBUF_SIZE]; time_t last_purge_edges = 0; + time_t last_sort_communities = 0; sss->start_time = time(NULL); @@ -989,7 +1027,7 @@ int run_sn_loop(n2n_sn_t *sss, int *keep_running) } purge_expired_communities(sss, &last_purge_edges, now); - + sort_communities (sss, &last_sort_communities, now); } /* while */ sn_term(sss);