Remove checkpoint tracing.
[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         sockaddr2str(&e->address, &address, &port);
43
44         x = send_request(c, "%d %x %s %s %s %s %lx %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         long int options;
62         int weight;
63
64         if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %lx %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)) {
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(!check_id(to_name)) {
80                 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name,
81                            c->hostname, _("invalid name"));
82                 return false;
83         }
84
85         if(seen_request(c->buffer))
86                 return true;
87
88         /* Lookup nodes */
89
90         from = lookup_node(from_name);
91         to = lookup_node(to_name);
92
93         if(tunnelserver &&
94            from != myself && from != c->node &&
95            to != myself && to != c->node) {
96                 /* ignore indirect edge registrations for tunnelserver */
97                 ifdebug(PROTOCOL) logger(LOG_WARNING,
98                    _("Ignoring indirect %s from %s (%s)"),
99                    "ADD_EDGE", c->name, c->hostname);
100                 return true;
101         }
102
103         if(!from) {
104                 from = new_node();
105                 from->name = xstrdup(from_name);
106                 node_add(from);
107         }
108
109         if(!to) {
110                 to = new_node();
111                 to->name = xstrdup(to_name);
112                 node_add(to);
113         }
114
115
116         /* Convert addresses */
117
118         address = str2sockaddr(to_address, to_port);
119
120         /* Check if edge already exists */
121
122         e = lookup_edge(from, to);
123
124         if(e) {
125                 if(e->weight != weight || e->options != options || sockaddrcmp(&e->address, &address)) {
126                         if(from == myself) {
127                                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not match existing entry"),
128                                                    "ADD_EDGE", c->name, c->hostname);
129                                 send_add_edge(c, e);
130                                 return true;
131                         } else {
132                                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) which does not match existing entry"),
133                                                    "ADD_EDGE", c->name, c->hostname);
134                                 edge_del(e);
135                                 graph();
136                         }
137                 } else
138                         return true;
139         } else if(from == myself) {
140                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not exist"),
141                                    "ADD_EDGE", c->name, c->hostname);
142                 e = new_edge();
143                 e->from = from;
144                 e->to = to;
145                 send_del_edge(c, e);
146                 free_edge(e);
147                 return true;
148         }
149
150         e = new_edge();
151         e->from = from;
152         e->to = to;
153         e->address = address;
154         e->options = options;
155         e->weight = weight;
156         edge_add(e);
157
158         /* Tell the rest about the new edge */
159
160         if(!tunnelserver)
161                 forward_request(c);
162
163         /* Run MST before or after we tell the rest? */
164
165         graph();
166
167         return true;
168 }
169
170 bool send_del_edge(connection_t *c, const edge_t *e) {
171         return send_request(c, "%d %x %s %s", DEL_EDGE, rand(),
172                                                 e->from->name, e->to->name);
173 }
174
175 bool del_edge_h(connection_t *c) {
176         edge_t *e;
177         char from_name[MAX_STRING_SIZE];
178         char to_name[MAX_STRING_SIZE];
179         node_t *from, *to;
180
181         if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING, from_name, to_name) != 2) {
182                 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_EDGE", c->name,
183                            c->hostname);
184                 return false;
185         }
186
187         /* Check if names are valid */
188
189         if(!check_id(from_name)) {
190                 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name,
191                            c->hostname, _("invalid name"));
192                 return false;
193         }
194
195         if(!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                 send_add_edge(c, e);    /* Send back a correction */
245                 return true;
246         }
247
248         /* Tell the rest about the deleted edge */
249
250         if(!tunnelserver)
251                 forward_request(c);
252
253         /* Delete the edge */
254
255         edge_del(e);
256
257         /* Run MST before or after we tell the rest? */
258
259         graph();
260
261         /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
262
263         if(!to->status.reachable) {
264                 e = lookup_edge(to, myself);
265                 if(e) {
266                         if(!tunnelserver)
267                                 send_del_edge(broadcast, e);
268                         edge_del(e);
269                 }
270         }
271
272         return true;
273 }