]> git.meshlink.io Git - meshlink/blob - src/protocol_edge.c
Add support for encrypted storage.
[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         char *from_submesh, *to_submesh;
41         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", ADD_EDGE, rand(),
78                          e->from->name, e->from->devclass, from_submesh, e->to->name, address, port,
79                          e->to->devclass, to_submesh, e->options, e->weight, contradictions);
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         edge_t *e;
88         node_t *from, *to;
89         char from_name[MAX_STRING_SIZE];
90         int from_devclass;
91         char from_submesh_name[MAX_STRING_SIZE] = "";
92         char to_name[MAX_STRING_SIZE];
93         char to_address[MAX_STRING_SIZE];
94         char to_port[MAX_STRING_SIZE];
95         int to_devclass;
96         char to_submesh_name[MAX_STRING_SIZE] = "";
97         sockaddr_t address;
98         uint32_t options;
99         int weight;
100         int contradictions = 0;
101         submesh_t *s = NULL;
102
103         if(sscanf(request, "%*d %*x "MAX_STRING" %d "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %d "MAX_STRING" %x %d %d",
104                         from_name, &from_devclass, from_submesh_name, to_name, to_address, to_port, &to_devclass, to_submesh_name,
105                         &options, &weight, &contradictions) < 10) {
106                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ADD_EDGE", c->name);
107                 return false;
108         }
109
110         // Check if devclasses are valid
111
112         if(from_devclass < 0 || from_devclass > _DEV_CLASS_MAX) {
113                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ADD_EDGE", c->name, "from devclass invalid");
114                 return false;
115         }
116
117         if(to_devclass < 0 || to_devclass > _DEV_CLASS_MAX) {
118                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ADD_EDGE", c->name, "to devclass invalid");
119                 return false;
120         }
121
122         if(0 == strcmp(from_submesh_name, "")) {
123                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ADD_EDGE", c->name, "invalid submesh id");
124                 return false;
125         }
126
127         if(0 == strcmp(to_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(seen_request(mesh, request)) {
133                 return true;
134         }
135
136         /* Lookup nodes */
137
138         from = lookup_node(mesh, from_name);
139         to = lookup_node(mesh, to_name);
140
141         if(!from) {
142                 from = new_node();
143                 from->status.dirty = true;
144                 from->status.blacklisted = mesh->default_blacklist;
145                 from->name = xstrdup(from_name);
146                 from->devclass = from_devclass;
147
148                 from->submesh = NULL;
149
150                 if(0 != strcmp(from_submesh_name, CORE_MESH)) {
151                         if(!(from->submesh = lookup_or_create_submesh(mesh, from_submesh_name))) {
152                                 return false;
153                         }
154                 }
155
156                 node_add(mesh, from);
157         }
158
159         if(contradictions > 50) {
160                 handle_duplicate_node(mesh, from);
161         }
162
163         from->devclass = from_devclass;
164
165         if(!to) {
166                 to = new_node();
167                 to->status.dirty = true;
168                 to->status.blacklisted = mesh->default_blacklist;
169                 to->name = xstrdup(to_name);
170                 to->devclass = to_devclass;
171
172                 to->submesh = NULL;
173
174                 if(0 != strcmp(to_submesh_name, CORE_MESH)) {
175                         if(!(to->submesh = lookup_or_create_submesh(mesh, to_submesh_name))) {
176                                 return false;
177
178                         }
179                 }
180
181                 node_add(mesh, to);
182         }
183
184         /* Convert addresses */
185
186         address = str2sockaddr(to_address, to_port);
187
188         /* Check if edge already exists */
189
190         e = lookup_edge(from, to);
191
192         if(e) {
193                 if(e->weight != weight || e->options != options || sockaddrcmp(&e->address, &address)) {
194                         if(from == mesh->self) {
195                                 logger(mesh, MESHLINK_WARNING, "Got %s from %s for ourself which does not match existing entry",
196                                        "ADD_EDGE", c->name);
197                                 send_add_edge(mesh, c, e, 0);
198                                 return true;
199                         } else {
200                                 logger(mesh, MESHLINK_WARNING, "Got %s from %s which does not match existing entry",
201                                        "ADD_EDGE", c->name);
202                                 edge_del(mesh, e);
203                                 graph(mesh);
204                         }
205                 } else {
206                         return true;
207                 }
208         } else if(from == mesh->self) {
209                 logger(mesh, MESHLINK_WARNING, "Got %s from %s for ourself which does not exist",
210                        "ADD_EDGE", c->name);
211                 mesh->contradicting_add_edge++;
212                 e = new_edge();
213                 e->from = from;
214                 e->to = to;
215                 send_del_edge(mesh, c, e, mesh->contradicting_add_edge);
216                 free_edge(e);
217                 return true;
218         }
219
220         e = new_edge();
221         e->from = from;
222         e->to = to;
223         e->address = address;
224         e->options = options;
225         e->weight = weight;
226         edge_add(mesh, e);
227
228         /* Run MST before or after we tell the rest? */
229
230         graph(mesh);
231
232         if(e->from->submesh && e->to->submesh && (e->from->submesh != e->to->submesh)) {
233                 logger(mesh, MESHLINK_ERROR, "Dropping add edge ( %s to %s )", e->from->submesh->name, e->to->submesh->name);
234                 return false;
235         }
236
237         if(e->from->submesh) {
238                 s = e->from->submesh;
239         } else {
240                 s = e->to->submesh;
241         }
242
243         /* Tell the rest about the new edge */
244
245         forward_request(mesh, c, s, request);
246
247         return true;
248 }
249
250 bool send_del_edge(meshlink_handle_t *mesh, connection_t *c, const edge_t *e, int contradictions) {
251         submesh_t *s = NULL;
252
253         if(c->node && c->node->submesh) {
254                 if(!submesh_allows_node(e->from->submesh, c->node)) {
255                         return true;
256                 }
257
258                 if(!submesh_allows_node(e->to->submesh, c->node)) {
259                         return true;
260                 }
261         }
262
263         if(e->from->submesh && e->to->submesh && (e->from->submesh != e->to->submesh)) {
264                 return true;
265         }
266
267
268         if(e->from->submesh) {
269                 s = e->from->submesh;
270         } else {
271                 s = e->to->submesh;
272         }
273
274         return send_request(mesh, c, s, "%d %x %s %s %d", DEL_EDGE, rand(),
275                             e->from->name, e->to->name, contradictions);
276 }
277
278 bool del_edge_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
279         edge_t *e;
280         char from_name[MAX_STRING_SIZE];
281         char to_name[MAX_STRING_SIZE];
282         node_t *from, *to;
283         int contradictions = 0;
284         submesh_t *s = NULL;
285
286         if(sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING" %d", from_name, to_name, &contradictions) < 2) {
287                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "DEL_EDGE", c->name);
288                 return false;
289         }
290
291         if(seen_request(mesh, request)) {
292                 return true;
293         }
294
295         /* Lookup nodes */
296
297         from = lookup_node(mesh, from_name);
298         to = lookup_node(mesh, to_name);
299
300         if(!from) {
301                 logger(mesh, MESHLINK_ERROR, "Got %s from %s which does not appear in the edge tree",
302                        "DEL_EDGE", c->name);
303                 return true;
304         }
305
306         if(!to) {
307                 logger(mesh, MESHLINK_ERROR, "Got %s from %s which does not appear in the edge tree",
308                        "DEL_EDGE", c->name);
309                 return true;
310         }
311
312         if(contradictions > 50) {
313                 handle_duplicate_node(mesh, from);
314         }
315
316         /* Check if edge exists */
317
318         e = lookup_edge(from, to);
319
320         if(!e) {
321                 logger(mesh, MESHLINK_WARNING, "Got %s from %s which does not appear in the edge tree",
322                        "DEL_EDGE", c->name);
323                 return true;
324         }
325
326         if(e->from == mesh->self) {
327                 logger(mesh, MESHLINK_WARNING, "Got %s from %s for ourself",
328                        "DEL_EDGE", c->name);
329                 mesh->contradicting_del_edge++;
330                 send_add_edge(mesh, c, e, mesh->contradicting_del_edge);    /* Send back a correction */
331                 return true;
332         }
333
334         /* Tell the rest about the deleted edge */
335
336
337         if(!e->from->submesh || !e->to->submesh || (e->from->submesh == e->to->submesh)) {
338                 if(e->from->submesh) {
339                         s = e->from->submesh;
340                 } else {
341                         s = e->to->submesh;
342                 }
343
344                 /* Tell the rest about the deleted edge */
345                 forward_request(mesh, c, s, request);
346
347         } else {
348                 logger(mesh, MESHLINK_ERROR, "Dropping del edge ( %s to %s )", e->from->submesh->name, e->to->submesh->name);
349                 return false;
350         }
351
352         /* Delete the edge */
353
354         edge_del(mesh, e);
355
356         /* Run MST before or after we tell the rest? */
357
358         graph(mesh);
359
360         /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
361
362         if(!to->status.reachable) {
363                 e = lookup_edge(to, mesh->self);
364
365                 if(e) {
366                         send_del_edge(mesh, mesh->everyone, e, 0);
367                         edge_del(mesh, e);
368                 }
369         }
370
371         return true;
372 }