tls_util.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * $Id$
  3. *
  4. * TLS module - common functions
  5. *
  6. * Copyright (C) 2001-2003 FhG FOKUS
  7. * Copyright (C) 2004,2005 Free Software Foundation, Inc.
  8. * Copyright (C) 2005 iptelorg GmbH
  9. *
  10. * This file is part of SIP-router, a free SIP server.
  11. *
  12. * SIP-router is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version
  16. *
  17. * SIP-router is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. */
  26. #define _GNU_SOURCE 1 /* Needed for strndup */
  27. #include <string.h>
  28. #include <libgen.h>
  29. #include "../../mem/shm_mem.h"
  30. #include "../../globals.h"
  31. #include "tls_mod.h"
  32. #include "tls_util.h"
  33. /*!
  34. * \file
  35. * \brief SIP-router TLS support :: Common functions
  36. * \ingroup tls
  37. * Module: \ref tls
  38. */
  39. /*
  40. * Make a shared memory copy of ASCII zero terminated string
  41. * Return value: -1 on error
  42. * 0 on success
  43. */
  44. int shm_asciiz_dup(char** dest, char* val)
  45. {
  46. char* ret;
  47. int len;
  48. if (!val) {
  49. *dest = NULL;
  50. return 0;
  51. }
  52. len = strlen(val);
  53. ret = shm_malloc(len + 1);
  54. if (!ret) {
  55. ERR("No memory left\n");
  56. return -1;
  57. }
  58. memcpy(ret, val, len + 1);
  59. *dest = ret;
  60. return 0;
  61. }
  62. /*
  63. * Delete old TLS configuration that is not needed anymore
  64. */
  65. void collect_garbage(void)
  66. {
  67. tls_domains_cfg_t* prev, *cur;
  68. /* Make sure we do not run two garbage collectors
  69. * at the same time
  70. */
  71. lock_get(tls_domains_cfg_lock);
  72. /* Skip the current configuration, garbage starts
  73. * with the 2nd element on the list
  74. */
  75. prev = *tls_domains_cfg;
  76. cur = (*tls_domains_cfg)->next;
  77. while(cur) {
  78. if (cur->ref_count == 0) {
  79. /* Not referenced by any existing connection */
  80. prev->next = cur->next;
  81. tls_free_cfg(cur);
  82. }
  83. prev = cur;
  84. cur = cur->next;
  85. }
  86. lock_release(tls_domains_cfg_lock);
  87. }