node.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. node.c -- node tree management
  3. Copyright (C) 2001-2016 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. #include "system.h"
  18. #include "avl_tree.h"
  19. #include "logger.h"
  20. #include "net.h"
  21. #include "netutl.h"
  22. #include "node.h"
  23. #include "utils.h"
  24. #include "xalloc.h"
  25. avl_tree_t *node_tree; /* Known nodes, sorted by name */
  26. avl_tree_t *node_udp_tree; /* Known nodes, sorted by address and port */
  27. node_t *myself;
  28. static int node_compare(const node_t *a, const node_t *b) {
  29. return strcmp(a->name, b->name);
  30. }
  31. static int node_udp_compare(const node_t *a, const node_t *b) {
  32. return sockaddrcmp(&a->address, &b->address);
  33. }
  34. void init_nodes(void) {
  35. node_tree = avl_alloc_tree((avl_compare_t) node_compare, (avl_action_t) free_node);
  36. node_udp_tree = avl_alloc_tree((avl_compare_t) node_udp_compare, NULL);
  37. }
  38. void exit_nodes(void) {
  39. avl_delete_tree(node_udp_tree);
  40. avl_delete_tree(node_tree);
  41. }
  42. node_t *new_node(void) {
  43. node_t *n = xmalloc_and_zero(sizeof(*n));
  44. if(replaywin) {
  45. n->late = xmalloc_and_zero(replaywin);
  46. }
  47. n->subnet_tree = new_subnet_tree();
  48. n->edge_tree = new_edge_tree();
  49. n->inctx = EVP_CIPHER_CTX_new();
  50. n->outctx = EVP_CIPHER_CTX_new();
  51. if(!n->inctx || !n->outctx) {
  52. abort();
  53. }
  54. n->mtu = MTU;
  55. n->maxmtu = MTU;
  56. return n;
  57. }
  58. void free_node(node_t *n) {
  59. if(n->inkey) {
  60. free(n->inkey);
  61. }
  62. if(n->outkey) {
  63. free(n->outkey);
  64. }
  65. if(n->subnet_tree) {
  66. free_subnet_tree(n->subnet_tree);
  67. }
  68. if(n->edge_tree) {
  69. free_edge_tree(n->edge_tree);
  70. }
  71. sockaddrfree(&n->address);
  72. EVP_CIPHER_CTX_free(n->outctx);
  73. EVP_CIPHER_CTX_free(n->inctx);
  74. if(n->mtuevent) {
  75. event_del(n->mtuevent);
  76. }
  77. if(n->hostname) {
  78. free(n->hostname);
  79. }
  80. if(n->name) {
  81. free(n->name);
  82. }
  83. if(n->late) {
  84. free(n->late);
  85. }
  86. free(n);
  87. }
  88. void node_add(node_t *n) {
  89. avl_insert(node_tree, n);
  90. }
  91. void node_del(node_t *n) {
  92. avl_node_t *node, *next;
  93. edge_t *e;
  94. subnet_t *s;
  95. for(node = n->subnet_tree->head; node; node = next) {
  96. next = node->next;
  97. s = node->data;
  98. subnet_del(n, s);
  99. }
  100. for(node = n->edge_tree->head; node; node = next) {
  101. next = node->next;
  102. e = node->data;
  103. edge_del(e);
  104. }
  105. avl_delete(node_udp_tree, n);
  106. avl_delete(node_tree, n);
  107. }
  108. node_t *lookup_node(char *name) {
  109. node_t n = {0};
  110. n.name = name;
  111. return avl_search(node_tree, &n);
  112. }
  113. node_t *lookup_node_udp(const sockaddr_t *sa) {
  114. node_t n = {0};
  115. n.address = *sa;
  116. n.name = NULL;
  117. return avl_search(node_udp_tree, &n);
  118. }
  119. void update_node_udp(node_t *n, const sockaddr_t *sa) {
  120. if(n == myself) {
  121. logger(LOG_WARNING, "Trying to update UDP address of myself!");
  122. return;
  123. }
  124. avl_delete(node_udp_tree, n);
  125. if(n->hostname) {
  126. free(n->hostname);
  127. }
  128. if(sa) {
  129. n->address = *sa;
  130. n->hostname = sockaddr2hostname(&n->address);
  131. avl_insert(node_udp_tree, n);
  132. ifdebug(PROTOCOL) logger(LOG_DEBUG, "UDP address of %s set to %s", n->name, n->hostname);
  133. } else {
  134. memset(&n->address, 0, sizeof(n->address));
  135. n->hostname = NULL;
  136. ifdebug(PROTOCOL) logger(LOG_DEBUG, "UDP address of %s cleared", n->name);
  137. }
  138. }
  139. void dump_nodes(void) {
  140. avl_node_t *node;
  141. node_t *n;
  142. logger(LOG_DEBUG, "Nodes:");
  143. for(node = node_tree->head; node; node = node->next) {
  144. n = node->data;
  145. logger(LOG_DEBUG, " %s at %s cipher %d digest %d maclength %d compression %d options %x status %04x nexthop %s via %s pmtu %d (min %d max %d)",
  146. n->name, n->hostname, n->outcipher ? EVP_CIPHER_nid(n->outcipher) : 0,
  147. n->outdigest ? EVP_MD_type(n->outdigest) : 0, n->outmaclength, n->outcompression,
  148. n->options, bitfield_to_int(&n->status, sizeof(n->status)), n->nexthop ? n->nexthop->name : "-",
  149. n->via ? n->via->name : "-", n->mtu, n->minmtu, n->maxmtu);
  150. }
  151. logger(LOG_DEBUG, "End of nodes.");
  152. }