dlist.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * $Id: dlist.c 5160 2008-11-03 17:51:22Z henningw $
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * Kamailio is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * Kamailio is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. * History:
  23. * ========
  24. * 2006-11-28 added get_number_of_users() (Jeffrey Magder - SOMA Networks)
  25. * 2007-09-12 added partitioning support for fetching all ul contacts
  26. * (bogdan)
  27. */
  28. /*! \file
  29. * \brief USRLOC - List of registered domains
  30. * \ingroup usrloc
  31. *
  32. * - Module: \ref usrloc
  33. */
  34. #include "dlist.h"
  35. #include <stdlib.h> /* abort */
  36. #include <string.h> /* strlen, memcmp */
  37. #include <stdio.h> /* printf */
  38. #include "../../ut.h"
  39. #include "../../lib/srdb1/db.h"
  40. #include "../../mem/shm_mem.h"
  41. #include "../../dprint.h"
  42. #include "../../ip_addr.h"
  43. #include "../../socket_info.h"
  44. #include "udomain.h" /* new_udomain, free_udomain */
  45. #include "utime.h"
  46. #include "p_usrloc_mod.h"
  47. #include "ul_db_layer.h"
  48. static struct domain_list_item *domain_list;
  49. static inline struct domain_list_item * find_dlist (str *name) {
  50. struct domain_list_item *item;
  51. for (item = domain_list; item != NULL; item = item->next) {
  52. if (item->name.len == name->len
  53. && memcmp (item->name.s, name->s, name->len) == 0) {
  54. return item;
  55. }
  56. }
  57. return NULL;
  58. }
  59. static inline struct domain_list_item * add_to_dlist (str *name, int type) {
  60. struct domain_list_item *item;
  61. int i;
  62. item = (struct domain_list_item *)
  63. pkg_malloc (sizeof (struct domain_list_item));
  64. if (item == NULL) {
  65. LM_ERR("Out of shared memory.\n");
  66. return NULL;
  67. }
  68. item->name.s = (char *) pkg_malloc (name->len + 1);
  69. if (item->name.s == NULL) {
  70. LM_ERR("Out of shared memory.\n");
  71. return NULL;
  72. }
  73. memcpy (item->name.s, name->s, name->len);
  74. item->name.s[name->len] = '\0';
  75. item->name.len = name->len;
  76. memset (&item->domain, 0, sizeof (struct udomain));
  77. item->domain.name = &item->name;
  78. item->domain.dbt = type;
  79. item->domain.table = (hslot_t*)pkg_malloc(sizeof(hslot_t) * ul_hash_size);
  80. if (!item->domain.table) {
  81. LM_ERR("no memory left 2\n");
  82. return NULL;
  83. }
  84. for(i = 0; i < ul_hash_size; i++) {
  85. init_slot(&item->domain, &(item->domain.table[i]), i);
  86. }
  87. item->domain.size = ul_hash_size;
  88. /* Everything else is not useful for now. */
  89. item->next = domain_list;
  90. domain_list = item;
  91. return item;
  92. }
  93. /*!
  94. * \brief Registers a new domain with usrloc
  95. *
  96. * Registers a new domain with usrloc. If the domain exists,
  97. * a pointer to existing structure will be returned, otherwise
  98. * a new domain will be created
  99. * \param _n domain name
  100. * \param _d new created domain
  101. * \return 0 on success, -1 on failure
  102. */
  103. int register_udomain(const char *name, udomain_t **domain) {
  104. struct domain_list_item *item;
  105. str name_str;
  106. ul_domain_db_t * d;
  107. name_str.s = (char *) name;
  108. name_str.len = strlen (name);
  109. item = find_dlist (&name_str);
  110. if (item == NULL) {
  111. if((d = ul_find_domain(name)) == NULL){
  112. LM_ERR("domain %s not found.\n", name);
  113. return -1;
  114. }
  115. item = add_to_dlist (&name_str, d->dbt);
  116. }
  117. if (item == NULL) {
  118. return -1;
  119. }
  120. *domain = &item->domain;
  121. LM_DBG("found domain %.*s, type: %s\n", (*domain)->name->len, (*domain)->name->s, (((*domain)->dbt) == DB_TYPE_CLUSTER ? "cluster" : "single"));
  122. return 0;
  123. }
  124. /*!
  125. * \brief Loops through all domains summing up the number of users
  126. * \return the number of users, could be zero
  127. */
  128. unsigned long get_number_of_users(void)
  129. {
  130. int numberOfUsers = 0;
  131. LM_INFO("not available with partitioned interface");
  132. return numberOfUsers;
  133. }
  134. int get_all_ucontacts(void *buf, int len, unsigned int flags,
  135. unsigned int part_idx, unsigned int part_max)
  136. {
  137. LM_INFO("not available with partitioned interface");
  138. return -1;
  139. }
  140. /*!
  141. * \brief Run timer handler of all domains
  142. * \return 0 if all timer return 0, != 0 otherwise
  143. */
  144. int synchronize_all_udomains(void)
  145. {
  146. int res = 0;
  147. LM_INFO("not available with partitioned interface");
  148. return res;
  149. }