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 extern bool node_write_devclass(meshlink_handle_t *mesh, node_t *n);
38 bool send_add_edge(meshlink_handle_t *mesh, connection_t *c, const edge_t *e) {
42 sockaddr2str(&e->address, &address, &port);
44 x = send_request(mesh, c, "%d %x %s %d %s %s %s %d %x %d", ADD_EDGE, rand(),
45 e->from->name, e->from->devclass, e->to->name, address, port, e->to->devclass,
46 e->options, e->weight);
53 bool add_edge_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
56 char from_name[MAX_STRING_SIZE];
58 char to_name[MAX_STRING_SIZE];
59 char to_address[MAX_STRING_SIZE];
60 char to_port[MAX_STRING_SIZE];
66 if(sscanf(request, "%*d %*x "MAX_STRING" %d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %x %d",
67 from_name, &from_devclass, to_name, to_address, to_port, &to_devclass, &options, &weight) != 8) {
68 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s (%s)", "ADD_EDGE", c->name,
73 /* Check if names are valid */
75 if(!check_id(from_name) || !check_id(to_name)) {
76 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s (%s): %s", "ADD_EDGE", c->name,
77 c->hostname, "invalid name");
81 // Check if devclasses are valid
83 if(from_devclass < 0 || from_devclass > _DEV_CLASS_MAX) {
84 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s (%s): %s", "ADD_EDGE", c->name,
85 c->hostname, "from devclass invalid");
89 if(to_devclass < 0 || to_devclass > _DEV_CLASS_MAX) {
90 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s (%s): %s", "ADD_EDGE", c->name,
91 c->hostname, "to devclass invalid");
95 if(seen_request(mesh, request))
100 from = lookup_node(mesh, from_name);
101 to = lookup_node(mesh, to_name);
105 from->name = xstrdup(from_name);
106 node_add(mesh, from);
109 from->devclass = from_devclass;
110 node_write_devclass(mesh, from);
114 to->name = xstrdup(to_name);
118 to->devclass = to_devclass;
119 node_write_devclass(mesh, to);
121 /* Convert addresses */
123 address = str2sockaddr(to_address, to_port);
125 /* Check if edge already exists */
127 e = lookup_edge(from, to);
130 if(e->weight != weight || e->options != options || sockaddrcmp(&e->address, &address)) {
131 if(from == mesh->self) {
132 logger(mesh, MESHLINK_WARNING, "Got %s from %s (%s) for ourself which does not match existing entry",
133 "ADD_EDGE", c->name, c->hostname);
134 send_add_edge(mesh, c, e);
137 logger(mesh, MESHLINK_WARNING, "Got %s from %s (%s) which does not match existing entry",
138 "ADD_EDGE", c->name, c->hostname);
144 } else if(from == mesh->self) {
145 logger(mesh, MESHLINK_WARNING, "Got %s from %s (%s) for ourself which does not exist",
146 "ADD_EDGE", c->name, c->hostname);
147 mesh->contradicting_add_edge++;
151 send_del_edge(mesh, c, e);
159 e->address = address;
160 e->options = options;
164 /* Tell the rest about the new edge */
166 forward_request(mesh, c, request);
168 /* Run MST before or after we tell the rest? */
175 bool send_del_edge(meshlink_handle_t *mesh, connection_t *c, const edge_t *e) {
176 return send_request(mesh, c, "%d %x %s %s", DEL_EDGE, rand(),
177 e->from->name, e->to->name);
180 bool del_edge_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
182 char from_name[MAX_STRING_SIZE];
183 char to_name[MAX_STRING_SIZE];
186 if(sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING, from_name, to_name) != 2) {
187 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s (%s)", "DEL_EDGE", c->name,
192 /* Check if names are valid */
194 if(!check_id(from_name) || !check_id(to_name)) {
195 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s (%s): %s", "DEL_EDGE", c->name,
196 c->hostname, "invalid name");
200 if(seen_request(mesh, request))
205 from = lookup_node(mesh, from_name);
206 to = lookup_node(mesh, to_name);
209 logger(mesh, MESHLINK_ERROR, "Got %s from %s (%s) which does not appear in the edge tree",
210 "DEL_EDGE", c->name, c->hostname);
215 logger(mesh, MESHLINK_ERROR, "Got %s from %s (%s) which does not appear in the edge tree",
216 "DEL_EDGE", c->name, c->hostname);
220 /* Check if edge exists */
222 e = lookup_edge(from, to);
225 logger(mesh, MESHLINK_WARNING, "Got %s from %s (%s) which does not appear in the edge tree",
226 "DEL_EDGE", c->name, c->hostname);
230 if(e->from == mesh->self) {
231 logger(mesh, MESHLINK_WARNING, "Got %s from %s (%s) for ourself",
232 "DEL_EDGE", c->name, c->hostname);
233 mesh->contradicting_del_edge++;
234 send_add_edge(mesh, c, e); /* Send back a correction */
238 /* Tell the rest about the deleted edge */
240 forward_request(mesh, c, request);
242 /* Delete the edge */
246 /* Run MST before or after we tell the rest? */
250 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
252 if(!to->status.reachable) {
253 e = lookup_edge(to, mesh->self);
255 send_del_edge(mesh, mesh->everyone, e);