hash.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Hash functions for cached domain table
  3. *
  4. * Copyright (C) 2002-2003 Juha Heinanen
  5. *
  6. * This file is part of sip-router, a free SIP server.
  7. *
  8. * sip-router is free software; you can redistribute it and/or modify it under
  9. * the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 2 of the License, or (at your option)
  11. * any later version
  12. *
  13. * sip-router is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <stdio.h>
  25. #include "uid_domain_mod.h"
  26. #include "../../dprint.h"
  27. #include "../../mem/shm_mem.h"
  28. #include "../../ut.h"
  29. #include "hash.h"
  30. /*
  31. * String hash function
  32. */
  33. static unsigned int calc_hash(str *key)
  34. {
  35. char *p;
  36. unsigned int h, len, i;
  37. h = 0;
  38. p = key->s;
  39. len = key->len;
  40. for (i = 0; i < len; i++) {
  41. h = ( h << 5 ) - h + *(p + i);
  42. }
  43. return h % HASH_SIZE;
  44. }
  45. /*
  46. * Create new hash_entry structure from given key and domain
  47. */
  48. static struct hash_entry* new_hash_entry(str* key, domain_t* domain)
  49. {
  50. struct hash_entry* e;
  51. if (!key || !domain) {
  52. ERR("Invalid parameter value\n");
  53. return 0;
  54. }
  55. e = (struct hash_entry*)shm_malloc(sizeof(struct hash_entry));
  56. if (!e) {
  57. ERR("Not enough memory left\n");
  58. return 0;
  59. }
  60. e->key = *key;
  61. e->domain = domain;
  62. e->next = 0;
  63. return e;
  64. }
  65. /*
  66. * Release all memory allocated for given hash_entry structure
  67. */
  68. static void free_hash_entry(struct hash_entry* e)
  69. {
  70. if (e) shm_free(e);
  71. }
  72. /*
  73. * Free memory allocated for entire hash table
  74. */
  75. void free_table(struct hash_entry** table)
  76. {
  77. struct hash_entry* e;
  78. int i;
  79. if (!table) return;
  80. for(i = 0; i < HASH_SIZE; i++) {
  81. while(table[i]) {
  82. e = table[i];
  83. table[i] = table[i]->next;
  84. free_hash_entry(e);
  85. }
  86. }
  87. }
  88. /*
  89. * Generate hash table, use domain names as hash keys
  90. */
  91. int gen_domain_table(struct hash_entry** table, domain_t* list)
  92. {
  93. struct hash_entry* e;
  94. unsigned int slot;
  95. int i;
  96. if (!table) {
  97. ERR("Invalid parameter value\n");
  98. return -1;
  99. }
  100. while(list) {
  101. for(i = 0; i < list->n; i++) {
  102. e = new_hash_entry(&list->domain[i], list);
  103. if (!e) goto error;
  104. slot = calc_hash(&list->domain[i]);
  105. e->next = table[slot];
  106. table[slot] = e;
  107. }
  108. list = list->next;
  109. }
  110. return 0;
  111. error:
  112. free_table(table);
  113. return -1;
  114. }
  115. /*
  116. * Generate hash table, use did as hash key
  117. */
  118. int gen_did_table(struct hash_entry** table, domain_t* list)
  119. {
  120. unsigned int slot;
  121. struct hash_entry* e;
  122. if (!table) {
  123. ERR("Invalid parameter value\n");
  124. return -1;
  125. }
  126. while(list) {
  127. e = new_hash_entry(&list->did, list);
  128. if (!e) goto error;
  129. slot = calc_hash(&list->did);
  130. e->next = table[slot];
  131. table[slot] = e;
  132. list = list->next;
  133. }
  134. return 0;
  135. error:
  136. free_table(table);
  137. return -1;
  138. }
  139. /*
  140. * Lookup key in the table
  141. */
  142. int hash_lookup(domain_t** d, struct hash_entry** table, str* key)
  143. {
  144. struct hash_entry* np;
  145. for (np = table[calc_hash(key)]; np != NULL; np = np->next) {
  146. if ((np->key.len == key->len) &&
  147. (strncmp(np->key.s, key->s, key->len) == 0)) {
  148. if (d) *d = np->domain;
  149. return 1;
  150. }
  151. }
  152. if (d) *d = 0;
  153. return -1;
  154. }