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.6 2002/03/22 13:31:18 guus 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.tcpaddress, &from_tcpaddress, &from_tcpport);
55 sockaddr2str(&e->from.udpaddress, &from_udpaddress, &from_udpport);
56 // sockaddr2str(&e->to.tcpaddress, &to_tcpaddress, &to_tcpport);
57 sockaddr2str(&e->to.udpaddress, &to_udpaddress, &to_udpport);
58 x = send_request(c, "%d %lx %s %s %s %s %s %s %lx %d", ADD_EDGE, random(),
59 e->from.node->name, from_udpaddress, from_udpport,
60 e->to.node->name, to_udpaddress, to_udpport,
61 e->options, e->weight);
62 // free(from_tcpaddress);
63 // free(from_tcpport);
64 free(from_udpaddress);
66 // free(to_tcpaddress);
74 int add_edge_h(connection_t *c)
79 char from_name[MAX_STRING_SIZE];
80 char to_name[MAX_STRING_SIZE];
81 char from_address[MAX_STRING_SIZE];
82 // char from_tcpport[MAX_STRING_SIZE];
83 char from_udpport[MAX_STRING_SIZE];
84 char to_address[MAX_STRING_SIZE];
85 // char to_tcpport[MAX_STRING_SIZE];
86 char to_udpport[MAX_STRING_SIZE];
87 sockaddr_t from_udpaddress;
88 sockaddr_t to_udpaddress;
93 if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %lx %d",
94 from_name, from_address, from_udpport,
95 to_name, to_address, to_udpport,
96 &options, &weight) != 8)
98 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_EDGE", c->name, c->hostname);
102 /* Check if names are valid */
104 if(check_id(from_name))
106 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("invalid name"));
110 if(check_id(to_name))
112 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("invalid name"));
116 if(seen_request(c->buffer))
121 from = lookup_node(from_name);
126 from->name = xstrdup(from_name);
130 to = lookup_node(to_name);
135 to->name = xstrdup(to_name);
139 /* Convert addresses */
141 // from_tcpaddress = str2sockaddr(from_address, from_tcpport);
142 from_udpaddress = str2sockaddr(from_address, from_udpport);
143 // to_tcpaddress = str2sockaddr(to_address, to_tcpport);
144 to_udpaddress = str2sockaddr(to_address, to_udpport);
146 /* Check if edge already exists */
148 e = lookup_edge(from, to);
152 if(e->weight != weight || e->options != options
153 || ((e->from.node == from) && (sockaddrcmp(&e->from.udpaddress, &from_udpaddress)|| sockaddrcmp(&e->to.udpaddress, &to_udpaddress)))
154 || ((e->from.node == to) && (sockaddrcmp(&e->from.udpaddress, &to_udpaddress) || sockaddrcmp(&e->to.udpaddress, &from_udpaddress)))
157 if(from == myself || to == myself)
159 if(debug_lvl >= DEBUG_PROTOCOL)
160 syslog(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not match existing entry"), "ADD_EDGE", c->name, c->hostname);
166 if(debug_lvl >= DEBUG_PROTOCOL)
167 syslog(LOG_WARNING, _("Got %s from %s (%s) which does not match existing entry"), "ADD_EDGE", c->name, c->hostname);
174 else if(from == myself || to == myself)
176 if(debug_lvl >= DEBUG_PROTOCOL)
177 syslog(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not exist"), "ADD_EDGE", c->name, c->hostname);
188 // e->from.tcpaddress = from_tcpaddress;
189 e->from.udpaddress = from_udpaddress;
191 // e->to.tcpaddress = to_tcpaddress;
192 e->to.udpaddress = to_udpaddress;
193 e->options = options;
197 /* Tell the rest about the new edge */
199 for(node = connection_tree->head; node; node = node->next)
201 other = (connection_t *)node->data;
202 if(other->status.active && other != c)
203 send_request(other, "%s", c->buffer);
206 /* Run MST before or after we tell the rest? */
213 int send_del_edge(connection_t *c, edge_t *e)
216 return send_request(c, "%d %lx %s %s", DEL_EDGE, random(),
217 e->from.node->name, e->to.node->name);
220 int del_edge_h(connection_t *c)
223 char from_name[MAX_STRING_SIZE];
224 char to_name[MAX_STRING_SIZE];
229 if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING"", from_name, to_name) != 2)
231 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_EDGE",
232 c->name, c->hostname);
236 /* Check if names are valid */
238 if(check_id(from_name))
240 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("invalid name"));
244 if(check_id(to_name))
246 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("invalid name"));
250 if(seen_request(c->buffer))
255 from = lookup_node(from_name);
259 if(debug_lvl >= DEBUG_PROTOCOL)
260 syslog(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"), "DEL_EDGE", c->name, c->hostname);
264 to = lookup_node(to_name);
268 if(debug_lvl >= DEBUG_PROTOCOL)
269 syslog(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"), "DEL_EDGE", c->name, c->hostname);
273 /* Check if edge exists */
275 e = lookup_edge(from, to);
279 if(debug_lvl >= DEBUG_PROTOCOL)
280 syslog(LOG_WARNING, _("Got %s from %s (%s) which does not appear in the edge tree"), "DEL_EDGE", c->name, c->hostname);
284 if(e->from.node == myself || e->to.node == myself)
286 if(debug_lvl >= DEBUG_PROTOCOL)
287 syslog(LOG_WARNING, _("Got %s from %s (%s) for ourself"), "DEL_EDGE", c->name, c->hostname);
288 send_add_edge(c, e); /* Send back a correction */
292 /* Tell the rest about the deleted edge */
294 for(node = connection_tree->head; node; node = node->next)
296 other = (connection_t *)node->data;
297 if(other->status.active && other != c)
298 send_request(other, "%s", c->buffer);
301 /* Delete the edge */
305 /* Run MST before or after we tell the rest? */