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"
36 bool send_add_edge(connection_t *c, const edge_t *e) {
40 sockaddr2str(&e->address, &address, &port);
42 if(e->local_address.sa.sa_family) {
43 char *local_address, *local_port;
44 sockaddr2str(&e->local_address, &local_address, &local_port);
46 x = send_request(c, "%d %x %s %s %s %s %x %d %s %s", ADD_EDGE, rand(),
47 e->from->name, e->to->name, address, port,
48 e->options, e->weight, local_address, local_port);
52 x = send_request(c, "%d %x %s %s %s %s %x %d", ADD_EDGE, rand(),
53 e->from->name, e->to->name, address, port,
54 e->options, e->weight);
63 bool add_edge_h(connection_t *c, const char *request) {
66 char from_name[MAX_STRING_SIZE];
67 char to_name[MAX_STRING_SIZE];
68 char to_address[MAX_STRING_SIZE];
69 char to_port[MAX_STRING_SIZE];
70 char address_local[MAX_STRING_SIZE];
71 char port_local[MAX_STRING_SIZE];
72 sockaddr_t address, local_address = {0};
76 int parameter_count = sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %x %d "MAX_STRING" "MAX_STRING,
77 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) || !strcmp(from_name, 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)) {
99 from = lookup_node(from_name);
100 to = lookup_node(to_name);
103 from != myself && from != c->node &&
104 to != myself && to != c->node) {
105 /* ignore indirect edge registrations for tunnelserver */
106 logger(DEBUG_PROTOCOL, LOG_WARNING,
107 "Ignoring indirect %s from %s (%s)",
108 "ADD_EDGE", c->name, c->hostname);
114 from->name = xstrdup(from_name);
120 to->name = xstrdup(to_name);
125 /* Convert addresses */
127 address = str2sockaddr(to_address, to_port);
129 if(parameter_count >= 8) {
130 local_address = str2sockaddr(address_local, port_local);
133 /* Check if edge already exists */
135 e = lookup_edge(from, to);
138 bool new_address = sockaddrcmp(&e->address, &address);
139 // local_address.sa.sa_family will be 0 if we got it from older tinc versions
140 // local_address.sa.sa_family will be 255 (AF_UNKNOWN) if we got it from newer versions
141 // but for edge which does not have local_address
142 bool new_local_address = local_address.sa.sa_family && local_address.sa.sa_family != AF_UNKNOWN &&
143 sockaddrcmp(&e->local_address, &local_address);
145 if(e->weight == weight && e->options == options && !new_address && !new_local_address) {
146 sockaddrfree(&address);
147 sockaddrfree(&local_address);
152 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself which does not match existing entry",
153 "ADD_EDGE", c->name, c->hostname);
155 sockaddrfree(&address);
156 sockaddrfree(&local_address);
160 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) which does not match existing entry",
161 "ADD_EDGE", c->name, c->hostname);
163 e->options = options;
166 sockaddrfree(&e->address);
167 e->address = address;
169 sockaddrfree(&address);
172 if(new_local_address) {
173 sockaddrfree(&e->local_address);
174 e->local_address = local_address;
176 sockaddrfree(&local_address);
179 if(e->weight != weight) {
180 splay_node_t *node = splay_unlink(&edge_weight_tree, e);
182 splay_insert_node(&edge_weight_tree, node);
184 } else if(from == myself) {
185 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself which does not exist",
186 "ADD_EDGE", c->name, c->hostname);
187 contradicting_add_edge++;
193 sockaddrfree(&address);
194 sockaddrfree(&local_address);
200 e->address = address;
201 e->local_address = local_address;
202 e->options = options;
207 /* Tell the rest about the new edge */
210 forward_request(c, request);
213 /* Run MST before or after we tell the rest? */
220 bool send_del_edge(connection_t *c, const edge_t *e) {
221 return send_request(c, "%d %x %s %s", DEL_EDGE, rand(),
222 e->from->name, e->to->name);
225 bool del_edge_h(connection_t *c, const char *request) {
227 char from_name[MAX_STRING_SIZE];
228 char to_name[MAX_STRING_SIZE];
231 if(sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING, from_name, to_name) != 2) {
232 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "DEL_EDGE", c->name,
237 /* Check if names are valid */
239 if(!check_id(from_name) || !check_id(to_name) || !strcmp(from_name, to_name)) {
240 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "DEL_EDGE", c->name,
241 c->hostname, "invalid name");
245 if(seen_request(request)) {
251 from = lookup_node(from_name);
252 to = lookup_node(to_name);
255 from != myself && from != c->node &&
256 to != myself && to != c->node) {
257 /* ignore indirect edge registrations for tunnelserver */
258 logger(DEBUG_PROTOCOL, LOG_WARNING,
259 "Ignoring indirect %s from %s (%s)",
260 "DEL_EDGE", c->name, c->hostname);
265 logger(DEBUG_PROTOCOL, LOG_ERR, "Got %s from %s (%s) which does not appear in the edge tree",
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);
276 /* Check if edge exists */
278 e = lookup_edge(from, to);
281 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) which does not appear in the edge tree",
282 "DEL_EDGE", c->name, c->hostname);
286 if(e->from == myself) {
287 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself",
288 "DEL_EDGE", c->name, c->hostname);
289 contradicting_del_edge++;
290 send_add_edge(c, e); /* Send back a correction */
294 /* Tell the rest about the deleted edge */
297 forward_request(c, request);
300 /* Delete the edge */
304 /* Run MST before or after we tell the rest? */
308 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
310 if(!to->status.reachable) {
311 e = lookup_edge(to, myself);
315 send_del_edge(everyone, e);