Really remove use of __DATE__ and __TIME__ to facilitate reproducible builds.
[oweals/tinc.git] / 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)) {
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         /* Lookup nodes */
83
84         from = lookup_node(from_name);
85         to = lookup_node(to_name);
86
87         if(tunnelserver &&
88            from != myself && from != c->node &&
89            to != myself && to != c->node) {
90                 /* ignore indirect edge registrations for tunnelserver */
91                 ifdebug(PROTOCOL) logger(LOG_WARNING,
92                    "Ignoring indirect %s from %s (%s)",
93                    "ADD_EDGE", c->name, c->hostname);
94                 return true;
95         }
96
97         if(!from) {
98                 from = new_node();
99                 from->name = xstrdup(from_name);
100                 node_add(from);
101         }
102
103         if(!to) {
104                 to = new_node();
105                 to->name = xstrdup(to_name);
106                 node_add(to);
107         }
108
109
110         /* Convert addresses */
111
112         address = str2sockaddr(to_address, to_port);
113
114         /* Check if edge already exists */
115
116         e = lookup_edge(from, to);
117
118         if(e) {
119                 if(e->weight != weight || e->options != options || sockaddrcmp(&e->address, &address)) {
120                         if(from == myself) {
121                                 ifdebug(PROTOCOL) logger(LOG_WARNING, "Got %s from %s (%s) for ourself which does not match existing entry",
122                                                    "ADD_EDGE", c->name, c->hostname);
123                                 send_add_edge(c, e);
124                                 return true;
125                         } else {
126                                 ifdebug(PROTOCOL) logger(LOG_WARNING, "Got %s from %s (%s) which does not match existing entry",
127                                                    "ADD_EDGE", c->name, c->hostname);
128                                 e->options = options;
129                                 if(sockaddrcmp(&e->address, &address)) {
130                                         sockaddrfree(&e->address);
131                                         e->address = address;
132                                 }
133                                 if(e->weight != weight) {
134                                         avl_node_t *node = avl_unlink(edge_weight_tree, e);
135                                         e->weight = weight;
136                                         avl_insert_node(edge_weight_tree, node);
137                                 }
138
139                                 goto done;
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                 contradicting_add_edge++;
147                 e = new_edge();
148                 e->from = from;
149                 e->to = to;
150                 send_del_edge(c, e);
151                 free_edge(e);
152                 return true;
153         }
154
155         e = new_edge();
156         e->from = from;
157         e->to = to;
158         e->address = address;
159         e->options = options;
160         e->weight = weight;
161         edge_add(e);
162
163 done:
164         /* Tell the rest about the new edge */
165
166         if(!tunnelserver)
167                 forward_request(c);
168
169         /* Run MST before or after we tell the rest? */
170
171         graph();
172
173         return true;
174 }
175
176 bool send_del_edge(connection_t *c, const edge_t *e) {
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         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) || !check_id(to_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(seen_request(c->buffer))
202                 return true;
203
204         /* Lookup nodes */
205
206         from = lookup_node(from_name);
207         to = lookup_node(to_name);
208
209         if(tunnelserver &&
210            from != myself && from != c->node &&
211            to != myself && to != c->node) {
212                 /* ignore indirect edge registrations for tunnelserver */
213                 ifdebug(PROTOCOL) logger(LOG_WARNING,
214                    "Ignoring indirect %s from %s (%s)",
215                    "DEL_EDGE", c->name, c->hostname);
216                 return true;
217         }
218
219         if(!from) {
220                 ifdebug(PROTOCOL) logger(LOG_ERR, "Got %s from %s (%s) which does not appear in the edge tree",
221                                    "DEL_EDGE", c->name, c->hostname);
222                 return true;
223         }
224
225         if(!to) {
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         /* 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                 contradicting_del_edge++;
245                 send_add_edge(c, e);    /* Send back a correction */
246                 return true;
247         }
248
249         /* Tell the rest about the deleted edge */
250
251         if(!tunnelserver)
252                 forward_request(c);
253
254         /* Delete the edge */
255
256         edge_del(e);
257
258         /* Run MST before or after we tell the rest? */
259
260         graph();
261
262         /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
263
264         if(!to->status.reachable) {
265                 e = lookup_edge(to, myself);
266                 if(e) {
267                         if(!tunnelserver)
268                                 send_del_edge(everyone, e);
269                         edge_del(e);
270                 }
271         }
272
273         return true;
274 }