2 protocol_edge.c -- handle the meta-protocol, edges
3 Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
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.
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.
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.
23 #include "connection.h"
25 #include "meshlink_internal.h"
36 bool send_add_edge(meshlink_handle_t *mesh, connection_t *c, const edge_t *e, int contradictions) {
39 const char *from_submesh, *to_submesh;
40 const submesh_t *s = NULL;
42 if(c->node && c->node->submesh) {
43 if(!submesh_allows_node(e->from->submesh, c->node)) {
47 if(!submesh_allows_node(e->to->submesh, c->node)) {
52 if(e->from->submesh && e->to->submesh && (e->from->submesh != e->to->submesh)) {
56 sockaddr2str(&e->address, &address, &port);
58 if(e->from->submesh) {
59 from_submesh = e->from->submesh->name;
61 from_submesh = CORE_MESH;
65 to_submesh = e->to->submesh->name;
67 to_submesh = CORE_MESH;
70 if(e->from->submesh) {
76 x = send_request(mesh, c, s, "%d %x %s %d %s %s %s %s %d %s %x %d %d %x", ADD_EDGE, prng(mesh, UINT_MAX),
77 e->from->name, e->from->devclass, from_submesh, e->to->name, address, port,
78 e->to->devclass, to_submesh, OPTION_PMTU_DISCOVERY, e->weight, contradictions, e->from->session_id);
86 bool add_edge_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
95 char from_name[MAX_STRING_SIZE];
97 char from_submesh_name[MAX_STRING_SIZE] = "";
98 char to_name[MAX_STRING_SIZE];
99 char to_address[MAX_STRING_SIZE];
100 char to_port[MAX_STRING_SIZE];
102 char to_submesh_name[MAX_STRING_SIZE] = "";
105 int contradictions = 0;
106 uint32_t session_id = 0;
109 if(sscanf(request, "%*d %*x "MAX_STRING" %d "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %d "MAX_STRING" %*x %d %d %x",
110 from_name, &from_devclass, from_submesh_name, to_name, to_address, to_port, &to_devclass, to_submesh_name,
111 &weight, &contradictions, &session_id) < 9) {
112 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ADD_EDGE", c->name);
116 // Check if devclasses are valid
118 if(from_devclass < 0 || from_devclass >= DEV_CLASS_COUNT) {
119 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ADD_EDGE", c->name, "from devclass invalid");
123 if(to_devclass < 0 || to_devclass >= DEV_CLASS_COUNT) {
124 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ADD_EDGE", c->name, "to devclass invalid");
128 if(0 == strcmp(from_submesh_name, "")) {
129 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ADD_EDGE", c->name, "invalid submesh id");
133 if(0 == strcmp(to_submesh_name, "")) {
134 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ADD_EDGE", c->name, "invalid submesh id");
138 if(seen_request(mesh, request)) {
144 from = lookup_node(mesh, from_name);
145 to = lookup_node(mesh, to_name);
149 from->status.dirty = true;
150 from->status.blacklisted = false;
151 from->name = xstrdup(from_name);
152 from->devclass = from_devclass;
154 from->submesh = NULL;
156 if(0 != strcmp(from_submesh_name, CORE_MESH)) {
157 if(!(from->submesh = lookup_or_create_submesh(mesh, from_submesh_name))) {
162 node_add(mesh, from);
165 if(contradictions > 50) {
166 handle_duplicate_node(mesh, from);
169 from->devclass = from_devclass;
171 if(!from->session_id) {
172 from->session_id = session_id;
177 to->status.dirty = true;
178 to->status.blacklisted = false;
179 to->name = xstrdup(to_name);
180 to->devclass = to_devclass;
184 if(0 != strcmp(to_submesh_name, CORE_MESH)) {
185 if(!(to->submesh = lookup_or_create_submesh(mesh, to_submesh_name))) {
194 to->devclass = to_devclass;
196 /* Convert addresses */
198 address = str2sockaddr(to_address, to_port);
200 /* Check if edge already exists */
202 e = lookup_edge(from, to);
205 if(e->weight != weight || e->session_id != session_id || sockaddrcmp(&e->address, &address)) {
206 if(from == mesh->self) {
207 /* The sender has outdated information, we own this edge to send a correction back */
208 logger(mesh, MESHLINK_DEBUG, "Got %s from %s for ourself which does not match existing entry", "ADD_EDGE", c->name);
209 send_add_edge(mesh, c, e, 0);
211 } else if(to == mesh->self && from != c->node && from->status.reachable) {
212 /* The sender has outdated information, someone else owns this node so they will correct */
213 logger(mesh, MESHLINK_DEBUG, "Got %s from %s which does not match existing entry, ignoring", "ADD_EDGE", c->name);
216 /* Might be outdated, but update our information, another node will send a correction if necessary */
217 logger(mesh, MESHLINK_DEBUG, "Got %s from %s which does not match existing entry", "ADD_EDGE", c->name);
223 } else if(from == mesh->self) {
224 logger(mesh, MESHLINK_WARNING, "Got %s from %s for ourself which does not exist", "ADD_EDGE", c->name);
225 mesh->contradicting_add_edge++;
229 e->session_id = session_id;
230 send_del_edge(mesh, c, e, mesh->contradicting_add_edge);
238 e->address = address;
240 e->session_id = session_id;
243 /* Run MST before or after we tell the rest? */
247 if(e->from->submesh && e->to->submesh && (e->from->submesh != e->to->submesh)) {
248 logger(mesh, MESHLINK_ERROR, "Dropping add edge ( %s to %s )", e->from->submesh->name, e->to->submesh->name);
252 if(e->from->submesh) {
253 s = e->from->submesh;
258 /* Tell the rest about the new edge */
260 forward_request(mesh, c, s, request);
263 /* TODO: Check if this is an edge we would own */
268 bool send_del_edge(meshlink_handle_t *mesh, connection_t *c, const edge_t *e, int contradictions) {
271 if(c->node && c->node->submesh) {
272 if(!submesh_allows_node(e->from->submesh, c->node)) {
276 if(!submesh_allows_node(e->to->submesh, c->node)) {
281 if(e->from->submesh && e->to->submesh && (e->from->submesh != e->to->submesh)) {
286 if(e->from->submesh) {
287 s = e->from->submesh;
292 return send_request(mesh, c, s, "%d %x %s %s %d %x", DEL_EDGE, prng(mesh, UINT_MAX),
293 e->from->name, e->to->name, contradictions, e->session_id);
297 bool del_edge_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
305 char from_name[MAX_STRING_SIZE];
306 char to_name[MAX_STRING_SIZE];
308 int contradictions = 0;
309 uint32_t session_id = 0;
312 if(sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING" %d %x", from_name, to_name, &contradictions, &session_id) < 2) {
313 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "DEL_EDGE", c->name);
317 if(seen_request(mesh, request)) {
323 from = lookup_node(mesh, from_name);
324 to = lookup_node(mesh, to_name);
327 logger(mesh, MESHLINK_WARNING, "Got %s from %s which does not appear in the edge tree", "DEL_EDGE", c->name);
332 logger(mesh, MESHLINK_WARNING, "Got %s from %s which does not appear in the edge tree", "DEL_EDGE", c->name);
336 if(contradictions > 50) {
337 handle_duplicate_node(mesh, from);
340 /* Check if edge exists */
342 e = lookup_edge(from, to);
345 logger(mesh, MESHLINK_WARNING, "Got %s from %s which does not appear in the edge tree", "DEL_EDGE", c->name);
349 if(e->from == mesh->self) {
350 logger(mesh, MESHLINK_WARNING, "Got %s from %s for ourself", "DEL_EDGE", c->name);
351 mesh->contradicting_del_edge++;
352 send_add_edge(mesh, c, e, mesh->contradicting_del_edge); /* Send back a correction */
356 /* Tell the rest about the deleted edge */
359 if(!e->from->submesh || !e->to->submesh || (e->from->submesh == e->to->submesh)) {
360 if(e->from->submesh) {
361 s = e->from->submesh;
366 /* Tell the rest about the deleted edge */
367 forward_request(mesh, c, s, request);
370 logger(mesh, MESHLINK_ERROR, "Dropping del edge ( %s to %s )", e->from->submesh->name, e->to->submesh->name);
374 /* Delete the edge */
378 /* Run MST before or after we tell the rest? */
382 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
384 if(!to->status.reachable) {
385 e = lookup_edge(to, mesh->self);
388 send_del_edge(mesh, mesh->everyone, e, 0);
395 /* TODO: Check if this is an edge we would own. */