Add $NAME for tinc-up/down scripts.
[tinc] / src / graph.c
1 /*
2     graph.c -- graph algorithms
3     Copyright (C) 2001-2002 Guus Sliepen <guus@sliepen.eu.org>,
4                   2001-2002 Ivo Timmermans <ivo@o2w.nl>
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: graph.c,v 1.1.2.21 2002/09/10 22:12:33 guus Exp $
21 */
22
23 /* We need to generate two trees from the graph:
24
25    1. A minimum spanning tree for broadcasts,
26    2. A single-source shortest path tree for unicasts.
27
28    Actually, the first one alone would suffice but would make unicast packets
29    take longer routes than necessary.
30
31    For the MST algorithm we can choose from Prim's or Kruskal's. I personally
32    favour Kruskal's, because we make an extra AVL tree of edges sorted on
33    weights (metric). That tree only has to be updated when an edge is added or
34    removed, and during the MST algorithm we just have go linearly through that
35    tree, adding safe edges until #edges = #nodes - 1. The implementation here
36    however is not so fast, because I tried to avoid having to make a forest and
37    merge trees.
38
39    For the SSSP algorithm Dijkstra's seems to be a nice choice. Currently a
40    simple breadth-first search is presented here.
41
42    The SSSP algorithm will also be used to determine whether nodes are directly,
43    indirectly or not reachable from the source. It will also set the correct
44    destination address and port of a node if possible.
45 */
46
47 #include "config.h"
48
49 #include <stdio.h>
50 #include <syslog.h>
51 #include <string.h>
52 #ifdef HAVE_SYS_PARAM_H
53 #include <sys/param.h>
54 #endif
55 #include <netinet/in.h>
56
57 #include <avl_tree.h>
58 #include <utils.h>
59
60 #include "netutl.h"
61 #include "node.h"
62 #include "edge.h"
63 #include "connection.h"
64 #include "process.h"
65 #include "device.h"
66
67 #include "system.h"
68
69 /* Implementation of Kruskal's algorithm.
70    Running time: O(EN)
71    Please note that sorting on weight is already done by add_edge().
72 */
73
74 void mst_kruskal(void)
75 {
76         avl_node_t *node, *next;
77         edge_t *e;
78         node_t *n;
79         connection_t *c;
80         int nodes = 0;
81         int safe_edges = 0;
82         int skipped;
83
84         cp();
85         
86         /* Clear MST status on connections */
87
88         for(node = connection_tree->head; node; node = node->next) {
89                 c = (connection_t *) node->data;
90                 c->status.mst = 0;
91         }
92
93         /* Do we have something to do at all? */
94
95         if(!edge_weight_tree->head)
96                 return;
97
98         if(debug_lvl >= DEBUG_SCARY_THINGS)
99                 syslog(LOG_DEBUG, "Running Kruskal's algorithm:");
100
101         /* Clear visited status on nodes */
102
103         for(node = node_tree->head; node; node = node->next) {
104                 n = (node_t *) node->data;
105                 n->status.visited = 0;
106                 nodes++;
107         }
108
109         /* Starting point */
110
111         ((edge_t *) edge_weight_tree->head->data)->from->status.visited = 1;
112
113         /* Add safe edges */
114
115         for(skipped = 0, node = edge_weight_tree->head; node; node = next) {
116                 next = node->next;
117                 e = (edge_t *) node->data;
118
119                 if(!e->reverse || e->from->status.visited == e->to->status.visited) {
120                         skipped = 1;
121                         continue;
122                 }
123
124                 e->from->status.visited = 1;
125                 e->to->status.visited = 1;
126
127                 if(e->connection)
128                         e->connection->status.mst = 1;
129
130                 if(e->reverse->connection)
131                         e->reverse->connection->status.mst = 1;
132
133                 safe_edges++;
134
135                 if(debug_lvl >= DEBUG_SCARY_THINGS)
136                         syslog(LOG_DEBUG, " Adding edge %s - %s weight %d", e->from->name,
137                                    e->to->name, e->weight);
138
139                 if(skipped) {
140                         skipped = 0;
141                         next = edge_weight_tree->head;
142                         continue;
143                 }
144         }
145
146         if(debug_lvl >= DEBUG_SCARY_THINGS)
147                 syslog(LOG_DEBUG, "Done, counted %d nodes and %d safe edges.", nodes,
148                            safe_edges);
149 }
150
151 /* Implementation of a simple breadth-first search algorithm.
152    Running time: O(E)
153 */
154
155 void sssp_bfs(void)
156 {
157         avl_node_t *node, *from, *next, *to;
158         edge_t *e;
159         node_t *n;
160         avl_tree_t *todo_tree;
161         int indirect;
162         char *name;
163         char *address, *port;
164         char *envp[7];
165         int i;
166
167         cp();
168
169         todo_tree = avl_alloc_tree(NULL, NULL);
170
171         /* Clear visited status on nodes */
172
173         for(node = node_tree->head; node; node = node->next) {
174                 n = (node_t *) node->data;
175                 n->status.visited = 0;
176                 n->status.indirect = 1;
177         }
178
179         /* Begin with myself */
180
181         myself->status.visited = 1;
182         myself->status.indirect = 0;
183         myself->nexthop = myself;
184         myself->via = myself;
185         node = avl_alloc_node();
186         node->data = myself;
187         avl_insert_top(todo_tree, node);
188
189         /* Loop while todo_tree is filled */
190
191         while(todo_tree->head) {
192                 for(from = todo_tree->head; from; from = next) {        /* "from" is the node from which we start */
193                         next = from->next;
194                         n = (node_t *) from->data;
195
196                         for(to = n->edge_tree->head; to; to = to->next) {       /* "to" is the edge connected to "from" */
197                                 e = (edge_t *) to->data;
198
199                                 if(!e->reverse)
200                                         continue;
201
202                                 /* Situation:
203
204                                    /
205                                    /
206                                    ------(n)-----(e->to)
207                                    \
208                                    \
209
210                                    n->address is set to the e->address of the edge left of n to n.
211                                    We are currently examining the edge e right of n from n:
212
213                                    - If e->reverse->address != n->address, then e->to is probably
214                                    not reachable for the nodes left of n. We do as if the indirectdata
215                                    flag is set on edge e.
216                                    - If edge e provides for better reachability of e->to, update
217                                    e->to and (re)add it to the todo_tree to (re)examine the reachability
218                                    of nodes behind it.
219                                  */
220
221                                 indirect = n->status.indirect || e->options & OPTION_INDIRECT
222                                         || ((n != myself)
223                                                 && sockaddrcmp(&n->address, &e->reverse->address));
224
225                                 if(e->to->status.visited
226                                    && (!e->to->status.indirect || indirect))
227                                         continue;
228
229                                 e->to->status.visited = 1;
230                                 e->to->status.indirect = indirect;
231                                 e->to->nexthop = (n->nexthop == myself) ? e->to : n->nexthop;
232                                 e->to->via = indirect ? n->via : e->to;
233                                 e->to->options = e->options;
234
235                                 if(sockaddrcmp(&e->to->address, &e->address)) {
236                                         node = avl_unlink(node_udp_tree, e->to);
237                                         e->to->address = e->address;
238
239                                         if(e->to->hostname)
240                                                 free(e->to->hostname);
241
242                                         e->to->hostname = sockaddr2hostname(&e->to->address);
243                                         avl_insert_node(node_udp_tree, node);
244                                 }
245
246                                 node = avl_alloc_node();
247                                 node->data = e->to;
248                                 avl_insert_before(todo_tree, from, node);
249                         }
250
251                         avl_delete_node(todo_tree, from);
252                 }
253         }
254
255         avl_free_tree(todo_tree);
256
257         /* Check reachability status. */
258
259         for(node = node_tree->head; node; node = next) {
260                 next = node->next;
261                 n = (node_t *) node->data;
262
263                 if(n->status.visited != n->status.reachable) {
264                         n->status.reachable = !n->status.reachable;
265
266                         if(debug_lvl >= DEBUG_TRAFFIC) {
267                                 if(n->status.reachable)
268                                         syslog(LOG_DEBUG, _("Node %s (%s) became reachable"),
269                                                    n->name, n->hostname);
270                                 else
271                                         syslog(LOG_DEBUG, _("Node %s (%s) became unreachable"),
272                                                    n->name, n->hostname);
273                         }
274
275                         n->status.validkey = 0;
276                         n->status.waitingforkey = 0;
277
278                         asprintf(&envp[0], "NETNAME=%s", netname ? : "");
279                         asprintf(&envp[1], "DEVICE=%s", device ? : "");
280                         asprintf(&envp[2], "INTERFACE=%s", interface ? : "");
281                         asprintf(&envp[3], "NODE=%s", n->name);
282                         sockaddr2str(&n->address, &address, &port);
283                         asprintf(&envp[4], "REMOTEADDRESS=%s", address);
284                         asprintf(&envp[5], "REMOTEPORT=%s", port);
285                         envp[6] = NULL;
286
287                         asprintf(&name,
288                                          n->status.reachable ? "hosts/%s-up" : "hosts/%s-down",
289                                          n->name);
290                         execute_script(name, envp);
291
292                         free(name);
293                         free(address);
294                         free(port);
295
296                         for(i = 0; i < 7; i++)
297                                 free(envp[i]);
298                 }
299         }
300 }
301
302 void graph(void)
303 {
304         mst_kruskal();
305         sssp_bfs();
306 }