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 <lucaderi@users.noreply.github.com>
This commit is contained in:
Logan oos Even 2020-11-13 10:55:00 +01:00 committed by GitHub
parent 72d5243650
commit 1574f1e1c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 25 deletions

View File

@ -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

View File

@ -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 );

View File

@ -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; i<ra.num_sn; i++){
skip_add = SN_ADD;
sn = add_sn_to_list_by_mac_or_sock(&(eee->conf.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;

View File

@ -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; i<ack.num_sn; i++) {
skip_add = SN_ADD;
tmp = add_sn_to_list_by_mac_or_sock(&(sss->federation->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;