domain/hash.c

Go to the documentation of this file.
00001 /*
00002  * $Id: hash.c 4518 2008-07-28 15:39:28Z henningw $
00003  *
00004  * Hash functions for cached domain table
00005  *
00006  * Copyright (C) 2002-2008 Juha Heinanen
00007  *
00008  * This file is part of Kamailio, a free SIP server.
00009  *
00010  * Kamailio is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * Kamailio is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License 
00021  * along with this program; if not, write to the Free Software 
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 /* Add domain to hash table */
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 /* Check if domain exists in hash table */
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 /* Free contents of hash table */
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 }

Generated on Tue May 22 16:00:27 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6