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"
27 #include "meshlink_internal.h"
37 extern bool node_write_devclass(meshlink_handle_t *mesh, node_t *n);
38 extern bool node_write_submesh(meshlink_handle_t *mesh, node_t *n);
40 bool send_add_edge(meshlink_handle_t *mesh, connection_t *c, const edge_t *e, int contradictions) {
43 char *from_submesh, *to_submesh;
46 if(c->node && c->node->submesh) {
47 if(!submesh_allows_node(e->from->submesh, c->node)) {
51 if(!submesh_allows_node(e->to->submesh, c->node)) {
56 if(e->from->submesh && e->to->submesh && (e->from->submesh != e->to->submesh)) {
60 sockaddr2str(&e->address, &address, &port);
62 if(e->from->submesh) {
63 from_submesh = e->from->submesh->name;
65 from_submesh = CORE_MESH;
69 to_submesh = e->to->submesh->name;
71 to_submesh = CORE_MESH;
74 if(e->from->submesh) {
80 x = send_request(mesh, c, s, "%d %x %s %d %s %s %s %s %d %s %x %d %d", ADD_EDGE, rand(),
81 e->from->name, e->from->devclass, from_submesh, e->to->name, address, port,
82 e->to->devclass, to_submesh, e->options, e->weight, contradictions);
89 bool add_edge_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
92 char from_name[MAX_STRING_SIZE];
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];
99 char to_submesh_name[MAX_STRING_SIZE] = "";
103 int contradictions = 0;
106 if(sscanf(request, "%*d %*x "MAX_STRING" %d "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %d "MAX_STRING" %x %d %d",
107 from_name, &from_devclass, from_submesh_name, to_name, to_address, to_port, &to_devclass, to_submesh_name,
108 &options, &weight, &contradictions) < 10) {
109 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ADD_EDGE", c->name);
113 // Check if devclasses are valid
115 if(from_devclass < 0 || from_devclass > _DEV_CLASS_MAX) {
116 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ADD_EDGE", c->name, "from devclass invalid");
120 if(to_devclass < 0 || to_devclass > _DEV_CLASS_MAX) {
121 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ADD_EDGE", c->name, "to devclass invalid");
125 if(0 == strcmp(from_submesh_name, "")) {
126 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ADD_EDGE", c->name, "invalid submesh id");
130 if(0 == strcmp(to_submesh_name, "")) {
131 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ADD_EDGE", c->name, "invalid submesh id");
135 if(seen_request(mesh, request)) {
141 from = lookup_node(mesh, from_name);
142 to = lookup_node(mesh, to_name);
146 from->status.blacklisted = mesh->default_blacklist;
147 from->name = xstrdup(from_name);
149 from->submesh = NULL;
151 if(0 != strcmp(from_submesh_name, CORE_MESH)) {
152 if(!(from->submesh = lookup_or_create_submesh(mesh, from_submesh_name))) {
157 node_add(mesh, from);
160 if(contradictions > 50) {
161 handle_duplicate_node(mesh, from);
164 from->devclass = from_devclass;
165 node_write_devclass(mesh, from);
168 node_write_submesh(mesh, from);
173 to->status.blacklisted = mesh->default_blacklist;
174 to->name = xstrdup(to_name);
178 if(0 != strcmp(to_submesh_name, CORE_MESH)) {
179 if(!(to->submesh = lookup_or_create_submesh(mesh, to_submesh_name))) {
188 to->devclass = to_devclass;
189 node_write_devclass(mesh, to);
192 node_write_submesh(mesh, to);
195 /* Convert addresses */
197 address = str2sockaddr(to_address, to_port);
199 /* Check if edge already exists */
201 e = lookup_edge(from, to);
204 if(e->weight != weight || e->options != options || sockaddrcmp(&e->address, &address)) {
205 if(from == mesh->self) {
206 logger(mesh, MESHLINK_WARNING, "Got %s from %s for ourself which does not match existing entry",
207 "ADD_EDGE", c->name);
208 send_add_edge(mesh, c, e, 0);
211 logger(mesh, MESHLINK_WARNING, "Got %s from %s which does not match existing entry",
212 "ADD_EDGE", c->name);
219 } else if(from == mesh->self) {
220 logger(mesh, MESHLINK_WARNING, "Got %s from %s for ourself which does not exist",
221 "ADD_EDGE", c->name);
222 mesh->contradicting_add_edge++;
226 send_del_edge(mesh, c, e, mesh->contradicting_add_edge);
234 e->address = address;
235 e->options = options;
239 /* Run MST before or after we tell the rest? */
243 if(e->from->submesh && e->to->submesh && (e->from->submesh != e->to->submesh)) {
244 logger(mesh, MESHLINK_ERROR, "Dropping add edge ( %s to %s )", e->from->submesh->name, e->to->submesh->name);
248 if(e->from->submesh) {
249 s = e->from->submesh;
254 /* Tell the rest about the new edge */
256 forward_request(mesh, c, s, request);
261 bool send_del_edge(meshlink_handle_t *mesh, connection_t *c, const edge_t *e, int contradictions) {
264 if(c->node && c->node->submesh) {
265 if(!submesh_allows_node(e->from->submesh, c->node)) {
269 if(!submesh_allows_node(e->to->submesh, c->node)) {
274 if(e->from->submesh && e->to->submesh && (e->from->submesh != e->to->submesh)) {
279 if(e->from->submesh) {
280 s = e->from->submesh;
285 return send_request(mesh, c, s, "%d %x %s %s %d", DEL_EDGE, rand(),
286 e->from->name, e->to->name, contradictions);
289 bool del_edge_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
291 char from_name[MAX_STRING_SIZE];
292 char to_name[MAX_STRING_SIZE];
294 int contradictions = 0;
297 if(sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING" %d", from_name, to_name, &contradictions) < 2) {
298 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "DEL_EDGE", c->name);
302 if(seen_request(mesh, request)) {
308 from = lookup_node(mesh, from_name);
309 to = lookup_node(mesh, to_name);
312 logger(mesh, MESHLINK_ERROR, "Got %s from %s which does not appear in the edge tree",
313 "DEL_EDGE", c->name);
318 logger(mesh, MESHLINK_ERROR, "Got %s from %s which does not appear in the edge tree",
319 "DEL_EDGE", c->name);
323 if(contradictions > 50) {
324 handle_duplicate_node(mesh, from);
327 /* Check if edge exists */
329 e = lookup_edge(from, to);
332 logger(mesh, MESHLINK_WARNING, "Got %s from %s which does not appear in the edge tree",
333 "DEL_EDGE", c->name);
337 if(e->from == mesh->self) {
338 logger(mesh, MESHLINK_WARNING, "Got %s from %s for ourself",
339 "DEL_EDGE", c->name);
340 mesh->contradicting_del_edge++;
341 send_add_edge(mesh, c, e, mesh->contradicting_del_edge); /* Send back a correction */
345 /* Tell the rest about the deleted edge */
348 if(!e->from->submesh || !e->to->submesh || (e->from->submesh == e->to->submesh)) {
349 if(e->from->submesh) {
350 s = e->from->submesh;
355 /* Tell the rest about the deleted edge */
356 forward_request(mesh, c, s, request);
359 logger(mesh, MESHLINK_ERROR, "Dropping del edge ( %s to %s )", e->from->submesh->name, e->to->submesh->name);
363 /* Delete the edge */
367 /* Run MST before or after we tell the rest? */
371 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
373 if(!to->status.reachable) {
374 e = lookup_edge(to, mesh->self);
377 send_del_edge(mesh, mesh->everyone, e, 0);