Duplicate addresses would be appended to the list, and could push out other
addresses. If the address already exists, only move it to the top if it is
not already there.
Also don't force an immediate write of the host config file when trying to
add an address that already exists.
}
if(naddress.unknown.family != AF_UNKNOWN) {
}
if(naddress.unknown.family != AF_UNKNOWN) {
- meshlink_hint_address(mesh, node, (struct sockaddr *)&naddress);
-
node_t *n = (node_t *)node;
connection_t *c = n->connection;
node_t *n = (node_t *)node;
connection_t *c = n->connection;
+ node_add_recent_address(mesh, n, &naddress);
+
if(c && c->outgoing && !c->status.active) {
c->outgoing->timeout = 0;
if(c && c->outgoing && !c->status.active) {
c->outgoing->timeout = 0;
pthread_mutex_lock(&mesh->mutex);
node_t *n = (node_t *)node;
pthread_mutex_lock(&mesh->mutex);
node_t *n = (node_t *)node;
- memmove(n->recent + 1, n->recent, (MAX_RECENT - 1) * sizeof(*n->recent));
- memcpy(n->recent, addr, SALEN(*addr));
- if(!node_write_config(mesh, n)) {
- logger(mesh, MESHLINK_DEBUG, "Could not update %s\n", n->name);
+ if(node_add_recent_address(mesh, n, (sockaddr_t *)addr)) {
+ if(!node_write_config(mesh, n)) {
+ logger(mesh, MESHLINK_DEBUG, "Could not update %s\n", n->name);
+ }
}
pthread_mutex_unlock(&mesh->mutex);
}
pthread_mutex_unlock(&mesh->mutex);
hash_insert(mesh->node_udp_cache, sa, n);
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);
if(mesh->log_level <= MESHLINK_DEBUG) {
char *hostname = sockaddr2hostname(&n->address);
+
+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;
+}
extern node_t *lookup_node(struct meshlink_handle *mesh, const char *name) __attribute__((__warn_unused_result__));
extern node_t *lookup_node_udp(struct meshlink_handle *mesh, const sockaddr_t *sa) __attribute__((__warn_unused_result__));
extern void update_node_udp(struct meshlink_handle *mesh, node_t *n, const sockaddr_t *sa);
extern node_t *lookup_node(struct meshlink_handle *mesh, const char *name) __attribute__((__warn_unused_result__));
extern node_t *lookup_node_udp(struct meshlink_handle *mesh, const sockaddr_t *sa) __attribute__((__warn_unused_result__));
extern void update_node_udp(struct meshlink_handle *mesh, node_t *n, const sockaddr_t *sa);
+extern bool node_add_recent_address(struct meshlink_handle *mesh, node_t *n, const sockaddr_t *addr);