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