K&R style braces
[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 "avl_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 %lx %s %s %s %s %lx %d", ADD_EDGE, random(),
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         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(c->buffer, "%*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(c->buffer))
91                 return true;
92
93         /* Lookup nodes */
94
95         from = lookup_node(from_name);
96
97         if(!from) {
98                 from = new_node();
99                 from->name = xstrdup(from_name);
100                 node_add(from);
101         }
102
103         to = lookup_node(to_name);
104
105         if(!to) {
106                 to = new_node();
107                 to->name = xstrdup(to_name);
108                 node_add(to);
109         }
110
111         if(tunnelserver && from != myself && from != c->node && to != myself && to != c->node)
112                 return false;
113
114         /* Convert addresses */
115
116         address = str2sockaddr(to_address, to_port);
117
118         /* Check if edge already exists */
119
120         e = lookup_edge(from, to);
121
122         if(e) {
123                 if(e->weight != weight || e->options != options || sockaddrcmp(&e->address, &address)) {
124                         if(from == myself) {
125                                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not match existing entry"),
126                                                    "ADD_EDGE", c->name, c->hostname);
127                                 send_add_edge(c, e);
128                                 return true;
129                         } else {
130                                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) which does not match existing entry"),
131                                                    "ADD_EDGE", c->name, c->hostname);
132                                 edge_del(e);
133                                 graph();
134                         }
135                 } else
136                         return true;
137         } else if(from == myself) {
138                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not exist"),
139                                    "ADD_EDGE", c->name, c->hostname);
140                 e = new_edge();
141                 e->from = from;
142                 e->to = to;
143                 send_del_edge(c, e);
144                 free_edge(e);
145                 return true;
146         }
147
148         e = new_edge();
149         e->from = from;
150         e->to = to;
151         e->address = address;
152         e->options = options;
153         e->weight = weight;
154         edge_add(e);
155
156         /* Tell the rest about the new edge */
157
158         if(!tunnelserver)
159                 forward_request(c);
160
161         /* Run MST before or after we tell the rest? */
162
163         graph();
164
165         return true;
166 }
167
168 bool send_del_edge(connection_t *c, const edge_t *e) {
169         cp();
170
171         return send_request(c, "%d %lx %s %s", DEL_EDGE, random(),
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         cp();
182
183         if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING, from_name, to_name) != 2) {
184                 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_EDGE", c->name,
185                            c->hostname);
186                 return false;
187         }
188
189         /* Check if names are valid */
190
191         if(!check_id(from_name)) {
192                 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name,
193                            c->hostname, _("invalid name"));
194                 return false;
195         }
196
197         if(!check_id(to_name)) {
198                 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name,
199                            c->hostname, _("invalid name"));
200                 return false;
201         }
202
203         if(seen_request(c->buffer))
204                 return true;
205
206         /* Lookup nodes */
207
208         from = lookup_node(from_name);
209
210         if(!from) {
211                 ifdebug(PROTOCOL) logger(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"),
212                                    "DEL_EDGE", c->name, c->hostname);
213                 return true;
214         }
215
216         to = lookup_node(to_name);
217
218         if(!to) {
219                 ifdebug(PROTOCOL) logger(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"),
220                                    "DEL_EDGE", c->name, c->hostname);
221                 return true;
222         }
223
224         if(tunnelserver && from != myself && from != c->node && to != myself && to != c->node)
225                 return false;
226
227         /* Check if edge exists */
228
229         e = lookup_edge(from, to);
230
231         if(!e) {
232                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("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(e->from == myself) {
238                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself"),
239                                    "DEL_EDGE", c->name, c->hostname);
240                 send_add_edge(c, e);    /* Send back a correction */
241                 return true;
242         }
243
244         /* Tell the rest about the deleted edge */
245
246         if(!tunnelserver)
247                 forward_request(c);
248
249         /* Delete the edge */
250
251         edge_del(e);
252
253         /* Run MST before or after we tell the rest? */
254
255         graph();
256
257         /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
258
259         if(!to->status.reachable) {
260                 e = lookup_edge(to, myself);
261                 if(e) {
262                         if(!tunnelserver)
263                                 send_del_edge(broadcast, e);
264                         edge_del(e);
265                 }
266         }
267
268         return true;
269 }