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