2 graph.c -- graph algorithms
3 Copyright (C) 2001-2009 Guus Sliepen <guus@tinc-vpn.org>,
4 2001-2005 Ivo Timmermans
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.
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.
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.
23 /* We need to generate two trees from the graph:
25 1. A minimum spanning tree for broadcasts,
26 2. A single-source shortest path tree for unicasts.
28 Actually, the first one alone would suffice but would make unicast packets
29 take longer routes than necessary.
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
39 For the SSSP algorithm Dijkstra's seems to be a nice choice. Currently a
40 simple breadth-first search is presented here.
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.
49 #include "splay_tree.h"
51 #include "connection.h"
62 /* Implementation of Kruskal's algorithm.
64 Please note that sorting on weight is already done by add_edge().
67 void mst_kruskal(void) {
68 splay_node_t *node, *next;
75 /* Clear MST status on connections */
77 for(node = connection_tree->head; node; node = node->next) {
79 c->status.mst = false;
82 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Running Kruskal's algorithm:");
84 /* Clear visited status on nodes */
86 for(node = node_tree->head; node; node = node->next) {
88 n->status.visited = false;
93 for(node = edge_weight_tree->head; node; node = next) {
97 if(!e->reverse || (e->from->status.visited && e->to->status.visited))
100 e->from->status.visited = true;
101 e->to->status.visited = true;
104 e->connection->status.mst = true;
106 if(e->reverse->connection)
107 e->reverse->connection->status.mst = true;
109 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, " Adding edge %s - %s weight %d", e->from->name,
110 e->to->name, e->weight);
114 /* Implementation of Dijkstra's algorithm.
118 void sssp_dijkstra(void) {
119 splay_node_t *node, *to;
123 list_node_t *lnode, *nnode;
128 todo_list = list_alloc(NULL);
130 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Running Dijkstra's algorithm:");
132 /* Clear visited status on nodes */
134 for(node = node_tree->head; node; node = node->next) {
136 n->status.visited = false;
137 n->status.indirect = true;
141 /* Begin with myself */
143 myself->status.indirect = false;
144 myself->nexthop = myself;
145 myself->via = myself;
146 myself->distance = 0;
147 list_insert_head(todo_list, myself);
149 /* Loop while todo_list is filled */
151 while(todo_list->head) {
155 /* Select node from todo_list with smallest distance */
157 for(lnode = todo_list->head; lnode; lnode = lnode->next) {
159 if(!n || m->status.indirect < n->status.indirect || m->distance < n->distance) {
165 /* Mark this node as visited and remove it from the todo_list */
167 n->status.visited = true;
168 list_unlink_node(todo_list, nnode);
170 /* Update distance of neighbours and add them to the todo_list */
172 for(to = n->edge_tree->head; to; to = to->next) { /* "to" is the edge connected to "from" */
175 if(e->to->status.visited || !e->reverse)
182 ----->(n)---e-->(e->to)
186 Where e is an edge, (n) and (e->to) are nodes.
187 n->address is set to the e->address of the edge left of n to n.
188 We are currently examining the edge e right of n from n:
190 - If e->reverse->address != n->address, then e->to is probably
191 not reachable for the nodes left of n. We do as if the indirectdata
192 flag is set on edge e.
193 - If edge e provides for better reachability of e->to, update e->to.
196 if(e->to->distance < 0)
197 list_insert_tail(todo_list, e->to);
199 indirect = n->status.indirect || e->options & OPTION_INDIRECT || ((n != myself) && sockaddrcmp(&n->address, &e->reverse->address));
201 if(e->to->distance >= 0 && (!e->to->status.indirect || indirect) && e->to->distance <= n->distance + e->weight)
204 e->to->distance = n->distance + e->weight;
205 e->to->status.indirect = indirect;
206 e->to->nexthop = (n->nexthop == myself) ? e->to : n->nexthop;
207 e->to->via = indirect ? n->via : e->to;
208 e->to->options = e->options;
210 if(sockaddrcmp(&e->to->address, &e->address)) {
211 node = splay_unlink(node_udp_tree, e->to);
212 sockaddrfree(&e->to->address);
213 sockaddrcpy(&e->to->address, &e->address);
216 free(e->to->hostname);
218 e->to->hostname = sockaddr2hostname(&e->to->address);
221 splay_insert_node(node_udp_tree, node);
223 if(e->to->options & OPTION_PMTU_DISCOVERY) {
224 e->to->mtuprobes = 0;
227 if(e->to->status.validkey)
228 send_mtu_probe(e->to);
232 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, " Updating edge %s - %s weight %d distance %d", e->from->name,
233 e->to->name, e->weight, e->to->distance);
237 list_free(todo_list);
240 /* Implementation of a simple breadth-first search algorithm.
244 void sssp_bfs(void) {
245 splay_node_t *node, *to;
249 list_node_t *from, *todonext;
254 todo_list = list_alloc(NULL);
256 /* Clear visited status on nodes */
258 for(node = node_tree->head; node; node = node->next) {
260 n->status.visited = false;
261 n->status.indirect = true;
264 /* Begin with myself */
266 myself->status.visited = true;
267 myself->status.indirect = false;
268 myself->nexthop = myself;
269 myself->via = myself;
270 list_insert_head(todo_list, myself);
272 /* Loop while todo_list is filled */
274 for(from = todo_list->head; from; from = todonext) { /* "from" is the node from which we start */
277 for(to = n->edge_tree->head; to; to = to->next) { /* "to" is the edge connected to "from" */
287 ----->(n)---e-->(e->to)
291 Where e is an edge, (n) and (e->to) are nodes.
292 n->address is set to the e->address of the edge left of n to n.
293 We are currently examining the edge e right of n from n:
295 - If e->reverse->address != n->address, then e->to is probably
296 not reachable for the nodes left of n. We do as if the indirectdata
297 flag is set on edge e.
298 - If edge e provides for better reachability of e->to, update
299 e->to and (re)add it to the todo_list to (re)examine the reachability
303 indirect = n->status.indirect || e->options & OPTION_INDIRECT
304 || ((n != myself) && sockaddrcmp(&n->address, &e->reverse->address));
306 if(e->to->status.visited
307 && (!e->to->status.indirect || indirect))
310 e->to->status.visited = true;
311 e->to->status.indirect = indirect;
312 e->to->nexthop = (n->nexthop == myself) ? e->to : n->nexthop;
313 e->to->via = indirect ? n->via : e->to;
314 e->to->options = e->options;
316 if(e->to->address.sa.sa_family == AF_UNSPEC && e->address.sa.sa_family != AF_UNKNOWN)
317 update_node_udp(e->to, &e->address);
319 list_insert_tail(todo_list, e->to);
322 todonext = from->next;
323 list_delete_node(todo_list, from);
326 list_free(todo_list);
329 void check_reachability() {
330 splay_node_t *node, *next;
333 char *address, *port;
337 /* Check reachability status. */
339 for(node = node_tree->head; node; node = next) {
343 if(n->status.visited != n->status.reachable) {
344 n->status.reachable = !n->status.reachable;
346 if(n->status.reachable) {
347 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Node %s (%s) became reachable"),
348 n->name, n->hostname);
350 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Node %s (%s) became unreachable"),
351 n->name, n->hostname);
354 /* TODO: only clear status.validkey if node is unreachable? */
356 n->status.validkey = false;
357 n->status.waitingforkey = false;
363 event_del(&n->mtuevent);
365 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
366 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
367 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
368 xasprintf(&envp[3], "NODE=%s", n->name);
369 sockaddr2str(&n->address, &address, &port);
370 xasprintf(&envp[4], "REMOTEADDRESS=%s", address);
371 xasprintf(&envp[5], "REMOTEPORT=%s", port);
374 execute_script(n->status.reachable ? "host-up" : "host-down", envp);
377 n->status.reachable ? "hosts/%s-up" : "hosts/%s-down",
379 execute_script(name, envp);
385 for(i = 0; i < 6; i++)
388 subnet_update(n, NULL, n->status.reachable);
393 /* Dump nodes and edges to a graphviz file.
395 The file can be converted to an image with
396 dot -Tpng graph_filename -o image_filename.png -Gconcentrate=true
399 int dump_graph(struct evbuffer *out) {
404 if(evbuffer_add_printf(out, "digraph {\n") == -1)
407 /* dump all nodes first */
408 for(node = node_tree->head; node; node = node->next) {
410 if(evbuffer_add_printf(out, " %s [label = \"%s\"];\n",
411 n->name, n->name) == -1)
415 /* now dump all edges */
416 for(node = edge_weight_tree->head; node; node = node->next) {
418 if(evbuffer_add_printf(out, " %s -> %s;\n",
419 e->from->name, e->to->name) == -1)
423 if(evbuffer_add_printf(out, "}\n") == -1)
430 subnet_cache_flush();
432 check_reachability();