Fix spelling errors found by codespell.
[tinc] / src / protocol_edge.c
1 /*
2     protocol_edge.c -- handle the meta-protocol, edges
3     Copyright (C) 1999-2005 Ivo Timmermans,
4                   2000-2016 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         sockaddr2str(&e->address, &address, &port);
43
44         x = send_request(c, "%d %x %s %s %s %s %x %d", ADD_EDGE, rand(),
45                          e->from->name, e->to->name, address, port,
46                          e->options, e->weight);
47         free(address);
48         free(port);
49
50         return x;
51 }
52
53 bool add_edge_h(connection_t *c) {
54         edge_t *e;
55         node_t *from, *to;
56         char from_name[MAX_STRING_SIZE];
57         char to_name[MAX_STRING_SIZE];
58         char to_address[MAX_STRING_SIZE];
59         char to_port[MAX_STRING_SIZE];
60         sockaddr_t address;
61         uint32_t options;
62         int weight;
63
64         if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %x %d",
65                         from_name, to_name, to_address, to_port, &options, &weight) != 6) {
66                 logger(LOG_ERR, "Got bad %s from %s (%s)", "ADD_EDGE", c->name,
67                        c->hostname);
68                 return false;
69         }
70
71         /* Check if names are valid */
72
73         if(!check_id(from_name) || !check_id(to_name) || !strcmp(from_name, to_name)) {
74                 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "ADD_EDGE", c->name,
75                        c->hostname, "invalid name");
76                 return false;
77         }
78
79         if(seen_request(c->buffer)) {
80                 return true;
81         }
82
83         /* Lookup nodes */
84
85         from = lookup_node(from_name);
86         to = lookup_node(to_name);
87
88         if(tunnelserver &&
89                         from != myself && from != c->node &&
90                         to != myself && to != c->node) {
91                 /* ignore indirect edge registrations for tunnelserver */
92                 ifdebug(PROTOCOL) logger(LOG_WARNING,
93                                          "Ignoring indirect %s from %s (%s)",
94                                          "ADD_EDGE", c->name, c->hostname);
95                 return true;
96         }
97
98         if(!from) {
99                 from = new_node();
100                 from->name = xstrdup(from_name);
101                 node_add(from);
102         }
103
104         if(!to) {
105                 to = new_node();
106                 to->name = xstrdup(to_name);
107                 node_add(to);
108         }
109
110
111         /* Convert addresses */
112
113         address = str2sockaddr(to_address, to_port);
114
115         /* Check if edge already exists */
116
117         e = lookup_edge(from, to);
118
119         if(e) {
120                 if(e->weight != weight || e->options != options || sockaddrcmp(&e->address, &address)) {
121                         if(from == myself) {
122                                 ifdebug(PROTOCOL) logger(LOG_WARNING, "Got %s from %s (%s) for ourself which does not match existing entry",
123                                                          "ADD_EDGE", c->name, c->hostname);
124                                 send_add_edge(c, e);
125                                 return true;
126                         } else {
127                                 ifdebug(PROTOCOL) logger(LOG_WARNING, "Got %s from %s (%s) which does not match existing entry",
128                                                          "ADD_EDGE", c->name, c->hostname);
129                                 e->options = options;
130
131                                 if(sockaddrcmp(&e->address, &address)) {
132                                         sockaddrfree(&e->address);
133                                         e->address = address;
134                                 }
135
136                                 if(e->weight != weight) {
137                                         avl_node_t *node = avl_unlink(edge_weight_tree, e);
138                                         e->weight = weight;
139                                         avl_insert_node(edge_weight_tree, node);
140                                 }
141
142                                 goto done;
143                         }
144                 } else {
145                         return true;
146                 }
147         } else if(from == myself) {
148                 ifdebug(PROTOCOL) logger(LOG_WARNING, "Got %s from %s (%s) for ourself which does not exist",
149                                          "ADD_EDGE", c->name, c->hostname);
150                 contradicting_add_edge++;
151                 e = new_edge();
152                 e->from = from;
153                 e->to = to;
154                 send_del_edge(c, e);
155                 free_edge(e);
156                 return true;
157         }
158
159         e = new_edge();
160         e->from = from;
161         e->to = to;
162         e->address = address;
163         e->options = options;
164         e->weight = weight;
165         edge_add(e);
166
167 done:
168         /* Tell the rest about the new edge */
169
170         if(!tunnelserver) {
171                 forward_request(c);
172         }
173
174         /* Run MST before or after we tell the rest? */
175
176         graph();
177
178         return true;
179 }
180
181 bool send_del_edge(connection_t *c, const edge_t *e) {
182         return send_request(c, "%d %x %s %s", DEL_EDGE, rand(),
183                             e->from->name, e->to->name);
184 }
185
186 bool del_edge_h(connection_t *c) {
187         edge_t *e;
188         char from_name[MAX_STRING_SIZE];
189         char to_name[MAX_STRING_SIZE];
190         node_t *from, *to;
191
192         if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING, from_name, to_name) != 2) {
193                 logger(LOG_ERR, "Got bad %s from %s (%s)", "DEL_EDGE", c->name,
194                        c->hostname);
195                 return false;
196         }
197
198         /* Check if names are valid */
199
200         if(!check_id(from_name) || !check_id(to_name) || !strcmp(from_name, to_name)) {
201                 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "DEL_EDGE", c->name,
202                        c->hostname, "invalid name");
203                 return false;
204         }
205
206         if(seen_request(c->buffer)) {
207                 return true;
208         }
209
210         /* Lookup nodes */
211
212         from = lookup_node(from_name);
213         to = lookup_node(to_name);
214
215         if(tunnelserver &&
216                         from != myself && from != c->node &&
217                         to != myself && to != c->node) {
218                 /* ignore indirect edge registrations for tunnelserver */
219                 ifdebug(PROTOCOL) logger(LOG_WARNING,
220                                          "Ignoring indirect %s from %s (%s)",
221                                          "DEL_EDGE", c->name, c->hostname);
222                 return true;
223         }
224
225         if(!from) {
226                 ifdebug(PROTOCOL) logger(LOG_ERR, "Got %s from %s (%s) which does not appear in the edge tree",
227                                          "DEL_EDGE", c->name, c->hostname);
228                 return true;
229         }
230
231         if(!to) {
232                 ifdebug(PROTOCOL) logger(LOG_ERR, "Got %s from %s (%s) which does not appear in the edge tree",
233                                          "DEL_EDGE", c->name, c->hostname);
234                 return true;
235         }
236
237         /* Check if edge exists */
238
239         e = lookup_edge(from, to);
240
241         if(!e) {
242                 ifdebug(PROTOCOL) logger(LOG_WARNING, "Got %s from %s (%s) which does not appear in the edge tree",
243                                          "DEL_EDGE", c->name, c->hostname);
244                 return true;
245         }
246
247         if(e->from == myself) {
248                 ifdebug(PROTOCOL) logger(LOG_WARNING, "Got %s from %s (%s) for ourself",
249                                          "DEL_EDGE", c->name, c->hostname);
250                 contradicting_del_edge++;
251                 send_add_edge(c, e);    /* Send back a correction */
252                 return true;
253         }
254
255         /* Tell the rest about the deleted edge */
256
257         if(!tunnelserver) {
258                 forward_request(c);
259         }
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
274                 if(e) {
275                         if(!tunnelserver) {
276                                 send_del_edge(everyone, e);
277                         }
278
279                         edge_del(e);
280                 }
281         }
282
283         return true;
284 }