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 const char *from_submesh, *to_submesh;
41 const submesh_t *s = NULL;
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 %x", ADD_EDGE, prng(mesh, UINT_MAX),
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, e->from->session_id);
86 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] = "";
102 int contradictions = 0;
103 uint32_t session_id = 0;
106 if(sscanf(request, "%*d %*x "MAX_STRING" %d "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %d "MAX_STRING" %*x %d %d %x",
107 from_name, &from_devclass, from_submesh_name, to_name, to_address, to_port, &to_devclass, to_submesh_name,
108 &weight, &contradictions, &session_id) < 9) {
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_COUNT) {
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_COUNT) {
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.dirty = true;
147 from->status.blacklisted = mesh->default_blacklist;
148 from->name = xstrdup(from_name);
149 from->devclass = from_devclass;
151 from->submesh = NULL;
153 if(0 != strcmp(from_submesh_name, CORE_MESH)) {
154 if(!(from->submesh = lookup_or_create_submesh(mesh, from_submesh_name))) {
159 node_add(mesh, from);
162 if(contradictions > 50) {
163 handle_duplicate_node(mesh, from);
166 from->devclass = from_devclass;
168 if(!from->session_id) {
169 from->session_id = session_id;
174 to->status.dirty = true;
175 to->status.blacklisted = mesh->default_blacklist;
176 to->name = xstrdup(to_name);
177 to->devclass = to_devclass;
181 if(0 != strcmp(to_submesh_name, CORE_MESH)) {
182 if(!(to->submesh = lookup_or_create_submesh(mesh, to_submesh_name))) {
191 to->devclass = to_devclass;
193 /* Convert addresses */
195 address = str2sockaddr(to_address, to_port);
197 /* Check if edge already exists */
199 e = lookup_edge(from, to);
202 if(e->weight != weight || e->session_id != session_id || sockaddrcmp(&e->address, &address)) {
203 if(from == mesh->self) {
204 logger(mesh, MESHLINK_DEBUG, "Got %s from %s for ourself which does not match existing entry", "ADD_EDGE", c->name);
205 send_add_edge(mesh, c, e, 0);
208 logger(mesh, MESHLINK_DEBUG, "Got %s from %s which does not match existing entry", "ADD_EDGE", c->name);
214 } else if(from == mesh->self) {
215 logger(mesh, MESHLINK_WARNING, "Got %s from %s for ourself which does not exist", "ADD_EDGE", c->name);
216 mesh->contradicting_add_edge++;
220 e->session_id = session_id;
221 send_del_edge(mesh, c, e, mesh->contradicting_add_edge);
229 e->address = address;
231 e->session_id = session_id;
234 /* Run MST before or after we tell the rest? */
238 if(e->from->submesh && e->to->submesh && (e->from->submesh != e->to->submesh)) {
239 logger(mesh, MESHLINK_ERROR, "Dropping add edge ( %s to %s )", e->from->submesh->name, e->to->submesh->name);
243 if(e->from->submesh) {
244 s = e->from->submesh;
249 /* Tell the rest about the new edge */
251 forward_request(mesh, c, s, request);
256 bool send_del_edge(meshlink_handle_t *mesh, connection_t *c, const edge_t *e, int contradictions) {
259 if(c->node && c->node->submesh) {
260 if(!submesh_allows_node(e->from->submesh, c->node)) {
264 if(!submesh_allows_node(e->to->submesh, c->node)) {
269 if(e->from->submesh && e->to->submesh && (e->from->submesh != e->to->submesh)) {
274 if(e->from->submesh) {
275 s = e->from->submesh;
280 return send_request(mesh, c, s, "%d %x %s %s %d %x", DEL_EDGE, prng(mesh, UINT_MAX),
281 e->from->name, e->to->name, contradictions, e->session_id);
284 bool del_edge_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
289 char from_name[MAX_STRING_SIZE];
290 char to_name[MAX_STRING_SIZE];
292 int contradictions = 0;
293 uint32_t session_id = 0;
296 if(sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING" %d %x", from_name, to_name, &contradictions, &session_id) < 2) {
297 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "DEL_EDGE", c->name);
301 if(seen_request(mesh, request)) {
307 from = lookup_node(mesh, from_name);
308 to = lookup_node(mesh, to_name);
311 logger(mesh, MESHLINK_WARNING, "Got %s from %s which does not appear in the edge tree", "DEL_EDGE", c->name);
316 logger(mesh, MESHLINK_WARNING, "Got %s from %s which does not appear in the edge tree", "DEL_EDGE", c->name);
320 if(contradictions > 50) {
321 handle_duplicate_node(mesh, from);
324 /* Check if edge exists */
326 e = lookup_edge(from, to);
329 logger(mesh, MESHLINK_WARNING, "Got %s from %s which does not appear in the edge tree", "DEL_EDGE", c->name);
333 if(e->from == mesh->self) {
334 logger(mesh, MESHLINK_WARNING, "Got %s from %s for ourself", "DEL_EDGE", c->name);
335 mesh->contradicting_del_edge++;
336 send_add_edge(mesh, c, e, mesh->contradicting_del_edge); /* Send back a correction */
340 /* Tell the rest about the deleted edge */
343 if(!e->from->submesh || !e->to->submesh || (e->from->submesh == e->to->submesh)) {
344 if(e->from->submesh) {
345 s = e->from->submesh;
350 /* Tell the rest about the deleted edge */
351 forward_request(mesh, c, s, request);
354 logger(mesh, MESHLINK_ERROR, "Dropping del edge ( %s to %s )", e->from->submesh->name, e->to->submesh->name);
358 /* Delete the edge */
362 /* Run MST before or after we tell the rest? */
366 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
368 if(!to->status.reachable) {
369 e = lookup_edge(to, mesh->self);
372 send_del_edge(mesh, mesh->everyone, e, 0);