2 protocol_edge.c -- handle the meta-protocol, edges
3 Copyright (C) 1999-2005 Ivo Timmermans <ivo@tinc-vpn.org>,
4 2000-2005 Guus Sliepen <guus@tinc-vpn.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include "connection.h"
39 bool send_add_edge(connection_t *c, const edge_t *e)
46 sockaddr2str(&e->address, &address, &port);
48 x = send_request(c, "%d %lx %s %s %s %s %lx %d", ADD_EDGE, random(),
49 e->from->name, e->to->name, address, port,
50 e->options, e->weight);
57 bool add_edge_h(connection_t *c)
61 char from_name[MAX_STRING_SIZE];
62 char to_name[MAX_STRING_SIZE];
63 char to_address[MAX_STRING_SIZE];
64 char to_port[MAX_STRING_SIZE];
71 if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %lx %d",
72 from_name, to_name, to_address, to_port, &options, &weight) != 6) {
73 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_EDGE", c->name,
78 /* Check if names are valid */
80 if(!check_id(from_name)) {
81 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name,
82 c->hostname, _("invalid name"));
86 if(!check_id(to_name)) {
87 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name,
88 c->hostname, _("invalid name"));
92 if(seen_request(c->buffer))
97 from = lookup_node(from_name);
101 from->name = xstrdup(from_name);
105 to = lookup_node(to_name);
109 to->name = xstrdup(to_name);
113 if(tunnelserver && from != myself && from != c->node && to != myself && to != c->node)
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 */
163 /* Run MST before or after we tell the rest? */
170 bool send_del_edge(connection_t *c, const edge_t *e)
174 return send_request(c, "%d %lx %s %s", DEL_EDGE, random(),
175 e->from->name, e->to->name);
178 bool del_edge_h(connection_t *c)
181 char from_name[MAX_STRING_SIZE];
182 char to_name[MAX_STRING_SIZE];
187 if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING, from_name, to_name) != 2) {
188 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_EDGE", c->name,
193 /* Check if names are valid */
195 if(!check_id(from_name)) {
196 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name,
197 c->hostname, _("invalid name"));
201 if(!check_id(to_name)) {
202 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name,
203 c->hostname, _("invalid name"));
207 if(seen_request(c->buffer))
212 from = lookup_node(from_name);
215 ifdebug(PROTOCOL) logger(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"),
216 "DEL_EDGE", c->name, c->hostname);
220 to = lookup_node(to_name);
223 ifdebug(PROTOCOL) logger(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"),
224 "DEL_EDGE", c->name, c->hostname);
228 if(tunnelserver && from != myself && from != c->node && to != myself && to != c->node)
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 */
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);