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/05/02 11:50:07 zarq Exp $
36 #include "interface.h"
41 #include "connection.h"
49 int send_add_edge(connection_t *c, edge_t *e)
52 char *from_udpaddress, *from_udpport;
53 char *to_udpaddress, *to_udpport;
55 /* sockaddr2str(&e->from.tcpaddress, &from_tcpaddress, &from_tcpport); */
56 sockaddr2str(&e->from.udpaddress, &from_udpaddress, &from_udpport);
57 /* sockaddr2str(&e->to.tcpaddress, &to_tcpaddress, &to_tcpport); */
58 sockaddr2str(&e->to.udpaddress, &to_udpaddress, &to_udpport);
59 x = send_request(c, "%d %lx %s %s %s %s %s %s %lx %d", ADD_EDGE, random(),
60 e->from.node->name, from_udpaddress, from_udpport,
61 e->to.node->name, to_udpaddress, to_udpport,
62 e->options, e->weight);
63 /* free(from_tcpaddress); */
64 /* free(from_tcpport); */
65 free(from_udpaddress);
67 /* free(to_tcpaddress); */
68 /* free(to_tcpport); */
75 int add_edge_h(connection_t *c)
80 char from_name[MAX_STRING_SIZE];
81 char to_name[MAX_STRING_SIZE];
82 char from_address[MAX_STRING_SIZE];
83 /* char from_tcpport[MAX_STRING_SIZE]; */
84 char from_udpport[MAX_STRING_SIZE];
85 char to_address[MAX_STRING_SIZE];
86 /* char to_tcpport[MAX_STRING_SIZE]; */
87 char to_udpport[MAX_STRING_SIZE];
88 sockaddr_t from_udpaddress;
89 sockaddr_t to_udpaddress;
94 if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %lx %d",
95 from_name, from_address, from_udpport,
96 to_name, to_address, to_udpport,
97 &options, &weight) != 8)
99 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_EDGE", c->name, c->hostname);
103 /* Check if names are valid */
105 if(check_id(from_name))
107 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("invalid name"));
111 if(check_id(to_name))
113 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("invalid name"));
117 if(seen_request(c->buffer))
122 from = lookup_node(from_name);
127 from->name = xstrdup(from_name);
131 to = lookup_node(to_name);
136 to->name = xstrdup(to_name);
140 /* Convert addresses */
142 /* from_tcpaddress = str2sockaddr(from_address, from_tcpport); */
143 from_udpaddress = str2sockaddr(from_address, from_udpport);
144 /* to_tcpaddress = str2sockaddr(to_address, to_tcpport); */
145 to_udpaddress = str2sockaddr(to_address, to_udpport);
147 /* Check if edge already exists */
149 e = lookup_edge(from, to);
153 if(e->weight != weight || e->options != options
154 || ((e->from.node == from) && (sockaddrcmp(&e->from.udpaddress, &from_udpaddress)|| sockaddrcmp(&e->to.udpaddress, &to_udpaddress)))
155 || ((e->from.node == to) && (sockaddrcmp(&e->from.udpaddress, &to_udpaddress) || sockaddrcmp(&e->to.udpaddress, &from_udpaddress)))
158 if(from == myself || to == myself)
160 if(debug_lvl >= DEBUG_PROTOCOL)
161 syslog(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not match existing entry"), "ADD_EDGE", c->name, c->hostname);
167 if(debug_lvl >= DEBUG_PROTOCOL)
168 syslog(LOG_WARNING, _("Got %s from %s (%s) which does not match existing entry"), "ADD_EDGE", c->name, c->hostname);
175 else if(from == myself || to == myself)
177 if(debug_lvl >= DEBUG_PROTOCOL)
178 syslog(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not exist"), "ADD_EDGE", c->name, c->hostname);
189 /* e->from.tcpaddress = from_tcpaddress; */
190 e->from.udpaddress = from_udpaddress;
192 /* e->to.tcpaddress = to_tcpaddress; */
193 e->to.udpaddress = to_udpaddress;
194 e->options = options;
198 /* Tell the rest about the new edge */
200 for(node = connection_tree->head; node; node = node->next)
202 other = (connection_t *)node->data;
203 if(other->status.active && other != c)
204 send_request(other, "%s", c->buffer);
207 /* Run MST before or after we tell the rest? */
214 int send_del_edge(connection_t *c, edge_t *e)
217 return send_request(c, "%d %lx %s %s", DEL_EDGE, random(),
218 e->from.node->name, e->to.node->name);
221 int del_edge_h(connection_t *c)
224 char from_name[MAX_STRING_SIZE];
225 char to_name[MAX_STRING_SIZE];
230 if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING"", from_name, to_name) != 2)
232 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_EDGE",
233 c->name, c->hostname);
237 /* Check if names are valid */
239 if(check_id(from_name))
241 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("invalid name"));
245 if(check_id(to_name))
247 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("invalid name"));
251 if(seen_request(c->buffer))
256 from = lookup_node(from_name);
260 if(debug_lvl >= DEBUG_PROTOCOL)
261 syslog(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"), "DEL_EDGE", c->name, c->hostname);
265 to = lookup_node(to_name);
269 if(debug_lvl >= DEBUG_PROTOCOL)
270 syslog(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"), "DEL_EDGE", c->name, c->hostname);
274 /* Check if edge exists */
276 e = lookup_edge(from, to);
280 if(debug_lvl >= DEBUG_PROTOCOL)
281 syslog(LOG_WARNING, _("Got %s from %s (%s) which does not appear in the edge tree"), "DEL_EDGE", c->name, c->hostname);
285 if(e->from.node == myself || e->to.node == myself)
287 if(debug_lvl >= DEBUG_PROTOCOL)
288 syslog(LOG_WARNING, _("Got %s from %s (%s) for ourself"), "DEL_EDGE", c->name, c->hostname);
289 send_add_edge(c, e); /* Send back a correction */
293 /* Tell the rest about the deleted edge */
295 for(node = connection_tree->head; node; node = node->next)
297 other = (connection_t *)node->data;
298 if(other->status.active && other != c)
299 send_request(other, "%s", c->buffer);
302 /* Delete the edge */
306 /* Run MST before or after we tell the rest? */