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.
26 #include "connection.h"
38 bool send_add_edge(connection_t *c, const edge_t *e) {
44 sockaddr2str(&e->address, &address, &port);
46 x = send_request(c, "%d %x %s %s %s %s %lx %d", ADD_EDGE, rand(),
47 e->from->name, e->to->name, address, port,
48 e->options, e->weight);
55 bool add_edge_h(connection_t *c) {
58 char from_name[MAX_STRING_SIZE];
59 char to_name[MAX_STRING_SIZE];
60 char to_address[MAX_STRING_SIZE];
61 char to_port[MAX_STRING_SIZE];
68 if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %lx %d",
69 from_name, to_name, to_address, to_port, &options, &weight) != 6) {
70 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_EDGE", c->name,
75 /* Check if names are valid */
77 if(!check_id(from_name)) {
78 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name,
79 c->hostname, _("invalid name"));
83 if(!check_id(to_name)) {
84 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name,
85 c->hostname, _("invalid name"));
89 if(seen_request(c->buffer))
94 from = lookup_node(from_name);
95 to = lookup_node(to_name);
98 from != myself && from != c->node &&
99 to != myself && to != c->node) {
100 /* ignore indirect edge registrations for tunnelserver */
101 ifdebug(PROTOCOL) logger(LOG_WARNING,
102 _("Ignoring indirect %s from %s (%s)"),
103 "ADD_EDGE", c->name, c->hostname);
109 from->name = xstrdup(from_name);
115 to->name = xstrdup(to_name);
120 /* Convert addresses */
122 address = str2sockaddr(to_address, to_port);
124 /* Check if edge already exists */
126 e = lookup_edge(from, to);
129 if(e->weight != weight || e->options != options || sockaddrcmp(&e->address, &address)) {
131 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not match existing entry"),
132 "ADD_EDGE", c->name, c->hostname);
136 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) which does not match existing entry"),
137 "ADD_EDGE", c->name, c->hostname);
143 } else if(from == myself) {
144 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not exist"),
145 "ADD_EDGE", c->name, c->hostname);
157 e->address = address;
158 e->options = options;
162 /* Tell the rest about the new edge */
167 /* Run MST before or after we tell the rest? */
174 bool send_del_edge(connection_t *c, const edge_t *e) {
177 return send_request(c, "%d %x %s %s", DEL_EDGE, rand(),
178 e->from->name, e->to->name);
181 bool del_edge_h(connection_t *c) {
183 char from_name[MAX_STRING_SIZE];
184 char to_name[MAX_STRING_SIZE];
189 if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING, from_name, to_name) != 2) {
190 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_EDGE", c->name,
195 /* Check if names are valid */
197 if(!check_id(from_name)) {
198 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name,
199 c->hostname, _("invalid name"));
203 if(!check_id(to_name)) {
204 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name,
205 c->hostname, _("invalid name"));
209 if(seen_request(c->buffer))
214 from = lookup_node(from_name);
215 to = lookup_node(to_name);
218 from != myself && from != c->node &&
219 to != myself && to != c->node) {
220 /* ignore indirect edge registrations for tunnelserver */
221 ifdebug(PROTOCOL) logger(LOG_WARNING,
222 _("Ignoring indirect %s from %s (%s)"),
223 "DEL_EDGE", c->name, c->hostname);
228 ifdebug(PROTOCOL) logger(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"),
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);
239 /* Check if edge exists */
241 e = lookup_edge(from, to);
244 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) which does not appear in the edge tree"),
245 "DEL_EDGE", c->name, c->hostname);
249 if(e->from == myself) {
250 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself"),
251 "DEL_EDGE", c->name, c->hostname);
252 send_add_edge(c, e); /* Send back a correction */
256 /* Tell the rest about the deleted edge */
261 /* Delete the edge */
265 /* Run MST before or after we tell the rest? */
269 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
271 if(!to->status.reachable) {
272 e = lookup_edge(to, myself);
275 send_del_edge(broadcast, e);