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 %d %s %s %s %d %x %d", ADD_EDGE, rand(),
43 e->from->name, e->from->dclass, e->to->name, address, port, e->to->dclass,
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];
56 char to_name[MAX_STRING_SIZE];
57 char to_address[MAX_STRING_SIZE];
58 char to_port[MAX_STRING_SIZE];
64 if(sscanf(request, "%*d %*x "MAX_STRING" %d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %x %d",
65 from_name, &from_dclass, to_name, to_address, to_port, &to_dclass, &options, &weight) != 8) {
66 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s (%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): %s", "ADD_EDGE", c->name,
75 c->hostname, "invalid name");
79 if(seen_request(mesh, request))
84 from = lookup_node(mesh, from_name);
85 to = lookup_node(mesh, to_name);
89 from->name = xstrdup(from_name);
93 from->dclass = from_dclass;
97 to->name = xstrdup(to_name);
101 to->dclass = to_dclass;
103 /* Convert addresses */
105 address = str2sockaddr(to_address, to_port);
107 /* Check if edge already exists */
109 e = lookup_edge(from, to);
112 if(e->weight != weight || e->options != options || sockaddrcmp(&e->address, &address)) {
113 if(from == mesh->self) {
114 logger(mesh, MESHLINK_WARNING, "Got %s from %s (%s) for ourself which does not match existing entry",
115 "ADD_EDGE", c->name, c->hostname);
116 send_add_edge(mesh, c, e);
119 logger(mesh, MESHLINK_WARNING, "Got %s from %s (%s) which does not match existing entry",
120 "ADD_EDGE", c->name, c->hostname);
126 } else if(from == mesh->self) {
127 logger(mesh, MESHLINK_WARNING, "Got %s from %s (%s) for ourself which does not exist",
128 "ADD_EDGE", c->name, c->hostname);
129 mesh->contradicting_add_edge++;
133 send_del_edge(mesh, c, e);
141 e->address = address;
142 e->options = options;
146 /* Tell the rest about the new edge */
148 forward_request(mesh, c, request);
150 /* Run MST before or after we tell the rest? */
157 bool send_del_edge(meshlink_handle_t *mesh, connection_t *c, const edge_t *e) {
158 return send_request(mesh, c, "%d %x %s %s", DEL_EDGE, rand(),
159 e->from->name, e->to->name);
162 bool del_edge_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
164 char from_name[MAX_STRING_SIZE];
165 char to_name[MAX_STRING_SIZE];
168 if(sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING, from_name, to_name) != 2) {
169 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s (%s)", "DEL_EDGE", c->name,
174 /* Check if names are valid */
176 if(!check_id(from_name) || !check_id(to_name)) {
177 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s (%s): %s", "DEL_EDGE", c->name,
178 c->hostname, "invalid name");
182 if(seen_request(mesh, request))
187 from = lookup_node(mesh, from_name);
188 to = lookup_node(mesh, to_name);
191 logger(mesh, MESHLINK_ERROR, "Got %s from %s (%s) which does not appear in the edge tree",
192 "DEL_EDGE", c->name, c->hostname);
197 logger(mesh, MESHLINK_ERROR, "Got %s from %s (%s) which does not appear in the edge tree",
198 "DEL_EDGE", c->name, c->hostname);
202 /* Check if edge exists */
204 e = lookup_edge(from, to);
207 logger(mesh, MESHLINK_WARNING, "Got %s from %s (%s) which does not appear in the edge tree",
208 "DEL_EDGE", c->name, c->hostname);
212 if(e->from == mesh->self) {
213 logger(mesh, MESHLINK_WARNING, "Got %s from %s (%s) for ourself",
214 "DEL_EDGE", c->name, c->hostname);
215 mesh->contradicting_del_edge++;
216 send_add_edge(mesh, c, e); /* Send back a correction */
220 /* Tell the rest about the deleted edge */
222 forward_request(mesh, c, request);
224 /* Delete the edge */
228 /* Run MST before or after we tell the rest? */
232 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
234 if(!to->status.reachable) {
235 e = lookup_edge(to, mesh->self);
237 send_del_edge(mesh, mesh->everyone, e);