]> git.meshlink.io Git - meshlink/blob - src/protocol_edge.c
Improve support for submeshes.
[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 extern bool node_write_devclass(meshlink_handle_t *mesh, node_t *n);
38 extern bool node_write_submesh(meshlink_handle_t *mesh, node_t *n);
39
40 bool send_add_edge(meshlink_handle_t *mesh, connection_t *c, const edge_t *e, int contradictions) {
41         bool x;
42         char *address, *port;
43         char *from_submesh, *to_submesh;
44         submesh_t *s = NULL;
45
46         if(c->node && c->node->submesh) {
47                 if(!submesh_allows_node(e->from->submesh, c->node)) {
48                         return true;
49                 }
50
51                 if(!submesh_allows_node(e->to->submesh, c->node)) {
52                         return true;
53                 }
54         }
55
56         if(e->from->submesh && e->to->submesh && (e->from->submesh != e->to->submesh)) {
57                 return true;
58         }
59
60         sockaddr2str(&e->address, &address, &port);
61
62         if(e->from->submesh) {
63                 from_submesh = e->from->submesh->name;
64         } else {
65                 from_submesh = CORE_MESH;
66         }
67
68         if(e->to->submesh) {
69                 to_submesh = e->to->submesh->name;
70         } else {
71                 to_submesh = CORE_MESH;
72         }
73
74         if(e->from->submesh) {
75                 s = e->from->submesh;
76         } else {
77                 s = e->to->submesh;
78         }
79
80         x = send_request(mesh, c, s, "%d %x %s %d %s %s %s %s %d %s %x %d %d", ADD_EDGE, rand(),
81                          e->from->name, e->from->devclass, from_submesh, e->to->name, address, port,
82                          e->to->devclass, to_submesh, e->options, e->weight, contradictions);
83         free(address);
84         free(port);
85
86         return x;
87 }
88
89 bool add_edge_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
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         uint32_t options;
102         int weight;
103         int contradictions = 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",
107                         from_name, &from_devclass, from_submesh_name, to_name, to_address, to_port, &to_devclass, to_submesh_name,
108                         &options, &weight, &contradictions) < 10) {
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_MAX) {
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_MAX) {
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.blacklisted = mesh->default_blacklist;
147                 from->name = xstrdup(from_name);
148
149                 from->submesh = NULL;
150
151                 if(0 != strcmp(from_submesh_name, CORE_MESH)) {
152                         if(!(from->submesh = lookup_or_create_submesh(mesh, from_submesh_name))) {
153                                 return false;
154                         }
155                 }
156
157                 node_add(mesh, from);
158         }
159
160         if(contradictions > 50) {
161                 handle_duplicate_node(mesh, from);
162         }
163
164         from->devclass = from_devclass;
165         node_write_devclass(mesh, from);
166
167         if(from->submesh) {
168                 node_write_submesh(mesh, from);
169         }
170
171         if(!to) {
172                 to = new_node();
173                 to->status.blacklisted = mesh->default_blacklist;
174                 to->name = xstrdup(to_name);
175
176                 to->submesh = NULL;
177
178                 if(0 != strcmp(to_submesh_name, CORE_MESH)) {
179                         if(!(to->submesh = lookup_or_create_submesh(mesh, to_submesh_name))) {
180                                 return false;
181
182                         }
183                 }
184
185                 node_add(mesh, to);
186         }
187
188         to->devclass = to_devclass;
189         node_write_devclass(mesh, to);
190
191         if(to->submesh) {
192                 node_write_submesh(mesh, to);
193         }
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->options != options || sockaddrcmp(&e->address, &address)) {
205                         if(from == mesh->self) {
206                                 logger(mesh, MESHLINK_WARNING, "Got %s from %s for ourself which does not match existing entry",
207                                        "ADD_EDGE", c->name);
208                                 send_add_edge(mesh, c, e, 0);
209                                 return true;
210                         } else {
211                                 logger(mesh, MESHLINK_WARNING, "Got %s from %s which does not match existing entry",
212                                        "ADD_EDGE", c->name);
213                                 edge_del(mesh, e);
214                                 graph(mesh);
215                         }
216                 } else {
217                         return true;
218                 }
219         } else if(from == mesh->self) {
220                 logger(mesh, MESHLINK_WARNING, "Got %s from %s for ourself which does not exist",
221                        "ADD_EDGE", c->name);
222                 mesh->contradicting_add_edge++;
223                 e = new_edge();
224                 e->from = from;
225                 e->to = to;
226                 send_del_edge(mesh, c, e, mesh->contradicting_add_edge);
227                 free_edge(e);
228                 return true;
229         }
230
231         e = new_edge();
232         e->from = from;
233         e->to = to;
234         e->address = address;
235         e->options = options;
236         e->weight = weight;
237         edge_add(mesh, e);
238
239         /* Run MST before or after we tell the rest? */
240
241         graph(mesh);
242
243         if(e->from->submesh && e->to->submesh && (e->from->submesh != e->to->submesh)) {
244                 logger(mesh, MESHLINK_ERROR, "Dropping add edge ( %s to %s )", e->from->submesh->name, e->to->submesh->name);
245                 return false;
246         }
247
248         if(e->from->submesh) {
249                 s = e->from->submesh;
250         } else {
251                 s = e->to->submesh;
252         }
253
254         /* Tell the rest about the new edge */
255
256         forward_request(mesh, c, s, request);
257
258         return true;
259 }
260
261 bool send_del_edge(meshlink_handle_t *mesh, connection_t *c, const edge_t *e, int contradictions) {
262         submesh_t *s = NULL;
263
264         if(c->node && c->node->submesh) {
265                 if(!submesh_allows_node(e->from->submesh, c->node)) {
266                         return true;
267                 }
268
269                 if(!submesh_allows_node(e->to->submesh, c->node)) {
270                         return true;
271                 }
272         }
273
274         if(e->from->submesh && e->to->submesh && (e->from->submesh != e->to->submesh)) {
275                 return true;
276         }
277
278
279         if(e->from->submesh) {
280                 s = e->from->submesh;
281         } else {
282                 s = e->to->submesh;
283         }
284
285         return send_request(mesh, c, s, "%d %x %s %s %d", DEL_EDGE, rand(),
286                             e->from->name, e->to->name, contradictions);
287 }
288
289 bool del_edge_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
290         edge_t *e;
291         char from_name[MAX_STRING_SIZE];
292         char to_name[MAX_STRING_SIZE];
293         node_t *from, *to;
294         int contradictions = 0;
295         submesh_t *s = NULL;
296
297         if(sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING" %d", from_name, to_name, &contradictions) < 2) {
298                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "DEL_EDGE", c->name);
299                 return false;
300         }
301
302         if(seen_request(mesh, request)) {
303                 return true;
304         }
305
306         /* Lookup nodes */
307
308         from = lookup_node(mesh, from_name);
309         to = lookup_node(mesh, to_name);
310
311         if(!from) {
312                 logger(mesh, MESHLINK_ERROR, "Got %s from %s which does not appear in the edge tree",
313                        "DEL_EDGE", c->name);
314                 return true;
315         }
316
317         if(!to) {
318                 logger(mesh, MESHLINK_ERROR, "Got %s from %s which does not appear in the edge tree",
319                        "DEL_EDGE", c->name);
320                 return true;
321         }
322
323         if(contradictions > 50) {
324                 handle_duplicate_node(mesh, from);
325         }
326
327         /* Check if edge exists */
328
329         e = lookup_edge(from, to);
330
331         if(!e) {
332                 logger(mesh, MESHLINK_WARNING, "Got %s from %s which does not appear in the edge tree",
333                        "DEL_EDGE", c->name);
334                 return true;
335         }
336
337         if(e->from == mesh->self) {
338                 logger(mesh, MESHLINK_WARNING, "Got %s from %s for ourself",
339                        "DEL_EDGE", c->name);
340                 mesh->contradicting_del_edge++;
341                 send_add_edge(mesh, c, e, mesh->contradicting_del_edge);    /* Send back a correction */
342                 return true;
343         }
344
345         /* Tell the rest about the deleted edge */
346
347
348         if(!e->from->submesh || !e->to->submesh || (e->from->submesh == e->to->submesh)) {
349                 if(e->from->submesh) {
350                         s = e->from->submesh;
351                 } else {
352                         s = e->to->submesh;
353                 }
354
355                 /* Tell the rest about the deleted edge */
356                 forward_request(mesh, c, s, request);
357
358         } else {
359                 logger(mesh, MESHLINK_ERROR, "Dropping del edge ( %s to %s )", e->from->submesh->name, e->to->submesh->name);
360                 return false;
361         }
362
363         /* Delete the edge */
364
365         edge_del(mesh, e);
366
367         /* Run MST before or after we tell the rest? */
368
369         graph(mesh);
370
371         /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
372
373         if(!to->status.reachable) {
374                 e = lookup_edge(to, mesh->self);
375
376                 if(e) {
377                         send_del_edge(mesh, mesh->everyone, e, 0);
378                         edge_del(mesh, e);
379                 }
380         }
381
382         return true;
383 }