X-Git-Url: http://git.meshlink.io/?p=meshlink;a=blobdiff_plain;f=src%2Fnode.c;h=b8caed6c7e06ca182f671fd046f8be4d44d76611;hp=4f4f599932caa1d6985afdc689b1a9d43b670310;hb=963c5055505f2fc117cd5efa06eaa02c9b2bf85d;hpb=c83c7948602acfb5fd0716ac6a47e0c9c9f9bfd8 diff --git a/src/node.c b/src/node.c index 4f4f5999..b8caed6c 100644 --- a/src/node.c +++ b/src/node.c @@ -57,7 +57,7 @@ node_t *new_node(void) { n->edge_tree = new_edge_tree(); n->mtu = MTU; n->maxmtu = MTU; - n->devclass = _DEV_CLASS_MAX; + n->devclass = DEV_CLASS_UNKNOWN; return n; } @@ -81,6 +81,7 @@ void free_node(node_t *n) { } free(n->name); + free(n->canonical_address); free(n); } @@ -134,12 +135,41 @@ void update_node_udp(meshlink_handle_t *mesh, node_t *n, const sockaddr_t *sa) { hash_insert(mesh->node_udp_cache, sa, n); - meshlink_hint_address(mesh, (meshlink_node_t *)n, &sa->sa); + node_add_recent_address(mesh, n, sa); - if(mesh->log_level >= MESHLINK_DEBUG) { + if(mesh->log_level <= MESHLINK_DEBUG) { char *hostname = sockaddr2hostname(&n->address); logger(mesh, MESHLINK_DEBUG, "UDP address of %s set to %s", n->name, hostname); free(hostname); } } } + +bool node_add_recent_address(meshlink_handle_t *mesh, node_t *n, const sockaddr_t *sa) { + (void)mesh; + bool found = false; + int i; + + /* Check if we already know this address */ + for(i = 0; i < MAX_RECENT && n->recent[i].sa.sa_family; i++) { + if(!sockaddrcmp(&n->recent[i], sa)) { + found = true; + break; + } + } + + if(found && i == 0) { + /* It's already the most recent address, nothing to do. */ + return false; + } + + if(i >= MAX_RECENT) { + i = MAX_RECENT - 1; + } + + memmove(n->recent + 1, n->recent, i * sizeof(*n->recent)); + memcpy(n->recent, sa, SALEN(sa->sa)); + + n->status.dirty = true; + return !found; +}