2 node.c -- node tree management
3 Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>,
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "meshlink_internal.h"
28 #include "splay_tree.h"
32 static int node_compare(const node_t *a, const node_t *b) {
33 return strcmp(a->name, b->name);
36 static int node_id_compare(const node_t *a, const node_t *b) {
39 } else if(a->id == b->id) {
46 void init_nodes(meshlink_handle_t *mesh) {
47 mesh->nodes = splay_alloc_tree((splay_compare_t) node_compare, (splay_action_t) free_node);
48 mesh->node_ids = splay_alloc_tree((splay_compare_t) node_id_compare, NULL);
51 void exit_nodes(meshlink_handle_t *mesh) {
53 splay_delete_tree(mesh->nodes);
57 splay_delete_tree(mesh->node_ids);
61 mesh->node_ids = NULL;
64 node_t *new_node(void) {
65 node_t *n = xzalloc(sizeof(*n));
67 n->edge_tree = new_edge_tree();
70 n->devclass = _DEV_CLASS_MAX;
75 void free_node(node_t *n) {
76 n->status.destroyed = true;
81 free_edge_tree(n->edge_tree);
84 sockaddrfree(&n->address);
87 sptps_stop(&n->sptps);
89 if(n->mtutimeout.cb) {
98 void node_add(meshlink_handle_t *mesh, node_t *n) {
100 splay_insert(mesh->nodes, n);
101 update_node_id(mesh, n);
104 void node_del(meshlink_handle_t *mesh, node_t *n) {
105 timeout_del(&mesh->loop, &n->mtutimeout);
107 for splay_each(edge_t, e, n->edge_tree) {
111 splay_delete(mesh->nodes, n);
114 node_t *lookup_node(meshlink_handle_t *mesh, const char *name) {
115 const node_t n = {.name = (char *)name};
116 return splay_search(mesh->nodes, &n);
119 node_t *lookup_node_id(meshlink_handle_t *mesh, uint64_t id) {
120 const node_t n = {.id = id};
121 return splay_search(mesh->node_ids, &n);
124 void update_node_id(meshlink_handle_t *mesh, node_t *n) {
126 logger(mesh, LOG_WARNING, "Node %s already has id %"PRIu64"\n", n->name, n->id);
138 memset(&input, 0, sizeof input);
140 strncpy(input.public, n->name, sizeof input.public);
144 sha512(&input, sizeof input, hash);
145 memcpy(&id, hash, sizeof id);
153 // Check if there is a conflict with an existing node
154 node_t *other = lookup_node_id(mesh, id);
155 int cmp = other ? strcmp(n->name, other->name) : 0;
157 // If yes and we sort after the other, try again
163 splay_delete(mesh->node_ids, other);
167 splay_insert(mesh->node_ids, n);
170 update_node_id(mesh, other);
177 void update_node_udp(meshlink_handle_t *mesh, node_t *n, const sockaddr_t *sa) {
178 if(n == mesh->self) {
179 logger(mesh, MESHLINK_WARNING, "Trying to update UDP address of mesh->self!");
187 for(int i = 0; i < mesh->listen_sockets; i++) {
188 if(mesh->listen_socket[i].sa.sa.sa_family == sa->sa.sa_family) {
194 meshlink_hint_address(mesh, (meshlink_node_t *)n, &sa->sa);
196 if(mesh->log_level >= MESHLINK_DEBUG) {
197 char *hostname = sockaddr2hostname(&n->address);
198 logger(mesh, MESHLINK_DEBUG, "UDP address of %s set to %s", n->name, hostname);