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.
51 #include "connection.h"
62 static bool graph_changed = true;
64 /* Implementation of Kruskal's algorithm.
66 Please note that sorting on weight is already done by add_edge().
69 void mst_kruskal(void)
71 avl_node_t *node, *next;
81 /* Clear MST status on connections */
83 for(node = connection_tree->head; node; node = node->next) {
85 c->status.mst = false;
88 /* Do we have something to do at all? */
90 if(!edge_weight_tree->head)
93 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Running Kruskal's algorithm:");
95 /* Clear visited status on nodes */
97 for(node = node_tree->head; node; node = node->next) {
99 n->status.visited = false;
105 for(node = edge_weight_tree->head; node; node = node->next) {
107 if(e->from->status.reachable) {
108 e->from->status.visited = true;
115 for(skipped = false, node = edge_weight_tree->head; node; node = next) {
119 if(!e->reverse || e->from->status.visited == e->to->status.visited) {
124 e->from->status.visited = true;
125 e->to->status.visited = true;
128 e->connection->status.mst = true;
130 if(e->reverse->connection)
131 e->reverse->connection->status.mst = true;
135 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, " Adding edge %s - %s weight %d", e->from->name,
136 e->to->name, e->weight);
140 next = edge_weight_tree->head;
145 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Done, counted %d nodes and %d safe edges.", nodes,
149 /* Implementation of a simple breadth-first search algorithm.
155 avl_node_t *node, *next, *to;
159 list_node_t *from, *todonext;
162 char *address, *port;
168 todo_list = list_alloc(NULL);
170 /* Clear visited status on nodes */
172 for(node = node_tree->head; node; node = node->next) {
174 n->status.visited = false;
175 n->status.indirect = true;
178 /* Begin with myself */
180 myself->status.visited = true;
181 myself->status.indirect = false;
182 myself->nexthop = myself;
183 myself->via = myself;
184 list_insert_head(todo_list, myself);
186 /* Loop while todo_list is filled */
188 for(from = todo_list->head; from; from = todonext) { /* "from" is the node from which we start */
191 for(to = n->edge_tree->head; to; to = to->next) { /* "to" is the edge connected to "from" */
201 ----->(n)---e-->(e->to)
205 Where e is an edge, (n) and (e->to) are nodes.
206 n->address is set to the e->address of the edge left of n to n.
207 We are currently examining the edge e right of n from n:
209 - If e->reverse->address != n->address, then e->to is probably
210 not reachable for the nodes left of n. We do as if the indirectdata
211 flag is set on edge e.
212 - If edge e provides for better reachability of e->to, update
213 e->to and (re)add it to the todo_list to (re)examine the reachability
217 indirect = n->status.indirect || e->options & OPTION_INDIRECT
218 || ((n != myself) && sockaddrcmp(&n->address, &e->reverse->address));
220 if(e->to->status.visited
221 && (!e->to->status.indirect || indirect))
224 e->to->status.visited = true;
225 e->to->status.indirect = indirect;
226 e->to->nexthop = (n->nexthop == myself) ? e->to : n->nexthop;
227 e->to->via = indirect ? n->via : e->to;
228 e->to->options = e->options;
230 if(e->to->address.sa.sa_family == AF_UNSPEC && e->address.sa.sa_family != AF_UNKNOWN)
231 update_node_udp(e->to, &e->address);
233 list_insert_tail(todo_list, e->to);
236 todonext = from->next;
237 list_delete_node(todo_list, from);
240 list_free(todo_list);
242 /* Check reachability status. */
244 for(node = node_tree->head; node; node = next) {
248 if(n->status.visited != n->status.reachable) {
249 n->status.reachable = !n->status.reachable;
251 if(n->status.reachable) {
252 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Node %s (%s) became reachable"),
253 n->name, n->hostname);
255 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Node %s (%s) became unreachable"),
256 n->name, n->hostname);
259 /* TODO: only clear status.validkey if node is unreachable? */
261 n->status.validkey = false;
262 n->status.waitingforkey = false;
269 event_del(n->mtuevent);
273 xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
274 xasprintf(&envp[1], "DEVICE=%s", device ? : "");
275 xasprintf(&envp[2], "INTERFACE=%s", iface ? : "");
276 xasprintf(&envp[3], "NODE=%s", n->name);
277 sockaddr2str(&n->address, &address, &port);
278 xasprintf(&envp[4], "REMOTEADDRESS=%s", address);
279 xasprintf(&envp[5], "REMOTEPORT=%s", port);
282 execute_script(n->status.reachable ? "host-up" : "host-down", envp);
285 n->status.reachable ? "hosts/%s-up" : "hosts/%s-down",
287 execute_script(name, envp);
293 for(i = 0; i < 6; i++)
296 subnet_update(n, NULL, n->status.reachable);
303 subnet_cache_flush();
306 graph_changed = true;
311 /* Dump nodes and edges to a graphviz file.
313 The file can be converted to an image with
314 dot -Tpng graph_filename -o image_filename.png -Gconcentrate=true
317 void dump_graph(void)
322 char *filename = NULL, *tmpname = NULL;
325 if(!graph_changed || !get_config_string(lookup_config(config_tree, "GraphDumpFile"), &filename))
328 graph_changed = false;
330 ifdebug(PROTOCOL) logger(LOG_NOTICE, "Dumping graph");
332 if(filename[0] == '|') {
333 file = popen(filename + 1, "w");
335 xasprintf(&tmpname, "%s.new", filename);
336 file = fopen(tmpname, "w");
340 logger(LOG_ERR, "Unable to open graph dump file %s: %s", filename, strerror(errno));
345 fprintf(file, "digraph {\n");
347 /* dump all nodes first */
348 for(node = node_tree->head; node; node = node->next) {
350 fprintf(file, " %s [label = \"%s\"];\n", n->name, n->name);
353 /* now dump all edges */
354 for(node = edge_weight_tree->head; node; node = node->next) {
356 fprintf(file, " %s -> %s;\n", e->from->name, e->to->name);
359 fprintf(file, "}\n");
361 if(filename[0] == '|') {
368 rename(tmpname, filename);