2 * IRC - Internet Relay Chat, ircd/hash.c
3 * Copyright (C) 1998 Andrea Cocito, completely rewritten version.
4 * Previous version was Copyright (C) 1991 Darren Reed, the concept
5 * of linked lists for each hash bucket and the move-to-head
6 * optimization has been borrowed from there.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 1, or (at your option)
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include "ircd_alloc.h"
28 #include "ircd_chattr.h"
30 #include "ircd_reply.h"
31 #include "ircd_string.h"
40 /* #include <assert.h> -- Now using assert in ircd_log.h */
47 * @brief Hash table management.
50 * This file used to use some very complicated hash function. Now it
51 * uses CRC-32, but effectively remaps each input byte according to a
52 * table initialized at startup.
55 /** Hash table for clients. */
56 static struct Client *clientTable[HASHSIZE];
57 /** Hash table for channels. */
58 static struct Channel *channelTable[HASHSIZE];
59 /** CRC-32 update table. */
60 static uint32_t crc32hash[256];
62 /** Initialize the map used by the hash function. */
65 unsigned int ii, jj, rand, poly;
67 /* First calculate a normal CRC-32 table. */
68 for (ii = 0, poly = 0xedb88320; ii < 256; ii++)
71 for (jj = 0; jj < 8; jj++)
72 rand = (rand & 1) ? poly ^ (rand >> 1) : rand >> 1;
76 /* Now reorder the hash table. */
77 for (ii = 0, rand = 0; ii < 256; ii++)
81 poly = ii + rand % (256 - ii);
83 crc32hash[ii] = crc32hash[poly];
89 /** Output type of hash function. */
90 typedef unsigned int HASHREGS;
92 /** Calculate hash value for a string.
93 * @param[in] n String to hash.
94 * @return Hash value for string.
96 static HASHREGS strhash(const char *n)
98 HASHREGS hash = crc32hash[ToLower(*n++) & 255];
100 hash = (hash >> 8) ^ crc32hash[(hash ^ ToLower(*n++)) & 255];
101 return hash % HASHSIZE;
104 /************************** Externally visible functions ********************/
106 /* Optimization note: in these functions I supposed that the CSE optimization
107 * (Common Subexpression Elimination) does its work decently, this means that
108 * I avoided introducing new variables to do the work myself and I did let
109 * the optimizer play with more free registers, actual tests proved this
110 * solution to be faster than doing things like tmp2=tmp->hnext... and then
111 * use tmp2 myself which would have given less freedom to the optimizer.
114 /** Prepend a client to the appropriate hash bucket.
115 * @param[in] cptr Client to add to hash table.
118 int hAddClient(struct Client *cptr)
120 HASHREGS hashv = strhash(cli_name(cptr));
122 cli_hnext(cptr) = clientTable[hashv];
123 clientTable[hashv] = cptr;
128 /** Prepend a channel to the appropriate hash bucket.
129 * @param[in] chptr Channel to add to hash table.
132 int hAddChannel(struct Channel *chptr)
134 HASHREGS hashv = strhash(chptr->chname);
136 chptr->hnext = channelTable[hashv];
137 channelTable[hashv] = chptr;
142 /** Remove a client from its hash bucket.
143 * @param[in] cptr Client to remove from hash table.
144 * @return Zero if the client is found and removed, -1 if not found.
146 int hRemClient(struct Client *cptr)
148 HASHREGS hashv = strhash(cli_name(cptr));
149 struct Client *tmp = clientTable[hashv];
152 clientTable[hashv] = cli_hnext(cptr);
153 cli_hnext(cptr) = cptr;
158 if (cli_hnext(tmp) == cptr) {
159 cli_hnext(tmp) = cli_hnext(cli_hnext(tmp));
160 cli_hnext(cptr) = cptr;
163 tmp = cli_hnext(tmp);
168 /** Rename a client in the hash table.
169 * @param[in] cptr Client whose nickname is changing.
170 * @param[in] newname New nickname for client.
173 int hChangeClient(struct Client *cptr, const char *newname)
175 HASHREGS newhash = strhash(newname);
180 cli_hnext(cptr) = clientTable[newhash];
181 clientTable[newhash] = cptr;
185 /** Remove a channel from its hash bucket.
186 * @param[in] chptr Channel to remove from hash table.
187 * @return Zero if the channel is found and removed, -1 if not found.
189 int hRemChannel(struct Channel *chptr)
191 HASHREGS hashv = strhash(chptr->chname);
192 struct Channel *tmp = channelTable[hashv];
195 channelTable[hashv] = chptr->hnext;
196 chptr->hnext = chptr;
201 if (tmp->hnext == chptr) {
202 tmp->hnext = tmp->hnext->hnext;
203 chptr->hnext = chptr;
212 /** Find a client by name, filtered by status mask.
213 * If a client is found, it is moved to the top of its hash bucket.
214 * @param[in] name Client name to search for.
215 * @param[in] TMask Bitmask of status bits, any of which are needed to match.
216 * @return Matching client, or NULL if none.
218 struct Client* hSeekClient(const char *name, int TMask)
220 HASHREGS hashv = strhash(name);
221 struct Client *cptr = clientTable[hashv];
224 if (0 == (cli_status(cptr) & TMask) || 0 != ircd_strcmp(name, cli_name(cptr))) {
226 while (prev = cptr, cptr = cli_hnext(cptr)) {
227 if ((cli_status(cptr) & TMask) && (0 == ircd_strcmp(name, cli_name(cptr)))) {
228 cli_hnext(prev) = cli_hnext(cptr);
229 cli_hnext(cptr) = clientTable[hashv];
230 clientTable[hashv] = cptr;
239 /** Find a channel by name.
240 * If a channel is found, it is moved to the top of its hash bucket.
241 * @param[in] name Channel name to search for.
242 * @return Matching channel, or NULL if none.
244 struct Channel* hSeekChannel(const char *name)
246 HASHREGS hashv = strhash(name);
247 struct Channel *chptr = channelTable[hashv];
250 if (0 != ircd_strcmp(name, chptr->chname)) {
251 struct Channel* prev;
252 while (prev = chptr, chptr = chptr->hnext) {
253 if (0 == ircd_strcmp(name, chptr->chname)) {
254 prev->hnext = chptr->hnext;
255 chptr->hnext = channelTable[hashv];
256 channelTable[hashv] = chptr;
266 /* I will add some useful(?) statistics here one of these days,
267 but not for DEBUGMODE: just to let the admins play with it,
268 coders are able to SIGCORE the server and look into what goes
271 /** Report hash table statistics to a client.
272 * @param[in] cptr Client that sent us this message.
273 * @param[in] sptr Client that originated the message.
274 * @param[in] parc Number of arguments.
275 * @param[in] parv Argument array.
278 int m_hash(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
287 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Hash Table Statistics", sptr);
289 for (i = 0; i < HASHSIZE; ++i) {
290 if ((cl = clientTable[i])) {
293 for ( ; cl; cl = cli_hnext(cl))
301 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Client: entries: %d buckets: %d "
302 "max chain: %d", sptr, count, buckets, max_chain);
308 for (i = 0; i < HASHSIZE; ++i) {
309 if ((ch = channelTable[i])) {
312 for ( ; ch; ch = ch->hnext)
320 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Channel: entries: %d buckets: %d "
321 "max chain: %d", sptr, count, buckets, max_chain);
325 /* Nick jupe utilities, these are in a static hash table with entry/bucket
326 ratio of one, collision shift up and roll in a circular fashion, the
327 lowest 12 bits of the hash value are used, deletion is not supported,
328 only addition, test for existence and cleanup of the table are.. */
330 /** Number of bits in jupe hash value. */
331 #define JUPEHASHBITS 12 /* 4096 entries, 64 nick jupes allowed */
332 /** Size of jupe hash table. */
333 #define JUPEHASHSIZE (1<<JUPEHASHBITS)
334 /** Bitmask to select into jupe hash table. */
335 #define JUPEHASHMASK (JUPEHASHSIZE-1)
336 /** Maximum number of jupes allowed. */
337 #define JUPEMAX (1<<(JUPEHASHBITS-6))
339 /** Hash table for jupes. */
340 static char jupeTable[JUPEHASHSIZE][NICKLEN + 1]; /* About 40k */
341 /** Count of jupes. */
342 static int jupesCount;
344 /** Check whether a nickname is juped.
345 * @param[in] nick Nickname to check.
346 * @return Non-zero of the nickname is juped, zero if not.
348 int isNickJuped(const char *nick)
353 for (pos = strhash(nick); (pos &= JUPEHASHMASK), jupeTable[pos][0]; pos++) {
354 if (0 == ircd_strcmp(nick, jupeTable[pos]))
358 return 0; /* A bogus pointer is NOT a juped nick, right ? :) */
361 /** Add a comma-separated list of nick jupes.
362 * @param[in] nicks List of nicks to jupe, separated by commas.
363 * @return Zero on success, non-zero on error.
365 int addNickJupes(const char *nicks)
367 static char temp[BUFSIZE + 1];
374 ircd_strncpy(temp, nicks, BUFSIZE);
375 temp[BUFSIZE] = '\0';
377 for (one = ircd_strtok(&p, temp, ","); one; one = ircd_strtok(&p, NULL, ","))
384 if (!jupeTable[pos][0])
386 if (jupesCount == JUPEMAX)
387 return 1; /* Error: Jupe table is full ! */
389 ircd_strncpy(jupeTable[pos], one, NICKLEN);
390 jupeTable[pos][NICKLEN] = '\000'; /* Better safe than sorry :) */
393 if (0 == ircd_strcmp(one, jupeTable[pos]))
402 /** Empty the table of juped nicknames. */
403 void clearNickJupes(void)
407 for (i = 0; i < JUPEHASHSIZE; i++)
408 jupeTable[i][0] = '\000';
411 /** Send more channels to a client in mid-LIST.
412 * @param[in] cptr Client to send the list to.
414 void list_next_channels(struct Client *cptr)
416 struct ListingArgs *args;
417 struct Channel *chptr;
419 /* Walk consecutive buckets until we hit the end. */
420 for (args = cli_listing(cptr); args->bucket < HASHSIZE; args->bucket++)
422 /* Send all the matching channels in the bucket. */
423 for (chptr = channelTable[args->bucket]; chptr; chptr = chptr->hnext)
425 if (chptr->users > args->min_users
426 && chptr->users < args->max_users
427 && chptr->creationtime > args->min_time
428 && chptr->creationtime < args->max_time
429 && (!(args->flags & LISTARG_TOPICLIMITS)
431 && chptr->topic_time > args->min_topic_time
432 && chptr->topic_time < args->max_topic_time))
433 && ((args->flags & LISTARG_SHOWSECRET)
434 || ShowChannel(cptr, chptr)))
436 send_reply(cptr, RPL_LIST, chptr->chname, chptr->users, chptr->topic);
439 /* If, at the end of the bucket, client sendq is more than half
441 if (MsgQLength(&cli_sendQ(cptr)) > cli_max_sendq(cptr) / 2)
445 /* If we did all buckets, clean the client and send RPL_LISTEND. */
446 if (args->bucket >= HASHSIZE)
448 MyFree(cli_listing(cptr));
449 cli_listing(cptr) = NULL;
450 send_reply(cptr, RPL_LISTEND);