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 #define hash_cli_fd(x) (x % CLI_FD_MAX)
44 static rb_dlink_list clientbyfdTable
[U_MAX
];
46 rb_dlink_list
*clientTable
;
47 rb_dlink_list
*channelTable
;
48 rb_dlink_list
*idTable
;
49 rb_dlink_list
*resvTable
;
50 rb_dlink_list
*hostTable
;
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
= rb_malloc(sizeof(rb_dlink_list
) * U_MAX
);
97 idTable
= rb_malloc(sizeof(rb_dlink_list
) * U_MAX
);
98 channelTable
= rb_malloc(sizeof(rb_dlink_list
) * CH_MAX
);
99 hostTable
= rb_malloc(sizeof(rb_dlink_list
) * HOST_MAX
);
100 resvTable
= rb_malloc(sizeof(rb_dlink_list
) * R_MAX
);
103 #ifndef RICER_HASHING
105 fnv_hash_upper(const unsigned char *s
, int bits
)
107 u_int32_t h
= FNV1_32_INIT
;
112 h
+= (h
<<1) + (h
<<4) + (h
<<7) + (h
<< 8) + (h
<< 24);
115 h
= ((h
>> bits
) ^ h
) & ((1<<bits
)-1);
120 fnv_hash(const unsigned char *s
, int bits
)
122 u_int32_t h
= FNV1_32_INIT
;
127 h
+= (h
<<1) + (h
<<4) + (h
<<7) + (h
<< 8) + (h
<< 24);
130 h
= ((h
>> bits
) ^ h
) & ((1<<bits
)-1);
135 fnv_hash_len(const unsigned char *s
, int bits
, int len
)
137 u_int32_t h
= FNV1_32_INIT
;
138 const unsigned char *x
= s
+ len
;
142 h
+= (h
<<1) + (h
<<4) + (h
<<7) + (h
<< 8) + (h
<< 24);
145 h
= ((h
>> bits
) ^ h
) & ((1<<bits
)-1);
150 fnv_hash_upper_len(const unsigned char *s
, int bits
, int len
)
152 u_int32_t h
= FNV1_32_INIT
;
153 const unsigned char *x
= s
+ len
;
157 h
+= (h
<<1) + (h
<<4) + (h
<<7) + (h
<< 8) + (h
<< 24);
160 h
= ((h
>> bits
) ^ h
) & ((1<<bits
)-1);
167 * hashes a nickname, first converting to lowercase
170 hash_nick(const char *name
)
172 return fnv_hash_upper((const unsigned char *) name
, U_MAX_BITS
);
177 * hashes an id, case is kept
180 hash_id(const char *name
)
182 return fnv_hash((const unsigned char *) name
, U_MAX_BITS
);
187 * hashes a channel name, based on first 30 chars only for efficiency
190 hash_channel(const char *name
)
192 return fnv_hash_upper_len((const unsigned char *) name
, CH_MAX_BITS
, 30);
197 * hashes a hostname, based on first 30 chars only, as thats likely to
198 * be more dynamic than rest.
201 hash_hostname(const char *name
)
203 return fnv_hash_upper_len((const unsigned char *) name
, HOST_MAX_BITS
, 30);
208 * hashes a resv channel name, based on first 30 chars only
211 hash_resv(const char *name
)
213 return fnv_hash_upper_len((const unsigned char *) name
, R_MAX_BITS
, 30);
218 * adds an entry to the id hash table
221 add_to_id_hash(const char *name
, struct Client
*client_p
)
225 if(EmptyString(name
) || (client_p
== NULL
))
228 hashv
= hash_id(name
);
229 rb_dlinkAddAlloc(client_p
, &idTable
[hashv
]);
232 /* add_to_client_hash()
234 * adds an entry (client/server) to the client hash table
237 add_to_client_hash(const char *name
, struct Client
*client_p
)
241 s_assert(name
!= NULL
);
242 s_assert(client_p
!= NULL
);
243 if(EmptyString(name
) || (client_p
== NULL
))
246 hashv
= hash_nick(name
);
247 rb_dlinkAddAlloc(client_p
, &clientTable
[hashv
]);
250 /* add_to_hostname_hash()
252 * adds a client entry to the hostname hash table
255 add_to_hostname_hash(const char *hostname
, struct Client
*client_p
)
259 s_assert(hostname
!= NULL
);
260 s_assert(client_p
!= NULL
);
261 if(EmptyString(hostname
) || (client_p
== NULL
))
264 hashv
= hash_hostname(hostname
);
265 rb_dlinkAddAlloc(client_p
, &hostTable
[hashv
]);
268 /* add_to_resv_hash()
270 * adds a resv channel entry to the resv hash table
273 add_to_resv_hash(const char *name
, struct ConfItem
*aconf
)
277 s_assert(!EmptyString(name
));
278 s_assert(aconf
!= NULL
);
279 if(EmptyString(name
) || aconf
== NULL
)
282 hashv
= hash_resv(name
);
283 rb_dlinkAddAlloc(aconf
, &resvTable
[hashv
]);
286 /* del_from_id_hash()
288 * removes an id from the id hash table
291 del_from_id_hash(const char *id
, struct Client
*client_p
)
295 s_assert(id
!= NULL
);
296 s_assert(client_p
!= NULL
);
297 if(EmptyString(id
) || client_p
== NULL
)
301 rb_dlinkFindDestroy(client_p
, &idTable
[hashv
]);
304 /* del_from_client_hash()
306 * removes a client/server from the client hash table
309 del_from_client_hash(const char *name
, struct Client
*client_p
)
313 /* no s_asserts, this can happen when removing a client that
316 if(EmptyString(name
) || client_p
== NULL
)
319 hashv
= hash_nick(name
);
320 rb_dlinkFindDestroy(client_p
, &clientTable
[hashv
]);
323 /* del_from_channel_hash()
325 * removes a channel from the channel hash table
328 del_from_channel_hash(const char *name
, struct Channel
*chptr
)
332 s_assert(name
!= NULL
);
333 s_assert(chptr
!= NULL
);
335 if(EmptyString(name
) || chptr
== NULL
)
338 hashv
= hash_channel(name
);
339 rb_dlinkFindDestroy(chptr
, &channelTable
[hashv
]);
342 /* del_from_hostname_hash()
344 * removes a client entry from the hostname hash table
347 del_from_hostname_hash(const char *hostname
, struct Client
*client_p
)
351 if(hostname
== NULL
|| client_p
== NULL
)
354 hashv
= hash_hostname(hostname
);
356 rb_dlinkFindDestroy(client_p
, &hostTable
[hashv
]);
359 /* del_from_resv_hash()
361 * removes a resv entry from the resv hash table
364 del_from_resv_hash(const char *name
, struct ConfItem
*aconf
)
368 s_assert(name
!= NULL
);
369 s_assert(aconf
!= NULL
);
370 if(EmptyString(name
) || aconf
== NULL
)
373 hashv
= hash_resv(name
);
375 rb_dlinkFindDestroy(aconf
, &resvTable
[hashv
]);
380 * finds a client entry from the id hash table
383 find_id(const char *name
)
385 struct Client
*target_p
;
389 if(EmptyString(name
))
392 hashv
= hash_id(name
);
394 RB_DLINK_FOREACH(ptr
, idTable
[hashv
].head
)
396 target_p
= ptr
->data
;
398 if(strcmp(name
, target_p
->id
) == 0)
407 * finds a client/server entry from the client hash table
410 find_client(const char *name
)
412 struct Client
*target_p
;
416 s_assert(name
!= NULL
);
417 if(EmptyString(name
))
420 /* hunting for an id, not a nick */
422 return (find_id(name
));
424 hashv
= hash_nick(name
);
426 RB_DLINK_FOREACH(ptr
, clientTable
[hashv
].head
)
428 target_p
= ptr
->data
;
430 if(irccmp(name
, target_p
->name
) == 0)
437 /* find_named_client()
439 * finds a client/server entry from the client hash table
442 find_named_client(const char *name
)
444 struct Client
*target_p
;
448 s_assert(name
!= NULL
);
449 if(EmptyString(name
))
452 hashv
= hash_nick(name
);
454 RB_DLINK_FOREACH(ptr
, clientTable
[hashv
].head
)
456 target_p
= ptr
->data
;
458 if(irccmp(name
, target_p
->name
) == 0)
467 * finds a server from the client hash table
470 find_server(struct Client
*source_p
, const char *name
)
472 struct Client
*target_p
;
476 if(EmptyString(name
))
479 if((source_p
== NULL
|| !MyClient(source_p
)) &&
480 IsDigit(*name
) && strlen(name
) == 3)
482 target_p
= find_id(name
);
486 hashv
= hash_nick(name
);
488 RB_DLINK_FOREACH(ptr
, clientTable
[hashv
].head
)
490 target_p
= ptr
->data
;
492 if((IsServer(target_p
) || IsMe(target_p
)) &&
493 irccmp(name
, target_p
->name
) == 0)
502 * finds a hostname rb_dlink list from the hostname hash table.
503 * we return the full rb_dlink list, because you can have multiple
504 * entries with the same hostname
507 find_hostname(const char *hostname
)
511 if(EmptyString(hostname
))
514 hashv
= hash_hostname(hostname
);
516 return hostTable
[hashv
].head
;
521 * finds a channel from the channel hash table
524 find_channel(const char *name
)
526 struct Channel
*chptr
;
530 s_assert(name
!= NULL
);
531 if(EmptyString(name
))
534 hashv
= hash_channel(name
);
536 RB_DLINK_FOREACH(ptr
, channelTable
[hashv
].head
)
540 if(irccmp(name
, chptr
->chname
) == 0)
548 * get_or_create_channel
549 * inputs - client pointer
551 * - pointer to int flag whether channel was newly created or not
552 * output - returns channel block or NULL if illegal name
553 * - also modifies *isnew
555 * Get Channel block for chname (and allocate a new channel
556 * block, if it didn't exist before).
559 get_or_create_channel(struct Client
*client_p
, const char *chname
, int *isnew
)
561 struct Channel
*chptr
;
565 const char *s
= chname
;
574 if(IsServer(client_p
))
576 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
577 "*** Long channel name from %s (%d > %d): %s",
578 client_p
->name
, len
, CHANNELLEN
, s
);
582 *(t
+ CHANNELLEN
) = '\0';
586 hashv
= hash_channel(s
);
588 RB_DLINK_FOREACH(ptr
, channelTable
[hashv
].head
)
592 if(irccmp(s
, chptr
->chname
) == 0)
603 chptr
= allocate_channel(s
);
605 rb_dlinkAdd(chptr
, &chptr
->node
, &global_channel_list
);
607 chptr
->channelts
= rb_current_time(); /* doesn't hurt to set it here */
609 rb_dlinkAddAlloc(chptr
, &channelTable
[hashv
]);
616 * hunts for a resv entry in the resv hash table
619 hash_find_resv(const char *name
)
621 struct ConfItem
*aconf
;
625 s_assert(name
!= NULL
);
626 if(EmptyString(name
))
629 hashv
= hash_resv(name
);
631 RB_DLINK_FOREACH(ptr
, resvTable
[hashv
].head
)
635 if(!irccmp(name
, aconf
->host
))
646 clear_resv_hash(void)
648 struct ConfItem
*aconf
;
650 rb_dlink_node
*next_ptr
;
653 HASH_WALK_SAFE(i
, R_MAX
, ptr
, next_ptr
, resvTable
)
657 /* skip temp resvs */
661 free_conf(ptr
->data
);
662 rb_dlinkDestroy(ptr
, &resvTable
[i
]);
668 add_to_cli_fd_hash(struct Client
*client_p
)
670 rb_dlinkAddAlloc(client_p
, &clientbyfdTable
[hash_cli_fd(rb_get_fd(client_p
->localClient
->F
))]);
675 del_from_cli_fd_hash(struct Client
*client_p
)
678 hashv
= hash_cli_fd(rb_get_fd(client_p
->localClient
->F
));
679 rb_dlinkFindDestroy(client_p
, &clientbyfdTable
[hashv
]);
683 find_cli_fd_hash(int fd
)
685 struct Client
*target_p
;
688 hashv
= hash_cli_fd(fd
);
689 RB_DLINK_FOREACH(ptr
, clientbyfdTable
[hashv
].head
)
691 target_p
= ptr
->data
;
692 if(rb_get_fd(target_p
->localClient
->F
) == fd
)
699 output_hash(struct Client
*source_p
, const char *name
, int length
, int *counts
, int deepest
)
701 unsigned long total
= 0;
705 sendto_one_numeric(source_p
, RPL_STATSDEBUG
,
706 "B :%s Hash Statistics", name
);
708 snprintf(buf
, sizeof buf
, "%.3f%%",
709 (float) ((counts
[0]*100) / (float) length
));
710 sendto_one_numeric(source_p
, RPL_STATSDEBUG
,
711 "B :Size: %d Empty: %d (%s)",
712 length
, counts
[0], buf
);
714 for(i
= 1; i
< 11; i
++)
716 total
+= (counts
[i
] * i
);
719 /* dont want to divide by 0! --fl */
720 if(counts
[0] != length
)
722 snprintf(buf
, sizeof buf
, "%.3f/%.3f",
723 (float) (total
/ (length
- counts
[0])),
724 (float) (total
/ length
));
725 sendto_one_numeric(source_p
, RPL_STATSDEBUG
,
726 "B :Average depth: %s Highest depth: %d",
730 for(i
= 0; i
< 11; i
++)
732 sendto_one_numeric(source_p
, RPL_STATSDEBUG
,
733 "B :Nodes with %d entries: %d",
740 count_hash(struct Client
*source_p
, rb_dlink_list
*table
, int length
, const char *name
)
746 memset(counts
, 0, sizeof(counts
));
748 for(i
= 0; i
< length
; i
++)
750 if(rb_dlink_list_length(&table
[i
]) >= 10)
753 counts
[rb_dlink_list_length(&table
[i
])]++;
755 if(rb_dlink_list_length(&table
[i
]) > deepest
)
756 deepest
= rb_dlink_list_length(&table
[i
]);
759 output_hash(source_p
, name
, length
, counts
, deepest
);
763 hash_stats(struct Client
*source_p
)
765 count_hash(source_p
, channelTable
, CH_MAX
, "Channel");
766 sendto_one_numeric(source_p
, RPL_STATSDEBUG
, "B :--");
767 count_hash(source_p
, clientTable
, U_MAX
, "Client");
768 sendto_one_numeric(source_p
, RPL_STATSDEBUG
, "B :--");
769 count_hash(source_p
, idTable
, U_MAX
, "ID");
770 sendto_one_numeric(source_p
, RPL_STATSDEBUG
, "B :--");
771 count_hash(source_p
, hostTable
, HOST_MAX
, "Hostname");