b86ef79e096cfb599bb4a31a081b24ea11498547
[tinc] / src / protocol_node.c
1 /*
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>
5
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.
10
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.
15
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.
19
20     $Id: protocol_node.c,v 1.1.4.3 2002/09/03 22:37:49 guus Exp $
21 */
22
23 #include "config.h"
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <syslog.h>
28 #include <stdio.h>
29 #include <stdarg.h>
30 #include <errno.h>
31
32 #include <utils.h>
33 #include <xalloc.h>
34 #include <avl_tree.h>
35
36 #include "conf.h"
37 #include "net.h"
38 #include "netutl.h"
39 #include "protocol.h"
40 #include "meta.h"
41 #include "connection.h"
42 #include "node.h"
43
44 #include "system.h"
45
46 int send_add_node(connection_t *c, node_t *n)
47 {
48   int x;
49   char *address, *port;
50 cp
51   if(!n->status.reachable)
52     return 0;
53
54   sockaddr2str(&n->address, &address, &port);
55   x = send_request(c, "%d %s %s %s %lx %d", ADD_NODE,
56                       n->name, address, port,
57                       n->options, n->distance + 1); // Alternatively, use n->distance + c->estimated_weight
58   free(address);
59   free(port);
60 cp
61   return x;
62 }
63
64 int add_node_h(connection_t *c)
65 {
66   connection_t *other;
67   node_t *n;
68   char name[MAX_STRING_SIZE];
69   char address[MAX_STRING_SIZE];
70   char port[MAX_STRING_SIZE];
71   long int options;
72   int distance;
73   avl_node_t *node;
74 cp
75   if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %lx %d",
76             name, address, port, &options, &distance) != 5)
77     {
78        syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_NODE", c->name, c->hostname);
79        return -1;
80     }
81
82   /* Check if names are valid */
83
84   if(check_id(name))
85     {
86       syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_NODE", c->name, c->hostname, _("invalid name"));
87       return -1;
88     }
89
90   /* This node is indirect if it's nexthop is as well */
91   
92   if(c->node->options & OPTION_INDIRECT)
93     options |= OPTION_INDIRECT;
94
95   /* Lookup nodes */
96
97   n = lookup_node(name);
98   
99   if(!n)
100     {
101       // It's a new node. Add it and tell the others.
102       n = new_node();
103       n->name = xstrdup(name);
104       n->address = str2sockaddr(address, port);
105       n->hostname = sockaddr2hostname(&n->address);
106       n->options = options;
107       n->distance = distance;
108       n->via = n->nexthop = c->node;
109       n->status.reachable = 1;
110       node_add(n);
111     }
112   else
113     {
114       // If this ADD_NODE is closer or more direct, use it instead of the old one.
115       if(!n->reachable || ((n->options & OPTION_INDIRECT) && !(options & OPTION_INDIRECT)) || n->distance > distance)
116         {
117           avl_node_t *node = avl_unlink(node_udp_tree, n);
118           n->address = str2sockaddr(address, port);
119           avl_insert_node(node_udp_tree, node);
120           if(n->hostname)
121             free(n->hostname);
122           n->hostname = sockaddr2hostname(&n->address);
123           n->options = options;
124           n->distance = distance;
125           n->via = n->nexthop = c->node;
126           n->status.reachable = 1;
127           n->status.validkey = 0;
128           n->status.waitingforkey = 0;
129         }
130       else
131         // Otherwise, just ignore it.
132         return 0;
133     }
134
135   /* Tell the rest about the new node */
136
137   for(node = connection_tree->head; node; node = node->next)
138     {
139       other = (connection_t *)node->data;
140       if(other->status.active && other != c)
141         send_add_node(other, n);
142     }
143
144 cp
145   return 0;
146 }
147
148 int send_del_node(connection_t *c, node_t *n)
149 {
150 cp
151   return send_request(c, "%d %s", DEL_NODE, n->name);
152 }
153
154 int del_node_h(connection_t *c)
155 {
156   char name[MAX_STRING_SIZE];
157   node_t *n;
158   connection_t *other;
159   avl_node_t *node;
160 cp
161   if(sscanf(c->buffer, "%*d "MAX_STRING, name) != 1)
162     {
163       syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_NODE",
164              c->name, c->hostname);
165       return -1;
166     }
167
168   /* Check if names are valid */
169
170   if(check_id(name))
171     {
172       syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_NODE", c->name, c->hostname, _("invalid name"));
173       return -1;
174     }
175
176   /* Lookup nodes */
177
178   n = lookup_node(name);
179   
180   if(!n)
181     {
182       if(debug_lvl >= DEBUG_PROTOCOL)
183         syslog(LOG_WARNING, _("Got %s from %s (%s) which does not appear in the node tree"), "DEL_NODE", c->name, c->hostname);
184       return 0;
185     }
186
187   /* If we got a DEL_NODE but we know of a different route to it, tell the one who send the DEL_NODE */
188
189   if(n->nexthop != c->node)
190     {
191       return send_add_node(c, n);
192     }
193   
194   /* Otherwise, tell the rest about the deleted node */
195
196   for(node = connection_tree->head; node; node = node->next)
197     {
198       other = (connection_t *)node->data;
199       if(other->status.active && other != c)
200         send_del_node(other, n);
201     }
202
203   /* "Delete" the node */
204   
205   n->status.reachable = 0;
206   n->status.validkey = 0;
207 cp
208   return 0;
209 }