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.
28 #include "connection.h"
40 bool send_add_edge(connection_t *c, const edge_t *e)
47 sockaddr2str(&e->address, &address, &port);
49 x = send_request(c, "%d %x %s %s %s %s %lx %d", ADD_EDGE, rand(),
50 e->from->name, e->to->name, address, port,
51 e->options, e->weight);
58 bool add_edge_h(connection_t *c)
62 char from_name[MAX_STRING_SIZE];
63 char to_name[MAX_STRING_SIZE];
64 char to_address[MAX_STRING_SIZE];
65 char to_port[MAX_STRING_SIZE];
72 if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %lx %d",
73 from_name, to_name, to_address, to_port, &options, &weight) != 6) {
74 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_EDGE", c->name,
79 /* Check if names are valid */
81 if(!check_id(from_name)) {
82 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name,
83 c->hostname, _("invalid name"));
87 if(!check_id(to_name)) {
88 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name,
89 c->hostname, _("invalid name"));
93 if(seen_request(c->buffer))
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 ifdebug(PROTOCOL) logger(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);
128 /* Check if edge already exists */
130 e = lookup_edge(from, to);
133 if(e->weight != weight || e->options != options || sockaddrcmp(&e->address, &address)) {
135 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not match existing entry"),
136 "ADD_EDGE", c->name, c->hostname);
140 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) which does not match existing entry"),
141 "ADD_EDGE", c->name, c->hostname);
147 } else if(from == myself) {
148 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not exist"),
149 "ADD_EDGE", c->name, c->hostname);
161 e->address = address;
162 e->options = options;
166 /* Tell the rest about the new edge */
171 /* Run MST before or after we tell the rest? */
178 bool send_del_edge(connection_t *c, const edge_t *e)
182 return send_request(c, "%d %x %s %s", DEL_EDGE, rand(),
183 e->from->name, e->to->name);
186 bool del_edge_h(connection_t *c)
189 char from_name[MAX_STRING_SIZE];
190 char to_name[MAX_STRING_SIZE];
195 if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING, from_name, to_name) != 2) {
196 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_EDGE", c->name,
201 /* Check if names are valid */
203 if(!check_id(from_name)) {
204 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name,
205 c->hostname, _("invalid name"));
209 if(!check_id(to_name)) {
210 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name,
211 c->hostname, _("invalid name"));
215 if(seen_request(c->buffer))
220 from = lookup_node(from_name);
221 to = lookup_node(to_name);
224 from != myself && from != c->node &&
225 to != myself && to != c->node) {
226 /* ignore indirect edge registrations for tunnelserver */
227 ifdebug(PROTOCOL) logger(LOG_WARNING,
228 _("Ignoring indirect %s from %s (%s)"),
229 "DEL_EDGE", c->name, c->hostname);
234 ifdebug(PROTOCOL) logger(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"),
235 "DEL_EDGE", c->name, c->hostname);
240 ifdebug(PROTOCOL) logger(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"),
241 "DEL_EDGE", c->name, c->hostname);
245 /* Check if edge exists */
247 e = lookup_edge(from, to);
250 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) which does not appear in the edge tree"),
251 "DEL_EDGE", c->name, c->hostname);
255 if(e->from == myself) {
256 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself"),
257 "DEL_EDGE", c->name, c->hostname);
258 send_add_edge(c, e); /* Send back a correction */
262 /* Tell the rest about the deleted edge */
267 /* Delete the edge */
271 /* Run MST before or after we tell the rest? */
275 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
277 if(!to->status.reachable) {
278 e = lookup_edge(to, myself);
281 send_del_edge(broadcast, e);