]>
jfr.im git - irc/rqf/shadowircd.git/blob - src/hash.c
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"
35 #include "irc_string.h"
42 #include "s_newconf.h"
44 dlink_list
*clientTable
;
45 dlink_list
*channelTable
;
47 dlink_list
*resvTable
;
48 dlink_list
*hostTable
;
51 * look in whowas.c for the missing ...[WW_MAX]; entry
57 * The server uses a chained hash table to provide quick and efficient
58 * hash table maintenance (providing the hash function works evenly over
59 * the input range). The hash table is thus not susceptible to problems
60 * of filling all the buckets or the need to rehash.
61 * It is expected that the hash table would look something like this
63 * +-----+ +-----+ +-----+ +-----+
64 * ---| 224 |----| 225 |----| 226 |---| 227 |---
65 * +-----+ +-----+ +-----+ +-----+
67 * +-----+ +-----+ +-----+
69 * +-----+ +-----+ +-----+
75 * A - GOPbot, B - chang, C - hanuaway, D - *.mu.OZ.AU
77 * The order shown above is just one instant of the server.
80 * The hash functions currently used are based Fowler/Noll/Vo hashes
81 * which work amazingly well and have a extremely low collision rate
82 * For more info see http://www.isthe.com/chongo/tech/comp/fnv/index.html
89 * clears the various hashtables
94 clientTable
= MyMalloc(sizeof(dlink_list
) * U_MAX
);
95 idTable
= MyMalloc(sizeof(dlink_list
) * U_MAX
);
96 channelTable
= MyMalloc(sizeof(dlink_list
) * CH_MAX
);
97 hostTable
= MyMalloc(sizeof(dlink_list
) * HOST_MAX
);
98 resvTable
= MyMalloc(sizeof(dlink_list
) * R_MAX
);
101 #ifndef RICER_HASHING
103 fnv_hash_upper(const unsigned char *s
, int bits
)
105 u_int32_t h
= FNV1_32_INIT
;
110 h
+= (h
<<1) + (h
<<4) + (h
<<7) + (h
<< 8) + (h
<< 24);
112 h
= (h
>> bits
) ^ (h
& ((2^bits
)-1));
117 fnv_hash(const unsigned char *s
, int bits
)
119 u_int32_t h
= FNV1_32_INIT
;
124 h
+= (h
<<1) + (h
<<4) + (h
<<7) + (h
<< 8) + (h
<< 24);
126 h
= (h
>> bits
) ^ (h
& ((2^bits
)-1));
131 fnv_hash_len(const unsigned char *s
, int bits
, int len
)
133 u_int32_t h
= FNV1_32_INIT
;
134 const unsigned char *x
= s
+ len
;
138 h
+= (h
<<1) + (h
<<4) + (h
<<7) + (h
<< 8) + (h
<< 24);
140 h
= (h
>> bits
) ^ (h
& ((2^bits
)-1));
145 fnv_hash_upper_len(const unsigned char *s
, int bits
, int len
)
147 u_int32_t h
= FNV1_32_INIT
;
148 const unsigned char *x
= s
+ len
;
152 h
+= (h
<<1) + (h
<<4) + (h
<<7) + (h
<< 8) + (h
<< 24);
154 h
= (h
>> bits
) ^ (h
& ((2^bits
)-1));
161 * hashes a nickname, first converting to lowercase
164 hash_nick(const char *name
)
166 return fnv_hash_upper((const unsigned char *) name
, U_MAX_BITS
);
171 * hashes an id, case is kept
174 hash_id(const char *name
)
176 return fnv_hash((const unsigned char *) name
, U_MAX_BITS
);
181 * hashes a channel name, based on first 30 chars only for efficiency
184 hash_channel(const char *name
)
186 return fnv_hash_upper_len((const unsigned char *) name
, CH_MAX_BITS
, 30);
191 * hashes a hostname, based on first 30 chars only, as thats likely to
192 * be more dynamic than rest.
195 hash_hostname(const char *name
)
197 return fnv_hash_upper_len((const unsigned char *) name
, HOST_MAX_BITS
, 30);
202 * hashes a resv channel name, based on first 30 chars only
205 hash_resv(const char *name
)
207 return fnv_hash_upper_len((const unsigned char *) name
, R_MAX_BITS
, 30);
212 * adds an entry to the id hash table
215 add_to_id_hash(const char *name
, struct Client
*client_p
)
219 if(EmptyString(name
) || (client_p
== NULL
))
222 hashv
= hash_id(name
);
223 dlinkAddAlloc(client_p
, &idTable
[hashv
]);
226 /* add_to_client_hash()
228 * adds an entry (client/server) to the client hash table
231 add_to_client_hash(const char *name
, struct Client
*client_p
)
235 s_assert(name
!= NULL
);
236 s_assert(client_p
!= NULL
);
237 if(EmptyString(name
) || (client_p
== NULL
))
240 hashv
= hash_nick(name
);
241 dlinkAddAlloc(client_p
, &clientTable
[hashv
]);
244 /* add_to_hostname_hash()
246 * adds a client entry to the hostname hash table
249 add_to_hostname_hash(const char *hostname
, struct Client
*client_p
)
253 s_assert(hostname
!= NULL
);
254 s_assert(client_p
!= NULL
);
255 if(EmptyString(hostname
) || (client_p
== NULL
))
258 hashv
= hash_hostname(hostname
);
259 dlinkAddAlloc(client_p
, &hostTable
[hashv
]);
262 /* add_to_resv_hash()
264 * adds a resv channel entry to the resv hash table
267 add_to_resv_hash(const char *name
, struct ConfItem
*aconf
)
271 s_assert(!EmptyString(name
));
272 s_assert(aconf
!= NULL
);
273 if(EmptyString(name
) || aconf
== NULL
)
276 hashv
= hash_resv(name
);
277 dlinkAddAlloc(aconf
, &resvTable
[hashv
]);
280 /* del_from_id_hash()
282 * removes an id from the id hash table
285 del_from_id_hash(const char *id
, struct Client
*client_p
)
289 s_assert(id
!= NULL
);
290 s_assert(client_p
!= NULL
);
291 if(EmptyString(id
) || client_p
== NULL
)
295 dlinkFindDestroy(client_p
, &idTable
[hashv
]);
298 /* del_from_client_hash()
300 * removes a client/server from the client hash table
303 del_from_client_hash(const char *name
, struct Client
*client_p
)
307 /* no s_asserts, this can happen when removing a client that
310 if(EmptyString(name
) || client_p
== NULL
)
313 hashv
= hash_nick(name
);
314 dlinkFindDestroy(client_p
, &clientTable
[hashv
]);
317 /* del_from_channel_hash()
319 * removes a channel from the channel hash table
322 del_from_channel_hash(const char *name
, struct Channel
*chptr
)
326 s_assert(name
!= NULL
);
327 s_assert(chptr
!= NULL
);
329 if(EmptyString(name
) || chptr
== NULL
)
332 hashv
= hash_channel(name
);
333 dlinkFindDestroy(chptr
, &channelTable
[hashv
]);
336 /* del_from_hostname_hash()
338 * removes a client entry from the hostname hash table
341 del_from_hostname_hash(const char *hostname
, struct Client
*client_p
)
345 if(hostname
== NULL
|| client_p
== NULL
)
348 hashv
= hash_hostname(hostname
);
350 dlinkFindDestroy(client_p
, &hostTable
[hashv
]);
353 /* del_from_resv_hash()
355 * removes a resv entry from the resv hash table
358 del_from_resv_hash(const char *name
, struct ConfItem
*aconf
)
362 s_assert(name
!= NULL
);
363 s_assert(aconf
!= NULL
);
364 if(EmptyString(name
) || aconf
== NULL
)
367 hashv
= hash_resv(name
);
369 dlinkFindDestroy(aconf
, &resvTable
[hashv
]);
374 * finds a client entry from the id hash table
377 find_id(const char *name
)
379 struct Client
*target_p
;
383 if(EmptyString(name
))
386 hashv
= hash_id(name
);
388 DLINK_FOREACH(ptr
, idTable
[hashv
].head
)
390 target_p
= ptr
->data
;
392 if(strcmp(name
, target_p
->id
) == 0)
401 * finds a client/server entry from the client hash table
404 find_client(const char *name
)
406 struct Client
*target_p
;
410 s_assert(name
!= NULL
);
411 if(EmptyString(name
))
414 /* hunting for an id, not a nick */
416 return (find_id(name
));
418 hashv
= hash_nick(name
);
420 DLINK_FOREACH(ptr
, clientTable
[hashv
].head
)
422 target_p
= ptr
->data
;
424 if(irccmp(name
, target_p
->name
) == 0)
431 /* find_named_client()
433 * finds a client/server entry from the client hash table
436 find_named_client(const char *name
)
438 struct Client
*target_p
;
442 s_assert(name
!= NULL
);
443 if(EmptyString(name
))
446 hashv
= hash_nick(name
);
448 DLINK_FOREACH(ptr
, clientTable
[hashv
].head
)
450 target_p
= ptr
->data
;
452 if(irccmp(name
, target_p
->name
) == 0)
461 * finds a server from the client hash table
464 find_server(struct Client
*source_p
, const char *name
)
466 struct Client
*target_p
;
470 if(EmptyString(name
))
473 if((source_p
== NULL
|| !MyClient(source_p
)) &&
474 IsDigit(*name
) && strlen(name
) == 3)
476 target_p
= find_id(name
);
480 hashv
= hash_nick(name
);
482 DLINK_FOREACH(ptr
, clientTable
[hashv
].head
)
484 target_p
= ptr
->data
;
486 if((IsServer(target_p
) || IsMe(target_p
)) &&
487 irccmp(name
, target_p
->name
) == 0)
496 * finds a hostname dlink list from the hostname hash table.
497 * we return the full dlink list, because you can have multiple
498 * entries with the same hostname
501 find_hostname(const char *hostname
)
505 if(EmptyString(hostname
))
508 hashv
= hash_hostname(hostname
);
510 return hostTable
[hashv
].head
;
515 * finds a channel from the channel hash table
518 find_channel(const char *name
)
520 struct Channel
*chptr
;
524 s_assert(name
!= NULL
);
525 if(EmptyString(name
))
528 hashv
= hash_channel(name
);
530 DLINK_FOREACH(ptr
, channelTable
[hashv
].head
)
534 if(irccmp(name
, chptr
->chname
) == 0)
542 * get_or_create_channel
543 * inputs - client pointer
545 * - pointer to int flag whether channel was newly created or not
546 * output - returns channel block or NULL if illegal name
547 * - also modifies *isnew
549 * Get Channel block for chname (and allocate a new channel
550 * block, if it didn't exist before).
553 get_or_create_channel(struct Client
*client_p
, const char *chname
, int *isnew
)
555 struct Channel
*chptr
;
559 const char *s
= chname
;
568 if(IsServer(client_p
))
570 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
571 "*** Long channel name from %s (%d > %d): %s",
572 client_p
->name
, len
, CHANNELLEN
, s
);
576 *(t
+ CHANNELLEN
) = '\0';
580 hashv
= hash_channel(s
);
582 DLINK_FOREACH(ptr
, channelTable
[hashv
].head
)
586 if(irccmp(s
, chptr
->chname
) == 0)
597 chptr
= allocate_channel(s
);
599 dlinkAdd(chptr
, &chptr
->node
, &global_channel_list
);
601 chptr
->channelts
= CurrentTime
; /* doesn't hurt to set it here */
603 dlinkAddAlloc(chptr
, &channelTable
[hashv
]);
610 * hunts for a resv entry in the resv hash table
613 hash_find_resv(const char *name
)
615 struct ConfItem
*aconf
;
619 s_assert(name
!= NULL
);
620 if(EmptyString(name
))
623 hashv
= hash_resv(name
);
625 DLINK_FOREACH(ptr
, resvTable
[hashv
].head
)
629 if(!irccmp(name
, aconf
->name
))
640 clear_resv_hash(void)
642 struct ConfItem
*aconf
;
644 dlink_node
*next_ptr
;
647 HASH_WALK_SAFE(i
, R_MAX
, ptr
, next_ptr
, resvTable
)
651 /* skip temp resvs */
655 free_conf(ptr
->data
);
656 dlinkDestroy(ptr
, &resvTable
[i
]);
662 output_hash(struct Client
*source_p
, const char *name
, int length
, int *counts
, int deepest
)
664 unsigned long total
= 0;
667 sendto_one_numeric(source_p
, RPL_STATSDEBUG
,
668 "B :%s Hash Statistics", name
);
670 sendto_one_numeric(source_p
, RPL_STATSDEBUG
,
671 "B :Size: %d Empty: %d (%.3f%%)",
673 (float) ((counts
[0]*100) / (float) length
));
675 for(i
= 1; i
< 11; i
++)
677 total
+= (counts
[i
] * i
);
680 /* dont want to divide by 0! --fl */
681 if(counts
[0] != length
)
682 sendto_one_numeric(source_p
, RPL_STATSDEBUG
,
683 "B :Average depth: %.3f/%.3f Highest depth: %d",
684 (float) (total
/ (length
- counts
[0])),
685 (float) (total
/ length
), deepest
);
687 for(i
= 0; i
< 11; i
++)
689 sendto_one_numeric(source_p
, RPL_STATSDEBUG
,
690 "B :Nodes with %d entries: %d",
697 count_hash(struct Client
*source_p
, dlink_list
*table
, int length
, const char *name
)
703 memset(counts
, 0, sizeof(counts
));
705 for(i
= 0; i
< length
; i
++)
707 if(dlink_list_length(&table
[i
]) >= 10)
710 counts
[dlink_list_length(&table
[i
])]++;
712 if(dlink_list_length(&table
[i
]) > deepest
)
713 deepest
= dlink_list_length(&table
[i
]);
716 output_hash(source_p
, name
, length
, counts
, deepest
);
720 hash_stats(struct Client
*source_p
)
722 count_hash(source_p
, channelTable
, CH_MAX
, "Channel");
723 sendto_one_numeric(source_p
, RPL_STATSDEBUG
, "B :--");
724 count_hash(source_p
, clientTable
, U_MAX
, "Client");
725 sendto_one_numeric(source_p
, RPL_STATSDEBUG
, "B :--");
726 count_hash(source_p
, idTable
, U_MAX
, "ID");
727 sendto_one_numeric(source_p
, RPL_STATSDEBUG
, "B :--");
728 count_hash(source_p
, hostTable
, HOST_MAX
, "Hostname");