2 protocol_edge.c -- handle the meta-protocol, edges
3 Copyright (C) 1999-2002 Ivo Timmermans <ivo@o2w.nl>,
4 2000-2002 Guus Sliepen <guus@sliepen.eu.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.
20 $Id: protocol_edge.c,v 1.1.4.9 2002/09/04 13:48:52 guus Exp $
41 #include "connection.h"
48 int send_add_edge(connection_t *c, edge_t *e)
53 sockaddr2str(&e->address, &address, &port);
54 x = send_request(c, "%d %lx %s %s %s %s %lx %d", ADD_EDGE, random(),
55 e->from->name, e->to->name, address, port,
56 e->options, e->weight);
63 int add_edge_h(connection_t *c)
68 char from_name[MAX_STRING_SIZE];
69 char to_name[MAX_STRING_SIZE];
70 char to_address[MAX_STRING_SIZE];
71 char to_port[MAX_STRING_SIZE];
77 if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %lx %d",
78 from_name, to_name, to_address, to_port, &options, &weight) != 6)
80 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_EDGE", c->name, c->hostname);
84 /* Check if names are valid */
86 if(check_id(from_name))
88 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("invalid name"));
94 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("invalid name"));
98 if(seen_request(c->buffer))
103 from = lookup_node(from_name);
108 from->name = xstrdup(from_name);
112 to = lookup_node(to_name);
117 to->name = xstrdup(to_name);
121 /* Convert addresses */
123 address = str2sockaddr(to_address, to_port);
125 /* Check if edge already exists */
127 e = lookup_edge(from, to);
131 if(e->weight != weight || e->options != options || sockaddrcmp(&e->address, &address))
135 if(debug_lvl >= DEBUG_PROTOCOL)
136 syslog(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not match existing entry"), "ADD_EDGE", c->name, c->hostname);
142 if(debug_lvl >= DEBUG_PROTOCOL)
143 syslog(LOG_WARNING, _("Got %s from %s (%s) which does not match existing entry"), "ADD_EDGE", c->name, c->hostname);
150 else if(from == myself)
152 if(debug_lvl >= DEBUG_PROTOCOL)
153 syslog(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not exist"), "ADD_EDGE", c->name, c->hostname);
165 e->address = address;
166 e->options = options;
170 /* Tell the rest about the new edge */
172 for(node = connection_tree->head; node; node = node->next)
174 other = (connection_t *)node->data;
175 if(other->status.active && other != c)
176 send_request(other, "%s", c->buffer);
179 /* Run MST before or after we tell the rest? */
186 int send_del_edge(connection_t *c, edge_t *e)
189 return send_request(c, "%d %lx %s %s", DEL_EDGE, random(),
190 e->from->name, e->to->name);
193 int del_edge_h(connection_t *c)
196 char from_name[MAX_STRING_SIZE];
197 char to_name[MAX_STRING_SIZE];
202 if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING"", from_name, to_name) != 2)
204 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_EDGE",
205 c->name, c->hostname);
209 /* Check if names are valid */
211 if(check_id(from_name))
213 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("invalid name"));
217 if(check_id(to_name))
219 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("invalid name"));
223 if(seen_request(c->buffer))
228 from = lookup_node(from_name);
232 if(debug_lvl >= DEBUG_PROTOCOL)
233 syslog(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"), "DEL_EDGE", c->name, c->hostname);
237 to = lookup_node(to_name);
241 if(debug_lvl >= DEBUG_PROTOCOL)
242 syslog(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"), "DEL_EDGE", c->name, c->hostname);
246 /* Check if edge exists */
248 e = lookup_edge(from, to);
252 if(debug_lvl >= DEBUG_PROTOCOL)
253 syslog(LOG_WARNING, _("Got %s from %s (%s) which does not appear in the edge tree"), "DEL_EDGE", c->name, c->hostname);
257 if(e->from == myself)
259 if(debug_lvl >= DEBUG_PROTOCOL)
260 syslog(LOG_WARNING, _("Got %s from %s (%s) for ourself"), "DEL_EDGE", c->name, c->hostname);
261 send_add_edge(c, e); /* Send back a correction */
265 /* Tell the rest about the deleted edge */
267 for(node = connection_tree->head; node; node = node->next)
269 other = (connection_t *)node->data;
270 if(other->status.active && other != c)
271 send_request(other, "%s", c->buffer);
274 /* Delete the edge */
278 /* Run MST before or after we tell the rest? */