Reverting to 398.. trying again with native charybdis hash

This commit is contained in:
Valery Yatsko 2008-04-02 14:16:31 +04:00
parent 002bc9e82c
commit ae78a57163
6 changed files with 913 additions and 980 deletions

View file

@ -288,8 +288,8 @@ struct LocalUser
auth_request_t *auth_request; auth_request_t *auth_request;
/* target change stuff */ /* target change stuff */
void *targets[10]; /* targets were aware of */ uint32_t targets[10]; /* targets were aware of (fnv32(use_id(target_p))) */
rb_uint8_t targinfo[2]; /* cyclic array, no in use */ unsigned int targinfo[2]; /* cyclic array, no in use */
time_t target_last; /* last time we cleared a slot */ time_t target_last; /* last time we cleared a slot */
list_client_t *safelist_data; list_client_t *safelist_data;

View file

@ -4,7 +4,7 @@
* *
* Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
* Copyright (C) 1996-2002 Hybrid Development Team * Copyright (C) 1996-2002 Hybrid Development Team
* Copyright (C) 2002-2005 ircd-ratbox development team * Copyright (C) 2002-2004 ircd-ratbox development team
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -18,98 +18,86 @@
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA * USA
* *
* $Id: hash.h 24794 2007-12-28 02:36:59Z androsyn $ * $Id: hash.h 3177 2007-02-01 00:19:14Z jilles $
*/ */
#ifndef INCLUDED_hash_h #ifndef INCLUDED_hash_h
#define INCLUDED_hash_h #define INCLUDED_hash_h
extern struct Dictionary *nd_dict; struct Dictionary;
extern rb_dlink_list resvTable[]; extern rb_dlink_list *clientTable;
extern rb_dlink_list ndTable[]; extern rb_dlink_list *channelTable;
extern rb_dlink_list *idTable;
extern rb_dlink_list *resvTable;
extern rb_dlink_list *hostTable;
extern rb_dlink_list *helpTable;
extern struct Dictionary *nd_dict;
/* Magic value for FNV hash functions */ /* Magic value for FNV hash functions */
#define FNV1_32_INIT 0x811c9dc5UL #define FNV1_32_INIT 0x811c9dc5UL
/* Client hash table size, used in hash.c/s_debug.c */ /* Client hash table size, used in hash.c/s_debug.c */
#define U_MAX_BITS 17 #define U_MAX_BITS 17
#define U_MAX (1<<U_MAX_BITS) #define U_MAX 131072 /* 2^17 */
/* Client fd hash table size, used in hash.c */
#define CLI_FD_MAX 4096
/* Channel hash table size, hash.c/s_debug.c */ /* Channel hash table size, hash.c/s_debug.c */
#define CH_MAX_BITS 16 #define CH_MAX_BITS 16
#define CH_MAX (1<<CH_MAX_BITS) /* 2^16 */ #define CH_MAX 65536 /* 2^16 */
/* hostname hash table size */ /* hostname hash table size */
#define HOST_MAX_BITS 17 #define HOST_MAX_BITS 17
#define HOST_MAX (1<<HOST_MAX_BITS) /* 2^17 */ #define HOST_MAX 131072 /* 2^17 */
/* RESV/XLINE hash table size, used in hash.c */ /* RESV/XLINE hash table size, used in hash.c */
#define R_MAX_BITS 10 #define R_MAX_BITS 10
#define R_MAX (1<<R_MAX_BITS) /* 2^10 */ #define R_MAX 1024 /* 2^10 */
#define HASH_WALK(i, max, ptr, table) for (i = 0; i < max; i++) { RB_DLINK_FOREACH(ptr, table[i].head) #define HASH_WALK(i, max, ptr, table) for (i = 0; i < max; i++) { RB_DLINK_FOREACH(ptr, table[i].head)
#define HASH_WALK_SAFE(i, max, ptr, nptr, table) for (i = 0; i < max; i++) { RB_DLINK_FOREACH_SAFE(ptr, nptr, table[i].head) #define HASH_WALK_SAFE(i, max, ptr, nptr, table) for (i = 0; i < max; i++) { RB_DLINK_FOREACH_SAFE(ptr, nptr, table[i].head)
#define HASH_WALK_END } #define HASH_WALK_END }
typedef enum
{
HASH_CLIENT,
HASH_ID,
HASH_CHANNEL,
HASH_HOSTNAME,
HASH_RESV
} hash_type;
struct Client; struct Client;
struct Channel; struct Channel;
struct ConfItem; struct ConfItem;
struct cachefile; struct cachefile;
struct nd_entry; struct nd_entry;
uint32_t fnv_hash_upper(const unsigned char *s, unsigned int bits, unsigned int unused); extern u_int32_t fnv_hash_upper(const unsigned char *s, int bits);
uint32_t fnv_hash(const unsigned char *s, unsigned int bits, unsigned int unused); extern u_int32_t fnv_hash(const unsigned char *s, int bits);
uint32_t fnv_hash_len(const unsigned char *s, unsigned int bits, unsigned int len); extern u_int32_t fnv_hash_len(const unsigned char *s, int bits, int len);
uint32_t fnv_hash_upper_len(const unsigned char *s, unsigned int bits, unsigned int len); extern u_int32_t fnv_hash_upper_len(const unsigned char *s, int bits, int len);
void init_hash(void); extern void init_hash(void);
void add_to_hash(hash_type, const char *, void *); extern void add_to_client_hash(const char *name, struct Client *client);
void del_from_hash(hash_type, const char *, void *); extern void del_from_client_hash(const char *name, struct Client *client);
extern struct Client *find_client(const char *name);
extern struct Client *find_named_client(const char *name);
extern struct Client *find_server(struct Client *source_p, const char *name);
struct Client *find_any_client(const char *name); extern void add_to_id_hash(const char *, struct Client *);
struct Client *find_client(const char *name); extern void del_from_id_hash(const char *name, struct Client *client);
struct Client *find_named_client(const char *name); extern struct Client *find_id(const char *name);
struct Client *find_server(struct Client *source_p, const char *name);
struct Client *find_id(const char *name); extern struct Channel *get_or_create_channel(struct Client *client_p, const char *chname, int *isnew);
extern void del_from_channel_hash(const char *name, struct Channel *chan);
extern struct Channel *find_channel(const char *name);
struct Channel *get_or_create_channel(struct Client *client_p, const char *chname, int *isnew); extern void add_to_hostname_hash(const char *, struct Client *);
struct Channel *find_channel(const char *name); extern void del_from_hostname_hash(const char *, struct Client *);
extern rb_dlink_node *find_hostname(const char *);
rb_dlink_node *find_hostname(const char *); extern void add_to_resv_hash(const char *name, struct ConfItem *aconf);
extern void del_from_resv_hash(const char *name, struct ConfItem *aconf);
extern struct ConfItem *hash_find_resv(const char *name);
extern void clear_resv_hash(void);
struct ConfItem *hash_find_resv(const char *name); extern void hash_stats(struct Client *);
void clear_resv_hash(void);
void add_to_help_hash(const char *name, struct cachefile *hptr);
void clear_help_hash(void);
struct cachefile *hash_find_help(const char *name, int flags);
void add_to_nd_hash(const char *name, struct nd_entry *nd);
struct nd_entry *hash_find_nd(const char *name);
void add_to_cli_fd_hash(struct Client *client_p);
void del_from_cli_fd_hash(struct Client *client_p);
struct Client *find_cli_fd_hash(int fd);
void hash_stats(struct Client *);
#endif /* INCLUDED_hash_h */ #endif /* INCLUDED_hash_h */

