K&R style braces.
[tinc] / src / protocol_edge.c
1 /*
2     protocol_edge.c -- handle the meta-protocol, edges
3     Copyright (C) 1999-2005 Ivo Timmermans,
4                   2000-2009 Guus Sliepen <guus@tinc-vpn.org>
5                   2009      Michael Tokarev <mjt@corpit.ru>
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License along
18     with this program; if not, write to the Free Software Foundation, Inc.,
19     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #include "system.h"
23
24 #include "avl_tree.h"
25 #include "conf.h"
26 #include "connection.h"
27 #include "edge.h"
28 #include "graph.h"
29 #include "logger.h"
30 #include "meta.h"
31 #include "net.h"
32 #include "netutl.h"
33 #include "node.h"
34 #include "protocol.h"
35 #include "utils.h"
36 #include "xalloc.h"
37
38 bool send_add_edge(connection_t *c, const edge_t *e) {
39         bool x;
40         char *address, *port;
41
42         cp();
43
44         sockaddr2str(&e->address, &address, &port);
45
46         x = send_request(c, "%d %x %s %s %s %s %lx %d", ADD_EDGE, rand(),
47                                          e->from->name, e->to->name, address, port,
48                                          e->options, e->weight);
49         free(address);
50         free(port);
51
52         return x;
53 }
54
55 bool add_edge_h(connection_t *c) {
56         edge_t *e;
57         node_t *from, *to;
58         char from_name[MAX_STRING_SIZE];
59         char to_name[MAX_STRING_SIZE];
60         char to_address[MAX_STRING_SIZE];
61         char to_port[MAX_STRING_SIZE];
62         sockaddr_t address;
63         long int options;
64         int weight;
65
66         cp();
67
68         if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %lx %d",
69                           from_name, to_name, to_address, to_port, &options, &weight) != 6) {
70                 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_EDGE", c->name,
71                            c->hostname);
72                 return false;
73         }
74
75         /* Check if names are valid */
76
77         if(!check_id(from_name)) {
78                 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name,
79                            c->hostname, _("invalid name"));
80                 return false;
81         }
82
83         if(!check_id(to_name)) {
84                 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name,
85                            c->hostname, _("invalid name"));
86                 return false;
87         }
88
89         if(seen_request(c->buffer))
90                 return true;
91
92         /* Lookup nodes */
93
94         from = lookup_node(from_name);
95         to = lookup_node(to_name);
96
97         if(tunnelserver &&
98            from != myself && from != c->node &&
99            to != myself && to != c->node) {
100                 /* ignore indirect edge registrations for tunnelserver */
101                 ifdebug(PROTOCOL) logger(LOG_WARNING,
102                    _("Ignoring indirect %s from %s (%s)"),
103                    "ADD_EDGE", c->name, c->hostname);
104                 return true;
105         }
106
107         if(!from) {
108                 from = new_node();
109                 from->name = xstrdup(from_name);
110                 node_add(from);
111         }
112
113         if(!to) {
114                 to = new_node();
115                 to->name = xstrdup(to_name);
116                 node_add(to);
117         }
118
119
120         /* Convert addresses */
121
122         address = str2sockaddr(to_address, to_port);
123
124         /* Check if edge already exists */
125
126         e = lookup_edge(from, to);
127
128         if(e) {
129                 if(e->weight != weight || e->options != options || sockaddrcmp(&e->address, &address)) {
130                         if(from == myself) {
131                                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not match existing entry"),
132                                                    "ADD_EDGE", c->name, c->hostname);
133                                 send_add_edge(c, e);
134                                 return true;
135                         } else {
136                                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) which does not match existing entry"),
137                                                    "ADD_EDGE", c->name, c->hostname);
138                                 edge_del(e);
139                                 graph();
140                         }
141                 } else
142                         return true;
143         } else if(from == myself) {
144                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not exist"),
145                                    "ADD_EDGE", c->name, c->hostname);
146                 e = new_edge();
147                 e->from = from;
148                 e->to = to;
149                 send_del_edge(c, e);
150                 free_edge(e);
151                 return true;
152         }
153
154         e = new_edge();
155         e->from = from;
156         e->to = to;
157         e->address = address;
158         e->options = options;
159         e->weight = weight;
160         edge_add(e);
161
162         /* Tell the rest about the new edge */
163
164         if(!tunnelserver)
165                 forward_request(c);
166
167         /* Run MST before or after we tell the rest? */
168
169         graph();
170
171         return true;
172 }
173
174 bool send_del_edge(connection_t *c, const edge_t *e) {
175         cp();
176
177         return send_request(c, "%d %x %s %s", DEL_EDGE, rand(),
178                                                 e->from->name, e->to->name);
179 }
180
181 bool del_edge_h(connection_t *c) {
182         edge_t *e;
183         char from_name[MAX_STRING_SIZE];
184         char to_name[MAX_STRING_SIZE];
185         node_t *from, *to;
186
187         cp();
188
189         if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING, from_name, to_name) != 2) {
190                 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_EDGE", c->name,
191                            c->hostname);
192                 return false;
193         }
194
195         /* Check if names are valid */
196
197         if(!check_id(from_name)) {
198                 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name,
199                            c->hostname, _("invalid name"));
200                 return false;
201         }
202
203         if(!check_id(to_name)) {
204                 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name,
205                            c->hostname, _("invalid name"));
206                 return false;
207         }
208
209         if(seen_request(c->buffer))
210                 return true;
211
212         /* Lookup nodes */
213
214         from = lookup_node(from_name);
215         to = lookup_node(to_name);
216
217         if(tunnelserver &&
218            from != myself && from != c->node &&
219            to != myself && to != c->node) {
220                 /* ignore indirect edge registrations for tunnelserver */
221                 ifdebug(PROTOCOL) logger(LOG_WARNING,
222                    _("Ignoring indirect %s from %s (%s)"),
223                    "DEL_EDGE", c->name, c->hostname);
224                 return true;
225         }
226
227         if(!from) {
228                 ifdebug(PROTOCOL) logger(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"),
229                                    "DEL_EDGE", c->name, c->hostname);
230                 return true;
231         }
232
233         if(!to) {
234                 ifdebug(PROTOCOL) logger(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"),
235                                    "DEL_EDGE", c->name, c->hostname);
236                 return true;
237         }
238
239         /* Check if edge exists */
240
241         e = lookup_edge(from, to);
242
243         if(!e) {
244                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) which does not appear in the edge tree"),
245                                    "DEL_EDGE", c->name, c->hostname);
246                 return true;
247         }
248
249         if(e->from == myself) {
250                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself"),
251                                    "DEL_EDGE", c->name, c->hostname);
252                 send_add_edge(c, e);    /* Send back a correction */
253                 return true;
254         }
255
256         /* Tell the rest about the deleted edge */
257
258         if(!tunnelserver)
259                 forward_request(c);
260
261         /* Delete the edge */
262
263         edge_del(e);
264
265         /* Run MST before or after we tell the rest? */
266
267         graph();
268
269         /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
270
271         if(!to->status.reachable) {
272                 e = lookup_edge(to, myself);
273                 if(e) {
274                         if(!tunnelserver)
275                                 send_del_edge(broadcast, e);
276                         edge_del(e);
277                 }
278         }
279
280         return true;
281 }