]> jfr.im git - solanum.git/blob - ircd/hash.c
ircd: hash: use an irc_radixtree for client names
[solanum.git] / ircd / hash.c
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * hash.c: Maintains hashtables.
4 *
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
8 *
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.
13 *
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.
18 *
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
22 * USA
23 *
24 * $Id: hash.c 3177 2007-02-01 00:19:14Z jilles $
25 */
26
27 #include "stdinc.h"
28 #include "ircd_defs.h"
29 #include "s_conf.h"
30 #include "channel.h"
31 #include "client.h"
32 #include "common.h"
33 #include "hash.h"
34 #include "match.h"
35 #include "ircd.h"
36 #include "numeric.h"
37 #include "send.h"
38 #include "msg.h"
39 #include "cache.h"
40 #include "s_newconf.h"
41 #include "s_assert.h"
42 #include "irc_dictionary.h"
43 #include "irc_radixtree.h"
44
45 rb_dlink_list *channelTable;
46 rb_dlink_list *hostTable;
47
48 struct Dictionary *client_connid_tree = NULL;
49 struct Dictionary *client_zconnid_tree = NULL;
50 struct irc_radixtree *client_id_tree = NULL;
51 struct irc_radixtree *client_name_tree = NULL;
52
53 struct irc_radixtree *resv_tree = NULL;
54
55 /*
56 * look in whowas.c for the missing ...[WW_MAX]; entry
57 */
58
59 /*
60 * Hashing.
61 *
62 * The server uses a chained hash table to provide quick and efficient
63 * hash table maintenance (providing the hash function works evenly over
64 * the input range). The hash table is thus not susceptible to problems
65 * of filling all the buckets or the need to rehash.
66 * It is expected that the hash table would look something like this
67 * during use:
68 * +-----+ +-----+ +-----+ +-----+
69 * ---| 224 |----| 225 |----| 226 |---| 227 |---
70 * +-----+ +-----+ +-----+ +-----+
71 * | | |
72 * +-----+ +-----+ +-----+
73 * | A | | C | | D |
74 * +-----+ +-----+ +-----+
75 * |
76 * +-----+
77 * | B |
78 * +-----+
79 *
80 * A - GOPbot, B - chang, C - hanuaway, D - *.mu.OZ.AU
81 *
82 * The order shown above is just one instant of the server.
83 *
84 *
85 * The hash functions currently used are based Fowler/Noll/Vo hashes
86 * which work amazingly well and have a extremely low collision rate
87 * For more info see http://www.isthe.com/chongo/tech/comp/fnv/index.html
88 *
89 *
90 */
91
92 /* init_hash()
93 *
94 * clears the various hashtables
95 */
96 void
97 init_hash(void)
98 {
99 channelTable = rb_malloc(sizeof(rb_dlink_list) * CH_MAX);
100 hostTable = rb_malloc(sizeof(rb_dlink_list) * HOST_MAX);
101
102 client_connid_tree = irc_dictionary_create("client connid", irc_uint32cmp);
103 client_zconnid_tree = irc_dictionary_create("client zconnid", irc_uint32cmp);
104 client_id_tree = irc_radixtree_create("client id", NULL);
105 client_name_tree = irc_radixtree_create("client name", irc_radixtree_irccasecanon);
106
107 resv_tree = irc_radixtree_create("resv", irc_radixtree_irccasecanon);
108 }
109
110 u_int32_t
111 fnv_hash_upper(const unsigned char *s, int bits)
112 {
113 u_int32_t h = FNV1_32_INIT;
114
115 while (*s)
116 {
117 h ^= ToUpper(*s++);
118 h += (h<<1) + (h<<4) + (h<<7) + (h << 8) + (h << 24);
119 }
120 if (bits < 32)
121 h = ((h >> bits) ^ h) & ((1<<bits)-1);
122 return h;
123 }
124
125 u_int32_t
126 fnv_hash(const unsigned char *s, int bits)
127 {
128 u_int32_t h = FNV1_32_INIT;
129
130 while (*s)
131 {
132 h ^= *s++;
133 h += (h<<1) + (h<<4) + (h<<7) + (h << 8) + (h << 24);
134 }
135 if (bits < 32)
136 h = ((h >> bits) ^ h) & ((1<<bits)-1);
137 return h;
138 }
139
140 u_int32_t
141 fnv_hash_len(const unsigned char *s, int bits, int len)
142 {
143 u_int32_t h = FNV1_32_INIT;
144 const unsigned char *x = s + len;
145 while (*s && s < x)
146 {
147 h ^= *s++;
148 h += (h<<1) + (h<<4) + (h<<7) + (h << 8) + (h << 24);
149 }
150 if (bits < 32)
151 h = ((h >> bits) ^ h) & ((1<<bits)-1);
152 return h;
153 }
154
155 u_int32_t
156 fnv_hash_upper_len(const unsigned char *s, int bits, int len)
157 {
158 u_int32_t h = FNV1_32_INIT;
159 const unsigned char *x = s + len;
160 while (*s && s < x)
161 {
162 h ^= ToUpper(*s++);
163 h += (h<<1) + (h<<4) + (h<<7) + (h << 8) + (h << 24);
164 }
165 if (bits < 32)
166 h = ((h >> bits) ^ h) & ((1<<bits)-1);
167 return h;
168 }
169
170 /* hash_channel()
171 *
172 * hashes a channel name, based on first 30 chars only for efficiency
173 */
174 static u_int32_t
175 hash_channel(const char *name)
176 {
177 return fnv_hash_upper_len((const unsigned char *) name, CH_MAX_BITS, 30);
178 }
179
180 /* hash_hostname()
181 *
182 * hashes a hostname, based on first 30 chars only, as thats likely to
183 * be more dynamic than rest.
184 */
185 static u_int32_t
186 hash_hostname(const char *name)
187 {
188 return fnv_hash_upper_len((const unsigned char *) name, HOST_MAX_BITS, 30);
189 }
190
191 /* add_to_id_hash()
192 *
193 * adds an entry to the id hash table
194 */
195 void
196 add_to_id_hash(const char *name, struct Client *client_p)
197 {
198 if(EmptyString(name) || (client_p == NULL))
199 return;
200
201 irc_radixtree_add(client_id_tree, name, client_p);
202 }
203
204 /* add_to_client_hash()
205 *
206 * adds an entry (client/server) to the client hash table
207 */
208 void
209 add_to_client_hash(const char *name, struct Client *client_p)
210 {
211 s_assert(name != NULL);
212 s_assert(client_p != NULL);
213 if(EmptyString(name) || (client_p == NULL))
214 return;
215
216 irc_radixtree_add(client_name_tree, name, client_p);
217 }
218
219 /* add_to_hostname_hash()
220 *
221 * adds a client entry to the hostname hash table
222 */
223 void
224 add_to_hostname_hash(const char *hostname, struct Client *client_p)
225 {
226 unsigned int hashv;
227
228 s_assert(hostname != NULL);
229 s_assert(client_p != NULL);
230 if(EmptyString(hostname) || (client_p == NULL))
231 return;
232
233 hashv = hash_hostname(hostname);
234 rb_dlinkAddAlloc(client_p, &hostTable[hashv]);
235 }
236
237 /* add_to_resv_hash()
238 *
239 * adds a resv channel entry to the resv hash table
240 */
241 void
242 add_to_resv_hash(const char *name, struct ConfItem *aconf)
243 {
244 s_assert(!EmptyString(name));
245 s_assert(aconf != NULL);
246 if(EmptyString(name) || aconf == NULL)
247 return;
248
249 irc_radixtree_add(resv_tree, name, aconf);
250 }
251
252 /* del_from_id_hash()
253 *
254 * removes an id from the id hash table
255 */
256 void
257 del_from_id_hash(const char *id, struct Client *client_p)
258 {
259 s_assert(id != NULL);
260 s_assert(client_p != NULL);
261 if(EmptyString(id) || client_p == NULL)
262 return;
263
264 irc_radixtree_delete(client_id_tree, id);
265 }
266
267 /* del_from_client_hash()
268 *
269 * removes a client/server from the client hash table
270 */
271 void
272 del_from_client_hash(const char *name, struct Client *client_p)
273 {
274 /* no s_asserts, this can happen when removing a client that
275 * is unregistered.
276 */
277 if(EmptyString(name) || client_p == NULL)
278 return;
279
280 irc_radixtree_delete(client_name_tree, name);
281 }
282
283 /* del_from_channel_hash()
284 *
285 * removes a channel from the channel hash table
286 */
287 void
288 del_from_channel_hash(const char *name, struct Channel *chptr)
289 {
290 unsigned int hashv;
291
292 s_assert(name != NULL);
293 s_assert(chptr != NULL);
294
295 if(EmptyString(name) || chptr == NULL)
296 return;
297
298 hashv = hash_channel(name);
299 rb_dlinkFindDestroy(chptr, &channelTable[hashv]);
300 }
301
302 /* del_from_hostname_hash()
303 *
304 * removes a client entry from the hostname hash table
305 */
306 void
307 del_from_hostname_hash(const char *hostname, struct Client *client_p)
308 {
309 unsigned int hashv;
310
311 if(hostname == NULL || client_p == NULL)
312 return;
313
314 hashv = hash_hostname(hostname);
315
316 rb_dlinkFindDestroy(client_p, &hostTable[hashv]);
317 }
318
319 /* del_from_resv_hash()
320 *
321 * removes a resv entry from the resv hash table
322 */
323 void
324 del_from_resv_hash(const char *name, struct ConfItem *aconf)
325 {
326 s_assert(name != NULL);
327 s_assert(aconf != NULL);
328 if(EmptyString(name) || aconf == NULL)
329 return;
330
331 irc_radixtree_delete(resv_tree, name);
332 }
333
334 /* find_id()
335 *
336 * finds a client entry from the id hash table
337 */
338 struct Client *
339 find_id(const char *name)
340 {
341 if(EmptyString(name))
342 return NULL;
343
344 return irc_radixtree_retrieve(client_id_tree, name);
345 }
346
347 /* find_client()
348 *
349 * finds a client/server entry from the client hash table
350 */
351 struct Client *
352 find_client(const char *name)
353 {
354 s_assert(name != NULL);
355 if(EmptyString(name))
356 return NULL;
357
358 /* hunting for an id, not a nick */
359 if(IsDigit(*name))
360 return (find_id(name));
361
362 return irc_radixtree_retrieve(client_name_tree, name);
363 }
364
365 /* find_named_client()
366 *
367 * finds a client/server entry from the client hash table
368 */
369 struct Client *
370 find_named_client(const char *name)
371 {
372 s_assert(name != NULL);
373 if(EmptyString(name))
374 return NULL;
375
376 return irc_radixtree_retrieve(client_name_tree, name);
377 }
378
379 /* find_server()
380 *
381 * finds a server from the client hash table
382 */
383 struct Client *
384 find_server(struct Client *source_p, const char *name)
385 {
386 struct Client *target_p;
387
388 if(EmptyString(name))
389 return NULL;
390
391 if((source_p == NULL || !MyClient(source_p)) &&
392 IsDigit(*name) && strlen(name) == 3)
393 {
394 target_p = find_id(name);
395 return(target_p);
396 }
397
398 target_p = irc_radixtree_retrieve(client_name_tree, name);
399 if (target_p != NULL)
400 {
401 if(IsServer(target_p) || IsMe(target_p))
402 return target_p;
403 }
404
405 return NULL;
406 }
407
408 /* find_hostname()
409 *
410 * finds a hostname rb_dlink list from the hostname hash table.
411 * we return the full rb_dlink list, because you can have multiple
412 * entries with the same hostname
413 */
414 rb_dlink_node *
415 find_hostname(const char *hostname)
416 {
417 unsigned int hashv;
418
419 if(EmptyString(hostname))
420 return NULL;
421
422 hashv = hash_hostname(hostname);
423
424 return hostTable[hashv].head;
425 }
426
427 /* find_channel()
428 *
429 * finds a channel from the channel hash table
430 */
431 struct Channel *
432 find_channel(const char *name)
433 {
434 struct Channel *chptr;
435 rb_dlink_node *ptr;
436 unsigned int hashv;
437
438 s_assert(name != NULL);
439 if(EmptyString(name))
440 return NULL;
441
442 hashv = hash_channel(name);
443
444 RB_DLINK_FOREACH(ptr, channelTable[hashv].head)
445 {
446 chptr = ptr->data;
447
448 if(irccmp(name, chptr->chname) == 0)
449 return chptr;
450 }
451
452 return NULL;
453 }
454
455 /*
456 * get_or_create_channel
457 * inputs - client pointer
458 * - channel name
459 * - pointer to int flag whether channel was newly created or not
460 * output - returns channel block or NULL if illegal name
461 * - also modifies *isnew
462 *
463 * Get Channel block for chname (and allocate a new channel
464 * block, if it didn't exist before).
465 */
466 struct Channel *
467 get_or_create_channel(struct Client *client_p, const char *chname, int *isnew)
468 {
469 struct Channel *chptr;
470 rb_dlink_node *ptr;
471 unsigned int hashv;
472 int len;
473 const char *s = chname;
474
475 if(EmptyString(s))
476 return NULL;
477
478 len = strlen(s);
479 if(len > CHANNELLEN)
480 {
481 char *t;
482 if(IsServer(client_p))
483 {
484 sendto_realops_snomask(SNO_DEBUG, L_ALL,
485 "*** Long channel name from %s (%d > %d): %s",
486 client_p->name, len, CHANNELLEN, s);
487 }
488 len = CHANNELLEN;
489 t = LOCAL_COPY(s);
490 *(t + CHANNELLEN) = '\0';
491 s = t;
492 }
493
494 hashv = hash_channel(s);
495
496 RB_DLINK_FOREACH(ptr, channelTable[hashv].head)
497 {
498 chptr = ptr->data;
499
500 if(irccmp(s, chptr->chname) == 0)
501 {
502 if(isnew != NULL)
503 *isnew = 0;
504 return chptr;
505 }
506 }
507
508 if(isnew != NULL)
509 *isnew = 1;
510
511 chptr = allocate_channel(s);
512
513 rb_dlinkAdd(chptr, &chptr->node, &global_channel_list);
514
515 chptr->channelts = rb_current_time(); /* doesn't hurt to set it here */
516
517 rb_dlinkAddAlloc(chptr, &channelTable[hashv]);
518
519 return chptr;
520 }
521
522 /* hash_find_resv()
523 *
524 * hunts for a resv entry in the resv hash table
525 */
526 struct ConfItem *
527 hash_find_resv(const char *name)
528 {
529 struct ConfItem *aconf;
530
531 s_assert(name != NULL);
532 if(EmptyString(name))
533 return NULL;
534
535 aconf = irc_radixtree_retrieve(resv_tree, name);
536 if (aconf != NULL)
537 {
538 aconf->port++;
539 return aconf;
540 }
541
542 return NULL;
543 }
544
545 void
546 clear_resv_hash(void)
547 {
548 struct ConfItem *aconf;
549 struct irc_radixtree_iteration_state iter;
550
551 IRC_RADIXTREE_FOREACH(aconf, &iter, resv_tree)
552 {
553 /* skip temp resvs */
554 if(aconf->hold)
555 continue;
556
557 irc_radixtree_delete(resv_tree, aconf->host);
558 free_conf(aconf);
559 }
560 }
561
562 void
563 add_to_zconnid_hash(struct Client *client_p)
564 {
565 irc_dictionary_add(client_zconnid_tree, IRC_UINT_TO_POINTER(client_p->localClient->zconnid), client_p);
566 }
567
568 void
569 del_from_zconnid_hash(struct Client *client_p)
570 {
571 irc_dictionary_delete(client_zconnid_tree, IRC_UINT_TO_POINTER(client_p->localClient->zconnid));
572 }
573
574 void
575 add_to_cli_connid_hash(struct Client *client_p)
576 {
577 irc_dictionary_add(client_connid_tree, IRC_UINT_TO_POINTER(client_p->localClient->connid), client_p);
578 }
579
580 void
581 del_from_cli_connid_hash(struct Client *client_p)
582 {
583 irc_dictionary_delete(client_connid_tree, IRC_UINT_TO_POINTER(client_p->localClient->connid));
584 }
585
586 struct Client *
587 find_cli_connid_hash(uint32_t connid)
588 {
589 struct Client *target_p;
590
591 target_p = irc_dictionary_retrieve(client_connid_tree, IRC_UINT_TO_POINTER(connid));
592 if (target_p != NULL)
593 return target_p;
594
595 target_p = irc_dictionary_retrieve(client_zconnid_tree, IRC_UINT_TO_POINTER(connid));
596 if (target_p != NULL)
597 return target_p;
598
599 return NULL;
600 }
601
602 static void
603 output_hash(struct Client *source_p, const char *name, int length, int *counts, unsigned long deepest)
604 {
605 unsigned long total = 0;
606 int i;
607 char buf[128];
608
609 sendto_one_numeric(source_p, RPL_STATSDEBUG,
610 "B :%s Hash Statistics", name);
611
612 snprintf(buf, sizeof buf, "%.3f%%",
613 (float) ((counts[0]*100) / (float) length));
614 sendto_one_numeric(source_p, RPL_STATSDEBUG,
615 "B :Size: %d Empty: %d (%s)",
616 length, counts[0], buf);
617
618 for(i = 1; i < 11; i++)
619 {
620 total += (counts[i] * i);
621 }
622
623 /* dont want to divide by 0! --fl */
624 if(counts[0] != length)
625 {
626 snprintf(buf, sizeof buf, "%.3f/%.3f",
627 (float) (total / (length - counts[0])),
628 (float) (total / length));
629 sendto_one_numeric(source_p, RPL_STATSDEBUG,
630 "B :Average depth: %s Highest depth: %lu",
631 buf, deepest);
632 }
633
634 for(i = 0; i < 11; i++)
635 {
636 sendto_one_numeric(source_p, RPL_STATSDEBUG,
637 "B :Nodes with %d entries: %d",
638 i, counts[i]);
639 }
640 }
641
642
643 static void
644 count_hash(struct Client *source_p, rb_dlink_list *table, int length, const char *name)
645 {
646 int counts[11];
647 unsigned long deepest = 0;
648 int i;
649
650 memset(counts, 0, sizeof(counts));
651
652 for(i = 0; i < length; i++)
653 {
654 if(rb_dlink_list_length(&table[i]) >= 10)
655 counts[10]++;
656 else
657 counts[rb_dlink_list_length(&table[i])]++;
658
659 if(rb_dlink_list_length(&table[i]) > deepest)
660 deepest = rb_dlink_list_length(&table[i]);
661 }
662
663 output_hash(source_p, name, length, counts, deepest);
664 }
665
666 void
667 hash_stats(struct Client *source_p)
668 {
669 count_hash(source_p, channelTable, CH_MAX, "Channel");
670 sendto_one_numeric(source_p, RPL_STATSDEBUG, "B :--");
671 count_hash(source_p, hostTable, HOST_MAX, "Hostname");
672 }