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 bool send_add_edge(meshlink_handle_t *mesh, connection_t *c, const edge_t *e, int contradictions) {
40 char *from_submesh, *to_submesh;
43 if(c->node && c->node->submesh) {
44 if(!submesh_allows_node(e->from->submesh, c->node)) {
48 if(!submesh_allows_node(e->to->submesh, c->node)) {
53 if(e->from->submesh && e->to->submesh && (e->from->submesh != e->to->submesh)) {
57 sockaddr2str(&e->address, &address, &port);
59 if(e->from->submesh) {
60 from_submesh = e->from->submesh->name;
62 from_submesh = CORE_MESH;
66 to_submesh = e->to->submesh->name;
68 to_submesh = CORE_MESH;
71 if(e->from->submesh) {
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);
86 bool add_edge_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
89 char from_name[MAX_STRING_SIZE];
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];
96 char to_submesh_name[MAX_STRING_SIZE] = "";
99 int contradictions = 0;
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);
109 // Check if devclasses are valid
111 if(from_devclass < 0 || from_devclass > _DEV_CLASS_MAX) {
112 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ADD_EDGE", c->name, "from devclass invalid");
116 if(to_devclass < 0 || to_devclass > _DEV_CLASS_MAX) {
117 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ADD_EDGE", c->name, "to devclass invalid");
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");
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");
131 if(seen_request(mesh, request)) {
137 from = lookup_node(mesh, from_name);
138 to = lookup_node(mesh, to_name);
142 from->status.dirty = true;
143 from->status.blacklisted = mesh->default_blacklist;
144 from->name = xstrdup(from_name);
145 from->devclass = from_devclass;
147 from->submesh = NULL;
149 if(0 != strcmp(from_submesh_name, CORE_MESH)) {
150 if(!(from->submesh = lookup_or_create_submesh(mesh, from_submesh_name))) {
155 node_add(mesh, from);
158 if(contradictions > 50) {
159 handle_duplicate_node(mesh, from);
162 from->devclass = from_devclass;
166 to->status.dirty = true;
167 to->status.blacklisted = mesh->default_blacklist;
168 to->name = xstrdup(to_name);
169 to->devclass = to_devclass;
173 if(0 != strcmp(to_submesh_name, CORE_MESH)) {
174 if(!(to->submesh = lookup_or_create_submesh(mesh, to_submesh_name))) {
183 to->devclass = to_devclass;
185 /* Convert addresses */
187 address = str2sockaddr(to_address, to_port);
189 /* Check if edge already exists */
191 e = lookup_edge(from, to);
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);
201 logger(mesh, MESHLINK_WARNING, "Got %s from %s which does not match existing entry",
202 "ADD_EDGE", c->name);
209 } else if(from == mesh->self) {
210 logger(mesh, MESHLINK_WARNING, "Got %s from %s for ourself which does not exist",
211 "ADD_EDGE", c->name);
212 mesh->contradicting_add_edge++;
216 send_del_edge(mesh, c, e, mesh->contradicting_add_edge);
224 e->address = address;
228 /* Run MST before or after we tell the rest? */
232 if(e->from->submesh && e->to->submesh && (e->from->submesh != e->to->submesh)) {
233 logger(mesh, MESHLINK_ERROR, "Dropping add edge ( %s to %s )", e->from->submesh->name, e->to->submesh->name);
237 if(e->from->submesh) {
238 s = e->from->submesh;
243 /* Tell the rest about the new edge */
245 forward_request(mesh, c, s, request);
250 bool send_del_edge(meshlink_handle_t *mesh, connection_t *c, const edge_t *e, int contradictions) {
253 if(c->node && c->node->submesh) {
254 if(!submesh_allows_node(e->from->submesh, c->node)) {
258 if(!submesh_allows_node(e->to->submesh, c->node)) {
263 if(e->from->submesh && e->to->submesh && (e->from->submesh != e->to->submesh)) {
268 if(e->from->submesh) {
269 s = e->from->submesh;
274 return send_request(mesh, c, s, "%d %x %s %s %d", DEL_EDGE, rand(),
275 e->from->name, e->to->name, contradictions);
278 bool del_edge_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
280 char from_name[MAX_STRING_SIZE];
281 char to_name[MAX_STRING_SIZE];
283 int contradictions = 0;
286 if(sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING" %d", from_name, to_name, &contradictions) < 2) {
287 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "DEL_EDGE", c->name);
291 if(seen_request(mesh, request)) {
297 from = lookup_node(mesh, from_name);
298 to = lookup_node(mesh, to_name);
301 logger(mesh, MESHLINK_ERROR, "Got %s from %s which does not appear in the edge tree",
302 "DEL_EDGE", c->name);
307 logger(mesh, MESHLINK_ERROR, "Got %s from %s which does not appear in the edge tree",
308 "DEL_EDGE", c->name);
312 if(contradictions > 50) {
313 handle_duplicate_node(mesh, from);
316 /* Check if edge exists */
318 e = lookup_edge(from, to);
321 logger(mesh, MESHLINK_WARNING, "Got %s from %s which does not appear in the edge tree",
322 "DEL_EDGE", c->name);
326 if(e->from == mesh->self) {
327 logger(mesh, MESHLINK_WARNING, "Got %s from %s for ourself",
328 "DEL_EDGE", c->name);
329 mesh->contradicting_del_edge++;
330 send_add_edge(mesh, c, e, mesh->contradicting_del_edge); /* Send back a correction */
334 /* Tell the rest about the deleted edge */
337 if(!e->from->submesh || !e->to->submesh || (e->from->submesh == e->to->submesh)) {
338 if(e->from->submesh) {
339 s = e->from->submesh;
344 /* Tell the rest about the deleted edge */
345 forward_request(mesh, c, s, request);
348 logger(mesh, MESHLINK_ERROR, "Dropping del edge ( %s to %s )", e->from->submesh->name, e->to->submesh->name);
352 /* Delete the edge */
356 /* Run MST before or after we tell the rest? */
360 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
362 if(!to->status.reachable) {
363 e = lookup_edge(to, mesh->self);
366 send_del_edge(mesh, mesh->everyone, e, 0);