7663a46fefa11133c5854f5e8a3fa5bd56eda95b
[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.10 2002/09/04 16:26:45 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 cp
53   sockaddr2str(&e->address, &address, &port);
54   x = send_request(c, "%d %lx %s %s %s %s %lx %d", ADD_EDGE, random(),
55                       e->from->name, e->to->name, address, port,
56                       e->options, e->weight);
57   free(address);
58   free(port);
59 cp
60   return x;
61 }
62
63 int add_edge_h(connection_t *c)
64 {
65   edge_t *e;
66   node_t *from, *to;
67   char from_name[MAX_STRING_SIZE];
68   char to_name[MAX_STRING_SIZE];
69   char to_address[MAX_STRING_SIZE];
70   char to_port[MAX_STRING_SIZE];
71   sockaddr_t address;
72   long int options;
73   int weight;
74 cp
75   if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %lx %d",
76             from_name, to_name, to_address, to_port, &options, &weight) != 6)
77     {
78        syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_EDGE", c->name, c->hostname);
79        return -1;
80     }
81
82   /* Check if names are valid */
83
84   if(check_id(from_name))
85     {
86       syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("invalid name"));
87       return -1;
88     }
89
90   if(check_id(to_name))
91     {
92       syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("invalid name"));
93       return -1;
94     }
95
96   if(seen_request(c->buffer))
97     return 0;
98
99   /* Lookup nodes */
100
101   from = lookup_node(from_name);
102   
103   if(!from)
104     {
105       from = new_node();
106       from->name = xstrdup(from_name);
107       node_add(from);
108     }
109
110   to = lookup_node(to_name);
111   
112   if(!to)
113     {
114       to = new_node();
115       to->name = xstrdup(to_name);
116       node_add(to);
117     }
118
119   /* Convert addresses */
120   
121   address = str2sockaddr(to_address, to_port);
122
123   /* Check if edge already exists */
124   
125   e = lookup_edge(from, to);
126   
127   if(e)
128   {
129     if(e->weight != weight || e->options != options || sockaddrcmp(&e->address, &address))
130     {
131       if(from == myself)
132       {
133         if(debug_lvl >= DEBUG_PROTOCOL)
134           syslog(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not match existing entry"), "ADD_EDGE", c->name, c->hostname);
135         send_add_edge(c, e);
136         return 0;
137       }
138       else
139       {
140         if(debug_lvl >= DEBUG_PROTOCOL)
141           syslog(LOG_WARNING, _("Got %s from %s (%s) which does not match existing entry"), "ADD_EDGE", c->name, c->hostname);
142         edge_del(e);
143       }
144     }
145     else
146       return 0;
147   }
148   else if(from == myself)
149   {
150     if(debug_lvl >= DEBUG_PROTOCOL)
151       syslog(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not exist"), "ADD_EDGE", c->name, c->hostname);
152     e = new_edge();
153     e->from = from;
154     e->to = to;
155     send_del_edge(c, e);
156     free_edge(e);
157     return 0;
158   }
159
160   e = new_edge();
161   e->from = from;
162   e->to = to;
163   e->address = address;
164   e->options = options;
165   e->weight = weight;
166   edge_add(e);
167
168   /* Tell the rest about the new edge */
169
170   forward_request(c);
171
172   /* Run MST before or after we tell the rest? */
173
174   graph();
175 cp
176   return 0;
177 }
178
179 int send_del_edge(connection_t *c, edge_t *e)
180 {
181 cp
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 cp
193   if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING"", from_name, to_name) != 2)
194     {
195       syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_EDGE",
196              c->name, c->hostname);
197       return -1;
198     }
199
200   /* Check if names are valid */
201
202   if(check_id(from_name))
203     {
204       syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("invalid name"));
205       return -1;
206     }
207
208   if(check_id(to_name))
209     {
210       syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, 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     {
223       if(debug_lvl >= DEBUG_PROTOCOL)
224         syslog(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"), "DEL_EDGE", c->name, c->hostname);
225       return 0;
226     }
227
228   to = lookup_node(to_name);
229   
230   if(!to)
231     {
232       if(debug_lvl >= DEBUG_PROTOCOL)
233         syslog(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"), "DEL_EDGE", c->name, c->hostname);
234       return 0;
235     }
236
237   /* Check if edge exists */
238   
239   e = lookup_edge(from, to);
240   
241   if(!e)
242   {
243     if(debug_lvl >= DEBUG_PROTOCOL)
244       syslog(LOG_WARNING, _("Got %s from %s (%s) which does not appear in the edge tree"), "DEL_EDGE", c->name, c->hostname);
245     return 0;
246   }
247
248   if(e->from == myself)
249   {
250     if(debug_lvl >= DEBUG_PROTOCOL)
251       syslog(LOG_WARNING, _("Got %s from %s (%s) for ourself"), "DEL_EDGE", c->name, c->hostname);
252     send_add_edge(c, e); /* Send back a correction */
253     return 0;
254   }
255
256   /* Tell the rest about the deleted edge */
257
258   forward_request(c);
259
260   /* Delete the edge */
261   
262   edge_del(e);
263
264   /* Run MST before or after we tell the rest? */
265
266   graph();
267 cp
268   return 0;
269 }