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.2 2002/04/09 15:26:00 zarq Exp $
41 #include "connection.h"
48 int send_add_edge(connection_t *c, edge_t *e)
51 char *from_udpaddress, *from_udpport;
52 char *to_udpaddress, *to_udpport;
54 sockaddr2str(&e->from.udpaddress, &from_udpaddress, &from_udpport);
55 sockaddr2str(&e->to.udpaddress, &to_udpaddress, &to_udpport);
56 x = send_request(c, "%d %lx %s %s %s %s %s %s %lx %d", ADD_EDGE, random(),
57 e->from.node->name, from_udpaddress, from_udpport,
58 e->to.node->name, to_udpaddress, to_udpport,
59 e->options, e->weight);
60 free(from_udpaddress);
68 int add_edge_h(connection_t *c)
73 char from_name[MAX_STRING_SIZE];
74 char to_name[MAX_STRING_SIZE];
75 char from_address[MAX_STRING_SIZE];
76 char from_udpport[MAX_STRING_SIZE];
77 char to_address[MAX_STRING_SIZE];
78 char to_udpport[MAX_STRING_SIZE];
79 sockaddr_t from_udpaddress;
80 sockaddr_t to_udpaddress;
85 if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %lx %d",
86 from_name, from_address, from_udpport,
87 to_name, to_address, to_udpport,
88 &options, &weight) != 8)
90 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_EDGE", c->name, c->hostname);
94 /* Check if names are valid */
96 if(check_id(from_name))
98 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("invalid name"));
102 if(check_id(to_name))
104 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("invalid name"));
108 if(seen_request(c->buffer))
113 from = lookup_node(from_name);
118 from->name = xstrdup(from_name);
122 to = lookup_node(to_name);
127 to->name = xstrdup(to_name);
131 /* Convert addresses */
133 from_udpaddress = str2sockaddr(from_address, from_udpport);
134 to_udpaddress = str2sockaddr(to_address, to_udpport);
136 /* Check if edge already exists */
138 e = lookup_edge(from, to);
142 if(e->weight != weight || e->options != options
143 || ((e->from.node == from) && (sockaddrcmp(&e->from.udpaddress, &from_udpaddress)|| sockaddrcmp(&e->to.udpaddress, &to_udpaddress)))
144 || ((e->from.node == to) && (sockaddrcmp(&e->from.udpaddress, &to_udpaddress) || sockaddrcmp(&e->to.udpaddress, &from_udpaddress)))
147 if(from == myself || to == myself)
149 if(debug_lvl >= DEBUG_PROTOCOL)
150 syslog(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not match existing entry"), "ADD_EDGE", c->name, c->hostname);
156 if(debug_lvl >= DEBUG_PROTOCOL)
157 syslog(LOG_WARNING, _("Got %s from %s (%s) which does not match existing entry"), "ADD_EDGE", c->name, c->hostname);
164 else if(from == myself || to == myself)
166 if(debug_lvl >= DEBUG_PROTOCOL)
167 syslog(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not exist"), "ADD_EDGE", c->name, c->hostname);
178 e->from.udpaddress = from_udpaddress;
180 e->to.udpaddress = to_udpaddress;
181 e->options = options;
185 /* Tell the rest about the new edge */
187 for(node = connection_tree->head; node; node = node->next)
189 other = (connection_t *)node->data;
190 if(other->status.active && other != c)
191 send_request(other, "%s", c->buffer);
194 /* Run MST before or after we tell the rest? */
201 int send_del_edge(connection_t *c, edge_t *e)
204 return send_request(c, "%d %lx %s %s", DEL_EDGE, random(),
205 e->from.node->name, e->to.node->name);
208 int del_edge_h(connection_t *c)
211 char from_name[MAX_STRING_SIZE];
212 char to_name[MAX_STRING_SIZE];
217 if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING"", from_name, to_name) != 2)
219 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_EDGE",
220 c->name, c->hostname);
224 /* Check if names are valid */
226 if(check_id(from_name))
228 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("invalid name"));
232 if(check_id(to_name))
234 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("invalid name"));
238 if(seen_request(c->buffer))
243 from = lookup_node(from_name);
247 if(debug_lvl >= DEBUG_PROTOCOL)
248 syslog(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"), "DEL_EDGE", c->name, c->hostname);
252 to = lookup_node(to_name);
256 if(debug_lvl >= DEBUG_PROTOCOL)
257 syslog(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"), "DEL_EDGE", c->name, c->hostname);
261 /* Check if edge exists */
263 e = lookup_edge(from, to);
267 if(debug_lvl >= DEBUG_PROTOCOL)
268 syslog(LOG_WARNING, _("Got %s from %s (%s) which does not appear in the edge tree"), "DEL_EDGE", c->name, c->hostname);
272 if(e->from.node == myself || e->to.node == myself)
274 if(debug_lvl >= DEBUG_PROTOCOL)
275 syslog(LOG_WARNING, _("Got %s from %s (%s) for ourself"), "DEL_EDGE", c->name, c->hostname);
276 send_add_edge(c, e); /* Send back a correction */
280 /* Tell the rest about the deleted edge */
282 for(node = connection_tree->head; node; node = node->next)
284 other = (connection_t *)node->data;
285 if(other->status.active && other != c)
286 send_request(other, "%s", c->buffer);
289 /* Delete the edge */
293 /* Run MST before or after we tell the rest? */