View file

@ -250,9 +250,6 @@ struct nd_entry
{ {
char name[NICKLEN+1]; char name[NICKLEN+1];
time_t expire; time_t expire;
unsigned int hashv;
rb_dlink_node hnode; /* node in hash */
rb_dlink_node lnode; /* node in ll */ rb_dlink_node lnode; /* node in ll */
}; };

View file

@ -140,7 +140,8 @@ extern int errno;
# define __noreturn # define __noreturn
#endif #endif
/* XXX must be removed in future!!! -- dwr */
#ifdef strdupa #ifdef strdupa
#define LOCAL_COPY(s) strdupa(s) #define LOCAL_COPY(s) strdupa(s)
#else #else
@ -149,28 +150,4 @@ extern int errno;
#else #else
# define LOCAL_COPY(s) strcpy(alloca(strlen(s) + 1), s) /* XXX Is that allowed? */ # define LOCAL_COPY(s) strcpy(alloca(strlen(s) + 1), s) /* XXX Is that allowed? */
#endif /* defined(__INTEL_COMPILER) || defined(__GNUC__) */ #endif /* defined(__INTEL_COMPILER) || defined(__GNUC__) */
/* LOCAL_COPY_N copies n part of string and adds one to terminate the string */
#ifdef strndupa
#define LOCAL_COPY_N(s, n) strndupa(s, n)
#else
#if defined(__INTEL_COMPILER) || defined(__GNUC__)
#define LOCAL_COPY_N(s, n) __extension__({ size_t _l = strlen(s); _l = n > _l ? _l : n; char *_s = alloca(_l+1); memcpy(_s, s, _l); _s[_l] = '\0' ; _s; })
#else
#define LOCAL_COPY_N(s, n) xc_strlcpy(alloca(strlen(s)+1), s, n)
INLINE_FUNC size_t
xc_strlcpy(char *dest, const char *src, size_t size)
{
size_t ret = strlen(src);
if (size) {
size_t len = (ret >= size) ? size-1 : ret;
memcpy(dest, src, len);
dest[len] = '\0';
}
return dest;
}
#endif /* defined(__INTEL_COMPILER) || defined(__GNUC__) */
#endif /* strndupa */
#endif /* strdupa */ #endif /* strdupa */

