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 22407 2006-05-07 15:39:43Z androsyn $
28 #include "ircd_defs.h"
35 #include "irc_string.h"
42 #include "s_newconf.h"
44 static dlink_list
*clientTable
;
45 static dlink_list
*channelTable
;
46 static dlink_list
*idTable
;
47 dlink_list
*resvTable
;
48 static dlink_list
*hostTable
;
49 static dlink_list
*helpTable
;
53 * look in whowas.c for the missing ...[WW_MAX]; entry
59 * The server uses a chained hash table to provide quick and efficient
60 * hash table maintenance (providing the hash function works evenly over
61 * the input range). The hash table is thus not susceptible to problems
62 * of filling all the buckets or the need to rehash.
63 * It is expected that the hash table would look something like this
65 * +-----+ +-----+ +-----+ +-----+
66 * ---| 224 |----| 225 |----| 226 |---| 227 |---
67 * +-----+ +-----+ +-----+ +-----+
69 * +-----+ +-----+ +-----+
71 * +-----+ +-----+ +-----+
77 * A - GOPbot, B - chang, C - hanuaway, D - *.mu.OZ.AU
79 * The order shown above is just one instant of the server.
82 * The hash functions currently used are based Fowler/Noll/Vo hashes
83 * which work amazingly well and have a extremely low collision rate
84 * For more info see http://www.isthe.com/chongo/tech/comp/fnv/index.html
91 * clears the various hashtables
96 clientTable
= MyMalloc(sizeof(dlink_list
) * U_MAX
);
97 idTable
= MyMalloc(sizeof(dlink_list
) * U_MAX
);
98 ndTable
= MyMalloc(sizeof(dlink_list
) * U_MAX
);
99 channelTable
= MyMalloc(sizeof(dlink_list
) * CH_MAX
);
100 hostTable
= MyMalloc(sizeof(dlink_list
) * HOST_MAX
);
101 resvTable
= MyMalloc(sizeof(dlink_list
) * R_MAX
);
102 helpTable
= MyMalloc(sizeof(dlink_list
) * HELP_MAX
);
107 fnv_hash_upper(const unsigned char *s
, int bits
)
109 uint32_t h
= FNV1_32_INIT
;
114 h
+= (h
<<1) + (h
<<4) + (h
<<7) + (h
<< 8) + (h
<< 24);
116 h
= (h
>> bits
) ^ (h
& ((2^bits
)-1));
121 fnv_hash(const unsigned char *s
, int bits
)
123 uint32_t h
= FNV1_32_INIT
;
128 h
+= (h
<<1) + (h
<<4) + (h
<<7) + (h
<< 8) + (h
<< 24);
130 h
= (h
>> bits
) ^ (h
& ((2^bits
)-1));
135 fnv_hash_len(const unsigned char *s
, int bits
, int len
)
137 uint32_t h
= FNV1_32_INIT
;
138 const unsigned char *x
= s
+ len
;
142 h
+= (h
<<1) + (h
<<4) + (h
<<7) + (h
<< 8) + (h
<< 24);
144 h
= (h
>> bits
) ^ (h
& ((2^bits
)-1));
149 fnv_hash_upper_len(const unsigned char *s
, int bits
, int len
)
151 uint32_t h
= FNV1_32_INIT
;
152 const unsigned char *x
= s
+ len
;
156 h
+= (h
<<1) + (h
<<4) + (h
<<7) + (h
<< 8) + (h
<< 24);
158 h
= (h
>> bits
) ^ (h
& ((2^bits
)-1));
164 * hashes a nickname, first converting to lowercase
167 hash_nick(const char *name
)
169 return fnv_hash_upper((const unsigned char *) name
, U_MAX_BITS
);
174 * hashes an id, case is kept
177 hash_id(const char *name
)
179 return fnv_hash((const unsigned char *) name
, U_MAX_BITS
);
184 * hashes a channel name, based on first 30 chars only for efficiency
187 hash_channel(const char *name
)
189 return fnv_hash_upper_len((const unsigned char *) name
, CH_MAX_BITS
, 30);
194 * hashes a hostname, based on first 30 chars only, as thats likely to
195 * be more dynamic than rest.
198 hash_hostname(const char *name
)
200 return fnv_hash_upper_len((const unsigned char *) name
, HOST_MAX_BITS
, 30);
205 * hashes a resv channel name, based on first 30 chars only
208 hash_resv(const char *name
)
210 return fnv_hash_upper_len((const unsigned char *) name
, R_MAX_BITS
, 30);
214 hash_help(const char *name
)
220 h
+= (unsigned int) (ToLower(*name
++) & 0xDF);
223 return (h
% HELP_MAX
);
228 * adds an entry to the id hash table
231 add_to_id_hash(const char *name
, struct Client
*client_p
)
235 if(EmptyString(name
) || (client_p
== NULL
))
238 hashv
= hash_id(name
);
239 dlinkAddAlloc(client_p
, &idTable
[hashv
]);
242 /* add_to_client_hash()
244 * adds an entry (client/server) to the client hash table
247 add_to_client_hash(const char *name
, struct Client
*client_p
)
251 s_assert(name
!= NULL
);
252 s_assert(client_p
!= NULL
);
253 if(EmptyString(name
) || (client_p
== NULL
))
256 hashv
= hash_nick(name
);
257 dlinkAddAlloc(client_p
, &clientTable
[hashv
]);
260 /* add_to_hostname_hash()
262 * adds a client entry to the hostname hash table
265 add_to_hostname_hash(const char *hostname
, struct Client
*client_p
)
269 s_assert(hostname
!= NULL
);
270 s_assert(client_p
!= NULL
);
271 if(EmptyString(hostname
) || (client_p
== NULL
))
274 hashv
= hash_hostname(hostname
);
275 dlinkAddAlloc(client_p
, &hostTable
[hashv
]);
278 /* add_to_resv_hash()
280 * adds a resv channel entry to the resv hash table
283 add_to_resv_hash(const char *name
, struct ConfItem
*aconf
)
287 s_assert(!EmptyString(name
));
288 s_assert(aconf
!= NULL
);
289 if(EmptyString(name
) || aconf
== NULL
)
292 hashv
= hash_resv(name
);
293 dlinkAddAlloc(aconf
, &resvTable
[hashv
]);
297 add_to_help_hash(const char *name
, struct cachefile
*hptr
)
301 if(EmptyString(name
) || hptr
== NULL
)
304 hashv
= hash_help(name
);
305 dlinkAddAlloc(hptr
, &helpTable
[hashv
]);
309 add_to_nd_hash(const char *name
, struct nd_entry
*nd
)
311 nd
->hashv
= hash_nick(name
);
312 dlinkAdd(nd
, &nd
->hnode
, &ndTable
[nd
->hashv
]);
315 /* del_from_id_hash()
317 * removes an id from the id hash table
320 del_from_id_hash(const char *id
, struct Client
*client_p
)
324 s_assert(id
!= NULL
);
325 s_assert(client_p
!= NULL
);
326 if(EmptyString(id
) || client_p
== NULL
)
330 dlinkFindDestroy(client_p
, &idTable
[hashv
]);
333 /* del_from_client_hash()
335 * removes a client/server from the client hash table
338 del_from_client_hash(const char *name
, struct Client
*client_p
)
342 /* no s_asserts, this can happen when removing a client that
345 if(EmptyString(name
) || client_p
== NULL
)
348 hashv
= hash_nick(name
);
349 dlinkFindDestroy(client_p
, &clientTable
[hashv
]);
352 /* del_from_channel_hash()
354 * removes a channel from the channel hash table
357 del_from_channel_hash(const char *name
, struct Channel
*chptr
)
361 s_assert(name
!= NULL
);
362 s_assert(chptr
!= NULL
);
364 if(EmptyString(name
) || chptr
== NULL
)
367 hashv
= hash_channel(name
);
368 dlinkFindDestroy(chptr
, &channelTable
[hashv
]);
371 /* del_from_hostname_hash()
373 * removes a client entry from the hostname hash table
376 del_from_hostname_hash(const char *hostname
, struct Client
*client_p
)
380 if(hostname
== NULL
|| client_p
== NULL
)
383 hashv
= hash_hostname(hostname
);
385 dlinkFindDestroy(client_p
, &hostTable
[hashv
]);
388 /* del_from_resv_hash()
390 * removes a resv entry from the resv hash table
393 del_from_resv_hash(const char *name
, struct ConfItem
*aconf
)
397 s_assert(name
!= NULL
);
398 s_assert(aconf
!= NULL
);
399 if(EmptyString(name
) || aconf
== NULL
)
402 hashv
= hash_resv(name
);
404 dlinkFindDestroy(aconf
, &resvTable
[hashv
]);
408 clear_help_hash(void)
411 dlink_node
*next_ptr
;
414 HASH_WALK_SAFE(i
, HELP_MAX
, ptr
, next_ptr
, helpTable
)
416 free_cachefile(ptr
->data
);
417 dlinkDestroy(ptr
, &helpTable
[i
]);
424 * finds a client entry from the id hash table
427 find_id(const char *name
)
429 struct Client
*target_p
;
433 if(EmptyString(name
))
436 hashv
= hash_id(name
);
438 DLINK_FOREACH(ptr
, idTable
[hashv
].head
)
440 target_p
= ptr
->data
;
442 if(strcmp(name
, target_p
->id
) == 0)
449 /* hash_find_masked_server()
451 * Whats happening in this next loop ? Well, it takes a name like
452 * foo.bar.edu and proceeds to earch for *.edu and then *.bar.edu.
453 * This is for checking full server names against masks although
454 * it isnt often done this way in lieu of using matches().
456 * Rewrote to do *.bar.edu first, which is the most likely case,
457 * also made const correct
460 static struct Client
*
461 hash_find_masked_server(struct Client
*source_p
, const char *name
)
463 char buf
[HOSTLEN
+ 1];
466 struct Client
*server
;
468 if('*' == *name
|| '.' == *name
)
471 /* copy it across to give us a buffer to work on */
472 strlcpy(buf
, name
, sizeof(buf
));
474 while ((s
= strchr(p
, '.')) != 0)
478 * Dont need to check IsServer() here since nicknames cant
479 * have *'s in them anyway.
481 if((server
= find_server(source_p
, s
)))
491 * finds a client/server/masked server entry from the hash
494 find_any_client(const char *name
)
496 struct Client
*target_p
;
500 s_assert(name
!= NULL
);
501 if(EmptyString(name
))
504 /* hunting for an id, not a nick */
506 return (find_id(name
));
508 hashv
= hash_nick(name
);
510 DLINK_FOREACH(ptr
, clientTable
[hashv
].head
)
512 target_p
= ptr
->data
;
514 if(irccmp(name
, target_p
->name
) == 0)
518 /* wasnt found, look for a masked server */
519 return hash_find_masked_server(NULL
, name
);
524 * finds a client/server entry from the client hash table
527 find_client(const char *name
)
529 struct Client
*target_p
;
533 s_assert(name
!= NULL
);
534 if(EmptyString(name
))
537 /* hunting for an id, not a nick */
539 return (find_id(name
));
541 hashv
= hash_nick(name
);
543 DLINK_FOREACH(ptr
, clientTable
[hashv
].head
)
545 target_p
= ptr
->data
;
547 if(irccmp(name
, target_p
->name
) == 0)
554 /* find_named_client()
556 * finds a client/server entry from the client hash table
559 find_named_client(const char *name
)
561 struct Client
*target_p
;
565 s_assert(name
!= NULL
);
566 if(EmptyString(name
))
569 hashv
= hash_nick(name
);
571 DLINK_FOREACH(ptr
, clientTable
[hashv
].head
)
573 target_p
= ptr
->data
;
575 if(irccmp(name
, target_p
->name
) == 0)
584 * finds a server from the client hash table
587 find_server(struct Client
*source_p
, const char *name
)
589 struct Client
*target_p
;
593 if(EmptyString(name
))
596 if((source_p
== NULL
|| !MyClient(source_p
)) &&
597 IsDigit(*name
) && strlen(name
) == 3)
599 target_p
= find_id(name
);
603 hashv
= hash_nick(name
);
605 DLINK_FOREACH(ptr
, clientTable
[hashv
].head
)
607 target_p
= ptr
->data
;
609 if((IsServer(target_p
) || IsMe(target_p
)) &&
610 irccmp(name
, target_p
->name
) == 0)
614 /* wasnt found, look for a masked server */
615 return hash_find_masked_server(source_p
, name
);
620 * finds a hostname dlink list from the hostname hash table.
621 * we return the full dlink list, because you can have multiple
622 * entries with the same hostname
625 find_hostname(const char *hostname
)
629 if(EmptyString(hostname
))
632 hashv
= hash_hostname(hostname
);
634 return hostTable
[hashv
].head
;
639 * finds a channel from the channel hash table
642 find_channel(const char *name
)
644 struct Channel
*chptr
;
648 s_assert(name
!= NULL
);
649 if(EmptyString(name
))
652 hashv
= hash_channel(name
);
654 DLINK_FOREACH(ptr
, channelTable
[hashv
].head
)
658 if(irccmp(name
, chptr
->chname
) == 0)
666 * get_or_create_channel
667 * inputs - client pointer
669 * - pointer to int flag whether channel was newly created or not
670 * output - returns channel block or NULL if illegal name
671 * - also modifies *isnew
673 * Get Channel block for chname (and allocate a new channel
674 * block, if it didn't exist before).
677 get_or_create_channel(struct Client
*client_p
, const char *chname
, int *isnew
)
679 struct Channel
*chptr
;
683 const char *s
= chname
;
692 if(IsServer(client_p
))
694 sendto_realops_flags(UMODE_DEBUG
, L_ALL
,
695 "*** Long channel name from %s (%d > %d): %s",
696 client_p
->name
, len
, CHANNELLEN
, s
);
700 *(t
+ CHANNELLEN
) = '\0';
704 hashv
= hash_channel(s
);
706 DLINK_FOREACH(ptr
, channelTable
[hashv
].head
)
710 if(irccmp(s
, chptr
->chname
) == 0)
721 chptr
= allocate_channel(s
);
723 dlinkAdd(chptr
, &chptr
->node
, &global_channel_list
);
725 chptr
->channelts
= CurrentTime
; /* doesn't hurt to set it here */
727 dlinkAddAlloc(chptr
, &channelTable
[hashv
]);
734 * hunts for a resv entry in the resv hash table
737 hash_find_resv(const char *name
)
739 struct ConfItem
*aconf
;
743 s_assert(name
!= NULL
);
744 if(EmptyString(name
))
747 hashv
= hash_resv(name
);
749 DLINK_FOREACH(ptr
, resvTable
[hashv
].head
)
753 if(!irccmp(name
, aconf
->name
))
764 hash_find_help(const char *name
, int flags
)
766 struct cachefile
*hptr
;
770 if(EmptyString(name
))
773 hashv
= hash_help(name
);
775 DLINK_FOREACH(ptr
, helpTable
[hashv
].head
)
779 if((irccmp(name
, hptr
->name
) == 0) &&
780 (hptr
->flags
& flags
))
788 clear_resv_hash(void)
790 struct ConfItem
*aconf
;
792 dlink_node
*next_ptr
;
795 HASH_WALK_SAFE(i
, R_MAX
, ptr
, next_ptr
, resvTable
)
799 /* skip temp resvs */
803 free_conf(ptr
->data
);
804 dlinkDestroy(ptr
, &resvTable
[i
]);
810 hash_find_nd(const char *name
)
816 if(EmptyString(name
))
819 hashv
= hash_nick(name
);
821 DLINK_FOREACH(ptr
, ndTable
[hashv
].head
)
825 if(!irccmp(name
, nd
->name
))
833 output_hash(struct Client
*source_p
, const char *name
, int length
, int *counts
, int deepest
)
835 unsigned long total
= 0;
838 sendto_one_numeric(source_p
, RPL_STATSDEBUG
,
839 "B :%s Hash Statistics", name
);
841 sendto_one_numeric(source_p
, RPL_STATSDEBUG
,
842 "B :Size: %d Empty: %d (%.3f%%)",
844 (float) ((counts
[0]*100) / (float) length
));
846 for(i
= 1; i
< 11; i
++)
848 total
+= (counts
[i
] * i
);
851 /* dont want to divide by 0! --fl */
852 if(counts
[0] != length
)
853 sendto_one_numeric(source_p
, RPL_STATSDEBUG
,
854 "B :Average depth: %.3f/%.3f Highest depth: %d",
855 (float) (total
/ (length
- counts
[0])),
856 (float) (total
/ length
), deepest
);
858 for(i
= 0; i
< 11; i
++)
860 sendto_one_numeric(source_p
, RPL_STATSDEBUG
,
861 "B :Nodes with %d entries: %d",
868 count_hash(struct Client
*source_p
, dlink_list
*table
, int length
, const char *name
)
874 memset(counts
, 0, sizeof(counts
));
876 for(i
= 0; i
< length
; i
++)
878 if(dlink_list_length(&table
[i
]) >= 10)
881 counts
[dlink_list_length(&table
[i
])]++;
883 if(dlink_list_length(&table
[i
]) > deepest
)
884 deepest
= dlink_list_length(&table
[i
]);
887 output_hash(source_p
, name
, length
, counts
, deepest
);
891 hash_stats(struct Client
*source_p
)
893 count_hash(source_p
, channelTable
, CH_MAX
, "Channel");
894 sendto_one_numeric(source_p
, RPL_STATSDEBUG
, "B :--");
895 count_hash(source_p
, clientTable
, U_MAX
, "Client");
896 sendto_one_numeric(source_p
, RPL_STATSDEBUG
, "B :--");
897 count_hash(source_p
, idTable
, U_MAX
, "ID");
898 sendto_one_numeric(source_p
, RPL_STATSDEBUG
, "B :--");
899 count_hash(source_p
, hostTable
, HOST_MAX
, "Hostname");