domain/hash.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "../../dprint.h"
00027 #include "../../ut.h"
00028 #include "../../hash_func.h"
00029 #include "../../mem/shm_mem.h"
00030 #include "../../mi/mi.h"
00031 #include "domain_mod.h"
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include <stdio.h>
00035 #include <ctype.h>
00036
00037 #define dom_hash(_s) core_case_hash( _s, 0, DOM_HASH_SIZE)
00038
00039
00040
00041 int hash_table_install (struct domain_list **hash_table, char *domain)
00042 {
00043 struct domain_list *np;
00044 unsigned int hash_val;
00045
00046 np = (struct domain_list *) shm_malloc(sizeof(*np));
00047 if (np == NULL) {
00048 LM_ERR("Cannot allocate memory for hash table entry\n");
00049 return -1;
00050 }
00051
00052 np->domain.len = strlen(domain);
00053 np->domain.s = (char *) shm_malloc(np->domain.len);
00054 if (np->domain.s == NULL) {
00055 LM_ERR("Cannot allocate memory for domain string\n");
00056 shm_free(np);
00057 return -1;
00058 }
00059 (void) strncpy(np->domain.s, domain, np->domain.len);
00060
00061 hash_val = dom_hash(&np->domain);
00062 np->next = hash_table[hash_val];
00063 hash_table[hash_val] = np;
00064
00065 return 1;
00066 }
00067
00068
00069
00070 int hash_table_lookup (str *domain)
00071 {
00072 struct domain_list *np;
00073
00074 for (np = (*hash_table)[dom_hash(domain)]; np != NULL; np = np->next) {
00075 if ((np->domain.len == domain->len) &&
00076 (strncasecmp(np->domain.s, domain->s, domain->len) == 0)) {
00077 return 1;
00078 }
00079 }
00080
00081 return -1;
00082 }
00083
00084
00085 int hash_table_mi_print(struct domain_list **hash_table, struct mi_node* rpl)
00086 {
00087 int i;
00088 struct domain_list *np;
00089 struct mi_node* node;
00090
00091 if(hash_table==0)
00092 return -1;
00093 for (i = 0; i < DOM_HASH_SIZE; i++) {
00094 np = hash_table[i];
00095 while (np) {
00096 node = add_mi_node_child(rpl, 0, 0, 0,
00097 np->domain.s, np->domain.len);
00098 if(node == 0)
00099 return -1;
00100
00101 np = np->next;
00102 }
00103 }
00104 return 0;
00105 }
00106
00107
00108 void hash_table_free (struct domain_list **hash_table)
00109 {
00110 int i;
00111 struct domain_list *np, *next;
00112
00113 if(hash_table==0)
00114 return;
00115
00116 for (i = 0; i < DOM_HASH_SIZE; i++) {
00117 np = hash_table[i];
00118 while (np) {
00119 shm_free(np->domain.s);
00120 next = np->next;
00121 shm_free(np);
00122 np = next;
00123 }
00124 hash_table[i] = NULL;
00125 }
00126 }