graph.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. graph.c -- graph algorithms
  3. Copyright (C) 2001-2002 Guus Sliepen <guus@sliepen.warande.net>,
  4. 2001-2002 Ivo Timmermans <itimmermans@bigfoot.com>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. $Id: graph.c,v 1.1.2.6 2002/02/10 21:57:54 guus Exp $
  17. */
  18. /* We need to generate two trees from the graph:
  19. 1. A minimum spanning tree for broadcasts,
  20. 2. A single-source shortest path tree for unicasts.
  21. Actually, the first one alone would suffice but would make unicast packets
  22. take longer routes than necessary.
  23. For the MST algorithm we can choose from Prim's or Kruskal's. I personally
  24. favour Kruskal's, because we make an extra AVL tree of edges sorted on
  25. weights (metric). That tree only has to be updated when an edge is added or
  26. removed, and during the MST algorithm we just have go linearly through that
  27. tree, adding safe edges until #edges = #nodes - 1. The implementation here
  28. however is not so fast, because I tried to avoid having to make a forest and
  29. merge trees.
  30. For the SSSP algorithm Dijkstra's seems to be a nice choice. Currently a
  31. simple breadth-first search is presented here.
  32. The SSSP algorithm will also be used to determine whether nodes are directly,
  33. indirectly or not reachable from the source. It will also set the correct
  34. destination address and port of a node if possible.
  35. */
  36. #include <syslog.h>
  37. #include "config.h"
  38. #include <string.h>
  39. #if defined(HAVE_FREEBSD) || defined(HAVE_OPENBSD)
  40. #include <sys/param.h>
  41. #endif
  42. #include <netinet/in.h>
  43. #include <avl_tree.h>
  44. #include <utils.h>
  45. #include "netutl.h"
  46. #include "node.h"
  47. #include "edge.h"
  48. #include "connection.h"
  49. #include "system.h"
  50. /* Implementation of Kruskal's algorithm.
  51. Running time: O(EN)
  52. Please note that sorting on weight is already done by add_edge().
  53. */
  54. void mst_kruskal(void)
  55. {
  56. avl_node_t *node, *next;
  57. edge_t *e;
  58. node_t *n;
  59. connection_t *c;
  60. int nodes = 0;
  61. int safe_edges = 0;
  62. int skipped;
  63. /* Do we have something to do at all? */
  64. if(!edge_weight_tree->head)
  65. return;
  66. /* Clear visited status on nodes */
  67. for(node = node_tree->head; node; node = node->next)
  68. {
  69. n = (node_t *)node->data;
  70. n->status.visited = 0;
  71. nodes++;
  72. }
  73. /* Starting point */
  74. ((edge_t *)edge_weight_tree->head->data)->from.node->status.visited = 1;
  75. /* Clear MST status on connections */
  76. for(node = connection_tree->head; node; node = node->next)
  77. {
  78. c = (connection_t *)node->data;
  79. c->status.mst = 0;
  80. }
  81. /* Add safe edges */
  82. for(skipped = 0, node = edge_weight_tree->head; node; node = next)
  83. {
  84. next = node->next;
  85. e = (edge_t *)node->data;
  86. if(e->from.node->status.visited == e->to.node->status.visited)
  87. {
  88. skipped = 1;
  89. continue;
  90. }
  91. e->from.node->status.visited = 1;
  92. e->to.node->status.visited = 1;
  93. if(e->connection)
  94. e->connection->status.mst = 1;
  95. safe_edges++;
  96. if(skipped)
  97. {
  98. next = edge_weight_tree->head;
  99. continue;
  100. }
  101. }
  102. }
  103. /* Implementation of a simple breadth-first search algorithm.
  104. Running time: O(E)
  105. */
  106. void sssp_bfs(void)
  107. {
  108. avl_node_t *node, *from, *next, *to;
  109. edge_t *e;
  110. node_t *n;
  111. halfconnection_t to_hc, from_hc;
  112. avl_tree_t *todo_tree;
  113. todo_tree = avl_alloc_tree(NULL, NULL);
  114. /* Clear visited status on nodes */
  115. for(node = node_tree->head; node; node = node->next)
  116. {
  117. n = (node_t *)node->data;
  118. n->status.visited = 0;
  119. }
  120. /* Begin with myself */
  121. myself->status.visited = 1;
  122. myself->nexthop = myself;
  123. myself->via = myself;
  124. node = avl_alloc_node();
  125. node->data = myself;
  126. avl_insert_top(todo_tree, node);
  127. /* Loop while todo_tree is filled */
  128. while(todo_tree->head)
  129. {
  130. for(from = todo_tree->head; from; from = next) /* "from" is the node from which we start */
  131. {
  132. next = from->next;
  133. n = (node_t *)from->data;
  134. for(to = n->edge_tree->head; to; to = to->next) /* "to" is the edge connected to "from" */
  135. {
  136. e = (edge_t *)to->data;
  137. if(e->from.node == n) /* "from_hc" is the halfconnection with .node == from */
  138. to_hc = e->to, from_hc = e->from;
  139. else
  140. to_hc = e->from, from_hc = e->to;
  141. if(!to_hc.node->status.visited)
  142. {
  143. to_hc.node->status.visited = 1;
  144. to_hc.node->nexthop = (n->nexthop == myself) ? to_hc.node : n->nexthop;
  145. to_hc.node->via = (e->options & OPTION_INDIRECT || n->via != n) ? n->via : to_hc.node;
  146. to_hc.node->options = e->options;
  147. if(to_hc.node->address != to_hc.address || to_hc.node->port != to_hc.port)
  148. {
  149. node = avl_unlink(node_udp_tree, to_hc.node);
  150. to_hc.node->address = to_hc.address;
  151. to_hc.node->port = to_hc.port;
  152. if(to_hc.node->hostname)
  153. free(to_hc.node->hostname);
  154. to_hc.node->hostname = hostlookup(htonl(to_hc.address));
  155. avl_insert_node(node_udp_tree, node);
  156. }
  157. to_hc.node->port = to_hc.port;
  158. node = avl_alloc_node();
  159. node->data = to_hc.node;
  160. avl_insert_before(todo_tree, from, node);
  161. }
  162. }
  163. avl_delete_node(todo_tree, from);
  164. }
  165. }
  166. avl_free_tree(todo_tree);
  167. /* Check reachability status. */
  168. for(node = node_tree->head; node; node = next)
  169. {
  170. next = node->next;
  171. n = (node_t *)node->data;
  172. if(n->status.visited)
  173. {
  174. if(!n->status.reachable)
  175. {
  176. if(debug_lvl >= DEBUG_TRAFFIC)
  177. syslog(LOG_DEBUG, _("Node %s (%s) became reachable"), n->name, n->hostname);
  178. n->status.reachable = 1;
  179. }
  180. }
  181. else
  182. {
  183. if(n->status.reachable)
  184. {
  185. if(debug_lvl >= DEBUG_TRAFFIC)
  186. syslog(LOG_DEBUG, _("Node %s (%s) became unreachable"), n->name, n->hostname);
  187. n->status.reachable = 0;
  188. n->status.validkey = 0;
  189. n->status.waitingforkey = 0;
  190. n->sent_seqno = 0;
  191. }
  192. }
  193. }
  194. }
  195. void graph(void)
  196. {
  197. mst_kruskal();
  198. sssp_bfs();
  199. }