2 * ircd-ratbox: A slightly useful ircd.
3 * hash.c: Maintains hashtables.
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 * $Id: hash.c 3177 2007-02-01 00:19:14Z jilles $
28 #include "ircd_defs.h"
40 #include "s_newconf.h"
42 #include "irc_dictionary.h"
43 #include "irc_radixtree.h"
45 rb_dlink_list
*hostTable
;
47 struct Dictionary
*client_connid_tree
= NULL
;
48 struct Dictionary
*client_zconnid_tree
= NULL
;
49 struct irc_radixtree
*client_id_tree
= NULL
;
50 struct irc_radixtree
*client_name_tree
= NULL
;
52 struct irc_radixtree
*channel_tree
= NULL
;
53 struct irc_radixtree
*resv_tree
= NULL
;
56 * look in whowas.c for the missing ...[WW_MAX]; entry
61 * clears the various hashtables
66 hostTable
= rb_malloc(sizeof(rb_dlink_list
) * HOST_MAX
);
68 client_connid_tree
= irc_dictionary_create("client connid", irc_uint32cmp
);
69 client_zconnid_tree
= irc_dictionary_create("client zconnid", irc_uint32cmp
);
70 client_id_tree
= irc_radixtree_create("client id", NULL
);
71 client_name_tree
= irc_radixtree_create("client name", irc_radixtree_irccasecanon
);
73 channel_tree
= irc_radixtree_create("channel", irc_radixtree_irccasecanon
);
74 resv_tree
= irc_radixtree_create("resv", irc_radixtree_irccasecanon
);
78 fnv_hash_upper(const unsigned char *s
, int bits
)
80 u_int32_t h
= FNV1_32_INIT
;
85 h
+= (h
<<1) + (h
<<4) + (h
<<7) + (h
<< 8) + (h
<< 24);
88 h
= ((h
>> bits
) ^ h
) & ((1<<bits
)-1);
93 fnv_hash(const unsigned char *s
, int bits
)
95 u_int32_t h
= FNV1_32_INIT
;
100 h
+= (h
<<1) + (h
<<4) + (h
<<7) + (h
<< 8) + (h
<< 24);
103 h
= ((h
>> bits
) ^ h
) & ((1<<bits
)-1);
108 fnv_hash_len(const unsigned char *s
, int bits
, int len
)
110 u_int32_t h
= FNV1_32_INIT
;
111 const unsigned char *x
= s
+ len
;
115 h
+= (h
<<1) + (h
<<4) + (h
<<7) + (h
<< 8) + (h
<< 24);
118 h
= ((h
>> bits
) ^ h
) & ((1<<bits
)-1);
123 fnv_hash_upper_len(const unsigned char *s
, int bits
, int len
)
125 u_int32_t h
= FNV1_32_INIT
;
126 const unsigned char *x
= s
+ len
;
130 h
+= (h
<<1) + (h
<<4) + (h
<<7) + (h
<< 8) + (h
<< 24);
133 h
= ((h
>> bits
) ^ h
) & ((1<<bits
)-1);
139 * hashes a hostname, based on first 30 chars only, as thats likely to
140 * be more dynamic than rest.
143 hash_hostname(const char *name
)
145 return fnv_hash_upper_len((const unsigned char *) name
, HOST_MAX_BITS
, 30);
150 * adds an entry to the id hash table
153 add_to_id_hash(const char *name
, struct Client
*client_p
)
155 if(EmptyString(name
) || (client_p
== NULL
))
158 irc_radixtree_add(client_id_tree
, name
, client_p
);
161 /* add_to_client_hash()
163 * adds an entry (client/server) to the client hash table
166 add_to_client_hash(const char *name
, struct Client
*client_p
)
168 s_assert(name
!= NULL
);
169 s_assert(client_p
!= NULL
);
170 if(EmptyString(name
) || (client_p
== NULL
))
173 irc_radixtree_add(client_name_tree
, name
, client_p
);
176 /* add_to_hostname_hash()
178 * adds a client entry to the hostname hash table
181 add_to_hostname_hash(const char *hostname
, struct Client
*client_p
)
185 s_assert(hostname
!= NULL
);
186 s_assert(client_p
!= NULL
);
187 if(EmptyString(hostname
) || (client_p
== NULL
))
190 hashv
= hash_hostname(hostname
);
191 rb_dlinkAddAlloc(client_p
, &hostTable
[hashv
]);
194 /* add_to_resv_hash()
196 * adds a resv channel entry to the resv hash table
199 add_to_resv_hash(const char *name
, struct ConfItem
*aconf
)
201 s_assert(!EmptyString(name
));
202 s_assert(aconf
!= NULL
);
203 if(EmptyString(name
) || aconf
== NULL
)
206 irc_radixtree_add(resv_tree
, name
, aconf
);
209 /* del_from_id_hash()
211 * removes an id from the id hash table
214 del_from_id_hash(const char *id
, struct Client
*client_p
)
216 s_assert(id
!= NULL
);
217 s_assert(client_p
!= NULL
);
218 if(EmptyString(id
) || client_p
== NULL
)
221 irc_radixtree_delete(client_id_tree
, id
);
224 /* del_from_client_hash()
226 * removes a client/server from the client hash table
229 del_from_client_hash(const char *name
, struct Client
*client_p
)
231 /* no s_asserts, this can happen when removing a client that
234 if(EmptyString(name
) || client_p
== NULL
)
237 irc_radixtree_delete(client_name_tree
, name
);
240 /* del_from_channel_hash()
242 * removes a channel from the channel hash table
245 del_from_channel_hash(const char *name
, struct Channel
*chptr
)
247 s_assert(name
!= NULL
);
248 s_assert(chptr
!= NULL
);
250 if(EmptyString(name
) || chptr
== NULL
)
253 irc_radixtree_delete(channel_tree
, name
);
256 /* del_from_hostname_hash()
258 * removes a client entry from the hostname hash table
261 del_from_hostname_hash(const char *hostname
, struct Client
*client_p
)
265 if(hostname
== NULL
|| client_p
== NULL
)
268 hashv
= hash_hostname(hostname
);
270 rb_dlinkFindDestroy(client_p
, &hostTable
[hashv
]);
273 /* del_from_resv_hash()
275 * removes a resv entry from the resv hash table
278 del_from_resv_hash(const char *name
, struct ConfItem
*aconf
)
280 s_assert(name
!= NULL
);
281 s_assert(aconf
!= NULL
);
282 if(EmptyString(name
) || aconf
== NULL
)
285 irc_radixtree_delete(resv_tree
, name
);
290 * finds a client entry from the id hash table
293 find_id(const char *name
)
295 if(EmptyString(name
))
298 return irc_radixtree_retrieve(client_id_tree
, name
);
303 * finds a client/server entry from the client hash table
306 find_client(const char *name
)
308 s_assert(name
!= NULL
);
309 if(EmptyString(name
))
312 /* hunting for an id, not a nick */
314 return (find_id(name
));
316 return irc_radixtree_retrieve(client_name_tree
, name
);
319 /* find_named_client()
321 * finds a client/server entry from the client hash table
324 find_named_client(const char *name
)
326 s_assert(name
!= NULL
);
327 if(EmptyString(name
))
330 return irc_radixtree_retrieve(client_name_tree
, name
);
335 * finds a server from the client hash table
338 find_server(struct Client
*source_p
, const char *name
)
340 struct Client
*target_p
;
342 if(EmptyString(name
))
345 if((source_p
== NULL
|| !MyClient(source_p
)) &&
346 IsDigit(*name
) && strlen(name
) == 3)
348 target_p
= find_id(name
);
352 target_p
= irc_radixtree_retrieve(client_name_tree
, name
);
353 if (target_p
!= NULL
)
355 if(IsServer(target_p
) || IsMe(target_p
))
364 * finds a hostname rb_dlink list from the hostname hash table.
365 * we return the full rb_dlink list, because you can have multiple
366 * entries with the same hostname
369 find_hostname(const char *hostname
)
373 if(EmptyString(hostname
))
376 hashv
= hash_hostname(hostname
);
378 return hostTable
[hashv
].head
;
383 * finds a channel from the channel hash table
386 find_channel(const char *name
)
388 s_assert(name
!= NULL
);
389 if(EmptyString(name
))
392 return irc_radixtree_retrieve(channel_tree
, name
);
396 * get_or_create_channel
397 * inputs - client pointer
399 * - pointer to int flag whether channel was newly created or not
400 * output - returns channel block or NULL if illegal name
401 * - also modifies *isnew
403 * Get Channel block for chname (and allocate a new channel
404 * block, if it didn't exist before).
407 get_or_create_channel(struct Client
*client_p
, const char *chname
, int *isnew
)
409 struct Channel
*chptr
;
411 const char *s
= chname
;
420 if(IsServer(client_p
))
422 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
423 "*** Long channel name from %s (%d > %d): %s",
424 client_p
->name
, len
, CHANNELLEN
, s
);
428 *(t
+ CHANNELLEN
) = '\0';
432 chptr
= irc_radixtree_retrieve(channel_tree
, s
);
443 chptr
= allocate_channel(s
);
444 chptr
->channelts
= rb_current_time(); /* doesn't hurt to set it here */
446 rb_dlinkAdd(chptr
, &chptr
->node
, &global_channel_list
);
447 irc_radixtree_add(channel_tree
, chptr
->chname
, chptr
);
454 * hunts for a resv entry in the resv hash table
457 hash_find_resv(const char *name
)
459 struct ConfItem
*aconf
;
461 s_assert(name
!= NULL
);
462 if(EmptyString(name
))
465 aconf
= irc_radixtree_retrieve(resv_tree
, name
);
476 clear_resv_hash(void)
478 struct ConfItem
*aconf
;
479 struct irc_radixtree_iteration_state iter
;
481 IRC_RADIXTREE_FOREACH(aconf
, &iter
, resv_tree
)
483 /* skip temp resvs */
487 irc_radixtree_delete(resv_tree
, aconf
->host
);
493 add_to_zconnid_hash(struct Client
*client_p
)
495 irc_dictionary_add(client_zconnid_tree
, IRC_UINT_TO_POINTER(client_p
->localClient
->zconnid
), client_p
);
499 del_from_zconnid_hash(struct Client
*client_p
)
501 irc_dictionary_delete(client_zconnid_tree
, IRC_UINT_TO_POINTER(client_p
->localClient
->zconnid
));
505 add_to_cli_connid_hash(struct Client
*client_p
)
507 irc_dictionary_add(client_connid_tree
, IRC_UINT_TO_POINTER(client_p
->localClient
->connid
), client_p
);
511 del_from_cli_connid_hash(struct Client
*client_p
)
513 irc_dictionary_delete(client_connid_tree
, IRC_UINT_TO_POINTER(client_p
->localClient
->connid
));
517 find_cli_connid_hash(uint32_t connid
)
519 struct Client
*target_p
;
521 target_p
= irc_dictionary_retrieve(client_connid_tree
, IRC_UINT_TO_POINTER(connid
));
522 if (target_p
!= NULL
)
525 target_p
= irc_dictionary_retrieve(client_zconnid_tree
, IRC_UINT_TO_POINTER(connid
));
526 if (target_p
!= NULL
)
533 output_hash(struct Client
*source_p
, const char *name
, int length
, int *counts
, unsigned long deepest
)
535 unsigned long total
= 0;
539 sendto_one_numeric(source_p
, RPL_STATSDEBUG
,
540 "B :%s Hash Statistics", name
);
542 snprintf(buf
, sizeof buf
, "%.3f%%",
543 (float) ((counts
[0]*100) / (float) length
));
544 sendto_one_numeric(source_p
, RPL_STATSDEBUG
,
545 "B :Size: %d Empty: %d (%s)",
546 length
, counts
[0], buf
);
548 for(i
= 1; i
< 11; i
++)
550 total
+= (counts
[i
] * i
);
553 /* dont want to divide by 0! --fl */
554 if(counts
[0] != length
)
556 snprintf(buf
, sizeof buf
, "%.3f/%.3f",
557 (float) (total
/ (length
- counts
[0])),
558 (float) (total
/ length
));
559 sendto_one_numeric(source_p
, RPL_STATSDEBUG
,
560 "B :Average depth: %s Highest depth: %lu",
567 count_hash(struct Client
*source_p
, rb_dlink_list
*table
, int length
, const char *name
)
570 unsigned long deepest
= 0;
573 memset(counts
, 0, sizeof(counts
));
575 for(i
= 0; i
< length
; i
++)
577 if(rb_dlink_list_length(&table
[i
]) >= 10)
580 counts
[rb_dlink_list_length(&table
[i
])]++;
582 if(rb_dlink_list_length(&table
[i
]) > deepest
)
583 deepest
= rb_dlink_list_length(&table
[i
]);
586 output_hash(source_p
, name
, length
, counts
, deepest
);
590 hash_stats(struct Client
*source_p
)
592 count_hash(source_p
, hostTable
, HOST_MAX
, "Hostname");