2 graph.c -- graph algorithms
3 Copyright (C) 2001-2004 Guus Sliepen <guus@tinc-vpn.org>,
4 2001-2004 Ivo Timmermans <ivo@tinc-vpn.org>
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 "support/avl.h"
52 #include "support/list.h"
54 /* Implementation of Kruskal's algorithm.
56 Please note that sorting on weight is already done by add_edge().
59 void mst_kruskal(void) {
60 avl_node_t *avl, *next;
66 /* Do we have something to do at all? */
71 logger(LOG_DEBUG, "Running Kruskal's algorithm:");
73 /* Clear MST status on edges */
75 avl_foreach(edges, edge, edge->status.mst = false);
77 /* Clear visited status on nodes */
79 avl_foreach(nodes, node, node->status.visited = false);
83 ((edge_t *) edges->head->data)->from->status.visited = true;
87 for(skipped = false, avl = edges->head; avl; avl = next) {
91 if(!edge->reverse || edge->from->status.visited == edge->to->status.visited) {
96 edge->from->status.visited = true;
97 edge->to->status.visited = true;
98 edge->status.mst = true;
99 edge->reverse->status.mst = true;
109 /* Implementation of a simple breadth-first search algorithm.
113 void sssp_bfs(void) {
115 list_node_t *todonode;
120 char *address, *port;
123 todo = list_new(NULL);
125 /* Clear visited status on nodes */
127 avl_foreach(nodes, node, {
128 node->status.visited = false;
129 node->status.indirect = true;
132 /* Begin with myself */
134 myself->status.visited = true;
135 myself->status.indirect = false;
136 myself->nexthop = myself;
137 myself->via = myself;
139 list_add_head(todo, myself);
141 /* Loop while todo list is filled */
144 list_foreach_node(todo, todonode, {
145 node = todonode->data;
147 avl_foreach(node->edges, edge, {
155 ----->(node)---edge-->(edge->to)
159 node->address is set to the ->address of the edge left of node.
160 We are currently examining the edge right of node:
162 - If edge->reverse->address != node->address, then edge->to is probably
163 not reachable for the nodes left of node. We do as if the indirectdata
165 - If edge provides for better reachability of edge->to, update
166 edge->to and (re)add it to the todo_tree to (re)examine the reachability
170 indirect = node->status.indirect || edge->options & NODE_OPTION_INDIRECT
171 || ((node != myself) && sockaddrcmp(&node->address, &edge->reverse->address));
173 if(edge->to->status.visited && (!edge->to->status.indirect || indirect))
176 edge->to->status.visited = true;
177 edge->to->status.indirect = indirect;
178 edge->to->nexthop = (node->nexthop == myself) ? edge->to : node->nexthop;
179 edge->to->via = indirect ? node->via : edge->to;
180 edge->to->options = edge->options;
182 list_add_head(todo, edge->to);
185 list_del_node(todo, todonode);
191 /* Check reachability status. */
193 avl_foreach(nodes, node, {
194 if(node->status.visited != node->status.reachable) {
195 node->status.reachable = !node->status.reachable;
197 if(node->status.reachable)
198 logger(LOG_DEBUG, _("Node %s became reachable"), node->name);
200 logger(LOG_DEBUG, _("Node %s became unreachable"), node->name);
203 asprintf(&envp[0], "NETNAME=%s", netname ? : "");
204 asprintf(&envp[1], "DEVICE=%s", device ? : "");
205 asprintf(&envp[2], "INTERFACE=%s", iface ? : "");
206 asprintf(&envp[3], "NODE=%s", n->name);
207 sockaddr2str(&n->address, &address, &port);
208 asprintf(&envp[4], "REMOTEADDRESS=%s", address);
209 asprintf(&envp[5], "REMOTEPORT=%s", port);
213 n->status.reachable ? "hosts/%s-up" : "hosts/%s-down",
215 execute_script(name, envp);
221 for(i = 0; i < 7; i++)