connection.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. connection.c -- connection list management
  3. Copyright (C) 2000-2016 Guus Sliepen <guus@tinc-vpn.org>,
  4. 2000-2005 Ivo Timmermans
  5. 2008 Max Rijevski <maksuf@gmail.com>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License along
  15. with this program; if not, write to the Free Software Foundation, Inc.,
  16. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include "system.h"
  19. #include "avl_tree.h"
  20. #include "conf.h"
  21. #include "logger.h"
  22. #include "subnet.h"
  23. #include "utils.h"
  24. #include "xalloc.h"
  25. avl_tree_t *connection_tree; /* Meta connections */
  26. connection_t *everyone;
  27. static int connection_compare(const connection_t *a, const connection_t *b) {
  28. return a < b ? -1 : a == b ? 0 : 1;
  29. }
  30. void init_connections(void) {
  31. connection_tree = avl_alloc_tree((avl_compare_t) connection_compare, (avl_action_t) free_connection);
  32. everyone = new_connection();
  33. everyone->name = xstrdup("everyone");
  34. everyone->hostname = xstrdup("BROADCAST");
  35. }
  36. void exit_connections(void) {
  37. avl_delete_tree(connection_tree);
  38. free_connection(everyone);
  39. }
  40. connection_t *new_connection(void) {
  41. connection_t *c;
  42. c = xmalloc_and_zero(sizeof(connection_t));
  43. if(!c) {
  44. return NULL;
  45. }
  46. gettimeofday(&c->start, NULL);
  47. return c;
  48. }
  49. void free_connection_partially(connection_t *c) {
  50. free(c->inkey);
  51. free(c->outkey);
  52. free(c->mychallenge);
  53. free(c->hischallenge);
  54. free(c->outbuf);
  55. c->inkey = NULL;
  56. c->outkey = NULL;
  57. c->mychallenge = NULL;
  58. c->hischallenge = NULL;
  59. c->outbuf = NULL;
  60. c->status.pinged = false;
  61. c->status.active = false;
  62. c->status.connecting = false;
  63. c->status.timeout = false;
  64. c->status.encryptout = false;
  65. c->status.decryptin = false;
  66. c->status.mst = false;
  67. c->options = 0;
  68. c->buflen = 0;
  69. c->reqlen = 0;
  70. c->tcplen = 0;
  71. c->allow_request = 0;
  72. c->outbuflen = 0;
  73. c->outbufsize = 0;
  74. c->outbufstart = 0;
  75. c->last_ping_time = 0;
  76. c->last_flushed_time = 0;
  77. c->inbudget = 0;
  78. c->outbudget = 0;
  79. if(c->inctx) {
  80. EVP_CIPHER_CTX_reset(c->inctx);
  81. free(c->inctx);
  82. c->inctx = NULL;
  83. }
  84. if(c->outctx) {
  85. EVP_CIPHER_CTX_reset(c->outctx);
  86. free(c->outctx);
  87. c->outctx = NULL;
  88. }
  89. if(c->rsa_key) {
  90. RSA_free(c->rsa_key);
  91. c->rsa_key = NULL;
  92. }
  93. }
  94. void free_connection(connection_t *c) {
  95. free_connection_partially(c);
  96. free(c->name);
  97. free(c->hostname);
  98. if(c->config_tree) {
  99. exit_configuration(&c->config_tree);
  100. }
  101. free(c);
  102. }
  103. void connection_add(connection_t *c) {
  104. avl_insert(connection_tree, c);
  105. }
  106. void connection_del(connection_t *c) {
  107. avl_delete(connection_tree, c);
  108. }
  109. void dump_connections(void) {
  110. avl_node_t *node;
  111. connection_t *c;
  112. logger(LOG_DEBUG, "Connections:");
  113. for(node = connection_tree->head; node; node = node->next) {
  114. c = node->data;
  115. logger(LOG_DEBUG, " %s at %s options %x socket %d status %04x outbuf %d/%d/%d",
  116. c->name, c->hostname, c->options, c->socket, bitfield_to_int(&c->status, sizeof(c->status)),
  117. c->outbufsize, c->outbufstart, c->outbuflen);
  118. }
  119. logger(LOG_DEBUG, "End of connections.");
  120. }