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
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "connection.h"
38 bool send_add_edge(connection_t *c, const edge_t *e)
45 sockaddr2str(&e->address, &address, &port);
47 x = send_request(c, "%d %x %s %s %s %s %lx %d", ADD_EDGE, rand(),
48 e->from->name, e->to->name, address, port,
49 e->options, e->weight);
56 bool add_edge_h(connection_t *c)
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];
70 if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %lx %d",
71 from_name, to_name, to_address, to_port, &options, &weight) != 6) {
72 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_EDGE", c->name,
77 /* Check if names are valid */
79 if(!check_id(from_name)) {
80 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name,
81 c->hostname, _("invalid name"));
85 if(!check_id(to_name)) {
86 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name,
87 c->hostname, _("invalid name"));
91 if(seen_request(c->buffer))
96 from = lookup_node(from_name);
97 to = lookup_node(to_name);
100 from != myself && from != c->node &&
101 to != myself && to != c->node) {
102 /* ignore indirect edge registrations for tunnelserver */
103 ifdebug(PROTOCOL) logger(LOG_WARNING,
104 _("Ignoring indirect %s from %s (%s)"),
105 "ADD_EDGE", c->name, c->hostname);
111 from->name = xstrdup(from_name);
117 to->name = xstrdup(to_name);
122 /* Convert addresses */
124 address = str2sockaddr(to_address, to_port);
126 /* Check if edge already exists */
128 e = lookup_edge(from, to);
131 if(e->weight != weight || e->options != options || sockaddrcmp(&e->address, &address)) {
133 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not match existing entry"),
134 "ADD_EDGE", c->name, c->hostname);
138 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) which does not match existing entry"),
139 "ADD_EDGE", c->name, c->hostname);
145 } else if(from == myself) {
146 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not exist"),
147 "ADD_EDGE", c->name, c->hostname);
159 e->address = address;
160 e->options = options;
164 /* Tell the rest about the new edge */
169 /* Run MST before or after we tell the rest? */
176 bool send_del_edge(connection_t *c, const edge_t *e)
180 return send_request(c, "%d %x %s %s", DEL_EDGE, rand(),
181 e->from->name, e->to->name);
184 bool del_edge_h(connection_t *c)
187 char from_name[MAX_STRING_SIZE];
188 char to_name[MAX_STRING_SIZE];
193 if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING, from_name, to_name) != 2) {
194 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_EDGE", c->name,
199 /* Check if names are valid */
201 if(!check_id(from_name)) {
202 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name,
203 c->hostname, _("invalid name"));
207 if(!check_id(to_name)) {
208 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name,
209 c->hostname, _("invalid name"));
213 if(seen_request(c->buffer))
218 from = lookup_node(from_name);
219 to = lookup_node(to_name);
222 from != myself && from != c->node &&
223 to != myself && to != c->node) {
224 /* ignore indirect edge registrations for tunnelserver */
225 ifdebug(PROTOCOL) logger(LOG_WARNING,
226 _("Ignoring indirect %s from %s (%s)"),
227 "DEL_EDGE", c->name, c->hostname);
232 ifdebug(PROTOCOL) logger(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"),
233 "DEL_EDGE", c->name, c->hostname);
238 ifdebug(PROTOCOL) logger(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"),
239 "DEL_EDGE", c->name, c->hostname);
243 /* Check if edge exists */
245 e = lookup_edge(from, to);
248 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) which does not appear in the edge tree"),
249 "DEL_EDGE", c->name, c->hostname);
253 if(e->from == myself) {
254 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself"),
255 "DEL_EDGE", c->name, c->hostname);
256 send_add_edge(c, e); /* Send back a correction */
260 /* Tell the rest about the deleted edge */
265 /* Delete the edge */
269 /* Run MST before or after we tell the rest? */
273 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
275 if(!to->status.reachable) {
276 e = lookup_edge(to, myself);
279 send_del_edge(broadcast, e);