-node_t *lookup_node_udp(meshlink_handle_t *mesh, const sockaddr_t *sa) {
- return hash_search(mesh->node_udp_cache, sa);
+void update_node_id(meshlink_handle_t *mesh, node_t *n) {
+ if(n->id) {
+ logger(mesh, LOG_WARNING, "Node %s already has id %"PRIu64"\n", n->name, n->id);
+ return;
+ }
+
+ struct {
+ uint8_t public[32];
+ uint32_t gen;
+ } input;
+
+ uint8_t hash[64];
+ uint64_t id;
+
+ memset(&input, 0, sizeof input);
+
+ strncpy(input.public, n->name, sizeof input.public);
+ input.gen = 0;
+
+ while(true) {
+ sha512(&input, sizeof input, hash);
+ memcpy(&id, hash, sizeof id);
+ input.gen++;
+
+ // ID 0 is reserved
+ if(!id) {
+ continue;
+ }
+
+ // Check if there is a conflict with an existing node
+ node_t *other = lookup_node_id(mesh, id);
+ int cmp = other ? strcmp(n->name, other->name) : 0;
+
+ // If yes and we sort after the other, try again
+ if(cmp > 0) {
+ continue;
+ }
+
+ if(other) {
+ splay_delete(mesh->node_ids, other);
+ }
+
+ n->id = id;
+ splay_insert(mesh->node_ids, n);
+
+ if(other) {
+ update_node_id(mesh, other);
+ }
+
+ break;
+ }