Run graph() after edge_del() when updating an edge.
[tinc] / src / protocol_edge.c
1 /*
2     protocol_edge.c -- handle the meta-protocol, edges
3     Copyright (C) 1999-2002 Ivo Timmermans <ivo@o2w.nl>,
4                   2000-2002 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.15 2002/09/24 11:43:34 guus Exp $
21 */
22
23 #include "config.h"
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <syslog.h>
28 #include <stdio.h>
29 #include <stdarg.h>
30 #include <errno.h>
31
32 #include <utils.h>
33 #include <xalloc.h>
34 #include <avl_tree.h>
35
36 #include "conf.h"
37 #include "net.h"
38 #include "netutl.h"
39 #include "protocol.h"
40 #include "meta.h"
41 #include "connection.h"
42 #include "node.h"
43 #include "edge.h"
44 #include "graph.h"
45
46 #include "system.h"
47
48 int send_add_edge(connection_t *c, edge_t *e)
49 {
50         int x;
51         char *address, *port;
52
53         cp();
54
55         sockaddr2str(&e->address, &address, &port);
56
57         x = send_request(c, "%d %lx %s %s %s %s %lx %d", ADD_EDGE, random(),
58                                          e->from->name, e->to->name, address, port,
59                                          e->options, e->weight);
60         free(address);
61         free(port);
62
63         return x;
64 }
65
66 int add_edge_h(connection_t *c)
67 {
68         edge_t *e;
69         node_t *from, *to;
70         char from_name[MAX_STRING_SIZE];
71         char to_name[MAX_STRING_SIZE];
72         char to_address[MAX_STRING_SIZE];
73         char to_port[MAX_STRING_SIZE];
74         sockaddr_t address;
75         long int options;
76         int weight;
77
78         cp();
79
80         if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %lx %d",
81                           from_name, to_name, to_address, to_port, &options, &weight) != 6) {
82                 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_EDGE", c->name,
83                            c->hostname);
84                 return -1;
85         }
86
87         /* Check if names are valid */
88
89         if(check_id(from_name)) {
90                 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name,
91                            c->hostname, _("invalid name"));
92                 return -1;
93         }
94
95         if(check_id(to_name)) {
96                 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name,
97                            c->hostname, _("invalid name"));
98                 return -1;
99         }
100
101         if(seen_request(c->buffer))
102                 return 0;
103
104         /* Lookup nodes */
105
106         from = lookup_node(from_name);
107
108         if(!from) {
109                 from = new_node();
110                 from->name = xstrdup(from_name);
111                 node_add(from);
112         }
113
114         to = lookup_node(to_name);
115
116         if(!to) {
117                 to = new_node();
118                 to->name = xstrdup(to_name);
119                 node_add(to);
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                                 if(debug_lvl >= DEBUG_PROTOCOL)
134                                         syslog(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 0;
138                         } else {
139                                 if(debug_lvl >= DEBUG_PROTOCOL)
140                                         syslog(LOG_WARNING, _("Got %s from %s (%s) which does not match existing entry"),
141                                                    "ADD_EDGE", c->name, c->hostname);
142                                 edge_del(e);
143                                 graph();
144                         }
145                 } else
146                         return 0;
147         } else if(from == myself) {
148                 if(debug_lvl >= DEBUG_PROTOCOL)
149                         syslog(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not exist"),
150                                    "ADD_EDGE", c->name, c->hostname);
151                 e = new_edge();
152                 e->from = from;
153                 e->to = to;
154                 send_del_edge(c, e);
155                 free_edge(e);
156                 return 0;
157         }
158
159         e = new_edge();
160         e->from = from;
161         e->to = to;
162         e->address = address;
163         e->options = options;
164         e->weight = weight;
165         edge_add(e);
166
167         /* Tell the rest about the new edge */
168
169         forward_request(c);
170
171         /* Run MST before or after we tell the rest? */
172
173         graph();
174
175         return 0;
176 }
177
178 int send_del_edge(connection_t *c, edge_t *e)
179 {
180         cp();
181
182         return send_request(c, "%d %lx %s %s", DEL_EDGE, random(),
183                                                 e->from->name, e->to->name);
184 }
185
186 int del_edge_h(connection_t *c)
187 {
188         edge_t *e;
189         char from_name[MAX_STRING_SIZE];
190         char to_name[MAX_STRING_SIZE];
191         node_t *from, *to;
192
193         cp();
194
195         if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING, from_name, to_name) != 2) {
196                 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_EDGE", c->name,
197                            c->hostname);
198                 return -1;
199         }
200
201         /* Check if names are valid */
202
203         if(check_id(from_name)) {
204                 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name,
205                            c->hostname, _("invalid name"));
206                 return -1;
207         }
208
209         if(check_id(to_name)) {
210                 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name,
211                            c->hostname, _("invalid name"));
212                 return -1;
213         }
214
215         if(seen_request(c->buffer))
216                 return 0;
217
218         /* Lookup nodes */
219
220         from = lookup_node(from_name);
221
222         if(!from) {
223                 if(debug_lvl >= DEBUG_PROTOCOL)
224                         syslog(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"),
225                                    "DEL_EDGE", c->name, c->hostname);
226                 return 0;
227         }
228
229         to = lookup_node(to_name);
230
231         if(!to) {
232                 if(debug_lvl >= DEBUG_PROTOCOL)
233                         syslog(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"),
234                                    "DEL_EDGE", c->name, c->hostname);
235                 return 0;
236         }
237
238         /* Check if edge exists */
239
240         e = lookup_edge(from, to);
241
242         if(!e) {
243                 if(debug_lvl >= DEBUG_PROTOCOL)
244                         syslog(LOG_WARNING, _("Got %s from %s (%s) which does not appear in the edge tree"),
245                                    "DEL_EDGE", c->name, c->hostname);
246                 return 0;
247         }
248
249         if(e->from == myself) {
250                 if(debug_lvl >= DEBUG_PROTOCOL)
251                         syslog(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 0;
255         }
256
257         /* Tell the rest about the deleted edge */
258
259         forward_request(c);
260
261         /* Delete the edge */
262
263         edge_del(e);
264
265         /* Run MST before or after we tell the rest? */
266
267         graph();
268
269         return 0;
270 }