Simplify logging, update copyrights and some minor cleanups.
[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.17 2003/07/12 17:41:47 guus Exp $
21 */
22
23 #include "config.h"
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <stdarg.h>
29 #include <errno.h>
30
31 #include <utils.h>
32 #include <xalloc.h>
33 #include <avl_tree.h>
34
35 #include "conf.h"
36 #include "net.h"
37 #include "netutl.h"
38 #include "protocol.h"
39 #include "meta.h"
40 #include "connection.h"
41 #include "node.h"
42 #include "edge.h"
43 #include "graph.h"
44 #include "logger.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                 logger(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                 logger(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                 logger(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                                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not match existing entry"),
134                                                    "ADD_EDGE", c->name, c->hostname);
135                                 send_add_edge(c, e);
136                                 return 0;
137                         } else {
138                                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) which does not match existing entry"),
139                                                    "ADD_EDGE", c->name, c->hostname);
140                                 edge_del(e);
141                                 graph();
142                         }
143                 } else
144                         return 0;
145         } else if(from == myself) {
146                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not exist"),
147                                    "ADD_EDGE", c->name, c->hostname);
148                 e = new_edge();
149                 e->from = from;
150                 e->to = to;
151                 send_del_edge(c, e);
152                 free_edge(e);
153                 return 0;
154         }
155
156         e = new_edge();
157         e->from = from;
158         e->to = to;
159         e->address = address;
160         e->options = options;
161         e->weight = weight;
162         edge_add(e);
163
164         /* Tell the rest about the new edge */
165
166         forward_request(c);
167
168         /* Run MST before or after we tell the rest? */
169
170         graph();
171
172         return 0;
173 }
174
175 int send_del_edge(connection_t *c, edge_t *e)
176 {
177         cp();
178
179         return send_request(c, "%d %lx %s %s", DEL_EDGE, random(),
180                                                 e->from->name, e->to->name);
181 }
182
183 int del_edge_h(connection_t *c)
184 {
185         edge_t *e;
186         char from_name[MAX_STRING_SIZE];
187         char to_name[MAX_STRING_SIZE];
188         node_t *from, *to;
189
190         cp();
191
192         if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING, from_name, to_name) != 2) {
193                 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_EDGE", c->name,
194                            c->hostname);
195                 return -1;
196         }
197
198         /* Check if names are valid */
199
200         if(check_id(from_name)) {
201                 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name,
202                            c->hostname, _("invalid name"));
203                 return -1;
204         }
205
206         if(check_id(to_name)) {
207                 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name,
208                            c->hostname, _("invalid name"));
209                 return -1;
210         }
211
212         if(seen_request(c->buffer))
213                 return 0;
214
215         /* Lookup nodes */
216
217         from = lookup_node(from_name);
218
219         if(!from) {
220                 ifdebug(PROTOCOL) logger(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"),
221                                    "DEL_EDGE", c->name, c->hostname);
222                 return 0;
223         }
224
225         to = lookup_node(to_name);
226
227         if(!to) {
228                 ifdebug(PROTOCOL) logger(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"),
229                                    "DEL_EDGE", c->name, c->hostname);
230                 return 0;
231         }
232
233         /* Check if edge exists */
234
235         e = lookup_edge(from, to);
236
237         if(!e) {
238                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) which does not appear in the edge tree"),
239                                    "DEL_EDGE", c->name, c->hostname);
240                 return 0;
241         }
242
243         if(e->from == myself) {
244                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself"),
245                                    "DEL_EDGE", c->name, c->hostname);
246                 send_add_edge(c, e);    /* Send back a correction */
247                 return 0;
248         }
249
250         /* Tell the rest about the deleted edge */
251
252         forward_request(c);
253
254         /* Delete the edge */
255
256         edge_del(e);
257
258         /* Run MST before or after we tell the rest? */
259
260         graph();
261
262         return 0;
263 }