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