Add a better autoconf check for libevent.
[tinc] / src / protocol_edge.c
1 /*
2     protocol_edge.c -- handle the meta-protocol, edges
3     Copyright (C) 1999-2005 Ivo Timmermans,
4                   2000-2006 Guus Sliepen <guus@tinc-vpn.org>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     $Id$
21 */
22
23 #include "system.h"
24
25 #include "splay_tree.h"
26 #include "conf.h"
27 #include "connection.h"
28 #include "edge.h"
29 #include "graph.h"
30 #include "logger.h"
31 #include "meta.h"
32 #include "net.h"
33 #include "netutl.h"
34 #include "node.h"
35 #include "protocol.h"
36 #include "utils.h"
37 #include "xalloc.h"
38
39 bool send_add_edge(connection_t *c, const edge_t *e) {
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, char *request) {
57         edge_t *e;
58         node_t *from, *to;
59         char from_name[MAX_STRING_SIZE];
60         char to_name[MAX_STRING_SIZE];
61         char to_address[MAX_STRING_SIZE];
62         char to_port[MAX_STRING_SIZE];
63         sockaddr_t address;
64         long int options;
65         int weight;
66
67         cp();
68
69         if(sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %lx %d",
70                           from_name, to_name, to_address, to_port, &options, &weight) != 6) {
71                 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_EDGE", c->name,
72                            c->hostname);
73                 return false;
74         }
75
76         /* Check if names are valid */
77
78         if(!check_id(from_name)) {
79                 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name,
80                            c->hostname, _("invalid name"));
81                 return false;
82         }
83
84         if(!check_id(to_name)) {
85                 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name,
86                            c->hostname, _("invalid name"));
87                 return false;
88         }
89
90         if(seen_request(request))
91                 return true;
92
93         /* Lookup nodes */
94
95         from = lookup_node(from_name);
96         to = lookup_node(to_name);
97
98         if(tunnelserver &&
99            from != myself && from != c->node &&
100            to != myself && to != c->node) {
101                 /* ignore indirect edge registrations for tunnelserver */
102                 ifdebug(PROTOCOL) logger(LOG_WARNING,
103                    _("Ignoring indirect %s from %s (%s)"),
104                    "ADD_EDGE", c->name, c->hostname);
105                 return true;
106         }
107
108         if(!from) {
109                 from = new_node();
110                 from->name = xstrdup(from_name);
111                 node_add(from);
112         }
113
114         if(!to) {
115                 to = new_node();
116                 to->name = xstrdup(to_name);
117                 node_add(to);
118         }
119
120
121         /* Convert addresses */
122
123         address = str2sockaddr(to_address, to_port);
124
125         /* Check if edge already exists */
126
127         e = lookup_edge(from, to);
128
129         if(e) {
130                 if(e->weight != weight || e->options != options || sockaddrcmp(&e->address, &address)) {
131                         if(from == myself) {
132                                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not match existing entry"),
133                                                    "ADD_EDGE", c->name, c->hostname);
134                                 send_add_edge(c, e);
135                                 return true;
136                         } else {
137                                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) which does not match existing entry"),
138                                                    "ADD_EDGE", c->name, c->hostname);
139                                 edge_del(e);
140                                 graph();
141                         }
142                 } else
143                         return true;
144         } else if(from == myself) {
145                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not exist"),
146                                    "ADD_EDGE", c->name, c->hostname);
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         /* Tell the rest about the new edge */
164
165         if(!tunnelserver)
166                 forward_request(c, request);
167
168         /* Run MST before or after we tell the rest? */
169
170         graph();
171
172         return true;
173 }
174
175 bool send_del_edge(connection_t *c, const edge_t *e) {
176         cp();
177
178         return send_request(c, "%d %x %s %s", DEL_EDGE, rand(),
179                                                 e->from->name, e->to->name);
180 }
181
182 bool del_edge_h(connection_t *c, char *request) {
183         edge_t *e;
184         char from_name[MAX_STRING_SIZE];
185         char to_name[MAX_STRING_SIZE];
186         node_t *from, *to;
187
188         cp();
189
190         if(sscanf(request, "%*d %*x "MAX_STRING" "MAX_STRING, from_name, to_name) != 2) {
191                 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_EDGE", c->name,
192                            c->hostname);
193                 return false;
194         }
195
196         /* Check if names are valid */
197
198         if(!check_id(from_name)) {
199                 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name,
200                            c->hostname, _("invalid name"));
201                 return false;
202         }
203
204         if(!check_id(to_name)) {
205                 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name,
206                            c->hostname, _("invalid name"));
207                 return false;
208         }
209
210         if(seen_request(request))
211                 return true;
212
213         /* Lookup nodes */
214
215         from = lookup_node(from_name);
216         to = lookup_node(to_name);
217
218         if(tunnelserver &&
219            from != myself && from != c->node &&
220            to != myself && to != c->node) {
221                 /* ignore indirect edge registrations for tunnelserver */
222                 ifdebug(PROTOCOL) logger(LOG_WARNING,
223                    _("Ignoring indirect %s from %s (%s)"),
224                    "DEL_EDGE", c->name, c->hostname);
225                 return true;
226         }
227
228         if(!from) {
229                 ifdebug(PROTOCOL) logger(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"),
230                                    "DEL_EDGE", c->name, c->hostname);
231                 return true;
232         }
233
234         if(!to) {
235                 ifdebug(PROTOCOL) logger(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"),
236                                    "DEL_EDGE", c->name, c->hostname);
237                 return true;
238         }
239
240         /* Check if edge exists */
241
242         e = lookup_edge(from, to);
243
244         if(!e) {
245                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) which does not appear in the edge tree"),
246                                    "DEL_EDGE", c->name, c->hostname);
247                 return true;
248         }
249
250         if(e->from == myself) {
251                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself"),
252                                    "DEL_EDGE", c->name, c->hostname);
253                 send_add_edge(c, e);    /* Send back a correction */
254                 return true;
255         }
256
257         /* Tell the rest about the deleted edge */
258
259         if(!tunnelserver)
260                 forward_request(c, request);
261
262         /* Delete the edge */
263
264         edge_del(e);
265
266         /* Run MST before or after we tell the rest? */
267
268         graph();
269
270         /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
271
272         if(!to->status.reachable) {
273                 e = lookup_edge(to, myself);
274                 if(e) {
275                         if(!tunnelserver)
276                                 send_del_edge(broadcast, e);
277                         edge_del(e);
278                 }
279         }
280
281         return true;
282 }