75b49950dcd2632fb8d8e3c27e7225c37fe5b0d2
[tinc] / src / protocol_edge.c
1 /*
2     protocol_edge.c -- handle the meta-protocol, edges
3     Copyright (C) 1999-2003 Ivo Timmermans <ivo@o2w.nl>,
4                   2000-2003 Guus Sliepen <guus@sliepen.eu.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: protocol_edge.c,v 1.1.4.18 2003/07/17 15:06:26 guus Exp $
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 int send_add_edge(connection_t *c, edge_t *e)
40 {
41         int 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 int 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 -1;
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 -1;
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 -1;
90         }
91
92         if(seen_request(c->buffer))
93                 return 0;
94
95         /* Lookup nodes */
96
97         from = lookup_node(from_name);
98
99         if(!from) {
100                 from = new_node();
101                 from->name = xstrdup(from_name);
102                 node_add(from);
103         }
104
105         to = lookup_node(to_name);
106
107         if(!to) {
108                 to = new_node();
109                 to->name = xstrdup(to_name);
110                 node_add(to);
111         }
112
113         /* Convert addresses */
114
115         address = str2sockaddr(to_address, to_port);
116
117         /* Check if edge already exists */
118
119         e = lookup_edge(from, to);
120
121         if(e) {
122                 if(e->weight != weight || e->options != options || sockaddrcmp(&e->address, &address)) {
123                         if(from == myself) {
124                                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not match existing entry"),
125                                                    "ADD_EDGE", c->name, c->hostname);
126                                 send_add_edge(c, e);
127                                 return 0;
128                         } else {
129                                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) which does not match existing entry"),
130                                                    "ADD_EDGE", c->name, c->hostname);
131                                 edge_del(e);
132                                 graph();
133                         }
134                 } else
135                         return 0;
136         } else if(from == myself) {
137                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not exist"),
138                                    "ADD_EDGE", c->name, c->hostname);
139                 e = new_edge();
140                 e->from = from;
141                 e->to = to;
142                 send_del_edge(c, e);
143                 free_edge(e);
144                 return 0;
145         }
146
147         e = new_edge();
148         e->from = from;
149         e->to = to;
150         e->address = address;
151         e->options = options;
152         e->weight = weight;
153         edge_add(e);
154
155         /* Tell the rest about the new edge */
156
157         forward_request(c);
158
159         /* Run MST before or after we tell the rest? */
160
161         graph();
162
163         return 0;
164 }
165
166 int send_del_edge(connection_t *c, edge_t *e)
167 {
168         cp();
169
170         return send_request(c, "%d %lx %s %s", DEL_EDGE, random(),
171                                                 e->from->name, e->to->name);
172 }
173
174 int del_edge_h(connection_t *c)
175 {
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 -1;
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 -1;
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 -1;
201         }
202
203         if(seen_request(c->buffer))
204                 return 0;
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 0;
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 0;
222         }
223
224         /* Check if edge exists */
225
226         e = lookup_edge(from, to);
227
228         if(!e) {
229                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) which does not appear in the edge tree"),
230                                    "DEL_EDGE", c->name, c->hostname);
231                 return 0;
232         }
233
234         if(e->from == myself) {
235                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself"),
236                                    "DEL_EDGE", c->name, c->hostname);
237                 send_add_edge(c, e);    /* Send back a correction */
238                 return 0;
239         }
240
241         /* Tell the rest about the deleted edge */
242
243         forward_request(c);
244
245         /* Delete the edge */
246
247         edge_del(e);
248
249         /* Run MST before or after we tell the rest? */
250
251         graph();
252
253         return 0;
254 }