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