Sensible defaults for $INTERFACE.
[tinc] / src / protocol_edge.c
1 /*
2     protocol_edge.c -- handle the meta-protocol, edges
3     Copyright (C) 1999-2002 Ivo Timmermans <itimmermans@bigfoot.com>,
4                   2000-2002 Guus Sliepen <guus@sliepen.warande.net>
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.1 2002/02/11 10:05:58 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 *from_addrstr, *to_addrstr;
52 cp
53   from_addrstr = address2str(e->from.address);
54   to_addrstr = address2str(e->to.address);
55   x = send_request(c, "%d %s %s %hd %s %s %hd %lx %d", ADD_EDGE,
56                       e->from.node->name, from_addrstr, e->from.port,
57                       e->to.node->name, to_addrstr, e->to.port,
58                       e->options, e->weight);
59   free(from_addrstr);
60   free(to_addrstr);
61 cp
62   return x;
63 }
64
65 int add_edge_h(connection_t *c)
66 {
67   connection_t *other;
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 from_addrstr[MAX_STRING_SIZE];
73   char to_addrstr[MAX_STRING_SIZE];
74   ipv4_t from_address, to_address;
75   port_t from_port, to_port;
76   long int options;
77   int weight;
78   avl_node_t *node;
79 cp
80   if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" %hd "MAX_STRING" "MAX_STRING" %hd %lx %d",
81             from_name, from_addrstr, &from_port,
82             to_name, to_addrstr, &to_port,
83             &options, &weight) != 8)
84     {
85        syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_EDGE", c->name, c->hostname);
86        return -1;
87     }
88
89   /* Check if names are valid */
90
91   if(check_id(from_name))
92     {
93       syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("invalid name"));
94       return -1;
95     }
96
97   if(check_id(to_name))
98     {
99       syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("invalid name"));
100       return -1;
101     }
102
103   /* Lookup nodes */
104
105   from = lookup_node(from_name);
106   
107   if(!from)
108     {
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     {
118       to = new_node();
119       to->name = xstrdup(to_name);
120       node_add(to);
121     }
122
123   /* Convert addresses */
124   
125   from_address = str2address(from_addrstr);
126   to_address = str2address(to_addrstr);
127
128   /* Check if edge already exists */
129   
130   e = lookup_edge(from, to);
131   
132   if(e)
133   {
134     if(e->weight != weight || e->options != options
135        || ((e->from.node == from) && (e->from.address != from_address || e->from.port != from_port || e->to.address != to_address || e->to.port != to_port))
136        || ((e->from.node == to) && (e->from.address != to_address || e->from.port != to_port || e->to.address != from_address || e->to.port != from_port))
137       )     
138     {
139       if(from == myself || to == myself)
140       {
141         if(debug_lvl >= DEBUG_PROTOCOL)
142           syslog(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not match existing entry"), "ADD_EDGE", c->name, c->hostname);
143         send_add_edge(c, e);
144         return 0;
145       }
146       else
147       {
148         if(debug_lvl >= DEBUG_PROTOCOL)
149           syslog(LOG_WARNING, _("Got %s from %s (%s) which does not match existing entry"), "ADD_EDGE", c->name, c->hostname);
150         edge_del(e);
151       }
152     }
153     else
154       return 0;
155   }
156   else if(from == myself || to == myself)
157   {
158     if(debug_lvl >= DEBUG_PROTOCOL)
159       syslog(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not exist"), "ADD_EDGE", c->name, c->hostname);
160     e = new_edge();
161     e->from.node = from;
162     e->to.node = to;
163     send_del_edge(c, e);
164     free_edge(e);
165     return 0;
166   }
167
168
169
170   e = new_edge();
171   e->from.node = from;
172   e->from.address = from_address;
173   e->from.port = from_port;
174   e->to.node = to;
175   e->to.address = to_address;
176   e->to.port = to_port;
177   e->options = options;
178   e->weight = weight;
179   edge_add(e);
180
181   /* Tell the rest about the new edge */
182
183   for(node = connection_tree->head; node; node = node->next)
184     {
185       other = (connection_t *)node->data;
186       if(other->status.active && other != c)
187         send_add_edge(other, e);
188     }
189
190   /* Run MST before or after we tell the rest? */
191
192   graph();
193 cp
194   return 0;
195 }
196
197 int send_del_edge(connection_t *c, edge_t *e)
198 {
199 cp
200   return send_request(c, "%d %s %s", DEL_EDGE,
201                       e->from.node->name, e->to.node->name);
202 }
203
204 int del_edge_h(connection_t *c)
205 {
206   edge_t *e;
207   char from_name[MAX_STRING_SIZE];
208   char to_name[MAX_STRING_SIZE];
209   node_t *from, *to;
210   connection_t *other;
211   avl_node_t *node;
212 cp
213   if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING"", from_name, to_name) != 2)
214     {
215       syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_EDGE",
216              c->name, c->hostname);
217       return -1;
218     }
219
220   /* Check if names are valid */
221
222   if(check_id(from_name))
223     {
224       syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("invalid name"));
225       return -1;
226     }
227
228   if(check_id(to_name))
229     {
230       syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("invalid name"));
231       return -1;
232     }
233
234   /* Lookup nodes */
235
236   from = lookup_node(from_name);
237   
238   if(!from)
239     {
240       if(debug_lvl >= DEBUG_PROTOCOL)
241         syslog(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"), "DEL_EDGE", c->name, c->hostname);
242       return 0;
243     }
244
245   to = lookup_node(to_name);
246   
247   if(!to)
248     {
249       if(debug_lvl >= DEBUG_PROTOCOL)
250         syslog(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"), "DEL_EDGE", c->name, c->hostname);
251       return 0;
252     }
253
254   /* Check if edge exists */
255   
256   e = lookup_edge(from, to);
257   
258   if(!e)
259   {
260     if(debug_lvl >= DEBUG_PROTOCOL)
261       syslog(LOG_WARNING, _("Got %s from %s (%s) which does not appear in the edge tree"), "DEL_EDGE", c->name, c->hostname);
262     return 0;
263   }
264
265   if(e->from.node == myself || e->to.node == myself)
266   {
267     if(debug_lvl >= DEBUG_PROTOCOL)
268       syslog(LOG_WARNING, _("Got %s from %s (%s) for ourself"), "DEL_EDGE", c->name, c->hostname);
269     send_add_edge(c, e); /* Send back a correction */
270     return 0;
271   }
272
273   /* Tell the rest about the deleted edge */
274
275   for(node = connection_tree->head; node; node = node->next)
276     {
277       other = (connection_t *)node->data;
278       if(other->status.active && other != c)
279         send_del_edge(other, e);
280     }
281
282   /* Delete the edge */
283   
284   edge_del(e);
285
286   /* Run MST before or after we tell the rest? */
287
288   graph();
289 cp
290   return 0;
291 }