-void dump_nodes(void)
-{
- avl_node_t *node;
- node_t *n;
-cp
- syslog(LOG_DEBUG, _("Nodes:"));
-
- for(node = node_tree->head; node; node = node->next)
- {
- n = (connection_t *)node->data;
- syslog(LOG_DEBUG, _(" %s at %s port %hd options %ld sockets %d, %d status %04x"),
- n->name, n->hostname, n->port, n->options,
- n->socket, n->meta_socket, n->status);
- }
-
- syslog(LOG_DEBUG, _("End of nodes."));
-cp
+node_t *lookup_node(meshlink_handle_t *mesh, const char *name) {
+ const node_t n = {.name = (char *)name};
+ node_t *result;
+
+ result = splay_search(mesh->nodes, &n);
+
+ return result;
+}
+
+node_t *lookup_node_udp(meshlink_handle_t *mesh, const sockaddr_t *sa) {
+ return hash_search(mesh->node_udp_cache, sa);
+}
+
+void update_node_udp(meshlink_handle_t *mesh, node_t *n, const sockaddr_t *sa) {
+ if(n == mesh->self) {
+ logger(mesh, MESHLINK_WARNING, "Trying to update UDP address of mesh->self!");
+ return;
+ }
+
+ hash_insert(mesh->node_udp_cache, &n->address, NULL);
+
+ if(sa) {
+ n->address = *sa;
+ n->sock = 0;
+
+ for(int i = 0; i < mesh->listen_sockets; i++) {
+ if(mesh->listen_socket[i].sa.sa.sa_family == sa->sa.sa_family) {
+ n->sock = i;
+ break;
+ }
+ }
+
+ hash_insert(mesh->node_udp_cache, sa, n);
+
+ node_add_recent_address(mesh, n, sa);
+
+ 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;