View file

@ -564,19 +564,30 @@ expire_tgchange(void *unused)
static int static int
add_target(struct Client *source_p, struct Client *target_p) add_target(struct Client *source_p, struct Client *target_p)
{ {
unsigned int i, j; int i, j;
/* messaging themselves, doesnt incur any penalties */ uint32_t hashv;
if(source_p == target_p)
/* can msg themselves or services without using any target slots */
if(source_p == target_p || IsService(target_p))
return 1; return 1;
/* special condition for those who have had PRIVMSG crippled to allow them
* to talk to IRCops still.
*
* XXX: is this controversial?
*/
if(source_p->localClient->target_last > rb_current_time() && IsOper(target_p))
return 1;
hashv = fnv_hash_upper((const unsigned char *)use_id(target_p), 32);
if(USED_TARGETS(source_p)) if(USED_TARGETS(source_p))
{ {
/* hunt for an existing target */ /* hunt for an existing target */
for(i = PREV_FREE_TARGET(source_p), j = USED_TARGETS(source_p); for(i = PREV_FREE_TARGET(source_p), j = USED_TARGETS(source_p);
j; j; --j, PREV_TARGET(i))
--j, PREV_TARGET(i))
{ {
if(source_p->localClient->targets[i] == target_p) if(source_p->localClient->targets[i] == hashv)
return 1; return 1;
} }
@ -601,6 +612,7 @@ add_target(struct Client *source_p, struct Client *target_p)
/* cant clear any, full target list */ /* cant clear any, full target list */
else if(USED_TARGETS(source_p) == 10) else if(USED_TARGETS(source_p) == 10)
{ {
ServerStats->is_tgch++;
add_tgchange(source_p->sockhost); add_tgchange(source_p->sockhost);
return 0; return 0;
} }
@ -614,7 +626,7 @@ add_target(struct Client *source_p, struct Client *target_p)
SetTGChange(source_p); SetTGChange(source_p);
} }
source_p->localClient->targets[FREE_TARGET(source_p)] = target_p; source_p->localClient->targets[FREE_TARGET(source_p)] = hashv;
NEXT_TARGET(FREE_TARGET(source_p)); NEXT_TARGET(FREE_TARGET(source_p));
++USED_TARGETS(source_p); ++USED_TARGETS(source_p);
return 1; return 1;

View file

@ -18,39 +18,36 @@
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA * USA
* *
* $Id: hash.c 25119 2008-03-13 16:57:05Z androsyn $ * $Id: hash.c 3177 2007-02-01 00:19:14Z jilles $
*/ */
#include "stdinc.h" #include "stdinc.h"
#include "ircd_defs.h"
#include "s_conf.h" #include "s_conf.h"
#include "channel.h" #include "channel.h"
#include "client.h" #include "client.h"
#include "common.h"
#include "hash.h" #include "hash.h"
#include "irc_string.h"
#include "ircd.h" #include "ircd.h"
#include "numeric.h" #include "numeric.h"
#include "send.h" #include "send.h"
#include "msg.h"
#include "cache.h" #include "cache.h"
#include "s_newconf.h" #include "s_newconf.h"
#define hash_nick(x) (fnv_hash_upper((const unsigned char *)(x), U_MAX_BITS, 0)) rb_dlink_list *clientTable;
#define hash_id(x) (fnv_hash((const unsigned char *)(x), U_MAX_BITS, 0)) rb_dlink_list *channelTable;
#define hash_channel(x) (fnv_hash_upper_len((const unsigned char *)(x), CH_MAX_BITS, 30)) rb_dlink_list *idTable;
#define hash_hostname(x) (fnv_hash_upper_len((const unsigned char *)(x), HOST_MAX_BITS, 30)) rb_dlink_list *resvTable;
#define hash_resv(x) (fnv_hash_upper_len((const unsigned char *)(x), R_MAX_BITS, 30)) rb_dlink_list *hostTable;
#define hash_cli_fd(x) (x % CLI_FD_MAX)
/*
static rb_dlink_list clientbyfdTable[U_MAX]; * look in whowas.c for the missing ...[WW_MAX]; entry
static rb_dlink_list clientTable[U_MAX]; */
static rb_dlink_list channelTable[CH_MAX];
static rb_dlink_list idTable[U_MAX];
rb_dlink_list resvTable[R_MAX];
static rb_dlink_list hostTable[HOST_MAX];
static rb_dlink_list helpTable[HELP_MAX];
rb_dlink_list ndTable[U_MAX];
/* /*
* Hashing. * Hashing.
@ -92,162 +89,287 @@ rb_dlink_list ndTable[U_MAX];
void void
init_hash(void) init_hash(void)
{ {
/* nothing to do here */ clientTable = rb_malloc(sizeof(rb_dlink_list) * U_MAX);
idTable = rb_malloc(sizeof(rb_dlink_list) * U_MAX);
channelTable = rb_malloc(sizeof(rb_dlink_list) * CH_MAX);
hostTable = rb_malloc(sizeof(rb_dlink_list) * HOST_MAX);
resvTable = rb_malloc(sizeof(rb_dlink_list) * R_MAX);
} }
#ifndef RICER_HASHING
uint32_t u_int32_t
fnv_hash_upper(const unsigned char *s, unsigned int bits, unsigned int unused) fnv_hash_upper(const unsigned char *s, int bits)
{ {
uint32_t h = FNV1_32_INIT; u_int32_t h = FNV1_32_INIT;
bits = 32 - bits;
while (*s) while (*s)
{ {
h ^= ToUpper(*s++); h ^= ToUpper(*s++);
h += (h<<1) + (h<<4) + (h<<7) + (h << 8) + (h << 24); h += (h<<1) + (h<<4) + (h<<7) + (h << 8) + (h << 24);
} }
h = (h >> bits) ^ (h & ((2^bits)-1)); if (bits < 32)
h = ((h >> bits) ^ h) & ((1<<bits)-1);
return h; return h;
} }
uint32_t u_int32_t
fnv_hash(const unsigned char *s, unsigned int bits, unsigned int unused) fnv_hash(const unsigned char *s, int bits)
{ {
uint32_t h = FNV1_32_INIT; u_int32_t h = FNV1_32_INIT;
bits = 32 - bits;
while (*s) while (*s)
{ {
h ^= *s++; h ^= *s++;
h += (h<<1) + (h<<4) + (h<<7) + (h << 8) + (h << 24); h += (h<<1) + (h<<4) + (h<<7) + (h << 8) + (h << 24);
} }
h = (h >> bits) ^ (h & ((2^bits)-1)); if (bits < 32)
h = ((h >> bits) ^ h) & ((1<<bits)-1);
return h; return h;
} }
uint32_t u_int32_t
fnv_hash_len(const unsigned char *s, unsigned int bits, unsigned int len) fnv_hash_len(const unsigned char *s, int bits, int len)
{ {
uint32_t h = FNV1_32_INIT; u_int32_t h = FNV1_32_INIT;
bits = 32 - bits;
const unsigned char *x = s + len; const unsigned char *x = s + len;
while (*s && s < x) while (*s && s < x)
{ {
h ^= *s++; h ^= *s++;
h += (h<<1) + (h<<4) + (h<<7) + (h << 8) + (h << 24); h += (h<<1) + (h<<4) + (h<<7) + (h << 8) + (h << 24);
} }
h = (h >> bits) ^ (h & ((2^bits)-1)); if (bits < 32)
h = ((h >> bits) ^ h) & ((1<<bits)-1);
return h; return h;
} }
uint32_t u_int32_t
fnv_hash_upper_len(const unsigned char *s, unsigned int bits, unsigned int len) fnv_hash_upper_len(const unsigned char *s, int bits, int len)
{ {
uint32_t h = FNV1_32_INIT; u_int32_t h = FNV1_32_INIT;
bits = 32 - bits;
const unsigned char *x = s + len; const unsigned char *x = s + len;
while (*s && s < x) while (*s && s < x)
{ {
h ^= ToUpper(*s++); h ^= ToUpper(*s++);
h += (h<<1) + (h<<4) + (h<<7) + (h << 8) + (h << 24); h += (h<<1) + (h<<4) + (h<<7) + (h << 8) + (h << 24);
} }
h = (h >> bits) ^ (h & ((2^bits)-1)); if (bits < 32)
h = ((h >> bits) ^ h) & ((1<<bits)-1);
return h; return h;
} }
#endif
static unsigned int /* hash_nick()
hash_help(const char *name) *
* hashes a nickname, first converting to lowercase
*/
static u_int32_t
hash_nick(const char *name)
{ {
unsigned int h = 0; return fnv_hash_upper((const unsigned char *) name, U_MAX_BITS);
while(*name)
{
h += (unsigned int) (ToLower(*name++) & 0xDF);
} }
return (h % HELP_MAX); /* hash_id()
*
* hashes an id, case is kept
*/
static u_int32_t
hash_id(const char *name)
{
return fnv_hash((const unsigned char *) name, U_MAX_BITS);
} }
static struct _hash_function /* hash_channel()
*
* hashes a channel name, based on first 30 chars only for efficiency
*/
static u_int32_t
hash_channel(const char *name)
{ {
uint32_t (*func) (unsigned const char *, unsigned int, unsigned int); return fnv_hash_upper_len((const unsigned char *) name, CH_MAX_BITS, 30);
rb_dlink_list *table; }
unsigned int hashbits;
unsigned int hashlen;
} hash_function[] = {
{ fnv_hash_upper, clientTable, U_MAX_BITS, 0 },
{ fnv_hash, idTable, U_MAX_BITS, 0 },
{ fnv_hash_upper_len, channelTable, CH_MAX_BITS, 30 },
{ fnv_hash_upper_len, hostTable, HOST_MAX_BITS, 30 },
{ fnv_hash_upper_len, resvTable, R_MAX_BITS, 30 }
};
/* hash_hostname()
*
* hashes a hostname, based on first 30 chars only, as thats likely to
* be more dynamic than rest.
*/
static u_int32_t
hash_hostname(const char *name)
{
return fnv_hash_upper_len((const unsigned char *) name, HOST_MAX_BITS, 30);
}
/* hash_resv()
*
* hashes a resv channel name, based on first 30 chars only
*/
static u_int32_t
hash_resv(const char *name)
{
return fnv_hash_upper_len((const unsigned char *) name, R_MAX_BITS, 30);
}
/* add_to_id_hash()
*
* adds an entry to the id hash table
*/
void void
add_to_hash(hash_type type, const char *hashindex, void *pointer) add_to_id_hash(const char *name, struct Client *client_p)
{
rb_dlink_list *table = hash_function[type].table;
unsigned int hashv;
if(EmptyString(hashindex) || (pointer == NULL))
return;
hashv = (hash_function[type].func)((const unsigned char *) hashindex,
hash_function[type].hashbits,
hash_function[type].hashlen);
// rb_dlinkAddAlloc(pointer, &hash_function[type].table[hashv]);
rb_dlinkAddAlloc(pointer, &table[hashv]);
}
void
del_from_hash(hash_type type, const char *hashindex, void *pointer)
{
rb_dlink_list *table = hash_function[type].table;
unsigned int hashv;
if(EmptyString(hashindex) || (pointer == NULL))
return;
hashv = (hash_function[type].func)((const unsigned char *) hashindex,
hash_function[type].hashbits,
hash_function[type].hashlen);
rb_dlinkFindDestroy(pointer, &table[hashv]);
}
void
add_to_help_hash(const char *name, struct cachefile *hptr)
{ {
unsigned int hashv; unsigned int hashv;
if(EmptyString(name) || hptr == NULL) if(EmptyString(name) || (client_p == NULL))
return; return;
hashv = hash_help(name); hashv = hash_id(name);
rb_dlinkAddAlloc(hptr, &helpTable[hashv]); rb_dlinkAddAlloc(client_p, &idTable[hashv]);
} }
/* add_to_client_hash()
*
* adds an entry (client/server) to the client hash table
*/
void void
add_to_nd_hash(const char *name, struct nd_entry *nd) add_to_client_hash(const char *name, struct Client *client_p)
{ {
nd->hashv = hash_nick(name); unsigned int hashv;
rb_dlinkAdd(nd, &nd->hnode, &ndTable[nd->hashv]);
s_assert(name != NULL);
s_assert(client_p != NULL);
if(EmptyString(name) || (client_p == NULL))
return;
hashv = hash_nick(name);
rb_dlinkAddAlloc(client_p, &clientTable[hashv]);
} }
/* add_to_hostname_hash()
*
* adds a client entry to the hostname hash table
*/
void void
clear_help_hash(void) add_to_hostname_hash(const char *hostname, struct Client *client_p)
{ {
rb_dlink_node *ptr; unsigned int hashv;
rb_dlink_node *next_ptr;
int i;
HASH_WALK_SAFE(i, HELP_MAX, ptr, next_ptr, helpTable) s_assert(hostname != NULL);
{ s_assert(client_p != NULL);
free_cachefile(ptr->data); if(EmptyString(hostname) || (client_p == NULL))
rb_dlinkDestroy(ptr, &helpTable[i]); return;
}
HASH_WALK_END hashv = hash_hostname(hostname);
rb_dlinkAddAlloc(client_p, &hostTable[hashv]);
} }
/* add_to_resv_hash()
*
* adds a resv channel entry to the resv hash table
*/
void
add_to_resv_hash(const char *name, struct ConfItem *aconf)
{
unsigned int hashv;
s_assert(!EmptyString(name));
s_assert(aconf != NULL);
if(EmptyString(name) || aconf == NULL)
return;
hashv = hash_resv(name);
rb_dlinkAddAlloc(aconf, &resvTable[hashv]);
}
/* del_from_id_hash()
*
* removes an id from the id hash table
*/
void
del_from_id_hash(const char *id, struct Client *client_p)
{
unsigned int hashv;
s_assert(id != NULL);
s_assert(client_p != NULL);
if(EmptyString(id) || client_p == NULL)
return;
hashv = hash_id(id);
rb_dlinkFindDestroy(client_p, &idTable[hashv]);
}
/* del_from_client_hash()
*
* removes a client/server from the client hash table
*/
void
del_from_client_hash(const char *name, struct Client *client_p)
{
unsigned int hashv;
/* no s_asserts, this can happen when removing a client that
* is unregistered.
*/
if(EmptyString(name) || client_p == NULL)
return;
hashv = hash_nick(name);
rb_dlinkFindDestroy(client_p, &clientTable[hashv]);
}
/* del_from_channel_hash()
*
* removes a channel from the channel hash table
*/
void
del_from_channel_hash(const char *name, struct Channel *chptr)
{
unsigned int hashv;
s_assert(name != NULL);
s_assert(chptr != NULL);
if(EmptyString(name) || chptr == NULL)
return;
hashv = hash_channel(name);
rb_dlinkFindDestroy(chptr, &channelTable[hashv]);
}
/* del_from_hostname_hash()
*
* removes a client entry from the hostname hash table
*/
void
del_from_hostname_hash(const char *hostname, struct Client *client_p)
{
unsigned int hashv;
if(hostname == NULL || client_p == NULL)
return;
hashv = hash_hostname(hostname);
rb_dlinkFindDestroy(client_p, &hostTable[hashv]);
}
/* del_from_resv_hash()
*
* removes a resv entry from the resv hash table
*/
void
del_from_resv_hash(const char *name, struct ConfItem *aconf)
{
unsigned int hashv;
s_assert(name != NULL);
s_assert(aconf != NULL);
if(EmptyString(name) || aconf == NULL)
return;
hashv = hash_resv(name);
rb_dlinkFindDestroy(aconf, &resvTable[hashv]);
}
/* find_id() /* find_id()
* *
@ -276,79 +398,6 @@ find_id(const char *name)
return NULL; return NULL;
} }
/* hash_find_masked_server()
*
* Whats happening in this next loop ? Well, it takes a name like
* foo.bar.edu and proceeds to earch for *.edu and then *.bar.edu.
* This is for checking full server names against masks although
* it isnt often done this way in lieu of using matches().
*
* Rewrote to do *.bar.edu first, which is the most likely case,
* also made const correct
* --Bleep
*/
static struct Client *
hash_find_masked_server(struct Client *source_p, const char *name)
{
char buf[HOSTLEN + 1];
char *p = buf;
char *s;
struct Client *server;
if('*' == *name || '.' == *name)
return NULL;
/* copy it across to give us a buffer to work on */
rb_strlcpy(buf, name, sizeof(buf));
while ((s = strchr(p, '.')) != 0)
{
*--s = '*';
/*
* Dont need to check IsServer() here since nicknames cant
* have *'s in them anyway.
*/
if((server = find_server(source_p, s)))
return server;
p = s + 2;
}
return NULL;
}
/* find_any_client()
*
* finds a client/server/masked server entry from the hash
*/
struct Client *
find_any_client(const char *name)
{
struct Client *target_p;
rb_dlink_node *ptr;
unsigned int hashv;
s_assert(name != NULL);
if(EmptyString(name))
return NULL;
/* hunting for an id, not a nick */
if(IsDigit(*name))
return (find_id(name));
hashv = hash_nick(name);
RB_DLINK_FOREACH(ptr, clientTable[hashv].head)
{
target_p = ptr->data;
if(irccmp(name, target_p->name) == 0)
return target_p;
}
/* wasnt found, look for a masked server */
return hash_find_masked_server(NULL, name);
}
/* find_client() /* find_client()
* *
* finds a client/server entry from the client hash table * finds a client/server entry from the client hash table
@ -441,14 +490,13 @@ find_server(struct Client *source_p, const char *name)
return target_p; return target_p;
} }
/* wasnt found, look for a masked server */ return NULL;
return hash_find_masked_server(source_p, name);
} }
/* find_hostname() /* find_hostname()
* *
* finds a hostname dlink list from the hostname hash table. * finds a hostname rb_dlink list from the hostname hash table.
* we return the full dlink list, because you can have multiple * we return the full rb_dlink list, because you can have multiple
* entries with the same hostname * entries with the same hostname
*/ */
rb_dlink_node * rb_dlink_node *
@ -518,6 +566,7 @@ get_or_create_channel(struct Client *client_p, const char *chname, int *isnew)
len = strlen(s); len = strlen(s);
if(len > CHANNELLEN) if(len > CHANNELLEN)
{ {
char *t;
if(IsServer(client_p)) if(IsServer(client_p))
{ {
sendto_realops_snomask(SNO_DEBUG, L_ALL, sendto_realops_snomask(SNO_DEBUG, L_ALL,
@ -525,7 +574,9 @@ get_or_create_channel(struct Client *client_p, const char *chname, int *isnew)
client_p->name, len, CHANNELLEN, s); client_p->name, len, CHANNELLEN, s);
} }
len = CHANNELLEN; len = CHANNELLEN;
s = LOCAL_COPY_N(s, CHANNELLEN); t = LOCAL_COPY(s);
*(t + CHANNELLEN) = '\0';
s = t;
} }
hashv = hash_channel(s); hashv = hash_channel(s);
@ -577,7 +628,7 @@ hash_find_resv(const char *name)
{ {
aconf = ptr->data; aconf = ptr->data;
if(!irccmp(name, aconf->host)) if(!irccmp(name, aconf->name))
{ {
aconf->port++; aconf->port++;
return aconf; return aconf;
@ -587,30 +638,6 @@ hash_find_resv(const char *name)
return NULL; return NULL;
} }
struct cachefile *
hash_find_help(const char *name, int flags)
{
struct cachefile *hptr;
rb_dlink_node *ptr;
unsigned int hashv;
if(EmptyString(name))
return NULL;
hashv = hash_help(name);
RB_DLINK_FOREACH(ptr, helpTable[hashv].head)
{
hptr = ptr->data;
if((irccmp(name, hptr->name) == 0) &&
(hptr->flags & flags))
return hptr;
}
return NULL;
}
void void
clear_resv_hash(void) clear_resv_hash(void)
{ {
@ -624,7 +651,7 @@ clear_resv_hash(void)
aconf = ptr->data; aconf = ptr->data;
/* skip temp resvs */ /* skip temp resvs */
if(aconf->flags & CONF_FLAGS_TEMPORARY) if(aconf->hold)
continue; continue;
free_conf(ptr->data); free_conf(ptr->data);
@ -633,78 +660,19 @@ clear_resv_hash(void)
HASH_WALK_END HASH_WALK_END
} }
struct nd_entry *
hash_find_nd(const char *name)
{
struct nd_entry *nd;
rb_dlink_node *ptr;
unsigned int hashv;
if(EmptyString(name))
return NULL;
hashv = hash_nick(name);
RB_DLINK_FOREACH(ptr, ndTable[hashv].head)
{
nd = ptr->data;
if(!irccmp(name, nd->name))
return nd;
}
return NULL;
}
void
add_to_cli_fd_hash(struct Client *client_p)
{
rb_dlinkAddAlloc(client_p, &clientbyfdTable[hash_cli_fd(rb_get_fd(client_p->localClient->F))]);
}
void
del_from_cli_fd_hash(struct Client *client_p)
{
unsigned int hashv;
hashv = hash_cli_fd(rb_get_fd(client_p->localClient->F));
rb_dlinkFindDestroy(client_p, &clientbyfdTable[hashv]);
}
struct Client *
find_cli_fd_hash(int fd)
{
struct Client *target_p;
rb_dlink_node *ptr;
unsigned int hashv;
hashv = hash_cli_fd(fd);
RB_DLINK_FOREACH(ptr, clientbyfdTable[hashv].head)
{
target_p = ptr->data;
if(rb_get_fd(target_p->localClient->F) == fd)
return target_p;
}
return NULL;
}
static void static void
output_hash(struct Client *source_p, const char *name, int length, int *counts, int deepest) output_hash(struct Client *source_p, const char *name, int length, int *counts, int deepest)
{ {
unsigned long total = 0; unsigned long total = 0;
char buf[128];
int i; int i;
sendto_one_numeric(source_p, RPL_STATSDEBUG, "B :%s Hash Statistics", name); sendto_one_numeric(source_p, RPL_STATSDEBUG,
"B :%s Hash Statistics", name);
/* rb_snprintf which sendto_one_* uses doesn't support float formats */ sendto_one_numeric(source_p, RPL_STATSDEBUG,
#ifdef HAVE_SNPRINTF "B :Size: %d Empty: %d (%.3f%%)",
snprintf(buf, sizeof(buf), length, counts[0],
#else (float) ((counts[0]*100) / (float) length));
sprintf(buf,
#endif
"%.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++) for(i = 1; i < 11; i++)
{ {
@ -713,18 +681,11 @@ output_hash(struct Client *source_p, const char *name, int length, int *counts,
/* dont want to divide by 0! --fl */ /* dont want to divide by 0! --fl */
if(counts[0] != length) if(counts[0] != length)
{
#ifdef HAVE_SNPRINTF
snprintf(buf, sizeof(buf),
#else
sprintf(buf,
#endif
"%.3f%%/%.3f%%", (float) (total / (length - counts[0])),
(float) (total / length));
sendto_one_numeric(source_p, RPL_STATSDEBUG, sendto_one_numeric(source_p, RPL_STATSDEBUG,
"B :Average depth: %s Highest depth: %d", "B :Average depth: %.3f/%.3f Highest depth: %d",
buf, deepest); (float) (total / (length - counts[0])),
} (float) (total / length), deepest);
for(i = 0; i < 11; i++) for(i = 0; i < 11; i++)
{ {
sendto_one_numeric(source_p, RPL_STATSDEBUG, sendto_one_numeric(source_p, RPL_STATSDEBUG,
@ -738,7 +699,7 @@ static void
count_hash(struct Client *source_p, rb_dlink_list *table, int length, const char *name) count_hash(struct Client *source_p, rb_dlink_list *table, int length, const char *name)
{ {
int counts[11]; int counts[11];
unsigned long deepest = 0; int deepest = 0;
int i; int i;
memset(counts, 0, sizeof(counts)); memset(counts, 0, sizeof(counts));
@ -767,6 +728,4 @@ hash_stats(struct Client *source_p)
count_hash(source_p, idTable, U_MAX, "ID"); count_hash(source_p, idTable, U_MAX, "ID");
sendto_one_numeric(source_p, RPL_STATSDEBUG, "B :--"); sendto_one_numeric(source_p, RPL_STATSDEBUG, "B :--");
count_hash(source_p, hostTable, HOST_MAX, "Hostname"); count_hash(source_p, hostTable, HOST_MAX, "Hostname");
sendto_one_numeric(source_p, RPL_STATSDEBUG, "B :--");
count_hash(source_p, clientbyfdTable, CLI_FD_MAX, "Client by FD");
} }