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) {
41 sockaddr2str(&e->address, &address, &port);
43 if(e->local_address.sa.sa_family) {
44 char *local_address, *local_port;
45 sockaddr2str(&e->local_address, &local_address, &local_port);
47 x = send_request(c, "%d %x %s %s %s %s %x %d %s %s", ADD_EDGE, rand(),
48 e->from->name, e->to->name, address, port,
49 e->options, e->weight, local_address, local_port);
53 x = send_request(c, "%d %x %s %s %s %s %x %d", ADD_EDGE, rand(),
54 e->from->name, e->to->name, address, port,
55 e->options, e->weight);
64 bool add_edge_h(connection_t *c, const char *request) {
67 char from_name[MAX_STRING_SIZE];
68 char to_name[MAX_STRING_SIZE];
69 char to_address[MAX_STRING_SIZE];
70 char to_port[MAX_STRING_SIZE];
71 char address_local[MAX_STRING_SIZE];
72 char port_local[MAX_STRING_SIZE];
73 sockaddr_t address, local_address = {{0}};
77 int parameter_count = sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %x %d "MAX_STRING" "MAX_STRING,
78 from_name, to_name, to_address, to_port, &options, &weight, address_local, port_local);
79 if (parameter_count != 6 && parameter_count != 8) {
80 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ADD_EDGE", c->name,
85 /* Check if names are valid */
87 if(!check_id(from_name) || !check_id(to_name)) {
88 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ADD_EDGE", c->name,
89 c->hostname, "invalid name");
93 if(seen_request(request))
98 from = lookup_node(from_name);
99 to = lookup_node(to_name);
102 from != myself && from != c->node &&
103 to != myself && to != c->node) {
104 /* ignore indirect edge registrations for tunnelserver */
105 logger(DEBUG_PROTOCOL, LOG_WARNING,
106 "Ignoring indirect %s from %s (%s)",
107 "ADD_EDGE", c->name, c->hostname);
113 from->name = xstrdup(from_name);
119 to->name = xstrdup(to_name);
124 /* Convert addresses */
126 address = str2sockaddr(to_address, to_port);
127 if(parameter_count >= 8)
128 local_address = str2sockaddr(address_local, port_local);
130 /* Check if edge already exists */
132 e = lookup_edge(from, to);
135 if(e->weight != weight || e->options != options || sockaddrcmp(&e->address, &address)) {
137 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself which does not match existing entry",
138 "ADD_EDGE", c->name, c->hostname);
140 sockaddrfree(&local_address);
143 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) which does not match existing entry",
144 "ADD_EDGE", c->name, c->hostname);
145 e->options = options;
146 if(sockaddrcmp(&e->address, &address)) {
147 sockaddrfree(&e->address);
148 e->address = address;
150 if(e->weight != weight) {
151 splay_node_t *node = splay_unlink(edge_weight_tree, e);
153 splay_insert_node(edge_weight_tree, node);
158 } else if(sockaddrcmp(&e->local_address, &local_address)) {
160 if(e->local_address.sa.sa_family && local_address.sa.sa_family) {
161 // Someone has the wrong local address for ourself. Correct then.
162 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself which does not match existing entry",
163 "ADD_EDGE", c->name, c->hostname);
165 sockaddrfree(&local_address);
168 // Otherwise, just ignore it.
169 sockaddrfree(&local_address);
171 } else if(local_address.sa.sa_family && local_address.sa.sa_family != AF_UNKNOWN) {
172 // We learned a new local address for this edge.
173 // local_address.sa.sa_family will be 0 if we got it from older tinc versions
174 // local_address.sa.sa_family will be 255 (AF_UNKNOWN) if we got it from newer versions
175 // but for edge which does not have local_address
176 sockaddrfree(&e->local_address);
177 e->local_address = local_address;
179 // Tell others about it.
181 forward_request(c, request);
185 sockaddrfree(&local_address);
189 sockaddrfree(&local_address);
192 } else if(from == myself) {
193 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself which does not exist",
194 "ADD_EDGE", c->name, c->hostname);
195 contradicting_add_edge++;
201 sockaddrfree(&local_address);
208 e->address = address;
209 e->local_address = local_address;
210 e->options = options;
215 /* Tell the rest about the new edge */
218 forward_request(c, request);
220 /* Run MST before or after we tell the rest? */
227 bool send_del_edge(connection_t *c, const edge_t *e) {
228 return send_request(c, "%d %x %s %s", DEL_EDGE, rand(),
229 e->from->name, e->to->name);
232 bool del_edge_h(connection_t *c, const char *request) {
234 char from_name[MAX_STRING_SIZE];
235 char to_name[MAX_STRING_SIZE];
238 if(sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING, from_name, to_name) != 2) {
239 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "DEL_EDGE", c->name,
244 /* Check if names are valid */
246 if(!check_id(from_name) || !check_id(to_name)) {
247 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "DEL_EDGE", c->name,
248 c->hostname, "invalid name");
252 if(seen_request(request))
257 from = lookup_node(from_name);
258 to = lookup_node(to_name);
261 from != myself && from != c->node &&
262 to != myself && to != c->node) {
263 /* ignore indirect edge registrations for tunnelserver */
264 logger(DEBUG_PROTOCOL, LOG_WARNING,
265 "Ignoring indirect %s from %s (%s)",
266 "DEL_EDGE", c->name, c->hostname);
271 logger(DEBUG_PROTOCOL, LOG_ERR, "Got %s from %s (%s) which does not appear in the edge tree",
272 "DEL_EDGE", c->name, c->hostname);
277 logger(DEBUG_PROTOCOL, LOG_ERR, "Got %s from %s (%s) which does not appear in the edge tree",
278 "DEL_EDGE", c->name, c->hostname);
282 /* Check if edge exists */
284 e = lookup_edge(from, to);
287 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) which does not appear in the edge tree",
288 "DEL_EDGE", c->name, c->hostname);
292 if(e->from == myself) {
293 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself",
294 "DEL_EDGE", c->name, c->hostname);
295 contradicting_del_edge++;
296 send_add_edge(c, e); /* Send back a correction */
300 /* Tell the rest about the deleted edge */
303 forward_request(c, request);
305 /* Delete the edge */
309 /* Run MST before or after we tell the rest? */
313 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
315 if(!to->status.reachable) {
316 e = lookup_edge(to, myself);
319 send_del_edge(everyone, e);