2 protocol_edge.c -- handle the meta-protocol, edges
3 Copyright (C) 1999-2002 Ivo Timmermans <itimmermans@bigfoot.com>,
4 2000-2002 Guus Sliepen <guus@sliepen.warande.net>
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.
20 $Id: protocol_edge.c,v 1.1.4.1 2002/02/11 10:05:58 guus Exp $
41 #include "connection.h"
48 int send_add_edge(connection_t *c, edge_t *e)
51 char *from_addrstr, *to_addrstr;
53 from_addrstr = address2str(e->from.address);
54 to_addrstr = address2str(e->to.address);
55 x = send_request(c, "%d %s %s %hd %s %s %hd %lx %d", ADD_EDGE,
56 e->from.node->name, from_addrstr, e->from.port,
57 e->to.node->name, to_addrstr, e->to.port,
58 e->options, e->weight);
65 int add_edge_h(connection_t *c)
70 char from_name[MAX_STRING_SIZE];
71 char to_name[MAX_STRING_SIZE];
72 char from_addrstr[MAX_STRING_SIZE];
73 char to_addrstr[MAX_STRING_SIZE];
74 ipv4_t from_address, to_address;
75 port_t from_port, to_port;
80 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" %hd "MAX_STRING" "MAX_STRING" %hd %lx %d",
81 from_name, from_addrstr, &from_port,
82 to_name, to_addrstr, &to_port,
83 &options, &weight) != 8)
85 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_EDGE", c->name, c->hostname);
89 /* Check if names are valid */
91 if(check_id(from_name))
93 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("invalid name"));
99 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("invalid name"));
105 from = lookup_node(from_name);
110 from->name = xstrdup(from_name);
114 to = lookup_node(to_name);
119 to->name = xstrdup(to_name);
123 /* Convert addresses */
125 from_address = str2address(from_addrstr);
126 to_address = str2address(to_addrstr);
128 /* Check if edge already exists */
130 e = lookup_edge(from, to);
134 if(e->weight != weight || e->options != options
135 || ((e->from.node == from) && (e->from.address != from_address || e->from.port != from_port || e->to.address != to_address || e->to.port != to_port))
136 || ((e->from.node == to) && (e->from.address != to_address || e->from.port != to_port || e->to.address != from_address || e->to.port != from_port))
139 if(from == myself || to == myself)
141 if(debug_lvl >= DEBUG_PROTOCOL)
142 syslog(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not match existing entry"), "ADD_EDGE", c->name, c->hostname);
148 if(debug_lvl >= DEBUG_PROTOCOL)
149 syslog(LOG_WARNING, _("Got %s from %s (%s) which does not match existing entry"), "ADD_EDGE", c->name, c->hostname);
156 else if(from == myself || to == myself)
158 if(debug_lvl >= DEBUG_PROTOCOL)
159 syslog(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not exist"), "ADD_EDGE", c->name, c->hostname);
172 e->from.address = from_address;
173 e->from.port = from_port;
175 e->to.address = to_address;
176 e->to.port = to_port;
177 e->options = options;
181 /* Tell the rest about the new edge */
183 for(node = connection_tree->head; node; node = node->next)
185 other = (connection_t *)node->data;
186 if(other->status.active && other != c)
187 send_add_edge(other, e);
190 /* Run MST before or after we tell the rest? */
197 int send_del_edge(connection_t *c, edge_t *e)
200 return send_request(c, "%d %s %s", DEL_EDGE,
201 e->from.node->name, e->to.node->name);
204 int del_edge_h(connection_t *c)
207 char from_name[MAX_STRING_SIZE];
208 char to_name[MAX_STRING_SIZE];
213 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING"", from_name, to_name) != 2)
215 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_EDGE",
216 c->name, c->hostname);
220 /* Check if names are valid */
222 if(check_id(from_name))
224 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("invalid name"));
228 if(check_id(to_name))
230 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("invalid name"));
236 from = lookup_node(from_name);
240 if(debug_lvl >= DEBUG_PROTOCOL)
241 syslog(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"), "DEL_EDGE", c->name, c->hostname);
245 to = lookup_node(to_name);
249 if(debug_lvl >= DEBUG_PROTOCOL)
250 syslog(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"), "DEL_EDGE", c->name, c->hostname);
254 /* Check if edge exists */
256 e = lookup_edge(from, to);
260 if(debug_lvl >= DEBUG_PROTOCOL)
261 syslog(LOG_WARNING, _("Got %s from %s (%s) which does not appear in the edge tree"), "DEL_EDGE", c->name, c->hostname);
265 if(e->from.node == myself || e->to.node == myself)
267 if(debug_lvl >= DEBUG_PROTOCOL)
268 syslog(LOG_WARNING, _("Got %s from %s (%s) for ourself"), "DEL_EDGE", c->name, c->hostname);
269 send_add_edge(c, e); /* Send back a correction */
273 /* Tell the rest about the deleted edge */
275 for(node = connection_tree->head; node; node = node->next)
277 other = (connection_t *)node->data;
278 if(other->status.active && other != c)
279 send_del_edge(other, e);
282 /* Delete the edge */
286 /* Run MST before or after we tell the rest? */