]> git.meshlink.io Git - meshlink/blobdiff - src/node.c
Don't add duplicates to the list of recently seen addresses.
[meshlink] / src / node.c
index 5f5d235cf4d28e35929754b38576080ac8fc8836..b8caed6c7e06ca182f671fd046f8be4d44d76611 100644 (file)
@@ -135,7 +135,7 @@ 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) {
                        char *hostname = sockaddr2hostname(&n->address);
@@ -144,3 +144,32 @@ void update_node_udp(meshlink_handle_t *mesh, node_t *n, const sockaddr_t *sa) {
                }
        }
 }
+
+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;
+}