3d0355fda4e3f28fa3ad767662b35aeceb2f50d5
[oweals/tinc.git] / 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.1 2002/09/02 22:40:42 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   sockaddr2str(&n->address, &address, &port);
52   x = send_request(c, "%d %s %s %s %lx %d", ADD_NODE,
53                       n->name, address, port,
54                       n->options, n->distance + 1);
55   free(address);
56   free(port);
57 cp
58   return x;
59 }
60
61 int add_node_h(connection_t *c)
62 {
63   connection_t *other;
64   node_t *n;
65   char name[MAX_STRING_SIZE];
66   char address[MAX_STRING_SIZE];
67   char port[MAX_STRING_SIZE];
68   long int options;
69   int distance;
70   avl_node_t *node;
71 cp
72   if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %lx %d",
73             name, address, port, &options, &distance) != 5)
74     {
75        syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_NODE", c->name, c->hostname);
76        return -1;
77     }
78
79   /* Check if names are valid */
80
81   if(check_id(name))
82     {
83       syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_NODE", c->name, c->hostname, _("invalid name"));
84       return -1;
85     }
86
87   /* Lookup nodes */
88
89   n = lookup_node(name);
90   
91   if(!n)
92     {
93       // It's a new node. Add it and tell the others.
94       n = new_node();
95       n->name = xstrdup(name);
96       n->address = str2sockaddr(address, port);
97       n->hostname = sockaddr2hostname(&n->address);
98       n->options = options;
99       n->distance = distance;
100       n->nexthop = c->node;
101       node_add(n);
102     }
103   else
104     {
105       // If this ADD_NODE is closer or more direct, use it instead of the old one.
106       if((n->options & OPTION_INDIRECT) && !(options & OPTION_INDIRECT) || n->distance > distance)
107         {
108           free(n->hostname);
109           n->address = str2sockaddr(address, port);
110           n->hostname = sockaddr2hostname(&n->address);
111           n->options = options;
112           n->distance = distance;
113           n->nexthop = c->node;
114         }
115       else
116         // Otherwise, just ignore it.
117         return 0;
118     }
119
120   /* Tell the rest about the new node */
121
122   for(node = connection_tree->head; node; node = node->next)
123     {
124       other = (connection_t *)node->data;
125       if(other->status.active && other != c)
126         send_add_node(other, n);
127     }
128
129 cp
130   return 0;
131 }
132
133 int send_del_node(connection_t *c, node_t *n)
134 {
135 cp
136   return send_request(c, "%d %s", DEL_NODE, n->name);
137 }
138
139 int del_node_h(connection_t *c)
140 {
141   char name[MAX_STRING_SIZE];
142   node_t *n;
143   connection_t *other;
144   avl_node_t *node;
145 cp
146   if(sscanf(c->buffer, "%*d "MAX_STRING, name) != 1)
147     {
148       syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_NODE",
149              c->name, c->hostname);
150       return -1;
151     }
152
153   /* Check if names are valid */
154
155   if(check_id(name))
156     {
157       syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_NODE", c->name, c->hostname, _("invalid name"));
158       return -1;
159     }
160
161   /* Lookup nodes */
162
163   n = lookup_node(name);
164   
165   if(!n)
166     {
167       if(debug_lvl >= DEBUG_PROTOCOL)
168         syslog(LOG_WARNING, _("Got %s from %s (%s) which does not appear in the node tree"), "DEL_NODE", c->name, c->hostname);
169       return 0;
170     }
171
172   /* If we got a DEL_NODE but we know of a different route to it, tell the one who send the DEL_NODE */
173
174   if(n->nexthop != c->node)
175     {
176       return send_add_node(c, n);
177     }
178   
179   /* Otherwise, tell the rest about the deleted node */
180
181   for(node = connection_tree->head; node; node = node->next)
182     {
183       other = (connection_t *)node->data;
184       if(other->status.active && other != c)
185         send_del_node(other, n);
186     }
187
188   /* Delete the node */
189   
190   node_del(n);
191
192   exit:
193 cp
194   return 0;
195 }