connection.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. connection.c -- connection list management
  3. Copyright (C) 2000-2012 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. gettimeofday(&c->start, NULL);
  46. return c;
  47. }
  48. void free_connection(connection_t *c) {
  49. if(c->name)
  50. free(c->name);
  51. if(c->hostname)
  52. free(c->hostname);
  53. if(c->inkey)
  54. free(c->inkey);
  55. if(c->outkey)
  56. free(c->outkey);
  57. if(c->inctx) {
  58. EVP_CIPHER_CTX_cleanup(c->inctx);
  59. free(c->inctx);
  60. }
  61. if(c->outctx) {
  62. EVP_CIPHER_CTX_cleanup(c->outctx);
  63. free(c->outctx);
  64. }
  65. if(c->mychallenge)
  66. free(c->mychallenge);
  67. if(c->hischallenge)
  68. free(c->hischallenge);
  69. if(c->config_tree)
  70. exit_configuration(&c->config_tree);
  71. if(c->outbuf)
  72. free(c->outbuf);
  73. if(c->rsa_key)
  74. RSA_free(c->rsa_key);
  75. free(c);
  76. }
  77. void connection_add(connection_t *c) {
  78. avl_insert(connection_tree, c);
  79. }
  80. void connection_del(connection_t *c) {
  81. avl_delete(connection_tree, c);
  82. }
  83. void dump_connections(void) {
  84. avl_node_t *node;
  85. connection_t *c;
  86. logger(LOG_DEBUG, "Connections:");
  87. for(node = connection_tree->head; node; node = node->next) {
  88. c = node->data;
  89. logger(LOG_DEBUG, " %s at %s options %x socket %d status %04x outbuf %d/%d/%d",
  90. c->name, c->hostname, c->options, c->socket, bitfield_to_int(&c->status, sizeof c->status),
  91. c->outbufsize, c->outbufstart, c->outbuflen);
  92. }
  93. logger(LOG_DEBUG, "End of connections.");
  94. }