2 protocol_edge.c -- handle the meta-protocol, edges
3 Copyright (C) 1999-2005 Ivo Timmermans,
4 2000-2012 Guus Sliepen <guus@tinc-vpn.org>
5 2009 Michael Tokarev <mjt@corpit.ru>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "connection.h"
37 bool send_add_edge(connection_t *c, const edge_t *e) {
40 char *local_address, *local_port;
42 sockaddr2str(&e->address, &address, &port);
43 sockaddr2str(&e->local_address, &local_address, &local_port);
45 x = send_request(c, "%d %x %s %s %s %s %x %d %s %s", ADD_EDGE, rand(),
46 e->from->name, e->to->name, address, port,
47 e->options, e->weight, local_address, local_port);
57 bool add_edge_h(connection_t *c, const char *request) {
60 char from_name[MAX_STRING_SIZE];
61 char to_name[MAX_STRING_SIZE];
62 char to_address[MAX_STRING_SIZE];
63 char to_port[MAX_STRING_SIZE];
64 char address_local[MAX_STRING_SIZE];
65 char port_local[MAX_STRING_SIZE];
66 sockaddr_t address, local_address = {{0}};
70 int parameter_count = sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %x %d "MAX_STRING" "MAX_STRING,
71 from_name, to_name, to_address, to_port, &options, &weight, address_local, port_local);
72 if (parameter_count != 6 && parameter_count != 8) {
73 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ADD_EDGE", c->name,
78 /* Check if names are valid */
80 if(!check_id(from_name) || !check_id(to_name)) {
81 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ADD_EDGE", c->name,
82 c->hostname, "invalid name");
86 if(seen_request(request))
91 from = lookup_node(from_name);
92 to = lookup_node(to_name);
95 from != myself && from != c->node &&
96 to != myself && to != c->node) {
97 /* ignore indirect edge registrations for tunnelserver */
98 logger(DEBUG_PROTOCOL, LOG_WARNING,
99 "Ignoring indirect %s from %s (%s)",
100 "ADD_EDGE", c->name, c->hostname);
106 from->name = xstrdup(from_name);
112 to->name = xstrdup(to_name);
117 /* Convert addresses */
119 address = str2sockaddr(to_address, to_port);
120 if(parameter_count >= 8)
121 local_address = str2sockaddr(address_local, port_local);
123 /* Check if edge already exists */
125 e = lookup_edge(from, to);
128 if(e->weight != weight || e->options != options || sockaddrcmp(&e->address, &address) || sockaddrcmp(&e->local_address, &local_address)) {
130 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself which does not match existing entry",
131 "ADD_EDGE", c->name, c->hostname);
135 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) which does not match existing entry",
136 "ADD_EDGE", c->name, c->hostname);
142 } else if(from == myself) {
143 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself which does not exist",
144 "ADD_EDGE", c->name, c->hostname);
145 contradicting_add_edge++;
157 e->address = address;
158 e->local_address = local_address;
159 e->options = options;
163 /* Tell the rest about the new edge */
166 forward_request(c, request);
168 /* Run MST before or after we tell the rest? */
175 bool send_del_edge(connection_t *c, const edge_t *e) {
176 return send_request(c, "%d %x %s %s", DEL_EDGE, rand(),
177 e->from->name, e->to->name);
180 bool del_edge_h(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(DEBUG_ALWAYS, LOG_ERR, "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(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "DEL_EDGE", c->name,
196 c->hostname, "invalid name");
200 if(seen_request(request))
205 from = lookup_node(from_name);
206 to = lookup_node(to_name);
209 from != myself && from != c->node &&
210 to != myself && to != c->node) {
211 /* ignore indirect edge registrations for tunnelserver */
212 logger(DEBUG_PROTOCOL, LOG_WARNING,
213 "Ignoring indirect %s from %s (%s)",
214 "DEL_EDGE", c->name, c->hostname);
219 logger(DEBUG_PROTOCOL, LOG_ERR, "Got %s from %s (%s) which does not appear in the edge tree",
220 "DEL_EDGE", c->name, c->hostname);
225 logger(DEBUG_PROTOCOL, LOG_ERR, "Got %s from %s (%s) which does not appear in the edge tree",
226 "DEL_EDGE", c->name, c->hostname);
230 /* Check if edge exists */
232 e = lookup_edge(from, to);
235 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) which does not appear in the edge tree",
236 "DEL_EDGE", c->name, c->hostname);
240 if(e->from == myself) {
241 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself",
242 "DEL_EDGE", c->name, c->hostname);
243 contradicting_del_edge++;
244 send_add_edge(c, e); /* Send back a correction */
248 /* Tell the rest about the deleted edge */
251 forward_request(c, request);
253 /* Delete the edge */
257 /* Run MST before or after we tell the rest? */
261 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
263 if(!to->status.reachable) {
264 e = lookup_edge(to, myself);
267 send_del_edge(everyone, e);