]> git.meshlink.io Git - meshlink/blob - src/protocol_edge.c
Never automatically try to bind to ports >= 32768.
[meshlink] / src / protocol_edge.c
1 /*
2     protocol_edge.c -- handle the meta-protocol, edges
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 "conf.h"
23 #include "connection.h"
24 #include "edge.h"
25 #include "graph.h"
26 #include "logger.h"
27 #include "meshlink_internal.h"
28 #include "meta.h"
29 #include "net.h"
30 #include "netutl.h"
31 #include "node.h"
32 #include "protocol.h"
33 #include "utils.h"
34 #include "xalloc.h"
35 #include "submesh.h"
36
37 bool send_add_edge(meshlink_handle_t *mesh, connection_t *c, const edge_t *e, int contradictions) {
38         bool x;
39         char *address, *port;
40         const char *from_submesh, *to_submesh;
41         const submesh_t *s = NULL;
42
43         if(c->node && c->node->submesh) {
44                 if(!submesh_allows_node(e->from->submesh, c->node)) {
45                         return true;
46                 }
47
48                 if(!submesh_allows_node(e->to->submesh, c->node)) {
49                         return true;
50                 }
51         }
52
53         if(e->from->submesh && e->to->submesh && (e->from->submesh != e->to->submesh)) {
54                 return true;
55         }
56
57         sockaddr2str(&e->address, &address, &port);
58
59         if(e->from->submesh) {
60                 from_submesh = e->from->submesh->name;
61         } else {
62                 from_submesh = CORE_MESH;
63         }
64
65         if(e->to->submesh) {
66                 to_submesh = e->to->submesh->name;
67         } else {
68                 to_submesh = CORE_MESH;
69         }
70
71         if(e->from->submesh) {
72                 s = e->from->submesh;
73         } else {
74                 s = e->to->submesh;
75         }
76
77         x = send_request(mesh, c, s, "%d %x %s %d %s %s %s %s %d %s %x %d %d %x", ADD_EDGE, prng(mesh, UINT_MAX),
78                          e->from->name, e->from->devclass, from_submesh, e->to->name, address, port,
79                          e->to->devclass, to_submesh, OPTION_PMTU_DISCOVERY, e->weight, contradictions, e->from->session_id);
80         free(address);
81         free(port);
82
83         return x;
84 }
85
86 bool add_edge_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
87         assert(request);
88         assert(*request);
89
90         edge_t *e;
91         node_t *from, *to;
92         char from_name[MAX_STRING_SIZE];
93         int from_devclass;
94         char from_submesh_name[MAX_STRING_SIZE] = "";
95         char to_name[MAX_STRING_SIZE];
96         char to_address[MAX_STRING_SIZE];
97         char to_port[MAX_STRING_SIZE];
98         int to_devclass;
99         char to_submesh_name[MAX_STRING_SIZE] = "";
100         sockaddr_t address;
101         int weight;
102         int contradictions = 0;
103         uint32_t session_id = 0;
104         submesh_t *s = NULL;
105
106         if(sscanf(request, "%*d %*x "MAX_STRING" %d "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %d "MAX_STRING" %*x %d %d %x",
107                         from_name, &from_devclass, from_submesh_name, to_name, to_address, to_port, &to_devclass, to_submesh_name,
108                         &weight, &contradictions, &session_id) < 9) {
109                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ADD_EDGE", c->name);
110                 return false;
111         }
112
113         // Check if devclasses are valid
114
115         if(from_devclass < 0 || from_devclass >= DEV_CLASS_COUNT) {
116                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ADD_EDGE", c->name, "from devclass invalid");
117                 return false;
118         }
119
120         if(to_devclass < 0 || to_devclass >= DEV_CLASS_COUNT) {
121                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ADD_EDGE", c->name, "to devclass invalid");
122                 return false;
123         }
124
125         if(0 == strcmp(from_submesh_name, "")) {
126                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ADD_EDGE", c->name, "invalid submesh id");
127                 return false;
128         }
129
130         if(0 == strcmp(to_submesh_name, "")) {
131                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ADD_EDGE", c->name, "invalid submesh id");
132                 return false;
133         }
134
135         if(seen_request(mesh, request)) {
136                 return true;
137         }
138
139         /* Lookup nodes */
140
141         from = lookup_node(mesh, from_name);
142         to = lookup_node(mesh, to_name);
143
144         if(!from) {
145                 from = new_node();
146                 from->status.dirty = true;
147                 from->status.blacklisted = mesh->default_blacklist;
148                 from->name = xstrdup(from_name);
149                 from->devclass = from_devclass;
150
151                 from->submesh = NULL;
152
153                 if(0 != strcmp(from_submesh_name, CORE_MESH)) {
154                         if(!(from->submesh = lookup_or_create_submesh(mesh, from_submesh_name))) {
155                                 return false;
156                         }
157                 }
158
159                 node_add(mesh, from);
160         }
161
162         if(contradictions > 50) {
163                 handle_duplicate_node(mesh, from);
164         }
165
166         from->devclass = from_devclass;
167
168         if(!from->session_id) {
169                 from->session_id = session_id;
170         }
171
172         if(!to) {
173                 to = new_node();
174                 to->status.dirty = true;
175                 to->status.blacklisted = mesh->default_blacklist;
176                 to->name = xstrdup(to_name);
177                 to->devclass = to_devclass;
178
179                 to->submesh = NULL;
180
181                 if(0 != strcmp(to_submesh_name, CORE_MESH)) {
182                         if(!(to->submesh = lookup_or_create_submesh(mesh, to_submesh_name))) {
183                                 return false;
184
185                         }
186                 }
187
188                 node_add(mesh, to);
189         }
190
191         to->devclass = to_devclass;
192
193         /* Convert addresses */
194
195         address = str2sockaddr(to_address, to_port);
196
197         /* Check if edge already exists */
198
199         e = lookup_edge(from, to);
200
201         if(e) {
202                 if(e->weight != weight || e->session_id != session_id || sockaddrcmp(&e->address, &address)) {
203                         if(from == mesh->self) {
204                                 /* The sender has outdated information, we own this edge to send a correction back */
205                                 logger(mesh, MESHLINK_DEBUG, "Got %s from %s for ourself which does not match existing entry", "ADD_EDGE", c->name);
206                                 send_add_edge(mesh, c, e, 0);
207                                 return true;
208                         } else if(to == mesh->self && from != c->node && from->status.reachable) {
209                                 /* The sender has outdated information, someone else owns this node so they will correct */
210                                 logger(mesh, MESHLINK_DEBUG, "Got %s from %s which does not match existing entry, ignoring", "ADD_EDGE", c->name);
211                                 return true;
212                         } else {
213                                 /* Might be outdated, but update our information, another node will send a correction if necessary */
214                                 logger(mesh, MESHLINK_DEBUG, "Got %s from %s which does not match existing entry", "ADD_EDGE", c->name);
215                                 edge_del(mesh, e);
216                         }
217                 } else {
218                         return true;
219                 }
220         } else if(from == mesh->self) {
221                 logger(mesh, MESHLINK_WARNING, "Got %s from %s for ourself which does not exist", "ADD_EDGE", c->name);
222                 mesh->contradicting_add_edge++;
223                 e = new_edge();
224                 e->from = from;
225                 e->to = to;
226                 e->session_id = session_id;
227                 send_del_edge(mesh, c, e, mesh->contradicting_add_edge);
228                 free_edge(e);
229                 return true;
230         }
231
232         e = new_edge();
233         e->from = from;
234         e->to = to;
235         e->address = address;
236         e->weight = weight;
237         e->session_id = session_id;
238         edge_add(mesh, e);
239
240         /* Run MST before or after we tell the rest? */
241
242         graph(mesh);
243
244         if(e->from->submesh && e->to->submesh && (e->from->submesh != e->to->submesh)) {
245                 logger(mesh, MESHLINK_ERROR, "Dropping add edge ( %s to %s )", e->from->submesh->name, e->to->submesh->name);
246                 return false;
247         }
248
249         if(e->from->submesh) {
250                 s = e->from->submesh;
251         } else {
252                 s = e->to->submesh;
253         }
254
255         /* Tell the rest about the new edge */
256
257         forward_request(mesh, c, s, request);
258
259         return true;
260 }
261
262 bool send_del_edge(meshlink_handle_t *mesh, connection_t *c, const edge_t *e, int contradictions) {
263         submesh_t *s = NULL;
264
265         if(c->node && c->node->submesh) {
266                 if(!submesh_allows_node(e->from->submesh, c->node)) {
267                         return true;
268                 }
269
270                 if(!submesh_allows_node(e->to->submesh, c->node)) {
271                         return true;
272                 }
273         }
274
275         if(e->from->submesh && e->to->submesh && (e->from->submesh != e->to->submesh)) {
276                 return true;
277         }
278
279
280         if(e->from->submesh) {
281                 s = e->from->submesh;
282         } else {
283                 s = e->to->submesh;
284         }
285
286         return send_request(mesh, c, s, "%d %x %s %s %d %x", DEL_EDGE, prng(mesh, UINT_MAX),
287                             e->from->name, e->to->name, contradictions, e->session_id);
288 }
289
290 bool del_edge_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
291         assert(request);
292         assert(*request);
293
294         edge_t *e;
295         char from_name[MAX_STRING_SIZE];
296         char to_name[MAX_STRING_SIZE];
297         node_t *from, *to;
298         int contradictions = 0;
299         uint32_t session_id = 0;
300         submesh_t *s = NULL;
301
302         if(sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING" %d %x", from_name, to_name, &contradictions, &session_id) < 2) {
303                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "DEL_EDGE", c->name);
304                 return false;
305         }
306
307         if(seen_request(mesh, request)) {
308                 return true;
309         }
310
311         /* Lookup nodes */
312
313         from = lookup_node(mesh, from_name);
314         to = lookup_node(mesh, to_name);
315
316         if(!from) {
317                 logger(mesh, MESHLINK_WARNING, "Got %s from %s which does not appear in the edge tree", "DEL_EDGE", c->name);
318                 return true;
319         }
320
321         if(!to) {
322                 logger(mesh, MESHLINK_WARNING, "Got %s from %s which does not appear in the edge tree", "DEL_EDGE", c->name);
323                 return true;
324         }
325
326         if(contradictions > 50) {
327                 handle_duplicate_node(mesh, from);
328         }
329
330         /* Check if edge exists */
331
332         e = lookup_edge(from, to);
333
334         if(!e) {
335                 logger(mesh, MESHLINK_WARNING, "Got %s from %s which does not appear in the edge tree", "DEL_EDGE", c->name);
336                 return true;
337         }
338
339         if(e->from == mesh->self) {
340                 logger(mesh, MESHLINK_WARNING, "Got %s from %s for ourself", "DEL_EDGE", c->name);
341                 mesh->contradicting_del_edge++;
342                 send_add_edge(mesh, c, e, mesh->contradicting_del_edge);    /* Send back a correction */
343                 return true;
344         }
345
346         /* Tell the rest about the deleted edge */
347
348
349         if(!e->from->submesh || !e->to->submesh || (e->from->submesh == e->to->submesh)) {
350                 if(e->from->submesh) {
351                         s = e->from->submesh;
352                 } else {
353                         s = e->to->submesh;
354                 }
355
356                 /* Tell the rest about the deleted edge */
357                 forward_request(mesh, c, s, request);
358
359         } else {
360                 logger(mesh, MESHLINK_ERROR, "Dropping del edge ( %s to %s )", e->from->submesh->name, e->to->submesh->name);
361                 return false;
362         }
363
364         /* Delete the edge */
365
366         edge_del(mesh, e);
367
368         /* Run MST before or after we tell the rest? */
369
370         graph(mesh);
371
372         /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
373
374         if(!to->status.reachable) {
375                 e = lookup_edge(to, mesh->self);
376
377                 if(e) {
378                         send_del_edge(mesh, mesh->everyone, e, 0);
379                         edge_del(mesh, e);
380                 }
381         }
382
383         return true;
384 }