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