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