/* * ircd-ratbox: A slightly useful ircd. * hash.c: Maintains hashtables. * * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center * Copyright (C) 1996-2002 Hybrid Development Team * Copyright (C) 2002-2005 ircd-ratbox development team * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA * * $Id: hash.c 3177 2007-02-01 00:19:14Z jilles $ */ #include "stdinc.h" #include "ircd_defs.h" #include "s_conf.h" #include "channel.h" #include "client.h" #include "common.h" #include "hash.h" #include "match.h" #include "ircd.h" #include "numeric.h" #include "send.h" #include "msg.h" #include "cache.h" #include "s_newconf.h" #include "s_assert.h" #include "irc_dictionary.h" #include "irc_radixtree.h" rb_dlink_list *hostTable; struct Dictionary *client_connid_tree = NULL; struct Dictionary *client_zconnid_tree = NULL; struct irc_radixtree *client_id_tree = NULL; struct irc_radixtree *client_name_tree = NULL; struct irc_radixtree *channel_tree = NULL; struct irc_radixtree *resv_tree = NULL; /* * look in whowas.c for the missing ...[WW_MAX]; entry */ /* init_hash() * * clears the various hashtables */ void init_hash(void) { hostTable = rb_malloc(sizeof(rb_dlink_list) * HOST_MAX); client_connid_tree = irc_dictionary_create("client connid", irc_uint32cmp); client_zconnid_tree = irc_dictionary_create("client zconnid", irc_uint32cmp); client_id_tree = irc_radixtree_create("client id", NULL); client_name_tree = irc_radixtree_create("client name", irc_radixtree_irccasecanon); channel_tree = irc_radixtree_create("channel", irc_radixtree_irccasecanon); resv_tree = irc_radixtree_create("resv", irc_radixtree_irccasecanon); } u_int32_t fnv_hash_upper(const unsigned char *s, int bits) { u_int32_t h = FNV1_32_INIT; while (*s) { h ^= ToUpper(*s++); h += (h<<1) + (h<<4) + (h<<7) + (h << 8) + (h << 24); } if (bits < 32) h = ((h >> bits) ^ h) & ((1<> bits) ^ h) & ((1<> bits) ^ h) & ((1<> bits) ^ h) & ((1< CHANNELLEN) { char *t; if(IsServer(client_p)) { sendto_realops_snomask(SNO_DEBUG, L_ALL, "*** Long channel name from %s (%d > %d): %s", client_p->name, len, CHANNELLEN, s); } len = CHANNELLEN; t = LOCAL_COPY(s); *(t + CHANNELLEN) = '\0'; s = t; } chptr = irc_radixtree_retrieve(channel_tree, s); if (chptr != NULL) { if (isnew != NULL) *isnew = 0; return chptr; } if(isnew != NULL) *isnew = 1; chptr = allocate_channel(s); chptr->channelts = rb_current_time(); /* doesn't hurt to set it here */ rb_dlinkAdd(chptr, &chptr->node, &global_channel_list); irc_radixtree_add(channel_tree, chptr->chname, chptr); return chptr; } /* hash_find_resv() * * hunts for a resv entry in the resv hash table */ struct ConfItem * hash_find_resv(const char *name) { struct ConfItem *aconf; s_assert(name != NULL); if(EmptyString(name)) return NULL; aconf = irc_radixtree_retrieve(resv_tree, name); if (aconf != NULL) { aconf->port++; return aconf; } return NULL; } void clear_resv_hash(void) { struct ConfItem *aconf; struct irc_radixtree_iteration_state iter; IRC_RADIXTREE_FOREACH(aconf, &iter, resv_tree) { /* skip temp resvs */ if(aconf->hold) continue; irc_radixtree_delete(resv_tree, aconf->host); free_conf(aconf); } } void add_to_zconnid_hash(struct Client *client_p) { irc_dictionary_add(client_zconnid_tree, IRC_UINT_TO_POINTER(client_p->localClient->zconnid), client_p); } void del_from_zconnid_hash(struct Client *client_p) { irc_dictionary_delete(client_zconnid_tree, IRC_UINT_TO_POINTER(client_p->localClient->zconnid)); } void add_to_cli_connid_hash(struct Client *client_p) { irc_dictionary_add(client_connid_tree, IRC_UINT_TO_POINTER(client_p->localClient->connid), client_p); } void del_from_cli_connid_hash(struct Client *client_p) { irc_dictionary_delete(client_connid_tree, IRC_UINT_TO_POINTER(client_p->localClient->connid)); } struct Client * find_cli_connid_hash(uint32_t connid) { struct Client *target_p; target_p = irc_dictionary_retrieve(client_connid_tree, IRC_UINT_TO_POINTER(connid)); if (target_p != NULL) return target_p; target_p = irc_dictionary_retrieve(client_zconnid_tree, IRC_UINT_TO_POINTER(connid)); if (target_p != NULL) return target_p; return NULL; } static void output_hash(struct Client *source_p, const char *name, int length, int *counts, unsigned long deepest) { unsigned long total = 0; int i; char buf[128]; sendto_one_numeric(source_p, RPL_STATSDEBUG, "B :%s Hash Statistics", name); snprintf(buf, sizeof buf, "%.3f%%", (float) ((counts[0]*100) / (float) length)); sendto_one_numeric(source_p, RPL_STATSDEBUG, "B :Size: %d Empty: %d (%s)", length, counts[0], buf); for(i = 1; i < 11; i++) { total += (counts[i] * i); } /* dont want to divide by 0! --fl */ if(counts[0] != length) { snprintf(buf, sizeof buf, "%.3f/%.3f", (float) (total / (length - counts[0])), (float) (total / length)); sendto_one_numeric(source_p, RPL_STATSDEBUG, "B :Average depth: %s Highest depth: %lu", buf, deepest); } } static void count_hash(struct Client *source_p, rb_dlink_list *table, int length, const char *name) { int counts[11]; unsigned long deepest = 0; int i; memset(counts, 0, sizeof(counts)); for(i = 0; i < length; i++) { if(rb_dlink_list_length(&table[i]) >= 10) counts[10]++; else counts[rb_dlink_list_length(&table[i])]++; if(rb_dlink_list_length(&table[i]) > deepest) deepest = rb_dlink_list_length(&table[i]); } output_hash(source_p, name, length, counts, deepest); } void hash_stats(struct Client *source_p) { count_hash(source_p, hostTable, HOST_MAX, "Hostname"); }