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