Remove unused '#include's.
[tinc] / src / protocol_edge.c
1 /*
2     protocol_edge.c -- handle the meta-protocol, edges
3     Copyright (C) 1999-2005 Ivo Timmermans,
4                   2000-2012 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 "conf.h"
25 #include "connection.h"
26 #include "edge.h"
27 #include "graph.h"
28 #include "logger.h"
29 #include "net.h"
30 #include "netutl.h"
31 #include "node.h"
32 #include "protocol.h"
33 #include "utils.h"
34 #include "xalloc.h"
35
36 bool send_add_edge(connection_t *c, const edge_t *e) {
37         bool x;
38         char *address, *port;
39
40         sockaddr2str(&e->address, &address, &port);
41
42         if(e->local_address.sa.sa_family) {
43                 char *local_address, *local_port;
44                 sockaddr2str(&e->local_address, &local_address, &local_port);
45
46                 x = send_request(c, "%d %x %s %s %s %s %x %d %s %s", ADD_EDGE, rand(),
47                                  e->from->name, e->to->name, address, port,
48                                  e->options, e->weight, local_address, local_port);
49                 free(local_address);
50                 free(local_port);
51         } else {
52                 x = send_request(c, "%d %x %s %s %s %s %x %d", ADD_EDGE, rand(),
53                                  e->from->name, e->to->name, address, port,
54                                  e->options, e->weight);
55         }
56
57         free(address);
58         free(port);
59
60         return x;
61 }
62
63 bool add_edge_h(connection_t *c, const char *request) {
64         edge_t *e;
65         node_t *from, *to;
66         char from_name[MAX_STRING_SIZE];
67         char to_name[MAX_STRING_SIZE];
68         char to_address[MAX_STRING_SIZE];
69         char to_port[MAX_STRING_SIZE];
70         char address_local[MAX_STRING_SIZE];
71         char port_local[MAX_STRING_SIZE];
72         sockaddr_t address, local_address = {0};
73         uint32_t options;
74         int weight;
75
76         int parameter_count = sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %x %d "MAX_STRING" "MAX_STRING,
77                                      from_name, to_name, to_address, to_port, &options, &weight, address_local, port_local);
78
79         if(parameter_count != 6 && parameter_count != 8) {
80                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ADD_EDGE", c->name,
81                        c->hostname);
82                 return false;
83         }
84
85         /* Check if names are valid */
86
87         if(!check_id(from_name) || !check_id(to_name) || !strcmp(from_name, to_name)) {
88                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ADD_EDGE", c->name,
89                        c->hostname, "invalid name");
90                 return false;
91         }
92
93         if(seen_request(request)) {
94                 return true;
95         }
96
97         /* Lookup nodes */
98
99         from = lookup_node(from_name);
100         to = lookup_node(to_name);
101
102         if(tunnelserver &&
103                         from != myself && from != c->node &&
104                         to != myself && to != c->node) {
105                 /* ignore indirect edge registrations for tunnelserver */
106                 logger(DEBUG_PROTOCOL, LOG_WARNING,
107                        "Ignoring indirect %s from %s (%s)",
108                        "ADD_EDGE", c->name, c->hostname);
109                 return true;
110         }
111
112         if(!from) {
113                 from = new_node();
114                 from->name = xstrdup(from_name);
115                 node_add(from);
116         }
117
118         if(!to) {
119                 to = new_node();
120                 to->name = xstrdup(to_name);
121                 node_add(to);
122         }
123
124
125         /* Convert addresses */
126
127         address = str2sockaddr(to_address, to_port);
128
129         if(parameter_count >= 8) {
130                 local_address = str2sockaddr(address_local, port_local);
131         }
132
133         /* Check if edge already exists */
134
135         e = lookup_edge(from, to);
136
137         if(e) {
138                 bool new_address = sockaddrcmp(&e->address, &address);
139                 // local_address.sa.sa_family will be 0 if we got it from older tinc versions
140                 // local_address.sa.sa_family will be 255 (AF_UNKNOWN) if we got it from newer versions
141                 // but for edge which does not have local_address
142                 bool new_local_address = local_address.sa.sa_family && local_address.sa.sa_family != AF_UNKNOWN &&
143                                          sockaddrcmp(&e->local_address, &local_address);
144
145                 if(e->weight == weight && e->options == options && !new_address && !new_local_address) {
146                         sockaddrfree(&address);
147                         sockaddrfree(&local_address);
148                         return true;
149                 }
150
151                 if(from == myself) {
152                         logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself which does not match existing entry",
153                                "ADD_EDGE", c->name, c->hostname);
154                         send_add_edge(c, e);
155                         sockaddrfree(&address);
156                         sockaddrfree(&local_address);
157                         return true;
158                 }
159
160                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) which does not match existing entry",
161                        "ADD_EDGE", c->name, c->hostname);
162
163                 e->options = options;
164
165                 if(new_address) {
166                         sockaddrfree(&e->address);
167                         e->address = address;
168                 } else {
169                         sockaddrfree(&address);
170                 }
171
172                 if(new_local_address) {
173                         sockaddrfree(&e->local_address);
174                         e->local_address = local_address;
175                 } else {
176                         sockaddrfree(&local_address);
177                 }
178
179                 if(e->weight != weight) {
180                         splay_node_t *node = splay_unlink(edge_weight_tree, e);
181                         e->weight = weight;
182                         splay_insert_node(edge_weight_tree, node);
183                 }
184         } else if(from == myself) {
185                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself which does not exist",
186                        "ADD_EDGE", c->name, c->hostname);
187                 contradicting_add_edge++;
188                 e = new_edge();
189                 e->from = from;
190                 e->to = to;
191                 send_del_edge(c, e);
192                 free_edge(e);
193                 sockaddrfree(&address);
194                 sockaddrfree(&local_address);
195                 return true;
196         } else {
197                 e = new_edge();
198                 e->from = from;
199                 e->to = to;
200                 e->address = address;
201                 e->local_address = local_address;
202                 e->options = options;
203                 e->weight = weight;
204                 edge_add(e);
205         }
206
207         /* Tell the rest about the new edge */
208
209         if(!tunnelserver) {
210                 forward_request(c, request);
211         }
212
213         /* Run MST before or after we tell the rest? */
214
215         graph();
216
217         return true;
218 }
219
220 bool send_del_edge(connection_t *c, const edge_t *e) {
221         return send_request(c, "%d %x %s %s", DEL_EDGE, rand(),
222                             e->from->name, e->to->name);
223 }
224
225 bool del_edge_h(connection_t *c, const char *request) {
226         edge_t *e;
227         char from_name[MAX_STRING_SIZE];
228         char to_name[MAX_STRING_SIZE];
229         node_t *from, *to;
230
231         if(sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING, from_name, to_name) != 2) {
232                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "DEL_EDGE", c->name,
233                        c->hostname);
234                 return false;
235         }
236
237         /* Check if names are valid */
238
239         if(!check_id(from_name) || !check_id(to_name) || !strcmp(from_name, to_name)) {
240                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "DEL_EDGE", c->name,
241                        c->hostname, "invalid name");
242                 return false;
243         }
244
245         if(seen_request(request)) {
246                 return true;
247         }
248
249         /* Lookup nodes */
250
251         from = lookup_node(from_name);
252         to = lookup_node(to_name);
253
254         if(tunnelserver &&
255                         from != myself && from != c->node &&
256                         to != myself && to != c->node) {
257                 /* ignore indirect edge registrations for tunnelserver */
258                 logger(DEBUG_PROTOCOL, LOG_WARNING,
259                        "Ignoring indirect %s from %s (%s)",
260                        "DEL_EDGE", c->name, c->hostname);
261                 return true;
262         }
263
264         if(!from) {
265                 logger(DEBUG_PROTOCOL, LOG_ERR, "Got %s from %s (%s) which does not appear in the edge tree",
266                        "DEL_EDGE", c->name, c->hostname);
267                 return true;
268         }
269
270         if(!to) {
271                 logger(DEBUG_PROTOCOL, LOG_ERR, "Got %s from %s (%s) which does not appear in the edge tree",
272                        "DEL_EDGE", c->name, c->hostname);
273                 return true;
274         }
275
276         /* Check if edge exists */
277
278         e = lookup_edge(from, to);
279
280         if(!e) {
281                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) which does not appear in the edge tree",
282                        "DEL_EDGE", c->name, c->hostname);
283                 return true;
284         }
285
286         if(e->from == myself) {
287                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself",
288                        "DEL_EDGE", c->name, c->hostname);
289                 contradicting_del_edge++;
290                 send_add_edge(c, e);    /* Send back a correction */
291                 return true;
292         }
293
294         /* Tell the rest about the deleted edge */
295
296         if(!tunnelserver) {
297                 forward_request(c, request);
298         }
299
300         /* Delete the edge */
301
302         edge_del(e);
303
304         /* Run MST before or after we tell the rest? */
305
306         graph();
307
308         /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
309
310         if(!to->status.reachable) {
311                 e = lookup_edge(to, myself);
312
313                 if(e) {
314                         if(!tunnelserver) {
315                                 send_del_edge(everyone, e);
316                         }
317
318                         edge_del(e);
319                 }
320         }
321
322         return true;
323 }