Revert to edge and graph stuff. This time, use a directed graph.
[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.9 2002/09/04 13:48:52 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   connection_t *other;
66   edge_t *e;
67   node_t *from, *to;
68   char from_name[MAX_STRING_SIZE];
69   char to_name[MAX_STRING_SIZE];
70   char to_address[MAX_STRING_SIZE];
71   char to_port[MAX_STRING_SIZE];
72   sockaddr_t address;
73   long int options;
74   int weight;
75   avl_node_t *node;
76 cp
77   if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING" "MAX_STRING" "MAX_STRING" %lx %d",
78             from_name, to_name, to_address, to_port, &options, &weight) != 6)
79     {
80        syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_EDGE", c->name, c->hostname);
81        return -1;
82     }
83
84   /* Check if names are valid */
85
86   if(check_id(from_name))
87     {
88       syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("invalid name"));
89       return -1;
90     }
91
92   if(check_id(to_name))
93     {
94       syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_EDGE", c->name, c->hostname, _("invalid name"));
95       return -1;
96     }
97
98   if(seen_request(c->buffer))
99     return 0;
100
101   /* Lookup nodes */
102
103   from = lookup_node(from_name);
104   
105   if(!from)
106     {
107       from = new_node();
108       from->name = xstrdup(from_name);
109       node_add(from);
110     }
111
112   to = lookup_node(to_name);
113   
114   if(!to)
115     {
116       to = new_node();
117       to->name = xstrdup(to_name);
118       node_add(to);
119     }
120
121   /* Convert addresses */
122   
123   address = str2sockaddr(to_address, to_port);
124
125   /* Check if edge already exists */
126   
127   e = lookup_edge(from, to);
128   
129   if(e)
130   {
131     if(e->weight != weight || e->options != options || sockaddrcmp(&e->address, &address))
132     {
133       if(from == myself)
134       {
135         if(debug_lvl >= DEBUG_PROTOCOL)
136           syslog(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not match existing entry"), "ADD_EDGE", c->name, c->hostname);
137         send_add_edge(c, e);
138         return 0;
139       }
140       else
141       {
142         if(debug_lvl >= DEBUG_PROTOCOL)
143           syslog(LOG_WARNING, _("Got %s from %s (%s) which does not match existing entry"), "ADD_EDGE", c->name, c->hostname);
144         edge_del(e);
145       }
146     }
147     else
148       return 0;
149   }
150   else if(from == myself)
151   {
152     if(debug_lvl >= DEBUG_PROTOCOL)
153       syslog(LOG_WARNING, _("Got %s from %s (%s) for ourself which does not exist"), "ADD_EDGE", c->name, c->hostname);
154     e = new_edge();
155     e->from = from;
156     e->to = to;
157     send_del_edge(c, e);
158     free_edge(e);
159     return 0;
160   }
161
162   e = new_edge();
163   e->from = from;
164   e->to = to;
165   e->address = address;
166   e->options = options;
167   e->weight = weight;
168   edge_add(e);
169
170   /* Tell the rest about the new edge */
171
172   for(node = connection_tree->head; node; node = node->next)
173     {
174       other = (connection_t *)node->data;
175       if(other->status.active && other != c)
176         send_request(other, "%s", c->buffer);
177     }
178
179   /* Run MST before or after we tell the rest? */
180
181   graph();
182 cp
183   return 0;
184 }
185
186 int send_del_edge(connection_t *c, edge_t *e)
187 {
188 cp
189   return send_request(c, "%d %lx %s %s", DEL_EDGE, random(),
190                       e->from->name, e->to->name);
191 }
192
193 int del_edge_h(connection_t *c)
194 {
195   edge_t *e;
196   char from_name[MAX_STRING_SIZE];
197   char to_name[MAX_STRING_SIZE];
198   node_t *from, *to;
199   connection_t *other;
200   avl_node_t *node;
201 cp
202   if(sscanf(c->buffer, "%*d %*x "MAX_STRING" "MAX_STRING"", from_name, to_name) != 2)
203     {
204       syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_EDGE",
205              c->name, c->hostname);
206       return -1;
207     }
208
209   /* Check if names are valid */
210
211   if(check_id(from_name))
212     {
213       syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("invalid name"));
214       return -1;
215     }
216
217   if(check_id(to_name))
218     {
219       syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_EDGE", c->name, c->hostname, _("invalid name"));
220       return -1;
221     }
222
223   if(seen_request(c->buffer))
224     return 0;
225
226   /* Lookup nodes */
227
228   from = lookup_node(from_name);
229   
230   if(!from)
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   to = lookup_node(to_name);
238   
239   if(!to)
240     {
241       if(debug_lvl >= DEBUG_PROTOCOL)
242         syslog(LOG_ERR, _("Got %s from %s (%s) which does not appear in the edge tree"), "DEL_EDGE", c->name, c->hostname);
243       return 0;
244     }
245
246   /* Check if edge exists */
247   
248   e = lookup_edge(from, to);
249   
250   if(!e)
251   {
252     if(debug_lvl >= DEBUG_PROTOCOL)
253       syslog(LOG_WARNING, _("Got %s from %s (%s) which does not appear in the edge tree"), "DEL_EDGE", c->name, c->hostname);
254     return 0;
255   }
256
257   if(e->from == myself)
258   {
259     if(debug_lvl >= DEBUG_PROTOCOL)
260       syslog(LOG_WARNING, _("Got %s from %s (%s) for ourself"), "DEL_EDGE", c->name, c->hostname);
261     send_add_edge(c, e); /* Send back a correction */
262     return 0;
263   }
264
265   /* Tell the rest about the deleted edge */
266
267   for(node = connection_tree->head; node; node = node->next)
268     {
269       other = (connection_t *)node->data;
270       if(other->status.active && other != c)
271         send_request(other, "%s", c->buffer);
272     }
273
274   /* Delete the edge */
275   
276   edge_del(e);
277
278   /* Run MST before or after we tell the rest? */
279
280   graph();
281 cp
282   return 0;
283 }