graph.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. graph.c -- graph algorithms
  3. Copyright (C) 2001-2014 Guus Sliepen <guus@tinc-vpn.org>,
  4. 2001-2005 Ivo Timmermans
  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 along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. /* We need to generate two trees from the graph:
  18. 1. A minimum spanning tree for broadcasts,
  19. 2. A single-source shortest path tree for unicasts.
  20. Actually, the first one alone would suffice but would make unicast packets
  21. take longer routes than necessary.
  22. For the MST algorithm we can choose from Prim's or Kruskal's. I personally
  23. favour Kruskal's, because we make an extra AVL tree of edges sorted on
  24. weights (metric). That tree only has to be updated when an edge is added or
  25. removed, and during the MST algorithm we just have go linearly through that
  26. tree, adding safe edges until #edges = #nodes - 1. The implementation here
  27. however is not so fast, because I tried to avoid having to make a forest and
  28. merge trees.
  29. For the SSSP algorithm Dijkstra's seems to be a nice choice. Currently a
  30. simple breadth-first search is presented here.
  31. The SSSP algorithm will also be used to determine whether nodes are directly,
  32. indirectly or not reachable from the source. It will also set the correct
  33. destination address and port of a node if possible.
  34. */
  35. #include "system.h"
  36. #include "avl_tree.h"
  37. #include "conf.h"
  38. #include "connection.h"
  39. #include "device.h"
  40. #include "edge.h"
  41. #include "graph.h"
  42. #include "logger.h"
  43. #include "netutl.h"
  44. #include "node.h"
  45. #include "process.h"
  46. #include "protocol.h"
  47. #include "subnet.h"
  48. #include "utils.h"
  49. #include "xalloc.h"
  50. static bool graph_changed = true;
  51. /* Implementation of Kruskal's algorithm.
  52. Running time: O(EN)
  53. Please note that sorting on weight is already done by add_edge().
  54. */
  55. static void mst_kruskal(void) {
  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. bool skipped;
  63. /* Clear MST status on connections */
  64. for(node = connection_tree->head; node; node = node->next) {
  65. c = node->data;
  66. c->status.mst = false;
  67. }
  68. /* Do we have something to do at all? */
  69. if(!edge_weight_tree->head) {
  70. return;
  71. }
  72. ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Running Kruskal's algorithm:");
  73. /* Clear visited status on nodes */
  74. for(node = node_tree->head; node; node = node->next) {
  75. n = node->data;
  76. n->status.visited = false;
  77. nodes++;
  78. }
  79. /* Starting point */
  80. for(node = edge_weight_tree->head; node; node = node->next) {
  81. e = node->data;
  82. if(e->from->status.reachable) {
  83. e->from->status.visited = true;
  84. break;
  85. }
  86. }
  87. /* Add safe edges */
  88. for(skipped = false, node = edge_weight_tree->head; node; node = next) {
  89. next = node->next;
  90. e = node->data;
  91. if(!e->reverse || e->from->status.visited == e->to->status.visited) {
  92. skipped = true;
  93. continue;
  94. }
  95. e->from->status.visited = true;
  96. e->to->status.visited = true;
  97. if(e->connection) {
  98. e->connection->status.mst = true;
  99. }
  100. if(e->reverse->connection) {
  101. e->reverse->connection->status.mst = true;
  102. }
  103. safe_edges++;
  104. ifdebug(SCARY_THINGS) logger(LOG_DEBUG, " Adding edge %s - %s weight %d", e->from->name,
  105. e->to->name, e->weight);
  106. if(skipped) {
  107. skipped = false;
  108. next = edge_weight_tree->head;
  109. continue;
  110. }
  111. }
  112. ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Done, counted %d nodes and %d safe edges.", nodes,
  113. safe_edges);
  114. }
  115. /* Implementation of a simple breadth-first search algorithm.
  116. Running time: O(E)
  117. */
  118. static void sssp_bfs(void) {
  119. avl_node_t *node, *next, *to;
  120. edge_t *e;
  121. node_t *n;
  122. list_t *todo_list;
  123. list_node_t *from, *todonext;
  124. bool indirect;
  125. char *name;
  126. char *address, *port;
  127. char *envp[8] = {NULL};
  128. int i;
  129. todo_list = list_alloc(NULL);
  130. /* Clear visited status on nodes */
  131. for(node = node_tree->head; node; node = node->next) {
  132. n = node->data;
  133. n->status.visited = false;
  134. n->status.indirect = true;
  135. }
  136. /* Begin with myself */
  137. myself->status.visited = true;
  138. myself->status.indirect = false;
  139. myself->nexthop = myself;
  140. myself->prevedge = NULL;
  141. myself->via = myself;
  142. list_insert_head(todo_list, myself);
  143. /* Loop while todo_list is filled */
  144. for(from = todo_list->head; from; from = todonext) { /* "from" is the node from which we start */
  145. n = from->data;
  146. for(to = n->edge_tree->head; to; to = to->next) { /* "to" is the edge connected to "from" */
  147. e = to->data;
  148. if(!e->reverse) {
  149. continue;
  150. }
  151. /* Situation:
  152. /
  153. /
  154. ----->(n)---e-->(e->to)
  155. \
  156. \
  157. Where e is an edge, (n) and (e->to) are nodes.
  158. n->address is set to the e->address of the edge left of n to n.
  159. We are currently examining the edge e right of n from n:
  160. - If edge e provides for better reachability of e->to, update
  161. e->to and (re)add it to the todo_list to (re)examine the reachability
  162. of nodes behind it.
  163. */
  164. indirect = n->status.indirect || e->options & OPTION_INDIRECT;
  165. if(e->to->status.visited
  166. && (!e->to->status.indirect || indirect)) {
  167. continue;
  168. }
  169. // Only update nexthop the first time we visit this node.
  170. if(!e->to->status.visited) {
  171. e->to->nexthop = (n->nexthop == myself) ? e->to : n->nexthop;
  172. }
  173. e->to->status.visited = true;
  174. e->to->status.indirect = indirect;
  175. e->to->prevedge = e;
  176. e->to->via = indirect ? n->via : e->to;
  177. e->to->options = e->options;
  178. if(e->to->address.sa.sa_family == AF_UNSPEC && e->address.sa.sa_family != AF_UNKNOWN) {
  179. update_node_udp(e->to, &e->address);
  180. }
  181. list_insert_tail(todo_list, e->to);
  182. }
  183. todonext = from->next;
  184. list_delete_node(todo_list, from);
  185. }
  186. list_free(todo_list);
  187. /* Check reachability status. */
  188. for(node = node_tree->head; node; node = next) {
  189. next = node->next;
  190. n = node->data;
  191. if(n->status.visited != n->status.reachable) {
  192. n->status.reachable = !n->status.reachable;
  193. if(n->status.reachable) {
  194. ifdebug(TRAFFIC) logger(LOG_DEBUG, "Node %s (%s) became reachable",
  195. n->name, n->hostname);
  196. } else {
  197. ifdebug(TRAFFIC) logger(LOG_DEBUG, "Node %s (%s) became unreachable",
  198. n->name, n->hostname);
  199. }
  200. /* TODO: only clear status.validkey if node is unreachable? */
  201. n->status.validkey = false;
  202. n->last_req_key = 0;
  203. n->maxmtu = MTU;
  204. n->minmtu = 0;
  205. n->mtuprobes = 0;
  206. if(n->mtuevent) {
  207. event_del(n->mtuevent);
  208. n->mtuevent = NULL;
  209. }
  210. xasprintf(&envp[0], "NETNAME=%s", netname ? netname : "");
  211. xasprintf(&envp[1], "DEVICE=%s", device ? device : "");
  212. xasprintf(&envp[2], "INTERFACE=%s", iface ? iface : "");
  213. xasprintf(&envp[3], "NODE=%s", n->name);
  214. sockaddr2str(&n->address, &address, &port);
  215. xasprintf(&envp[4], "REMOTEADDRESS=%s", address);
  216. xasprintf(&envp[5], "REMOTEPORT=%s", port);
  217. xasprintf(&envp[6], "NAME=%s", myself->name);
  218. execute_script(n->status.reachable ? "host-up" : "host-down", envp);
  219. xasprintf(&name,
  220. n->status.reachable ? "hosts/%s-up" : "hosts/%s-down",
  221. n->name);
  222. execute_script(name, envp);
  223. free(name);
  224. free(address);
  225. free(port);
  226. for(i = 0; i < 7; i++) {
  227. free(envp[i]);
  228. }
  229. subnet_update(n, NULL, n->status.reachable);
  230. if(!n->status.reachable) {
  231. update_node_udp(n, NULL);
  232. memset(&n->status, 0, sizeof(n->status));
  233. n->options = 0;
  234. } else if(n->connection) {
  235. send_ans_key(n);
  236. }
  237. }
  238. }
  239. }
  240. void graph(void) {
  241. subnet_cache_flush();
  242. sssp_bfs();
  243. mst_kruskal();
  244. graph_changed = true;
  245. }
  246. /* Dump nodes and edges to a graphviz file.
  247. The file can be converted to an image with
  248. dot -Tpng graph_filename -o image_filename.png -Gconcentrate=true
  249. */
  250. void dump_graph(void) {
  251. avl_node_t *node;
  252. node_t *n;
  253. edge_t *e;
  254. char *filename = NULL, *tmpname = NULL;
  255. FILE *file, *pipe = NULL;
  256. if(!graph_changed || !get_config_string(lookup_config(config_tree, "GraphDumpFile"), &filename)) {
  257. return;
  258. }
  259. graph_changed = false;
  260. ifdebug(PROTOCOL) logger(LOG_NOTICE, "Dumping graph");
  261. if(filename[0] == '|') {
  262. file = pipe = popen(filename + 1, "w");
  263. } else {
  264. xasprintf(&tmpname, "%s.new", filename);
  265. file = fopen(tmpname, "w");
  266. }
  267. if(!file) {
  268. logger(LOG_ERR, "Unable to open graph dump file %s: %s", filename, strerror(errno));
  269. free(filename);
  270. free(tmpname);
  271. return;
  272. }
  273. fprintf(file, "digraph {\n");
  274. /* dump all nodes first */
  275. for(node = node_tree->head; node; node = node->next) {
  276. n = node->data;
  277. fprintf(file, " \"%s\" [label = \"%s\"];\n", n->name, n->name);
  278. }
  279. /* now dump all edges */
  280. for(node = edge_weight_tree->head; node; node = node->next) {
  281. e = node->data;
  282. fprintf(file, " \"%s\" -> \"%s\";\n", e->from->name, e->to->name);
  283. }
  284. fprintf(file, "}\n");
  285. if(pipe) {
  286. pclose(pipe);
  287. } else {
  288. fclose(file);
  289. #ifdef HAVE_MINGW
  290. unlink(filename);
  291. #endif
  292. if(rename(tmpname, filename)) {
  293. logger(LOG_ERR, "Could not rename %s to %s: %s\n", tmpname, filename, strerror(errno));
  294. }
  295. free(tmpname);
  296. }
  297. free(filename);
  298. }