2 protocol_edge.c -- handle the meta-protocol, edges
3 Copyright (C) 1999-2005 Ivo Timmermans,
4 2000-2009 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.
24 #include "splay_tree.h"
26 #include "connection.h"
38 bool send_add_edge(connection_t *c, const edge_t *e) {
42 sockaddr2str(&e->address, &address, &port);
44 x = send_request(c, "%d %x %s %s %s %s %lx %d", ADD_EDGE, rand(),
45 e->from->name, e->to->name, address, port,
46 e->options, e->weight);
53 bool add_edge_h(connection_t *c, char *request) {
56 char from_name[MAX_STRING_SIZE];
57 char to_name[MAX_STRING_SIZE];
58 char to_address[MAX_STRING_SIZE];
59 char to_port[MAX_STRING_SIZE];
64 if(sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %lx %d",
65 from_name, to_name, to_address, to_port, &options, &weight) != 6) {
66 logger(LOG_ERR, "Got bad %s from %s (%s)", "ADD_EDGE", c->name,
71 /* Check if names are valid */
73 if(!check_id(from_name)) {
74 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "ADD_EDGE", c->name,
75 c->hostname, "invalid name");
79 if(!check_id(to_name)) {
80 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "ADD_EDGE", c->name,
81 c->hostname, "invalid name");
85 if(seen_request(request))
90 from = lookup_node(from_name);
91 to = lookup_node(to_name);
94 from != myself && from != c->node &&
95 to != myself && to != c->node) {
96 /* ignore indirect edge registrations for tunnelserver */
97 ifdebug(PROTOCOL) logger(LOG_WARNING,
98 "Ignoring indirect %s from %s (%s)",
99 "ADD_EDGE", c->name, c->hostname);
105 from->name = xstrdup(from_name);
111 to->name = xstrdup(to_name);
116 /* Convert addresses */
118 address = str2sockaddr(to_address, to_port);
120 /* Check if edge already exists */
122 e = lookup_edge(from, to);
125 if(e->weight != weight || e->options != options || sockaddrcmp(&e->address, &address)) {
127 ifdebug(PROTOCOL) logger(LOG_WARNING, "Got %s from %s (%s) for ourself which does not match existing entry",
128 "ADD_EDGE", c->name, c->hostname);
132 ifdebug(PROTOCOL) logger(LOG_WARNING, "Got %s from %s (%s) which does not match existing entry",
133 "ADD_EDGE", c->name, c->hostname);
139 } else if(from == myself) {
140 ifdebug(PROTOCOL) logger(LOG_WARNING, "Got %s from %s (%s) for ourself which does not exist",
141 "ADD_EDGE", c->name, c->hostname);
153 e->address = address;
154 e->options = options;
158 /* Tell the rest about the new edge */
161 forward_request(c, request);
163 /* Run MST before or after we tell the rest? */
170 bool send_del_edge(connection_t *c, const edge_t *e) {
171 return send_request(c, "%d %x %s %s", DEL_EDGE, rand(),
172 e->from->name, e->to->name);
175 bool del_edge_h(connection_t *c, char *request) {
177 char from_name[MAX_STRING_SIZE];
178 char to_name[MAX_STRING_SIZE];
181 if(sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING, from_name, to_name) != 2) {
182 logger(LOG_ERR, "Got bad %s from %s (%s)", "DEL_EDGE", c->name,
187 /* Check if names are valid */
189 if(!check_id(from_name)) {
190 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "DEL_EDGE", c->name,
191 c->hostname, "invalid name");
195 if(!check_id(to_name)) {
196 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "DEL_EDGE", c->name,
197 c->hostname, "invalid name");
201 if(seen_request(request))
206 from = lookup_node(from_name);
207 to = lookup_node(to_name);
210 from != myself && from != c->node &&
211 to != myself && to != c->node) {
212 /* ignore indirect edge registrations for tunnelserver */
213 ifdebug(PROTOCOL) logger(LOG_WARNING,
214 "Ignoring indirect %s from %s (%s)",
215 "DEL_EDGE", c->name, c->hostname);
220 ifdebug(PROTOCOL) logger(LOG_ERR, "Got %s from %s (%s) which does not appear in the edge tree",
221 "DEL_EDGE", c->name, c->hostname);
226 ifdebug(PROTOCOL) logger(LOG_ERR, "Got %s from %s (%s) which does not appear in the edge tree",
227 "DEL_EDGE", c->name, c->hostname);
231 /* Check if edge exists */
233 e = lookup_edge(from, to);
236 ifdebug(PROTOCOL) logger(LOG_WARNING, "Got %s from %s (%s) which does not appear in the edge tree",
237 "DEL_EDGE", c->name, c->hostname);
241 if(e->from == myself) {
242 ifdebug(PROTOCOL) logger(LOG_WARNING, "Got %s from %s (%s) for ourself",
243 "DEL_EDGE", c->name, c->hostname);
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(broadcast, e);