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