Small fix.
[tinc] / src / protocol_edge.c
1 /*
2     protocol_edge.c -- handle the meta-protocol, edges
3     Copyright (C) 1999-2004 Ivo Timmermans <ivo@tinc-vpn.org>,
4                   2000-2004 Guus Sliepen <guus@tinc-vpn.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$
21 */
22
23 #include "system.h"
24
25 #include "avl_tree.h"
26 #include "conf.h"
27 #include "connection.h"
28 #include "edge.h"
29 #include "graph.h"
30 #include "logger.h"
31 #include "meta.h"
32 #include "net.h"
33 #include "netutl.h"
34 #include "node.h"
35 #include "protocol.h"
36 #include "utils.h"
37 #include "xalloc.h"
38
39 bool send_add_edge(connection_t *c, const edge_t *e)
40 {
41         bool x;
42         char *address, *port;
43
44         cp();
45
46         sockaddr2str(&e->address, &address, &port);
47
48         x = send_request(c, "%d %lx %s %s %s %s %lx %d", ADD_EDGE, random(),
49                                          e->from->name, e->to->name, address, port,
50                                          e->options, e->weight);
51         free(address);
52         free(port);
53
54         return x;
55 }
56
57 bool add_edge_h(connection_t *c)
58 {
59         edge_t *e;
60         node_t *from, *to;
61         char from_name[MAX_STRING_SIZE];
62         char to_name[MAX_STRING_SIZE];
63         char to_address[MAX_STRING_SIZE];
64         char to_port[MAX_STRING_SIZE];
65         sockaddr_t address;
66         long int options;
67         int weight;
68
69         cp();
70
71         if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %lx %d",
72                           from_name, to_name, to_address, to_port, &options, &weight) != 6) {
73                 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_EDGE", c->name,
74                            c->hostname);
75                 return false;
76         }
77
78         /* Check if names are valid */
79
80         if(!check_id(from_name)) {
81                 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name,
82                            c->hostname, _("invalid name"));
83                 return false;
84         }
85
86         if(!check_id(to_name)) {
87                 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name,
88                            c->hostname, _("invalid name"));
89                 return false;
90         }
91
92         if(seen_request(c->buffer))
93                 return true;
94
95         /* Lookup nodes */
96
97         from = lookup_node(from_name);
98
99         if(!from) {
100                 from = new_node();
101                 from->name = xstrdup(from_name);
102                 node_add(from);
103         }
104
105         to = lookup_node(to_name);
106
107         if(!to) {
108                 to = new_node();
109                 to->name = xstrdup(to_name);
110                 node_add(to);
111         }
112
113         if(tunnelserver && from != myself && from != c->node && to != myself && to != c->node)
114                 return false;
115
116         /* Convert addresses */
117
118         address = str2sockaddr(to_address, to_port);
119
120         /* Check if edge already exists */
121
122         e = lookup_edge(from, to);
123
124         if(e) {
125                 if(e->weight != weight || e->options != options || sockaddrcmp(&e->address, &address)) {
126                         if(from == myself) {
127                                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not match existing entry"),
128                                                    "ADD_EDGE", c->name, c->hostname);
129                                 send_add_edge(c, e);
130                                 return true;
131                         } else {
132                                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) which does not match existing entry"),
133                                                    "ADD_EDGE", c->name, c->hostname);
134                                 edge_del(e);
135                                 graph();
136                         }
137                 } else
138                         return true;
139         } else if(from == myself) {
140                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not exist"),
141                                    "ADD_EDGE", c->name, c->hostname);
142                 e = new_edge();
143                 e->from = from;
144                 e->to = to;
145                 send_del_edge(c, e);
146                 free_edge(e);
147                 return true;
148         }
149
150         e = new_edge();
151         e->from = from;
152         e->to = to;
153         e->address = address;
154         e->options = options;
155         e->weight = weight;
156         edge_add(e);
157
158         /* Tell the rest about the new edge */
159
160         if(!tunnelserver)
161                 forward_request(c);
162
163         /* Run MST before or after we tell the rest? */
164
165         graph();
166
167         return true;
168 }
169
170 bool send_del_edge(connection_t *c, const edge_t *e)
171 {
172         cp();
173
174         return send_request(c, "%d %lx %s %s", DEL_EDGE, random(),
175                                                 e->from->name, e->to->name);
176 }
177
178 bool del_edge_h(connection_t *c)
179 {
180         edge_t *e;
181         char from_name[MAX_STRING_SIZE];
182         char to_name[MAX_STRING_SIZE];
183         node_t *from, *to;
184
185         cp();
186
187         if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING, from_name, to_name) != 2) {
188                 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_EDGE", c->name,
189                            c->hostname);
190                 return false;
191         }
192
193         /* Check if names are valid */
194
195         if(!check_id(from_name)) {
196                 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name,
197                            c->hostname, _("invalid name"));
198                 return false;
199         }
200
201         if(!check_id(to_name)) {
202                 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name,
203                            c->hostname, _("invalid name"));
204                 return false;
205         }
206
207         if(seen_request(c->buffer))
208                 return true;
209
210         /* Lookup nodes */
211
212         from = lookup_node(from_name);
213
214         if(!from) {
215                 ifdebug(PROTOCOL) logger(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"),
216                                    "DEL_EDGE", c->name, c->hostname);
217                 return true;
218         }
219
220         to = lookup_node(to_name);
221
222         if(!to) {
223                 ifdebug(PROTOCOL) logger(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"),
224                                    "DEL_EDGE", c->name, c->hostname);
225                 return true;
226         }
227
228         if(tunnelserver && from != myself && from != c->node && to != myself && to != c->node)
229                 return false;
230
231         /* Check if edge exists */
232
233         e = lookup_edge(from, to);
234
235         if(!e) {
236                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) which does not appear in the edge tree"),
237                                    "DEL_EDGE", c->name, c->hostname);
238                 return true;
239         }
240
241         if(e->from == myself) {
242                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself"),
243                                    "DEL_EDGE", c->name, c->hostname);
244                 send_add_edge(c, e);    /* Send back a correction */
245                 return true;
246         }
247
248         /* Tell the rest about the deleted edge */
249
250         if(!tunnelserver)
251                 forward_request(c);
252
253         /* Delete the edge */
254
255         edge_del(e);
256
257         /* Run MST before or after we tell the rest? */
258
259         graph();
260
261         /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
262
263         if(!to->status.reachable) {
264                 e = lookup_edge(to, myself);
265                 if(e) {
266                         if(!tunnelserver)
267                                 send_del_edge(broadcast, e);
268                         edge_del(e);
269                 }
270         }
271
272         return true;
273 }