node.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. node.c -- node tree management
  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: node.c,v 1.1.2.8 2002/02/10 21:57:54 guus Exp $
  17. */
  18. #include "config.h"
  19. #include <string.h>
  20. #include <syslog.h>
  21. #include <avl_tree.h>
  22. #include "node.h"
  23. #include "net.h"
  24. #include <utils.h>
  25. #include <xalloc.h>
  26. #include "system.h"
  27. avl_tree_t *node_tree; /* Known nodes, sorted by name */
  28. avl_tree_t *node_udp_tree; /* Known nodes, sorted by address and port */
  29. node_t *myself;
  30. int node_compare(node_t *a, node_t *b)
  31. {
  32. return strcmp(a->name, b->name);
  33. }
  34. int node_udp_compare(node_t *a, node_t *b)
  35. {
  36. if(a->address < b->address)
  37. return -1;
  38. if (a->address > b->address)
  39. return 1;
  40. if (a->port < b->port)
  41. return -1;
  42. if (a->port > b->port)
  43. return 1;
  44. return (a->name && b->name)?strcmp(a->name, b->name):0;
  45. }
  46. void init_nodes(void)
  47. {
  48. cp
  49. node_tree = avl_alloc_tree((avl_compare_t)node_compare, NULL);
  50. node_udp_tree = avl_alloc_tree((avl_compare_t)node_udp_compare, NULL);
  51. cp
  52. }
  53. void exit_nodes(void)
  54. {
  55. cp
  56. avl_delete_tree(node_tree);
  57. avl_delete_tree(node_udp_tree);
  58. cp
  59. }
  60. node_t *new_node(void)
  61. {
  62. node_t *n = (node_t *)xmalloc_and_zero(sizeof(*n));
  63. cp
  64. n->subnet_tree = new_subnet_tree();
  65. n->edge_tree = new_edge_tree();
  66. n->queue = list_alloc((list_action_t)free);
  67. cp
  68. return n;
  69. }
  70. void free_node(node_t *n)
  71. {
  72. cp
  73. if(n->queue)
  74. list_delete_list(n->queue);
  75. if(n->name)
  76. free(n->name);
  77. if(n->hostname)
  78. free(n->hostname);
  79. if(n->key)
  80. free(n->key);
  81. if(n->subnet_tree)
  82. free_subnet_tree(n->subnet_tree);
  83. if(n->edge_tree)
  84. free_edge_tree(n->edge_tree);
  85. free(n);
  86. cp
  87. }
  88. void node_add(node_t *n)
  89. {
  90. cp
  91. avl_insert(node_tree, n);
  92. avl_insert(node_udp_tree, n);
  93. cp
  94. }
  95. void node_del(node_t *n)
  96. {
  97. avl_node_t *node, *next;
  98. edge_t *e;
  99. subnet_t *s;
  100. cp
  101. for(node = n->subnet_tree->head; node; node = next)
  102. {
  103. next = node->next;
  104. s = (subnet_t *)node->data;
  105. subnet_del(n, s);
  106. }
  107. for(node = n->subnet_tree->head; node; node = next)
  108. {
  109. next = node->next;
  110. e = (edge_t *)node->data;
  111. edge_del(e);
  112. }
  113. cp
  114. avl_delete(node_tree, n);
  115. avl_delete(node_udp_tree, n);
  116. cp
  117. }
  118. node_t *lookup_node(char *name)
  119. {
  120. node_t n;
  121. cp
  122. n.name = name;
  123. return avl_search(node_tree, &n);
  124. }
  125. node_t *lookup_node_udp(ipv4_t address, port_t port)
  126. {
  127. node_t n;
  128. cp
  129. n.name = NULL;
  130. n.address = address;
  131. n.port = port;
  132. return avl_search(node_udp_tree, &n);
  133. }
  134. void dump_nodes(void)
  135. {
  136. avl_node_t *node;
  137. node_t *n;
  138. cp
  139. syslog(LOG_DEBUG, _("Nodes:"));
  140. for(node = node_tree->head; node; node = node->next)
  141. {
  142. n = (node_t *)node->data;
  143. syslog(LOG_DEBUG, _(" %s at %s port %hd cipher %d digest %d maclength %d options %ld status %04x nexthop %s via %s"),
  144. n->name, n->hostname, n->port, n->cipher?n->cipher->nid:0, n->digest?n->digest->type:0, n->maclength, n->options,
  145. n->status, n->nexthop?n->nexthop->name:"-", n->via?n->via->name:"-");
  146. }
  147. syslog(LOG_DEBUG, _("End of nodes."));
  148. cp
  149. }