Switch to K&R style indentation.
[oweals/tinc.git] / 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.12 2002/09/09 21:24:48 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                         }
144                 } else
145                         return 0;
146         } else if(from == myself) {
147                 if(debug_lvl >= DEBUG_PROTOCOL)
148                         syslog(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not exist"),
149                                    "ADD_EDGE", c->name, c->hostname);
150                 e = new_edge();
151                 e->from = from;
152                 e->to = to;
153                 send_del_edge(c, e);
154                 free_edge(e);
155                 return 0;
156         }
157
158         e = new_edge();
159         e->from = from;
160         e->to = to;
161         e->address = address;
162         e->options = options;
163         e->weight = weight;
164         edge_add(e);
165
166         /* Tell the rest about the new edge */
167
168         forward_request(c);
169
170         /* Run MST before or after we tell the rest? */
171
172         graph();
173
174         return 0;
175 }
176
177 int send_del_edge(connection_t * c, 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 int 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                 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_EDGE", c->name,
196                            c->hostname);
197                 return -1;
198         }
199
200         /* Check if names are valid */
201
202         if(check_id(from_name)) {
203                 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name,
204                            c->hostname, _("invalid name"));
205                 return -1;
206         }
207
208         if(check_id(to_name)) {
209                 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name,
210                            c->hostname, _("invalid name"));
211                 return -1;
212         }
213
214         if(seen_request(c->buffer))
215                 return 0;
216
217         /* Lookup nodes */
218
219         from = lookup_node(from_name);
220
221         if(!from) {
222                 if(debug_lvl >= DEBUG_PROTOCOL)
223                         syslog(LOG_ERR,
224                                    _
225                                    ("Got %s from %s (%s) which does not appear in the edge tree"),
226                                    "DEL_EDGE", c->name, c->hostname);
227                 return 0;
228         }
229
230         to = lookup_node(to_name);
231
232         if(!to) {
233                 if(debug_lvl >= DEBUG_PROTOCOL)
234                         syslog(LOG_ERR,
235                                    _
236                                    ("Got %s from %s (%s) which does not appear in the edge tree"),
237                                    "DEL_EDGE", c->name, c->hostname);
238                 return 0;
239         }
240
241         /* Check if edge exists */
242
243         e = lookup_edge(from, to);
244
245         if(!e) {
246                 if(debug_lvl >= DEBUG_PROTOCOL)
247                         syslog(LOG_WARNING,
248                                    _
249                                    ("Got %s from %s (%s) which does not appear in the edge tree"),
250                                    "DEL_EDGE", c->name, c->hostname);
251                 return 0;
252         }
253
254         if(e->from == myself) {
255                 if(debug_lvl >= DEBUG_PROTOCOL)
256                         syslog(LOG_WARNING, _("Got %s from %s (%s) for ourself"),
257                                    "DEL_EDGE", c->name, c->hostname);
258                 send_add_edge(c, e);    /* Send back a correction */
259                 return 0;
260         }
261
262         /* Tell the rest about the deleted edge */
263
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         return 0;
275 }