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) {
40 sockaddr2str(&e->address, &address, &port);
42 x = send_request(mesh, c, "%d %x %s %s %s %s %x %d", ADD_EDGE, rand(),
43 e->from->name, e->to->name, address, port,
44 e->options, e->weight);
51 bool add_edge_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
54 char from_name[MAX_STRING_SIZE];
55 char to_name[MAX_STRING_SIZE];
56 char to_address[MAX_STRING_SIZE];
57 char to_port[MAX_STRING_SIZE];
62 if(sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %x %d",
63 from_name, to_name, to_address, to_port, &options, &weight) != 6) {
64 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ADD_EDGE", c->name,
69 /* Check if names are valid */
71 if(!check_id(from_name) || !check_id(to_name)) {
72 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ADD_EDGE", c->name,
73 c->hostname, "invalid name");
77 if(seen_request(mesh, request))
82 from = lookup_node(mesh, from_name);
83 to = lookup_node(mesh, to_name);
87 from->name = xstrdup(from_name);
93 to->name = xstrdup(to_name);
98 /* Convert addresses */
100 address = str2sockaddr(to_address, to_port);
102 /* Check if edge already exists */
104 e = lookup_edge(from, to);
107 if(e->weight != weight || e->options != options || sockaddrcmp(&e->address, &address)) {
108 if(from == mesh->self) {
109 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself which does not match existing entry",
110 "ADD_EDGE", c->name, c->hostname);
111 send_add_edge(mesh, c, e);
114 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) which does not match existing entry",
115 "ADD_EDGE", c->name, c->hostname);
121 } else if(from == mesh->self) {
122 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself which does not exist",
123 "ADD_EDGE", c->name, c->hostname);
124 mesh->contradicting_add_edge++;
128 send_del_edge(mesh, c, e);
136 e->address = address;
137 e->options = options;
141 /* Tell the rest about the new edge */
143 forward_request(mesh, c, request);
145 /* Run MST before or after we tell the rest? */
152 bool send_del_edge(meshlink_handle_t *mesh, connection_t *c, const edge_t *e) {
153 return send_request(mesh, c, "%d %x %s %s", DEL_EDGE, rand(),
154 e->from->name, e->to->name);
157 bool del_edge_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
159 char from_name[MAX_STRING_SIZE];
160 char to_name[MAX_STRING_SIZE];
163 if(sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING, from_name, to_name) != 2) {
164 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "DEL_EDGE", c->name,
169 /* Check if names are valid */
171 if(!check_id(from_name) || !check_id(to_name)) {
172 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "DEL_EDGE", c->name,
173 c->hostname, "invalid name");
177 if(seen_request(mesh, request))
182 from = lookup_node(mesh, from_name);
183 to = lookup_node(mesh, to_name);
186 logger(DEBUG_PROTOCOL, LOG_ERR, "Got %s from %s (%s) which does not appear in the edge tree",
187 "DEL_EDGE", c->name, c->hostname);
192 logger(DEBUG_PROTOCOL, LOG_ERR, "Got %s from %s (%s) which does not appear in the edge tree",
193 "DEL_EDGE", c->name, c->hostname);
197 /* Check if edge exists */
199 e = lookup_edge(from, to);
202 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) which does not appear in the edge tree",
203 "DEL_EDGE", c->name, c->hostname);
207 if(e->from == mesh->self) {
208 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself",
209 "DEL_EDGE", c->name, c->hostname);
210 mesh->contradicting_del_edge++;
211 send_add_edge(mesh, c, e); /* Send back a correction */
215 /* Tell the rest about the deleted edge */
217 forward_request(mesh, c, request);
219 /* Delete the edge */
223 /* Run MST before or after we tell the rest? */
227 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
229 if(!to->status.reachable) {
230 e = lookup_edge(to, mesh->self);
232 send_del_edge(mesh, mesh->everyone, e);