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"
34 #include "irc_string.h"
40 #include "s_newconf.h"
42 #define hash_cli_fd(x) (x % CLI_FD_MAX)
44 rb_dlink_list
*clientTable
;
45 rb_dlink_list
*channelTable
;
46 rb_dlink_list
*idTable
;
47 rb_dlink_list
*resvTable
;
48 rb_dlink_list
*hostTable
;
49 static rb_dlink_list clientbyfdTable
[U_MAX
];
52 * look in whowas.c for the missing ...[WW_MAX]; entry
58 * The server uses a chained hash table to provide quick and efficient
59 * hash table maintenance (providing the hash function works evenly over
60 * the input range). The hash table is thus not susceptible to problems
61 * of filling all the buckets or the need to rehash.
62 * It is expected that the hash table would look something like this
64 * +-----+ +-----+ +-----+ +-----+
65 * ---| 224 |----| 225 |----| 226 |---| 227 |---
66 * +-----+ +-----+ +-----+ +-----+
68 * +-----+ +-----+ +-----+
70 * +-----+ +-----+ +-----+
76 * A - GOPbot, B - chang, C - hanuaway, D - *.mu.OZ.AU
78 * The order shown above is just one instant of the server.
81 * The hash functions currently used are based Fowler/Noll/Vo hashes
82 * which work amazingly well and have a extremely low collision rate
83 * For more info see http://www.isthe.com/chongo/tech/comp/fnv/index.html
90 * clears the various hashtables
95 clientTable
= rb_malloc(sizeof(rb_dlink_list
) * U_MAX
);
96 idTable
= rb_malloc(sizeof(rb_dlink_list
) * U_MAX
);
97 channelTable
= rb_malloc(sizeof(rb_dlink_list
) * CH_MAX
);
98 hostTable
= rb_malloc(sizeof(rb_dlink_list
) * HOST_MAX
);
99 resvTable
= rb_malloc(sizeof(rb_dlink_list
) * R_MAX
);
102 #ifndef RICER_HASHING
104 fnv_hash_upper(const unsigned char *s
, int bits
)
106 u_int32_t h
= FNV1_32_INIT
;
111 h
+= (h
<<1) + (h
<<4) + (h
<<7) + (h
<< 8) + (h
<< 24);
114 h
= ((h
>> bits
) ^ h
) & ((1<<bits
)-1);
119 fnv_hash(const unsigned char *s
, int bits
)
121 u_int32_t h
= FNV1_32_INIT
;
126 h
+= (h
<<1) + (h
<<4) + (h
<<7) + (h
<< 8) + (h
<< 24);
129 h
= ((h
>> bits
) ^ h
) & ((1<<bits
)-1);
134 fnv_hash_len(const unsigned char *s
, int bits
, int len
)
136 u_int32_t h
= FNV1_32_INIT
;
137 const unsigned char *x
= s
+ len
;
141 h
+= (h
<<1) + (h
<<4) + (h
<<7) + (h
<< 8) + (h
<< 24);
144 h
= ((h
>> bits
) ^ h
) & ((1<<bits
)-1);
149 fnv_hash_upper_len(const unsigned char *s
, int bits
, int len
)
151 u_int32_t h
= FNV1_32_INIT
;
152 const unsigned char *x
= s
+ len
;
156 h
+= (h
<<1) + (h
<<4) + (h
<<7) + (h
<< 8) + (h
<< 24);
159 h
= ((h
>> bits
) ^ h
) & ((1<<bits
)-1);
166 * hashes a nickname, first converting to lowercase
169 hash_nick(const char *name
)
171 return fnv_hash_upper((const unsigned char *) name
, U_MAX_BITS
);
176 * hashes an id, case is kept
179 hash_id(const char *name
)
181 return fnv_hash((const unsigned char *) name
, U_MAX_BITS
);
186 * hashes a channel name, based on first 30 chars only for efficiency
189 hash_channel(const char *name
)
191 return fnv_hash_upper_len((const unsigned char *) name
, CH_MAX_BITS
, 30);
196 * hashes a hostname, based on first 30 chars only, as thats likely to
197 * be more dynamic than rest.
200 hash_hostname(const char *name
)
202 return fnv_hash_upper_len((const unsigned char *) name
, HOST_MAX_BITS
, 30);
207 * hashes a resv channel name, based on first 30 chars only
210 hash_resv(const char *name
)
212 return fnv_hash_upper_len((const unsigned char *) name
, R_MAX_BITS
, 30);
217 * adds an entry to the id hash table
220 add_to_id_hash(const char *name
, struct Client
*client_p
)
224 if(EmptyString(name
) || (client_p
== NULL
))
227 hashv
= hash_id(name
);
228 rb_dlinkAddAlloc(client_p
, &idTable
[hashv
]);
231 /* add_to_client_hash()
233 * adds an entry (client/server) to the client hash table
236 add_to_client_hash(const char *name
, struct Client
*client_p
)
240 s_assert(name
!= NULL
);
241 s_assert(client_p
!= NULL
);
242 if(EmptyString(name
) || (client_p
== NULL
))
245 hashv
= hash_nick(name
);
246 rb_dlinkAddAlloc(client_p
, &clientTable
[hashv
]);
249 /* add_to_hostname_hash()
251 * adds a client entry to the hostname hash table
254 add_to_hostname_hash(const char *hostname
, struct Client
*client_p
)
258 s_assert(hostname
!= NULL
);
259 s_assert(client_p
!= NULL
);
260 if(EmptyString(hostname
) || (client_p
== NULL
))
263 hashv
= hash_hostname(hostname
);
264 rb_dlinkAddAlloc(client_p
, &hostTable
[hashv
]);
267 /* add_to_resv_hash()
269 * adds a resv channel entry to the resv hash table
272 add_to_resv_hash(const char *name
, struct ConfItem
*aconf
)
276 s_assert(!EmptyString(name
));
277 s_assert(aconf
!= NULL
);
278 if(EmptyString(name
) || aconf
== NULL
)
281 hashv
= hash_resv(name
);
282 rb_dlinkAddAlloc(aconf
, &resvTable
[hashv
]);
285 /* del_from_id_hash()
287 * removes an id from the id hash table
290 del_from_id_hash(const char *id
, struct Client
*client_p
)
294 s_assert(id
!= NULL
);
295 s_assert(client_p
!= NULL
);
296 if(EmptyString(id
) || client_p
== NULL
)
300 rb_dlinkFindDestroy(client_p
, &idTable
[hashv
]);
303 /* del_from_client_hash()
305 * removes a client/server from the client hash table
308 del_from_client_hash(const char *name
, struct Client
*client_p
)
312 /* no s_asserts, this can happen when removing a client that
315 if(EmptyString(name
) || client_p
== NULL
)
318 hashv
= hash_nick(name
);
319 rb_dlinkFindDestroy(client_p
, &clientTable
[hashv
]);
322 /* del_from_channel_hash()
324 * removes a channel from the channel hash table
327 del_from_channel_hash(const char *name
, struct Channel
*chptr
)
331 s_assert(name
!= NULL
);
332 s_assert(chptr
!= NULL
);
334 if(EmptyString(name
) || chptr
== NULL
)
337 hashv
= hash_channel(name
);
338 rb_dlinkFindDestroy(chptr
, &channelTable
[hashv
]);
341 /* del_from_hostname_hash()
343 * removes a client entry from the hostname hash table
346 del_from_hostname_hash(const char *hostname
, struct Client
*client_p
)
350 if(hostname
== NULL
|| client_p
== NULL
)
353 hashv
= hash_hostname(hostname
);
355 rb_dlinkFindDestroy(client_p
, &hostTable
[hashv
]);
358 /* del_from_resv_hash()
360 * removes a resv entry from the resv hash table
363 del_from_resv_hash(const char *name
, struct ConfItem
*aconf
)
367 s_assert(name
!= NULL
);
368 s_assert(aconf
!= NULL
);
369 if(EmptyString(name
) || aconf
== NULL
)
372 hashv
= hash_resv(name
);
374 rb_dlinkFindDestroy(aconf
, &resvTable
[hashv
]);
379 * finds a client entry from the id hash table
382 find_id(const char *name
)
384 struct Client
*target_p
;
388 if(EmptyString(name
))
391 hashv
= hash_id(name
);
393 RB_DLINK_FOREACH(ptr
, idTable
[hashv
].head
)
395 target_p
= ptr
->data
;
397 if(strcmp(name
, target_p
->id
) == 0)
406 * finds a client/server entry from the client hash table
409 find_client(const char *name
)
411 struct Client
*target_p
;
415 s_assert(name
!= NULL
);
416 if(EmptyString(name
))
419 /* hunting for an id, not a nick */
421 return (find_id(name
));
423 hashv
= hash_nick(name
);
425 RB_DLINK_FOREACH(ptr
, clientTable
[hashv
].head
)
427 target_p
= ptr
->data
;
429 if(irccmp(name
, target_p
->name
) == 0)
436 /* find_named_client()
438 * finds a client/server entry from the client hash table
441 find_named_client(const char *name
)
443 struct Client
*target_p
;
447 s_assert(name
!= NULL
);
448 if(EmptyString(name
))
451 hashv
= hash_nick(name
);
453 RB_DLINK_FOREACH(ptr
, clientTable
[hashv
].head
)
455 target_p
= ptr
->data
;
457 if(irccmp(name
, target_p
->name
) == 0)
466 * finds a server from the client hash table
469 find_server(struct Client
*source_p
, const char *name
)
471 struct Client
*target_p
;
475 if(EmptyString(name
))
478 if((source_p
== NULL
|| !MyClient(source_p
)) &&
479 IsDigit(*name
) && strlen(name
) == 3)
481 target_p
= find_id(name
);
485 hashv
= hash_nick(name
);
487 RB_DLINK_FOREACH(ptr
, clientTable
[hashv
].head
)
489 target_p
= ptr
->data
;
491 if((IsServer(target_p
) || IsMe(target_p
)) &&
492 irccmp(name
, target_p
->name
) == 0)
501 * finds a hostname rb_dlink list from the hostname hash table.
502 * we return the full rb_dlink list, because you can have multiple
503 * entries with the same hostname
506 find_hostname(const char *hostname
)
510 if(EmptyString(hostname
))
513 hashv
= hash_hostname(hostname
);
515 return hostTable
[hashv
].head
;
520 * finds a channel from the channel hash table
523 find_channel(const char *name
)
525 struct Channel
*chptr
;
529 s_assert(name
!= NULL
);
530 if(EmptyString(name
))
533 hashv
= hash_channel(name
);
535 RB_DLINK_FOREACH(ptr
, channelTable
[hashv
].head
)
539 if(irccmp(name
, chptr
->chname
) == 0)
547 * get_or_create_channel
548 * inputs - client pointer
550 * - pointer to int flag whether channel was newly created or not
551 * output - returns channel block or NULL if illegal name
552 * - also modifies *isnew
554 * Get Channel block for chname (and allocate a new channel
555 * block, if it didn't exist before).
558 get_or_create_channel(struct Client
*client_p
, const char *chname
, int *isnew
)
560 struct Channel
*chptr
;
564 const char *s
= chname
;
573 if(IsServer(client_p
))
575 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
576 "*** Long channel name from %s (%d > %d): %s",
577 client_p
->name
, len
, CHANNELLEN
, s
);
581 *(t
+ CHANNELLEN
) = '\0';
585 hashv
= hash_channel(s
);
587 RB_DLINK_FOREACH(ptr
, channelTable
[hashv
].head
)
591 if(irccmp(s
, chptr
->chname
) == 0)
602 chptr
= allocate_channel(s
);
604 rb_dlinkAdd(chptr
, &chptr
->node
, &global_channel_list
);
606 chptr
->channelts
= rb_current_time(); /* doesn't hurt to set it here */
608 rb_dlinkAddAlloc(chptr
, &channelTable
[hashv
]);
615 * hunts for a resv entry in the resv hash table
618 hash_find_resv(const char *name
)
620 struct ConfItem
*aconf
;
624 s_assert(name
!= NULL
);
625 if(EmptyString(name
))
628 hashv
= hash_resv(name
);
630 RB_DLINK_FOREACH(ptr
, resvTable
[hashv
].head
)
634 if(!irccmp(name
, aconf
->name
))
645 clear_resv_hash(void)
647 struct ConfItem
*aconf
;
649 rb_dlink_node
*next_ptr
;
652 HASH_WALK_SAFE(i
, R_MAX
, ptr
, next_ptr
, resvTable
)
656 /* skip temp resvs */
660 free_conf(ptr
->data
);
661 rb_dlinkDestroy(ptr
, &resvTable
[i
]);
667 add_to_cli_fd_hash(struct Client
*client_p
)
669 rb_dlinkAddAlloc(client_p
, &clientbyfdTable
[hash_cli_fd(rb_get_fd(client_p
->localClient
->F
))]);
674 del_from_cli_fd_hash(struct Client
*client_p
)
677 hashv
= hash_cli_fd(rb_get_fd(client_p
->localClient
->F
));
678 rb_dlinkFindDestroy(client_p
, &clientbyfdTable
[hashv
]);
682 find_cli_fd_hash(int fd
)
684 struct Client
*target_p
;
687 hashv
= hash_cli_fd(fd
);
688 RB_DLINK_FOREACH(ptr
, clientbyfdTable
[hashv
].head
)
690 target_p
= ptr
->data
;
691 if(rb_get_fd(target_p
->localClient
->F
) == fd
)
698 output_hash(struct Client
*source_p
, const char *name
, int length
, int *counts
, int deepest
)
700 unsigned long total
= 0;
703 sendto_one_numeric(source_p
, RPL_STATSDEBUG
,
704 "B :%s Hash Statistics", name
);
706 sendto_one_numeric(source_p
, RPL_STATSDEBUG
,
707 "B :Size: %d Empty: %d (%.3f%%)",
709 (float) ((counts
[0]*100) / (float) length
));
711 for(i
= 1; i
< 11; i
++)
713 total
+= (counts
[i
] * i
);
716 /* dont want to divide by 0! --fl */
717 if(counts
[0] != length
)
718 sendto_one_numeric(source_p
, RPL_STATSDEBUG
,
719 "B :Average depth: %.3f/%.3f Highest depth: %d",
720 (float) (total
/ (length
- counts
[0])),
721 (float) (total
/ length
), deepest
);
723 for(i
= 0; i
< 11; i
++)
725 sendto_one_numeric(source_p
, RPL_STATSDEBUG
,
726 "B :Nodes with %d entries: %d",
733 count_hash(struct Client
*source_p
, rb_dlink_list
*table
, int length
, const char *name
)
739 memset(counts
, 0, sizeof(counts
));
741 for(i
= 0; i
< length
; i
++)
743 if(rb_dlink_list_length(&table
[i
]) >= 10)
746 counts
[rb_dlink_list_length(&table
[i
])]++;
748 if(rb_dlink_list_length(&table
[i
]) > deepest
)
749 deepest
= rb_dlink_list_length(&table
[i
]);
752 output_hash(source_p
, name
, length
, counts
, deepest
);
756 hash_stats(struct Client
*source_p
)
758 count_hash(source_p
, channelTable
, CH_MAX
, "Channel");
759 sendto_one_numeric(source_p
, RPL_STATSDEBUG
, "B :--");
760 count_hash(source_p
, clientTable
, U_MAX
, "Client");
761 sendto_one_numeric(source_p
, RPL_STATSDEBUG
, "B :--");
762 count_hash(source_p
, idTable
, U_MAX
, "ID");
763 sendto_one_numeric(source_p
, RPL_STATSDEBUG
, "B :--");
764 count_hash(source_p
, hostTable
, HOST_MAX
, "Hostname");