From 1574f1e1c6823764ec380580fa9a1f6d2a9cac47 Mon Sep 17 00:00:00 2001 From: Logan oos Even <46396513+Logan007@users.noreply.github.com> Date: Fri, 13 Nov 2020 10:55:00 +0100 Subject: [PATCH] added payload struct to carry supernode data (#501) * added payload struct to carry supernode data * added payload struct to carry supernode data * added payload struct to carry supernode data Co-authored-by: Luca Deri --- include/n2n_define.h | 5 ++--- include/n2n_wire.h | 9 +++++++++ src/edge_utils.c | 21 +++++++++------------ src/sn_utils.c | 17 +++++++---------- 4 files changed, 27 insertions(+), 25 deletions(-) diff --git a/include/n2n_define.h b/include/n2n_define.h index 5c5247f..a921eab 100644 --- a/include/n2n_define.h +++ b/include/n2n_define.h @@ -32,11 +32,10 @@ /* Max available space to add supernodes' informations (sockets and MACs) in REGISTER_SUPER_ACK * Field sizes of REGISTER_SUPER_ACK as used in encode/decode fucntions in src/wire.c * REVISIT: replace 255 by DEFAULT_MTU as soon as header encryption allows for longer packets to be encrypted. */ -#define REG_SUPER_ACK_PAYLOAD_SPACE \ - (255-(1+1+2+sizeof(n2n_common_t)+sizeof(n2n_cookie_t)+sizeof(n2n_mac_t)+1+2+4+1+sizeof(n2n_sock_t)+1)) \ +#define REG_SUPER_ACK_PAYLOAD_SPACE (255-(sizeof(n2n_common_t)+sizeof(n2n_REGISTER_SUPER_ACK_t))) /* Space needed to store socket and MAC address of a supernode */ -#define REG_SUPER_ACK_PAYLOAD_ENTRY_SIZE (sizeof(n2n_sock_t)+sizeof(n2n_mac_t)) +#define REG_SUPER_ACK_PAYLOAD_ENTRY_SIZE (sizeof(n2n_REGISTER_SUPER_ACK_payload_t)) #define PURGE_REGISTRATION_FREQUENCY 30 #define RE_REG_AND_PURGE_FREQUENCY 10 diff --git a/include/n2n_wire.h b/include/n2n_wire.h index 1883e8f..f651042 100644 --- a/include/n2n_wire.h +++ b/include/n2n_wire.h @@ -34,6 +34,15 @@ #include "sn_selection.h" + +/* REGISTER_SUPER_ACK may contain extra payload (their number given by num_sn) + * of following type describing a(nother) supernode */ +typedef struct n2n_REGISTER_SUPER_ACK_payload { + n2n_sock_t sock; /**< socket of supernode */ + n2n_mac_t mac; /**< MAC of supernode */ +} n2n_REGISTER_SUPER_ACK_payload_t; + + int encode_uint8( uint8_t * base, size_t * idx, const uint8_t v ); diff --git a/src/edge_utils.c b/src/edge_utils.c index da88909..eb5ba12 100644 --- a/src/edge_utils.c +++ b/src/edge_utils.c @@ -1995,8 +1995,7 @@ void readFromIPSocket(n2n_edge_t * eee, int in_sock) { char * ip_str = NULL; n2n_REGISTER_SUPER_ACK_t ra; uint8_t tmpbuf[REG_SUPER_ACK_PAYLOAD_SPACE]; - n2n_sock_t *tmp_sock; - n2n_mac_t *tmp_mac; + n2n_REGISTER_SUPER_ACK_payload_t *payload; int i; int skip_add; struct peer_info *sn; @@ -2039,30 +2038,28 @@ void readFromIPSocket(n2n_edge_t * eee, int in_sock) { if(0 == memcmp(ra.cookie, eee->curr_sn->last_cookie, N2N_COOKIE_SIZE)) { - tmp_sock = (void*)&tmpbuf; - tmp_mac = (void*)&tmpbuf[sizeof(n2n_sock_t)]; + payload = (n2n_REGISTER_SUPER_ACK_payload_t*)tmpbuf; for(i=0; iconf.supernodes), tmp_sock, tmp_mac, &skip_add); + sn = add_sn_to_list_by_mac_or_sock(&(eee->conf.supernodes), &(payload->sock), &(payload->mac), &skip_add); if(skip_add == SN_ADD_ADDED){ sn->ip_addr = calloc(1,N2N_EDGE_SN_HOST_SIZE); if(sn->ip_addr != NULL){ - inet_ntop(tmp_sock->family, - (tmp_sock->family == AF_INET)?(void*)&tmp_sock->addr.v4:(void*)&tmp_sock->addr.v6, + inet_ntop(payload->sock.family, + (payload->sock.family == AF_INET)?(void*)&(payload->sock.addr.v4):(void*)&(payload->sock.addr.v6), sn->ip_addr, N2N_EDGE_SN_HOST_SIZE-1); - sprintf (sn->ip_addr, "%s:%u", sn->ip_addr, (uint16_t)tmp_sock->port); - //sock_to_cstr(sn->ip_addr, tmp_sock); + sprintf (sn->ip_addr, "%s:%u", sn->ip_addr, (uint16_t)(payload->sock.port)); + //sock_to_cstr(sn->ip_addr, payload->sock); } sn_selection_criterion_default(&(sn->selection_criterion)); sn->last_seen = now - LAST_SEEN_SN_NEW; sn->last_valid_time_stamp = initial_time_stamp(); traceEvent(TRACE_NORMAL, "Supernode '%s' added to the list of supernodes.", sn->ip_addr); } - - tmp_sock = (void*)tmp_sock + REG_SUPER_ACK_PAYLOAD_ENTRY_SIZE; - tmp_mac = (void*)tmp_sock + sizeof(n2n_sock_t); + // shfiting to the next payload entry + payload++; } eee->last_sup = now; diff --git a/src/sn_utils.c b/src/sn_utils.c index bf54f1d..ad3b00a 100644 --- a/src/sn_utils.c +++ b/src/sn_utils.c @@ -1209,9 +1209,9 @@ static int process_udp(n2n_sn_t * sss, * their SN_ACTIVE time before they get re-registred to. */ if(((++num)*REG_SUPER_ACK_PAYLOAD_ENTRY_SIZE) > REG_SUPER_ACK_PAYLOAD_SPACE) break; /* no more space available in REGISTER_SUPER_ACK payload */ - memcpy((void*)tmp_dst, (void*)&(peer->sock), sizeof(n2n_sock_t)); + memcpy(tmp_dst, &(peer->sock), sizeof(n2n_sock_t)); tmp_dst += sizeof(n2n_sock_t); - memcpy((void*)tmp_dst, (void*)&(peer->mac_addr), sizeof(n2n_mac_t)); + memcpy(tmp_dst, &(peer->mac_addr), sizeof(n2n_mac_t)); tmp_dst += sizeof(n2n_mac_t); } ack.num_sn = num; @@ -1255,8 +1255,7 @@ static int process_udp(n2n_sn_t * sss, macstr_t mac_buf1; n2n_sock_t sender; n2n_sock_t *orig_sender; - n2n_sock_t *tmp_sock; - n2n_mac_t *tmp_mac; + n2n_REGISTER_SUPER_ACK_payload_t *payload; int i; uint8_t dec_tmpbuf[REG_SUPER_ACK_PAYLOAD_SPACE]; int skip_add; @@ -1308,20 +1307,18 @@ static int process_udp(n2n_sn_t * sss, } } - tmp_sock = (n2n_sock_t *)dec_tmpbuf; - tmp_mac = (n2n_mac_t *)(dec_tmpbuf + sizeof(n2n_sock_t)); + payload = (n2n_REGISTER_SUPER_ACK_payload_t *)dec_tmpbuf; for(i=0; ifederation->edges), tmp_sock, tmp_mac, &skip_add); + tmp = add_sn_to_list_by_mac_or_sock(&(sss->federation->edges), &(payload->sock), &(payload->mac), &skip_add); if(skip_add == SN_ADD_ADDED) { tmp->last_seen = now - LAST_SEEN_SN_NEW; } - /* REVISIT: find a more elegant expression to increase following pointers. */ - tmp_sock = tmp_sock + REG_SUPER_ACK_PAYLOAD_ENTRY_SIZE; - tmp_mac = (n2n_mac_t *)(tmp_sock + sizeof(n2n_sock_t)); + // shift to next payload entry + payload++; } break;