]> git.meshlink.io Git - meshlink/blob - src/node.c
Move the routing header out of the SPTPS payload.
[meshlink] / src / node.c
1 /*
2     node.c -- node tree management
3     Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>,
4
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.
9
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.
14
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.
18 */
19
20 #include "system.h"
21
22 #include "hash.h"
23 #include "logger.h"
24 #include "meshlink_internal.h"
25 #include "net.h"
26 #include "netutl.h"
27 #include "node.h"
28 #include "splay_tree.h"
29 #include "utils.h"
30 #include "xalloc.h"
31
32 static int node_compare(const node_t *a, const node_t *b) {
33         return strcmp(a->name, b->name);
34 }
35
36 static int node_id_compare(const node_t *a, const node_t *b) {
37         if(a->id < b->id) {
38                 return -1;
39         } else if(a->id == b->id) {
40                 return 0;
41         } else {
42                 return 1;
43         }
44 }
45
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);
49 }
50
51 void exit_nodes(meshlink_handle_t *mesh) {
52         if(mesh->nodes) {
53                 splay_delete_tree(mesh->nodes);
54         }
55
56         if(mesh->node_ids) {
57                 splay_delete_tree(mesh->node_ids);
58         }
59
60         mesh->nodes = NULL;
61         mesh->node_ids = NULL;
62 }
63
64 node_t *new_node(void) {
65         node_t *n = xzalloc(sizeof(*n));
66
67         n->edge_tree = new_edge_tree();
68         n->mtu = MTU;
69         n->maxmtu = MTU;
70         n->devclass = _DEV_CLASS_MAX;
71
72         return n;
73 }
74
75 void free_node(node_t *n) {
76         n->status.destroyed = true;
77
78         utcp_exit(n->utcp);
79
80         if(n->edge_tree) {
81                 free_edge_tree(n->edge_tree);
82         }
83
84         sockaddrfree(&n->address);
85
86         ecdsa_free(n->ecdsa);
87         sptps_stop(&n->sptps);
88
89         if(n->mtutimeout.cb) {
90                 abort();
91         }
92
93         free(n->name);
94
95         free(n);
96 }
97
98 void node_add(meshlink_handle_t *mesh, node_t *n) {
99         n->mesh = mesh;
100         splay_insert(mesh->nodes, n);
101         update_node_id(mesh, n);
102 }
103
104 void node_del(meshlink_handle_t *mesh, node_t *n) {
105         timeout_del(&mesh->loop, &n->mtutimeout);
106
107         for splay_each(edge_t, e, n->edge_tree) {
108                 edge_del(mesh, e);
109         }
110
111         splay_delete(mesh->nodes, n);
112 }
113
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);
117 }
118
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);
122 }
123
124 void update_node_id(meshlink_handle_t *mesh, node_t *n) {
125         if(n->id) {
126                 logger(mesh, LOG_WARNING, "Node %s already has id %"PRIu64"\n", n->name, n->id);
127                 return;
128         }
129
130         struct {
131                 uint8_t public[32];
132                 uint32_t gen;
133         } input;
134
135         uint8_t hash[64];
136         uint64_t id;
137
138         memset(&input, 0, sizeof input);
139
140         strncpy(input.public, n->name, sizeof input.public);
141         input.gen = 0;
142
143         while(true) {
144                 sha512(&input, sizeof input, hash);
145                 memcpy(&id, hash, sizeof id);
146                 input.gen++;
147
148                 // ID 0 is reserved
149                 if(!id) {
150                         continue;
151                 }
152
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;
156
157                 // If yes and we sort after the other, try again
158                 if(cmp > 0) {
159                         continue;
160                 }
161
162                 if(other) {
163                         splay_delete(mesh->node_ids, other);
164                 }
165
166                 n->id = id;
167                 splay_insert(mesh->node_ids, n);
168
169                 if(other) {
170                         update_node_id(mesh, other);
171                 }
172
173                 break;
174         }
175 }
176
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!");
180                 return;
181         }
182
183         if(sa) {
184                 n->address = *sa;
185                 n->sock = 0;
186
187                 for(int i = 0; i < mesh->listen_sockets; i++) {
188                         if(mesh->listen_socket[i].sa.sa.sa_family == sa->sa.sa_family) {
189                                 n->sock = i;
190                                 break;
191                         }
192                 }
193
194                 meshlink_hint_address(mesh, (meshlink_node_t *)n, &sa->sa);
195
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);
199                         free(hostname);
200                 }
201         }
202 }