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