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"
36 bool send_add_edge(meshlink_handle_t *mesh, connection_t *c, const edge_t *e, int contradictions) {
40 sockaddr2str(&e->address, &address, &port);
42 x = send_request(mesh, c, "%d %x %s %d %s %s %s %d %x %d %d", ADD_EDGE, rand(),
43 e->from->name, e->from->devclass, e->to->name, address, port, e->to->devclass,
44 e->options, e->weight, contradictions);
51 bool add_edge_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
54 char from_name[MAX_STRING_SIZE];
56 char to_name[MAX_STRING_SIZE];
57 char to_address[MAX_STRING_SIZE];
58 char to_port[MAX_STRING_SIZE];
63 int contradictions = 0;
65 if(sscanf(request, "%*d %*x "MAX_STRING" %d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %x %d %d",
66 from_name, &from_devclass, to_name, to_address, to_port, &to_devclass, &options, &weight, &contradictions) < 8) {
67 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ADD_EDGE", c->name);
71 /* Check if names are valid */
73 if(!check_id(from_name) || !check_id(to_name)) {
74 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ADD_EDGE", c->name, "invalid name");
78 // Check if devclasses are valid
80 if(from_devclass < 0 || from_devclass > _DEV_CLASS_MAX) {
81 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ADD_EDGE", c->name, "from devclass invalid");
85 if(to_devclass < 0 || to_devclass > _DEV_CLASS_MAX) {
86 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ADD_EDGE", c->name, "to devclass invalid");
90 if(seen_request(mesh, request)) {
96 from = lookup_node(mesh, from_name);
97 to = lookup_node(mesh, to_name);
101 from->status.dirty = true;
102 from->status.blacklisted = mesh->default_blacklist;
103 from->name = xstrdup(from_name);
104 from->devclass = from_devclass;
105 node_add(mesh, from);
108 if(contradictions > 50) {
109 handle_duplicate_node(mesh, from);
112 from->devclass = from_devclass;
116 to->status.dirty = true;
117 to->status.blacklisted = mesh->default_blacklist;
118 to->name = xstrdup(to_name);
119 to->devclass = to_devclass;
123 /* Convert addresses */
125 address = str2sockaddr(to_address, to_port);
127 /* Check if edge already exists */
129 e = lookup_edge(from, to);
132 if(e->weight != weight || e->options != options || sockaddrcmp(&e->address, &address)) {
133 if(from == mesh->self) {
134 logger(mesh, MESHLINK_WARNING, "Got %s from %s for ourself which does not match existing entry",
135 "ADD_EDGE", c->name);
136 send_add_edge(mesh, c, e, 0);
139 logger(mesh, MESHLINK_WARNING, "Got %s from %s which does not match existing entry",
140 "ADD_EDGE", c->name);
147 } else if(from == mesh->self) {
148 logger(mesh, MESHLINK_WARNING, "Got %s from %s for ourself which does not exist",
149 "ADD_EDGE", c->name);
150 mesh->contradicting_add_edge++;
154 send_del_edge(mesh, c, e, mesh->contradicting_add_edge);
162 e->address = address;
163 e->options = options;
167 /* Tell the rest about the new edge */
169 forward_request(mesh, c, request);
171 /* Run MST before or after we tell the rest? */
178 bool send_del_edge(meshlink_handle_t *mesh, connection_t *c, const edge_t *e, int contradictions) {
179 return send_request(mesh, c, "%d %x %s %s %d", DEL_EDGE, rand(),
180 e->from->name, e->to->name, contradictions);
183 bool del_edge_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
185 char from_name[MAX_STRING_SIZE];
186 char to_name[MAX_STRING_SIZE];
188 int contradictions = 0;
190 if(sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING" %d", from_name, to_name, &contradictions) < 2) {
191 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "DEL_EDGE", c->name);
195 /* Check if names are valid */
197 if(!check_id(from_name) || !check_id(to_name)) {
198 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "DEL_EDGE", c->name, "invalid name");
202 if(seen_request(mesh, request)) {
208 from = lookup_node(mesh, from_name);
209 to = lookup_node(mesh, to_name);
212 logger(mesh, MESHLINK_ERROR, "Got %s from %s which does not appear in the edge tree",
213 "DEL_EDGE", c->name);
218 logger(mesh, MESHLINK_ERROR, "Got %s from %s which does not appear in the edge tree",
219 "DEL_EDGE", c->name);
223 if(contradictions > 50) {
224 handle_duplicate_node(mesh, from);
227 /* Check if edge exists */
229 e = lookup_edge(from, to);
232 logger(mesh, MESHLINK_WARNING, "Got %s from %s which does not appear in the edge tree",
233 "DEL_EDGE", c->name);
237 if(e->from == mesh->self) {
238 logger(mesh, MESHLINK_WARNING, "Got %s from %s for ourself",
239 "DEL_EDGE", c->name);
240 mesh->contradicting_del_edge++;
241 send_add_edge(mesh, c, e, mesh->contradicting_del_edge); /* Send back a correction */
245 /* Tell the rest about the deleted edge */
247 forward_request(mesh, c, request);
249 /* Delete the edge */
253 /* Run MST before or after we tell the rest? */
257 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
259 if(!to->status.reachable) {
260 e = lookup_edge(to, mesh->self);
263 send_del_edge(mesh, mesh->everyone, e, 0);