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"
29 #include "ircd_reply.h"
30 #include "ircd_string.h"
46 * @brief Hash table management.
49 * This file used to use some very complicated hash function. Now it
50 * uses CRC-32, but effectively remaps each input byte according to a
51 * table initialized at startup.
54 /** Hash table for clients. */
55 static struct Client *clientTable[HASHSIZE];
56 /** Hash table for channels. */
57 static struct Channel *channelTable[HASHSIZE];
58 /** CRC-32 update table. */
59 static uint32_t crc32hash[256];
61 /** Initialize the map used by the hash function. */
64 unsigned int ii, jj, rand, poly;
66 /* First calculate a normal CRC-32 table. */
67 for (ii = 0, poly = 0xedb88320; ii < 256; ii++)
70 for (jj = 0; jj < 8; jj++)
71 rand = (rand & 1) ? poly ^ (rand >> 1) : rand >> 1;
75 /* Now reorder the hash table. */
76 for (ii = 0, rand = 0; ii < 256; ii++)
80 poly = ii + rand % (256 - ii);
82 crc32hash[ii] = crc32hash[poly];
88 /** Output type of hash function. */
89 typedef unsigned int HASHREGS;
91 /** Calculate hash value for a string.
92 * @param[in] n String to hash.
93 * @return Hash value for string.
95 static HASHREGS strhash(const char *n)
97 HASHREGS hash = crc32hash[ToLower(*n++) & 255];
99 hash = (hash >> 8) ^ crc32hash[(hash ^ ToLower(*n++)) & 255];
100 return hash % HASHSIZE;
103 /************************** Externally visible functions ********************/
105 /* Optimization note: in these functions I supposed that the CSE optimization
106 * (Common Subexpression Elimination) does its work decently, this means that
107 * I avoided introducing new variables to do the work myself and I did let
108 * the optimizer play with more free registers, actual tests proved this
109 * solution to be faster than doing things like tmp2=tmp->hnext... and then
110 * use tmp2 myself wich would have given less freedom to the optimizer.
113 /** Prepend a client to the appropriate hash bucket.
114 * @param[in] cptr Client to add to hash table.
117 int hAddClient(struct Client *cptr)
119 HASHREGS hashv = strhash(cli_name(cptr));
121 cli_hnext(cptr) = clientTable[hashv];
122 clientTable[hashv] = cptr;
127 /** Prepend a channel to the appropriate hash bucket.
128 * @param[in] chptr Channel to add to hash table.
131 int hAddChannel(struct Channel *chptr)
133 HASHREGS hashv = strhash(chptr->chname);
135 chptr->hnext = channelTable[hashv];
136 channelTable[hashv] = chptr;
141 /** Remove a client from its hash bucket.
142 * @param[in] cptr Client to remove from hash table.
143 * @return Zero if the client is found and removed, -1 if not found.
145 int hRemClient(struct Client *cptr)
147 HASHREGS hashv = strhash(cli_name(cptr));
148 struct Client *tmp = clientTable[hashv];
151 clientTable[hashv] = cli_hnext(cptr);
152 cli_hnext(cptr) = cptr;
157 if (cli_hnext(tmp) == cptr) {
158 cli_hnext(tmp) = cli_hnext(cli_hnext(tmp));
159 cli_hnext(cptr) = cptr;
162 tmp = cli_hnext(tmp);
167 /** Rename a client in the hash table.
168 * @param[in] cptr Client whose nickname is changing.
169 * @param[in] newname New nickname for client.
172 int hChangeClient(struct Client *cptr, const char *newname)
174 HASHREGS newhash = strhash(newname);
179 cli_hnext(cptr) = clientTable[newhash];
180 clientTable[newhash] = cptr;
184 /** Remove a channel from its hash bucket.
185 * @param[in] chptr Channel to remove from hash table.
186 * @return Zero if the channel is found and removed, -1 if not found.
188 int hRemChannel(struct Channel *chptr)
190 HASHREGS hashv = strhash(chptr->chname);
191 struct Channel *tmp = channelTable[hashv];
194 channelTable[hashv] = chptr->hnext;
195 chptr->hnext = chptr;
200 if (tmp->hnext == chptr) {
201 tmp->hnext = tmp->hnext->hnext;
202 chptr->hnext = chptr;
211 /** Find a client by name, filtered by status mask.
212 * If a client is found, it is moved to the top of its hash bucket.
213 * @param[in] name Client name to search for.
214 * @param[in] TMask Bitmask of status bits, any of which are needed to match.
215 * @return Matching client, or NULL if none.
217 struct Client* hSeekClient(const char *name, int TMask)
219 HASHREGS hashv = strhash(name);
220 struct Client *cptr = clientTable[hashv];
223 if (0 == (cli_status(cptr) & TMask) || 0 != ircd_strcmp(name, cli_name(cptr))) {
225 while (prev = cptr, cptr = cli_hnext(cptr)) {
226 if ((cli_status(cptr) & TMask) && (0 == ircd_strcmp(name, cli_name(cptr)))) {
227 cli_hnext(prev) = cli_hnext(cptr);
228 cli_hnext(cptr) = clientTable[hashv];
229 clientTable[hashv] = cptr;
238 /** Find a channel by name.
239 * If a channel is found, it is moved to the top of its hash bucket.
240 * @param[in] name Channel name to search for.
241 * @return Matching channel, or NULL if none.
243 struct Channel* hSeekChannel(const char *name)
245 HASHREGS hashv = strhash(name);
246 struct Channel *chptr = channelTable[hashv];
249 if (0 != ircd_strcmp(name, chptr->chname)) {
250 struct Channel* prev;
251 while (prev = chptr, chptr = chptr->hnext) {
252 if (0 == ircd_strcmp(name, chptr->chname)) {
253 prev->hnext = chptr->hnext;
254 chptr->hnext = channelTable[hashv];
255 channelTable[hashv] = chptr;
265 /* I will add some useful(?) statistics here one of these days,
266 but not for DEBUGMODE: just to let the admins play with it,
267 coders are able to SIGCORE the server and look into what goes
270 /** Report hash table statistics to a client.
271 * @param[in] cptr Client that sent us this message.
272 * @param[in] sptr Client that originated the message.
273 * @param[in] parc Number of arguments.
274 * @param[in] parv Argument array.
277 int m_hash(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
286 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Hash Table Statistics", sptr);
288 for (i = 0; i < HASHSIZE; ++i) {
289 if ((cl = clientTable[i])) {
292 for ( ; cl; cl = cli_hnext(cl))
300 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Client: entries: %d buckets: %d "
301 "max chain: %d", sptr, count, buckets, max_chain);
307 for (i = 0; i < HASHSIZE; ++i) {
308 if ((ch = channelTable[i])) {
311 for ( ; ch; ch = ch->hnext)
319 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :Channel: entries: %d buckets: %d "
320 "max chain: %d", sptr, count, buckets, max_chain);
324 /* Nick jupe utilities, these are in a static hash table with entry/bucket
325 ratio of one, collision shift up and roll in a circular fashion, the
326 lowest 12 bits of the hash value are used, deletion is not supported,
327 only addition, test for existence and cleanup of the table are.. */
329 /** Number of bits in jupe hash value. */
330 #define JUPEHASHBITS 12 /* 4096 entries, 64 nick jupes allowed */
331 /** Size of jupe hash table. */
332 #define JUPEHASHSIZE (1<<JUPEHASHBITS)
333 /** Bitmask to select into jupe hash table. */
334 #define JUPEHASHMASK (JUPEHASHSIZE-1)
335 /** Maximum number of jupes allowed. */
336 #define JUPEMAX (1<<(JUPEHASHBITS-6))
338 /** Hash table for jupes. */
339 static char jupeTable[JUPEHASHSIZE][NICKLEN + 1]; /* About 40k */
340 /** Count of jupes. */
341 static int jupesCount;
343 /** Check whether a nickname is juped.
344 * @param[in] nick Nickname to check.
345 * @return Non-zero of the nickname is juped, zero if not.
347 int isNickJuped(const char *nick)
352 for (pos = strhash(nick); (pos &= JUPEHASHMASK), jupeTable[pos][0]; pos++) {
353 if (0 == ircd_strcmp(nick, jupeTable[pos]))
357 return 0; /* A bogus pointer is NOT a juped nick, right ? :) */
360 /** Add a comma-separated list of nick jupes.
361 * @param[in] nicks List of nicks to jupe, separated by commas.
362 * @return Zero on success, non-zero on error.
364 int addNickJupes(const char *nicks)
366 static char temp[BUFSIZE + 1];
373 ircd_strncpy(temp, nicks, BUFSIZE);
374 temp[BUFSIZE] = '\0';
376 for (one = ircd_strtok(&p, temp, ","); one; one = ircd_strtok(&p, NULL, ","))
383 if (!jupeTable[pos][0])
385 if (jupesCount == JUPEMAX)
386 return 1; /* Error: Jupe table is full ! */
388 ircd_strncpy(jupeTable[pos], one, NICKLEN);
389 jupeTable[pos][NICKLEN] = '\000'; /* Better safe than sorry :) */
392 if (0 == ircd_strcmp(one, jupeTable[pos]))
401 /** Empty the table of juped nicknames. */
402 void clearNickJupes(void)
406 for (i = 0; i < JUPEHASHSIZE; i++)
407 jupeTable[i][0] = '\000';
410 /** Send more channels to a client in mid-LIST.
411 * @param[in] cptr Client to send the list to.
413 void list_next_channels(struct Client *cptr)
415 struct ListingArgs *args;
416 struct Channel *chptr;
418 /* Walk consecutive buckets until we hit the end. */
419 for (args = cli_listing(cptr); args->bucket < HASHSIZE; args->bucket++)
421 /* Send all the matching channels in the bucket. */
422 for (chptr = channelTable[args->bucket]; chptr; chptr = chptr->hnext)
424 if (chptr->users > args->min_users
425 && chptr->users < args->max_users
426 && chptr->creationtime > args->min_time
427 && chptr->creationtime < args->max_time
428 && (!(args->flags & LISTARG_TOPICLIMITS)
430 && chptr->topic_time > args->min_topic_time
431 && chptr->topic_time < args->max_topic_time))
432 && ((args->flags & LISTARG_SHOWSECRET)
433 || ShowChannel(cptr, chptr)))
435 send_reply(cptr, RPL_LIST, chptr->chname, chptr->users, chptr->topic);
438 /* If, at the end of the bucket, client sendq is more than half
440 if (MsgQLength(&cli_sendQ(cptr)) > cli_max_sendq(cptr) / 2)
444 /* If we did all buckets, clean the client and send RPL_LISTEND. */
445 if (args->bucket >= HASHSIZE)
447 MyFree(cli_listing(cptr));
448 cli_listing(cptr) = NULL;
449 send_reply(cptr, RPL_LISTEND);