]>
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);
113 h
= ((h
>> bits
) ^ h
) & ((1<<bits
)-1);
118 fnv_hash(const unsigned char *s
, int bits
)
120 u_int32_t h
= FNV1_32_INIT
;
125 h
+= (h
<<1) + (h
<<4) + (h
<<7) + (h
<< 8) + (h
<< 24);
128 h
= ((h
>> bits
) ^ h
) & ((1<<bits
)-1);
133 fnv_hash_len(const unsigned char *s
, int bits
, int len
)
135 u_int32_t h
= FNV1_32_INIT
;
136 const unsigned char *x
= s
+ len
;
140 h
+= (h
<<1) + (h
<<4) + (h
<<7) + (h
<< 8) + (h
<< 24);
143 h
= ((h
>> bits
) ^ h
) & ((1<<bits
)-1);
148 fnv_hash_upper_len(const unsigned char *s
, int bits
, int len
)
150 u_int32_t h
= FNV1_32_INIT
;
151 const unsigned char *x
= s
+ len
;
155 h
+= (h
<<1) + (h
<<4) + (h
<<7) + (h
<< 8) + (h
<< 24);
158 h
= ((h
>> bits
) ^ h
) & ((1<<bits
)-1);
165 * hashes a nickname, first converting to lowercase
168 hash_nick(const char *name
)
170 return fnv_hash_upper((const unsigned char *) name
, U_MAX_BITS
);
175 * hashes an id, case is kept
178 hash_id(const char *name
)
180 return fnv_hash((const unsigned char *) name
, U_MAX_BITS
);
185 * hashes a channel name, based on first 30 chars only for efficiency
188 hash_channel(const char *name
)
190 return fnv_hash_upper_len((const unsigned char *) name
, CH_MAX_BITS
, 30);
195 * hashes a hostname, based on first 30 chars only, as thats likely to
196 * be more dynamic than rest.
199 hash_hostname(const char *name
)
201 return fnv_hash_upper_len((const unsigned char *) name
, HOST_MAX_BITS
, 30);
206 * hashes a resv channel name, based on first 30 chars only
209 hash_resv(const char *name
)
211 return fnv_hash_upper_len((const unsigned char *) name
, R_MAX_BITS
, 30);
216 * adds an entry to the id hash table
219 add_to_id_hash(const char *name
, struct Client
*client_p
)
223 if(EmptyString(name
) || (client_p
== NULL
))
226 hashv
= hash_id(name
);
227 dlinkAddAlloc(client_p
, &idTable
[hashv
]);
230 /* add_to_client_hash()
232 * adds an entry (client/server) to the client hash table
235 add_to_client_hash(const char *name
, struct Client
*client_p
)
239 s_assert(name
!= NULL
);
240 s_assert(client_p
!= NULL
);
241 if(EmptyString(name
) || (client_p
== NULL
))
244 hashv
= hash_nick(name
);
245 dlinkAddAlloc(client_p
, &clientTable
[hashv
]);
248 /* add_to_hostname_hash()
250 * adds a client entry to the hostname hash table
253 add_to_hostname_hash(const char *hostname
, struct Client
*client_p
)
257 s_assert(hostname
!= NULL
);
258 s_assert(client_p
!= NULL
);
259 if(EmptyString(hostname
) || (client_p
== NULL
))
262 hashv
= hash_hostname(hostname
);
263 dlinkAddAlloc(client_p
, &hostTable
[hashv
]);
266 /* add_to_resv_hash()
268 * adds a resv channel entry to the resv hash table
271 add_to_resv_hash(const char *name
, struct ConfItem
*aconf
)
275 s_assert(!EmptyString(name
));
276 s_assert(aconf
!= NULL
);
277 if(EmptyString(name
) || aconf
== NULL
)
280 hashv
= hash_resv(name
);
281 dlinkAddAlloc(aconf
, &resvTable
[hashv
]);
284 /* del_from_id_hash()
286 * removes an id from the id hash table
289 del_from_id_hash(const char *id
, struct Client
*client_p
)
293 s_assert(id
!= NULL
);
294 s_assert(client_p
!= NULL
);
295 if(EmptyString(id
) || client_p
== NULL
)
299 dlinkFindDestroy(client_p
, &idTable
[hashv
]);
302 /* del_from_client_hash()
304 * removes a client/server from the client hash table
307 del_from_client_hash(const char *name
, struct Client
*client_p
)
311 /* no s_asserts, this can happen when removing a client that
314 if(EmptyString(name
) || client_p
== NULL
)
317 hashv
= hash_nick(name
);
318 dlinkFindDestroy(client_p
, &clientTable
[hashv
]);
321 /* del_from_channel_hash()
323 * removes a channel from the channel hash table
326 del_from_channel_hash(const char *name
, struct Channel
*chptr
)
330 s_assert(name
!= NULL
);
331 s_assert(chptr
!= NULL
);
333 if(EmptyString(name
) || chptr
== NULL
)
336 hashv
= hash_channel(name
);
337 dlinkFindDestroy(chptr
, &channelTable
[hashv
]);
340 /* del_from_hostname_hash()
342 * removes a client entry from the hostname hash table
345 del_from_hostname_hash(const char *hostname
, struct Client
*client_p
)
349 if(hostname
== NULL
|| client_p
== NULL
)
352 hashv
= hash_hostname(hostname
);
354 dlinkFindDestroy(client_p
, &hostTable
[hashv
]);
357 /* del_from_resv_hash()
359 * removes a resv entry from the resv hash table
362 del_from_resv_hash(const char *name
, struct ConfItem
*aconf
)
366 s_assert(name
!= NULL
);
367 s_assert(aconf
!= NULL
);
368 if(EmptyString(name
) || aconf
== NULL
)
371 hashv
= hash_resv(name
);
373 dlinkFindDestroy(aconf
, &resvTable
[hashv
]);
378 * finds a client entry from the id hash table
381 find_id(const char *name
)
383 struct Client
*target_p
;
387 if(EmptyString(name
))
390 hashv
= hash_id(name
);
392 DLINK_FOREACH(ptr
, idTable
[hashv
].head
)
394 target_p
= ptr
->data
;
396 if(strcmp(name
, target_p
->id
) == 0)
405 * finds a client/server entry from the client hash table
408 find_client(const char *name
)
410 struct Client
*target_p
;
414 s_assert(name
!= NULL
);
415 if(EmptyString(name
))
418 /* hunting for an id, not a nick */
420 return (find_id(name
));
422 hashv
= hash_nick(name
);
424 DLINK_FOREACH(ptr
, clientTable
[hashv
].head
)
426 target_p
= ptr
->data
;
428 if(irccmp(name
, target_p
->name
) == 0)
435 /* find_named_client()
437 * finds a client/server entry from the client hash table
440 find_named_client(const char *name
)
442 struct Client
*target_p
;
446 s_assert(name
!= NULL
);
447 if(EmptyString(name
))
450 hashv
= hash_nick(name
);
452 DLINK_FOREACH(ptr
, clientTable
[hashv
].head
)
454 target_p
= ptr
->data
;
456 if(irccmp(name
, target_p
->name
) == 0)
465 * finds a server from the client hash table
468 find_server(struct Client
*source_p
, const char *name
)
470 struct Client
*target_p
;
474 if(EmptyString(name
))
477 if((source_p
== NULL
|| !MyClient(source_p
)) &&
478 IsDigit(*name
) && strlen(name
) == 3)
480 target_p
= find_id(name
);
484 hashv
= hash_nick(name
);
486 DLINK_FOREACH(ptr
, clientTable
[hashv
].head
)
488 target_p
= ptr
->data
;
490 if((IsServer(target_p
) || IsMe(target_p
)) &&
491 irccmp(name
, target_p
->name
) == 0)
500 * finds a hostname dlink list from the hostname hash table.
501 * we return the full dlink list, because you can have multiple
502 * entries with the same hostname
505 find_hostname(const char *hostname
)
509 if(EmptyString(hostname
))
512 hashv
= hash_hostname(hostname
);
514 return hostTable
[hashv
].head
;
519 * finds a channel from the channel hash table
522 find_channel(const char *name
)
524 struct Channel
*chptr
;
528 s_assert(name
!= NULL
);
529 if(EmptyString(name
))
532 hashv
= hash_channel(name
);
534 DLINK_FOREACH(ptr
, channelTable
[hashv
].head
)
538 if(irccmp(name
, chptr
->chname
) == 0)
546 * get_or_create_channel
547 * inputs - client pointer
549 * - pointer to int flag whether channel was newly created or not
550 * output - returns channel block or NULL if illegal name
551 * - also modifies *isnew
553 * Get Channel block for chname (and allocate a new channel
554 * block, if it didn't exist before).
557 get_or_create_channel(struct Client
*client_p
, const char *chname
, int *isnew
)
559 struct Channel
*chptr
;
563 const char *s
= chname
;
572 if(IsServer(client_p
))
574 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
575 "*** Long channel name from %s (%d > %d): %s",
576 client_p
->name
, len
, CHANNELLEN
, s
);
580 *(t
+ CHANNELLEN
) = '\0';
584 hashv
= hash_channel(s
);
586 DLINK_FOREACH(ptr
, channelTable
[hashv
].head
)
590 if(irccmp(s
, chptr
->chname
) == 0)
601 chptr
= allocate_channel(s
);
603 dlinkAdd(chptr
, &chptr
->node
, &global_channel_list
);
605 chptr
->channelts
= CurrentTime
; /* doesn't hurt to set it here */
607 dlinkAddAlloc(chptr
, &channelTable
[hashv
]);
614 * hunts for a resv entry in the resv hash table
617 hash_find_resv(const char *name
)
619 struct ConfItem
*aconf
;
623 s_assert(name
!= NULL
);
624 if(EmptyString(name
))
627 hashv
= hash_resv(name
);
629 DLINK_FOREACH(ptr
, resvTable
[hashv
].head
)
633 if(!irccmp(name
, aconf
->name
))
644 clear_resv_hash(void)
646 struct ConfItem
*aconf
;
648 dlink_node
*next_ptr
;
651 HASH_WALK_SAFE(i
, R_MAX
, ptr
, next_ptr
, resvTable
)
655 /* skip temp resvs */
659 free_conf(ptr
->data
);
660 dlinkDestroy(ptr
, &resvTable
[i
]);
666 output_hash(struct Client
*source_p
, const char *name
, int length
, int *counts
, int deepest
)
668 unsigned long total
= 0;
671 sendto_one_numeric(source_p
, RPL_STATSDEBUG
,
672 "B :%s Hash Statistics", name
);
674 sendto_one_numeric(source_p
, RPL_STATSDEBUG
,
675 "B :Size: %d Empty: %d (%.3f%%)",
677 (float) ((counts
[0]*100) / (float) length
));
679 for(i
= 1; i
< 11; i
++)
681 total
+= (counts
[i
] * i
);
684 /* dont want to divide by 0! --fl */
685 if(counts
[0] != length
)
686 sendto_one_numeric(source_p
, RPL_STATSDEBUG
,
687 "B :Average depth: %.3f/%.3f Highest depth: %d",
688 (float) (total
/ (length
- counts
[0])),
689 (float) (total
/ length
), deepest
);
691 for(i
= 0; i
< 11; i
++)
693 sendto_one_numeric(source_p
, RPL_STATSDEBUG
,
694 "B :Nodes with %d entries: %d",
701 count_hash(struct Client
*source_p
, dlink_list
*table
, int length
, const char *name
)
707 memset(counts
, 0, sizeof(counts
));
709 for(i
= 0; i
< length
; i
++)
711 if(dlink_list_length(&table
[i
]) >= 10)
714 counts
[dlink_list_length(&table
[i
])]++;
716 if(dlink_list_length(&table
[i
]) > deepest
)
717 deepest
= dlink_list_length(&table
[i
]);
720 output_hash(source_p
, name
, length
, counts
, deepest
);
724 hash_stats(struct Client
*source_p
)
726 count_hash(source_p
, channelTable
, CH_MAX
, "Channel");
727 sendto_one_numeric(source_p
, RPL_STATSDEBUG
, "B :--");
728 count_hash(source_p
, clientTable
, U_MAX
, "Client");
729 sendto_one_numeric(source_p
, RPL_STATSDEBUG
, "B :--");
730 count_hash(source_p
, idTable
, U_MAX
, "ID");
731 sendto_one_numeric(source_p
, RPL_STATSDEBUG
, "B :--");
732 count_hash(source_p
, hostTable
, HOST_MAX
, "Hostname");