tls_util.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * TLS module
  3. *
  4. * Copyright (C) 2005 iptelorg GmbH
  5. * Copyright (C) 2013 Motorola Solutions, Inc.
  6. *
  7. * Permission to use, copy, modify, and distribute this software for any
  8. * purpose with or without fee is hereby granted, provided that the above
  9. * copyright notice and this permission notice appear in all copies.
  10. *
  11. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  12. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  13. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  14. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  15. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  16. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  17. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  18. */
  19. /*!
  20. * \file
  21. * \brief Kamailio TLS support :: Common functions
  22. * \ingroup tls
  23. * Module: \ref tls
  24. */
  25. #define _GNU_SOURCE 1 /* Needed for strndup */
  26. #include <string.h>
  27. #include <libgen.h>
  28. #include "../../mem/shm_mem.h"
  29. #include "../../globals.h"
  30. #include "../../dprint.h"
  31. #include "tls_mod.h"
  32. #include "tls_util.h"
  33. /*
  34. * Make a shared memory copy of ASCII zero terminated string
  35. * Return value: -1 on error
  36. * 0 on success
  37. */
  38. int shm_asciiz_dup(char** dest, char* val)
  39. {
  40. char* ret;
  41. int len;
  42. if (!val) {
  43. *dest = NULL;
  44. return 0;
  45. }
  46. len = strlen(val);
  47. ret = shm_malloc(len + 1);
  48. if (!ret) {
  49. ERR("No memory left\n");
  50. return -1;
  51. }
  52. memcpy(ret, val, len + 1);
  53. *dest = ret;
  54. return 0;
  55. }
  56. /*
  57. * Delete old TLS configuration that is not needed anymore
  58. */
  59. void collect_garbage(void)
  60. {
  61. tls_domains_cfg_t *prev, *cur, *next;
  62. /* Make sure we do not run two garbage collectors
  63. * at the same time
  64. */
  65. lock_get(tls_domains_cfg_lock);
  66. /* Skip the current configuration, garbage starts
  67. * with the 2nd element on the list
  68. */
  69. prev = *tls_domains_cfg;
  70. cur = (*tls_domains_cfg)->next;
  71. while(cur) {
  72. next = cur->next;
  73. if (atomic_get(&cur->ref_count) == 0) {
  74. /* Not referenced by any existing connection */
  75. prev->next = cur->next;
  76. tls_free_cfg(cur);
  77. } else {
  78. /* Only update prev if we didn't remove cur */
  79. prev = cur;
  80. }
  81. cur = next;
  82. }
  83. lock_release(tls_domains_cfg_lock);
  84. }