From 746962510ff7cfd468876f146fc9a0999ca70265 Mon Sep 17 00:00:00 2001 From: francesco_carli <62562180+fcarli3@users.noreply.github.com> Date: Thu, 24 Sep 2020 20:00:32 +0200 Subject: [PATCH 1/6] Add new enum data type and indicators --- include/n2n_define.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/n2n_define.h b/include/n2n_define.h index 97e913d..abba7cb 100644 --- a/include/n2n_define.h +++ b/include/n2n_define.h @@ -63,6 +63,10 @@ #define N2N_COMPRESSION_ID_ZSTD 3 /* set if '-z2' cli option is present, available only if compiled with zstd lib */ #define ZSTD_COMPRESSION_LEVEL 7 /* 1 (faster) ... 22 (more compression) */ +/* Federation name and indicators */ +#define FEDERATION_NAME "*Federation" +enum federation{IS_NO_FEDERATION = 0,IS_FEDERATION = 1}; + /* (un)purgeable community indicator (supernode) */ #define COMMUNITY_UNPURGEABLE 0 #define COMMUNITY_PURGEABLE 1 From 6e875e7cab791389ca6369433ea958a0558eb15c Mon Sep 17 00:00:00 2001 From: francesco_carli <62562180+fcarli3@users.noreply.github.com> Date: Thu, 24 Sep 2020 20:04:07 +0200 Subject: [PATCH 2/6] Add new federation flag to community structure --- include/n2n.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/n2n.h b/include/n2n.h index a9735f5..6f8cd05 100644 --- a/include/n2n.h +++ b/include/n2n.h @@ -358,6 +358,7 @@ typedef struct sn_stats struct sn_community { char community[N2N_COMMUNITY_SIZE]; + uint8_t is_federation; /* if not-zero, then the current community is the federation of supernodes */ uint8_t purgeable; /* indicates purgeable community (fixed-name, predetermined (-c parameter) communties usually are unpurgeable) */ uint8_t header_encryption; /* Header encryption indicator. */ he_context_t *header_encryption_ctx; /* Header encryption cipher context. */ @@ -491,6 +492,7 @@ int quick_edge_init(char *device_name, char *community_name, char *local_ip_address, char *supernode_ip_address_port, int *keep_on_running); +int comm_init(struct sn_community *comm, char *cmn); int sn_init(n2n_sn_t *sss); void sn_term(n2n_sn_t *sss); int run_sn_loop(n2n_sn_t *sss, int *keep_running); From b5d4a709834bb97e45c1dce3a6e332a05415bc1a Mon Sep 17 00:00:00 2001 From: francesco_carli <62562180+fcarli3@users.noreply.github.com> Date: Thu, 24 Sep 2020 20:07:33 +0200 Subject: [PATCH 3/6] Add comm_init() function --- src/sn.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/sn.c b/src/sn.c index f19bfb8..b672dab 100644 --- a/src/sn.c +++ b/src/sn.c @@ -45,6 +45,7 @@ static int load_allowed_sn_community(n2n_sn_t *sss, char *path) { } HASH_ITER(hh, sss->communities, s, tmp) { + if(s->is_federation) continue; HASH_DEL(sss->communities, s); if (NULL != s->header_encryption_ctx) free (s->header_encryption_ctx); @@ -89,11 +90,9 @@ static int load_allowed_sn_community(n2n_sn_t *sss, char *path) { } } - s = (struct sn_community*)calloc(1,sizeof(struct sn_community)); + comm_init(s,cmn_str); if(s != NULL) { - strncpy((char*)s->community, cmn_str, N2N_COMMUNITY_SIZE-1); - s->community[N2N_COMMUNITY_SIZE-1] = '\0'; /* loaded from file, this community is unpurgeable */ s->purgeable = COMMUNITY_UNPURGEABLE; /* we do not know if header encryption is used in this community, From acafbc61a171ab15d59f260a3fd3154bbc0ae937 Mon Sep 17 00:00:00 2001 From: francesco_carli <62562180+fcarli3@users.noreply.github.com> Date: Thu, 24 Sep 2020 20:12:20 +0200 Subject: [PATCH 4/6] Add comm_init() function --- src/sn_utils.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/sn_utils.c b/src/sn_utils.c index b2cc1c8..0de9f18 100644 --- a/src/sn_utils.c +++ b/src/sn_utils.c @@ -202,6 +202,19 @@ static int try_broadcast(n2n_sn_t * sss, return 0; } +/** Initialise some fields of the community structure **/ +int comm_init(struct sn_community *comm, char *cmn){ + + comm = (struct sn_community*)malloc(sizeof(struct sn_community)); + memset(comm, 0, sizeof(struct sn_community)); + + strncpy((char*)comm->community, cmn, N2N_COMMUNITY_SIZE-1); + comm->community[N2N_COMMUNITY_SIZE-1] = '\0'; + comm->is_federation = IS_NO_FEDERATION; + + return 0; /* OK */ +} + /** Initialise the supernode structure */ int sn_init(n2n_sn_t *sss) { @@ -969,11 +982,10 @@ static int process_udp(n2n_sn_t * sss, } if(!comm && (!sss->lock_communities || (match == 1))) { - comm = calloc(1, sizeof(struct sn_community)); + + comm_init(comm,(char *)cmn.community); if(comm) { - strncpy(comm->community, (char*)cmn.community, N2N_COMMUNITY_SIZE-1); - comm->community[N2N_COMMUNITY_SIZE-1] = '\0'; /* new communities introduced by REGISTERs could not have had encrypted header... */ comm->header_encryption = HEADER_ENCRYPTION_NONE; comm->header_encryption_ctx = NULL; From 9dbc54f443c5fc444d697c75bc38119a372cb790 Mon Sep 17 00:00:00 2001 From: francesco_carli <62562180+fcarli3@users.noreply.github.com> Date: Fri, 25 Sep 2020 12:17:37 +0200 Subject: [PATCH 5/6] Update sn.c --- src/sn.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/sn.c b/src/sn.c index b672dab..4187cf1 100644 --- a/src/sn.c +++ b/src/sn.c @@ -90,6 +90,7 @@ static int load_allowed_sn_community(n2n_sn_t *sss, char *path) { } } + s = (struct sn_community*)calloc(1,sizeof(struct sn_community)); comm_init(s,cmn_str); if(s != NULL) { From 31269dde52852baeedbc9fd34c31e4b3863dbc4c Mon Sep 17 00:00:00 2001 From: francesco_carli <62562180+fcarli3@users.noreply.github.com> Date: Fri, 25 Sep 2020 12:19:20 +0200 Subject: [PATCH 6/6] Update sn_utils.c --- src/sn_utils.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/sn_utils.c b/src/sn_utils.c index 0de9f18..e241199 100644 --- a/src/sn_utils.c +++ b/src/sn_utils.c @@ -204,9 +204,6 @@ static int try_broadcast(n2n_sn_t * sss, /** Initialise some fields of the community structure **/ int comm_init(struct sn_community *comm, char *cmn){ - - comm = (struct sn_community*)malloc(sizeof(struct sn_community)); - memset(comm, 0, sizeof(struct sn_community)); strncpy((char*)comm->community, cmn, N2N_COMMUNITY_SIZE-1); comm->community[N2N_COMMUNITY_SIZE-1] = '\0'; @@ -983,6 +980,7 @@ static int process_udp(n2n_sn_t * sss, if(!comm && (!sss->lock_communities || (match == 1))) { + comm = (struct sn_community*)calloc(1,sizeof(struct sn_community)); comm_init(comm,(char *)cmn.community); if(comm) {