2 protocol_node.c -- handle the meta-protocol, nodes
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_node.c,v 1.1.4.7 2002/09/09 19:40:08 guus Exp $
41 #include "connection.h"
46 int send_add_node(connection_t *c, node_t *n)
51 if(!n->status.reachable)
54 sockaddr2str(&n->address, &address, &port);
55 x = send_request(c, "%d %s %s %s %lx %d %s %s", ADD_NODE,
56 n->name, address, port,
57 n->options, n->distance + 1, // Alternatively, use n->distance + c->estimated_weight
58 n->prevhop->name, n->via->name);
65 int add_node_h(connection_t *c)
68 node_t *n, *prevhop, *via;
69 char name[MAX_STRING_SIZE];
70 char address[MAX_STRING_SIZE];
71 char port[MAX_STRING_SIZE];
72 char prevhopname[MAX_STRING_SIZE];
73 char vianame[MAX_STRING_SIZE];
78 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %lx %d "MAX_STRING" "MAX_STRING,
79 name, address, port, &options, &distance, prevhopname, vianame) != 7)
81 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_NODE", c->name, c->hostname);
85 /* Check if names are valid */
89 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_NODE", c->name, c->hostname, _("invalid name"));
93 /* This node is indirect if it's nexthop is as well */
95 if(c->node->options & OPTION_INDIRECT)
96 options |= OPTION_INDIRECT;
100 prevhop = lookup_node(prevhopname);
104 prevhop = new_node();
105 prevhop->name = xstrdup(prevhopname);
109 via = lookup_node(vianame);
114 via->name = xstrdup(vianame);
118 n = lookup_node(name);
122 // It's a new node. Add it and tell the others.
124 n->name = xstrdup(name);
125 n->address = str2sockaddr(address, port);
126 n->hostname = sockaddr2hostname(&n->address);
127 n->options = options;
128 n->distance = distance;
129 n->nexthop = c->node;
130 n->prevhop = prevhop;
133 if(prevhop == myself)
135 syslog(LOG_WARNING, _("Got ADD_NODE %s prevhop %s via %s from %s, sending back a DEL_NODE!"), name, prevhopname, vianame, c->name);
136 // send_del_node(c, n);
139 n->status.reachable = 1;
143 // If this ADD_NODE is closer or more direct, use it instead of the old one.
144 if(!n->status.reachable || ((n->options & OPTION_INDIRECT) && !(options & OPTION_INDIRECT)) || n->distance > distance)
146 if(prevhop == myself)
148 syslog(LOG_WARNING, _("Got ADD_NODE %s prevhop %s via %s from %s!"), name, prevhopname, vianame, c->name);
149 // send_del_node(c, n);
152 node = avl_unlink(node_udp_tree, n);
153 n->address = str2sockaddr(address, port);
154 avl_insert_node(node_udp_tree, node);
157 n->hostname = sockaddr2hostname(&n->address);
158 n->options = options;
159 n->distance = distance;
160 n->via = n->nexthop = c->node;
161 n->status.reachable = 1;
162 n->status.validkey = 0;
163 n->status.waitingforkey = 0;
166 // Otherwise, just ignore it.
170 /* Tell the rest about the new node */
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_add_node(other, n);
183 int send_del_node(connection_t *c, node_t *n)
186 return send_request(c, "%d %s %s", DEL_NODE, n->name, n->prevhop->name);
189 int del_node_h(connection_t *c)
191 char name[MAX_STRING_SIZE];
192 char prevhopname[MAX_STRING_SIZE];
197 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING, name, prevhopname) != 2)
199 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_NODE",
200 c->name, c->hostname);
204 /* Check if names are valid */
208 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_NODE", c->name, c->hostname, _("invalid name"));
214 n = lookup_node(name);
215 prevhop = lookup_node(prevhopname);
219 if(debug_lvl >= DEBUG_PROTOCOL)
220 syslog(LOG_WARNING, _("Got %s from %s (%s) which does not appear in the node tree"), "DEL_NODE", c->name, c->hostname);
224 /* If we got a DEL_NODE but we know of a different route to it, tell the one who send the DEL_NODE */
226 if(n->nexthop != c->node)
228 return send_add_node(c, n);
231 /* Otherwise, tell the rest about the deleted node */
233 for(node = connection_tree->head; node; node = node->next)
235 other = (connection_t *)node->data;
236 if(other->status.active && other != c)
237 send_del_node(other, n);
240 /* "Delete" the node */
242 n->status.reachable = 0;
243 n->status.validkey = 0;