edge.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. edge.c -- edge tree management
  3. Copyright (C) 2000-2006 Guus Sliepen <guus@tinc-vpn.org>,
  4. 2000-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. #include "system.h"
  18. #include "avl_tree.h"
  19. #include "edge.h"
  20. #include "logger.h"
  21. #include "netutl.h"
  22. #include "node.h"
  23. #include "utils.h"
  24. #include "xalloc.h"
  25. avl_tree_t *edge_weight_tree; /* Tree with all edges, sorted on weight */
  26. static int edge_compare(const edge_t *a, const edge_t *b) {
  27. return strcmp(a->to->name, b->to->name);
  28. }
  29. static int edge_weight_compare(const edge_t *a, const edge_t *b) {
  30. int result;
  31. result = a->weight - b->weight;
  32. if(result) {
  33. return result;
  34. }
  35. result = strcmp(a->from->name, b->from->name);
  36. if(result) {
  37. return result;
  38. }
  39. return strcmp(a->to->name, b->to->name);
  40. }
  41. void init_edges(void) {
  42. edge_weight_tree = avl_alloc_tree((avl_compare_t) edge_weight_compare, NULL);
  43. }
  44. avl_tree_t *new_edge_tree(void) {
  45. return avl_alloc_tree((avl_compare_t) edge_compare, (avl_action_t) free_edge);
  46. }
  47. void free_edge_tree(avl_tree_t *edge_tree) {
  48. avl_delete_tree(edge_tree);
  49. }
  50. void exit_edges(void) {
  51. avl_delete_tree(edge_weight_tree);
  52. }
  53. /* Creation and deletion of connection elements */
  54. edge_t *new_edge(void) {
  55. return xmalloc_and_zero(sizeof(edge_t));
  56. }
  57. void free_edge(edge_t *e) {
  58. sockaddrfree(&e->address);
  59. free(e);
  60. }
  61. void edge_add(edge_t *e) {
  62. avl_insert(edge_weight_tree, e);
  63. avl_insert(e->from->edge_tree, e);
  64. e->reverse = lookup_edge(e->to, e->from);
  65. if(e->reverse) {
  66. e->reverse->reverse = e;
  67. }
  68. }
  69. void edge_del(edge_t *e) {
  70. if(e->reverse) {
  71. e->reverse->reverse = NULL;
  72. }
  73. avl_delete(edge_weight_tree, e);
  74. avl_delete(e->from->edge_tree, e);
  75. }
  76. edge_t *lookup_edge(node_t *from, node_t *to) {
  77. edge_t v;
  78. v.from = from;
  79. v.to = to;
  80. return avl_search(from->edge_tree, &v);
  81. }
  82. void dump_edges(void) {
  83. avl_node_t *node, *node2;
  84. node_t *n;
  85. edge_t *e;
  86. char *address;
  87. logger(LOG_DEBUG, "Edges:");
  88. for(node = node_tree->head; node; node = node->next) {
  89. n = node->data;
  90. for(node2 = n->edge_tree->head; node2; node2 = node2->next) {
  91. e = node2->data;
  92. address = sockaddr2hostname(&e->address);
  93. logger(LOG_DEBUG, " %s to %s at %s options %x weight %d",
  94. e->from->name, e->to->name, address, e->options, e->weight);
  95. free(address);
  96. }
  97. }
  98. logger(LOG_DEBUG, "End of edges.");
  99. }