From 9dd7e301e61ecf3e15aa6611751515d844fa8dae Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Sun, 1 Dec 2019 23:29:39 +0100 Subject: [PATCH] Add a #define for the maximum number of tracked recently seen addresses. --- src/meshlink.c | 4 ++-- src/net_setup.c | 24 +++++++++++++----------- src/node.h | 4 +++- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/meshlink.c b/src/meshlink.c index 9b30a2a6..f30fcdf3 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -2700,7 +2700,7 @@ char *meshlink_export(meshlink_handle_t *mesh) { uint32_t count = 0; - for(uint32_t i = 0; i < 5; i++) { + for(uint32_t i = 0; i < MAX_RECENT; i++) { if(mesh->self->recent[i].sa.sa_family) { count++; } else { @@ -3063,7 +3063,7 @@ void meshlink_hint_address(meshlink_handle_t *mesh, meshlink_node_t *node, const pthread_mutex_lock(&mesh->mutex); node_t *n = (node_t *)node; - memmove(n->recent + 1, n->recent, 4 * sizeof(*n->recent)); + memmove(n->recent + 1, n->recent, (MAX_RECENT - 1) * sizeof(*n->recent)); memcpy(n->recent, addr, SALEN(*addr)); if(!node_write_config(mesh, n)) { diff --git a/src/net_setup.c b/src/net_setup.c index 559f0496..ec16b400 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -100,7 +100,7 @@ bool node_read_public_key(meshlink_handle_t *mesh, node_t *n) { // Append any known addresses in the config file to the list we currently have uint32_t known_count = 0; - for(uint32_t i = 0; i < 5; i++) { + for(uint32_t i = 0; i < MAX_RECENT; i++) { if(n->recent[i].sa.sa_family) { known_count++; } @@ -108,12 +108,14 @@ bool node_read_public_key(meshlink_handle_t *mesh, node_t *n) { uint32_t count = packmsg_get_array(&in); - if(count > 5 - known_count) { - count = 5 - known_count; + for(uint32_t i = 0; i < count; i++) { + if(i < MAX_RECENT - known_count) { + n->recent[i + known_count] = packmsg_get_sockaddr(&in); + } else { + packmsg_skip_element(&in); + } } - for(uint32_t i = 0; i < count; i++) { - n->recent[i + known_count] = packmsg_get_sockaddr(&in); } config_free(&config); @@ -182,12 +184,12 @@ bool node_read_from_config(meshlink_handle_t *mesh, node_t *n, const config_t *c n->canonical_address = packmsg_get_str_dup(&in); uint32_t count = packmsg_get_array(&in); - if(count > 5) { - count = 5; - } - for(uint32_t i = 0; i < count; i++) { - n->recent[i] = packmsg_get_sockaddr(&in); + if(i < MAX_RECENT) { + n->recent[i] = packmsg_get_sockaddr(&in); + } else { + packmsg_skip_element(&in); + } } return packmsg_done(&in); @@ -217,7 +219,7 @@ bool node_write_config(meshlink_handle_t *mesh, node_t *n) { uint32_t count = 0; - for(uint32_t i = 0; i < 5; i++) { + for(uint32_t i = 0; i < MAX_RECENT; i++) { if(n->recent[i].sa.sa_family) { count++; } else { diff --git a/src/node.h b/src/node.h index d307e5f2..1a2fdca0 100644 --- a/src/node.h +++ b/src/node.h @@ -40,6 +40,8 @@ typedef struct node_status_t { uint16_t want_udp: 1; /* 1 if we want working UDP because we have data to send */ } node_status_t; +#define MAX_RECENT 5 + typedef struct node_t { // Public member variables char *name; /* name of this node */ @@ -84,7 +86,7 @@ typedef struct node_t { time_t last_successfull_connection; char *canonical_address; /* The canonical address of this node, if known */ - sockaddr_t recent[5]; /* Recently seen addresses */ + sockaddr_t recent[MAX_RECENT]; /* Recently seen addresses */ // Graph-related member variables int distance; -- 2.39.2