]> jfr.im git - irc/rqf/shadowircd.git/blob - src/hash.c
Add topic TS and channel TS constraints for /LIST.
[irc/rqf/shadowircd.git] / src / 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 */
25
26 #include "stdinc.h"
27 #include "ircd_defs.h"
28 #include "s_conf.h"
29 #include "channel.h"
30 #include "client.h"
31 #include "common.h"
32 #include "hash.h"
33 #include "match.h"
34 #include "ircd.h"
35 #include "numeric.h"
36 #include "send.h"
37 #include "msg.h"
38 #include "cache.h"
39 #include "s_newconf.h"
40
41 #define hash_cli_fd(x) (x % CLI_FD_MAX)
42
43 static rb_dlink_list clientbyfdTable[U_MAX];
44
45 rb_dlink_list *clientTable;
46 rb_dlink_list *channelTable;
47 rb_dlink_list *idTable;
48 rb_dlink_list *resvTable;
49 rb_dlink_list *hostTable;
50
51 /*
52 * look in whowas.c for the missing ...[WW_MAX]; entry
53 */
54
55 /*
56 * Hashing.
57 *
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
63 * during use:
64 * +-----+ +-----+ +-----+ +-----+
65 * ---| 224 |----| 225 |----| 226 |---| 227 |---
66 * +-----+ +-----+ +-----+ +-----+
67 * | | |
68 * +-----+ +-----+ +-----+
69 * | A | | C | | D |
70 * +-----+ +-----+ +-----+
71 * |
72 * +-----+
73 * | B |
74 * +-----+
75 *
76 * A - GOPbot, B - chang, C - hanuaway, D - *.mu.OZ.AU
77 *
78 * The order shown above is just one instant of the server.
79 *
80 *
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
84 *
85 *
86 */
87
88 /* init_hash()
89 *
90 * clears the various hashtables
91 */
92 void
93 init_hash(void)
94 {
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);
100 }
101
102 #ifndef RICER_HASHING
103 u_int32_t
104 fnv_hash_upper(const unsigned char *s, int bits)
105 {
106 u_int32_t h = FNV1_32_INIT;
107
108 while (*s)
109 {
110 h ^= ToUpper(*s++);
111 h += (h<<1) + (h<<4) + (h<<7) + (h << 8) + (h << 24);
112 }
113 if (bits < 32)
114 h = ((h >> bits) ^ h) & ((1<<bits)-1);
115 return h;
116 }
117
118 u_int32_t
119 fnv_hash(const unsigned char *s, int bits)
120 {
121 u_int32_t h = FNV1_32_INIT;
122
123 while (*s)
124 {
125 h ^= *s++;
126 h += (h<<1) + (h<<4) + (h<<7) + (h << 8) + (h << 24);
127 }
128 if (bits < 32)
129 h = ((h >> bits) ^ h) & ((1<<bits)-1);
130 return h;
131 }
132
133 u_int32_t
134 fnv_hash_len(const unsigned char *s, int bits, int len)
135 {
136 u_int32_t h = FNV1_32_INIT;
137 const unsigned char *x = s + len;
138 while (*s && s < x)
139 {
140 h ^= *s++;
141 h += (h<<1) + (h<<4) + (h<<7) + (h << 8) + (h << 24);
142 }
143 if (bits < 32)
144 h = ((h >> bits) ^ h) & ((1<<bits)-1);
145 return h;
146 }
147
148 u_int32_t
149 fnv_hash_upper_len(const unsigned char *s, int bits, int len)
150 {
151 u_int32_t h = FNV1_32_INIT;
152 const unsigned char *x = s + len;
153 while (*s && s < x)
154 {
155 h ^= ToUpper(*s++);
156 h += (h<<1) + (h<<4) + (h<<7) + (h << 8) + (h << 24);
157 }
158 if (bits < 32)
159 h = ((h >> bits) ^ h) & ((1<<bits)-1);
160 return h;
161 }
162 #endif
163
164 /* hash_nick()
165 *
166 * hashes a nickname, first converting to lowercase
167 */
168 static u_int32_t
169 hash_nick(const char *name)
170 {
171 return fnv_hash_upper((const unsigned char *) name, U_MAX_BITS);
172 }
173
174 /* hash_id()
175 *
176 * hashes an id, case is kept
177 */
178 static u_int32_t
179 hash_id(const char *name)
180 {
181 return fnv_hash((const unsigned char *) name, U_MAX_BITS);
182 }
183
184 /* hash_channel()
185 *
186 * hashes a channel name, based on first 30 chars only for efficiency
187 */
188 static u_int32_t
189 hash_channel(const char *name)
190 {
191 return fnv_hash_upper_len((const unsigned char *) name, CH_MAX_BITS, 30);
192 }
193
194 /* hash_hostname()
195 *
196 * hashes a hostname, based on first 30 chars only, as thats likely to
197 * be more dynamic than rest.
198 */
199 static u_int32_t
200 hash_hostname(const char *name)
201 {
202 return fnv_hash_upper_len((const unsigned char *) name, HOST_MAX_BITS, 30);
203 }
204
205 /* hash_resv()
206 *
207 * hashes a resv channel name, based on first 30 chars only
208 */
209 static u_int32_t
210 hash_resv(const char *name)
211 {
212 return fnv_hash_upper_len((const unsigned char *) name, R_MAX_BITS, 30);
213 }
214
215 /* add_to_id_hash()
216 *
217 * adds an entry to the id hash table
218 */
219 void
220 add_to_id_hash(const char *name, struct Client *client_p)
221 {
222 unsigned int hashv;
223
224 if(EmptyString(name) || (client_p == NULL))
225 return;
226
227 hashv = hash_id(name);
228 rb_dlinkAddAlloc(client_p, &idTable[hashv]);
229 }
230
231 /* add_to_client_hash()
232 *
233 * adds an entry (client/server) to the client hash table
234 */
235 void
236 add_to_client_hash(const char *name, struct Client *client_p)
237 {
238 unsigned int hashv;
239
240 s_assert(name != NULL);
241 s_assert(client_p != NULL);
242 if(EmptyString(name) || (client_p == NULL))
243 return;
244
245 hashv = hash_nick(name);
246 rb_dlinkAddAlloc(client_p, &clientTable[hashv]);
247 }
248
249 /* add_to_hostname_hash()
250 *
251 * adds a client entry to the hostname hash table
252 */
253 void
254 add_to_hostname_hash(const char *hostname, struct Client *client_p)
255 {
256 unsigned int hashv;
257
258 s_assert(hostname != NULL);
259 s_assert(client_p != NULL);
260 if(EmptyString(hostname) || (client_p == NULL))
261 return;
262
263 hashv = hash_hostname(hostname);
264 rb_dlinkAddAlloc(client_p, &hostTable[hashv]);
265 }
266
267 /* add_to_resv_hash()
268 *
269 * adds a resv channel entry to the resv hash table
270 */
271 void
272 add_to_resv_hash(const char *name, struct ConfItem *aconf)
273 {
274 unsigned int hashv;
275
276 s_assert(!EmptyString(name));
277 s_assert(aconf != NULL);
278 if(EmptyString(name) || aconf == NULL)
279 return;
280
281 hashv = hash_resv(name);
282 rb_dlinkAddAlloc(aconf, &resvTable[hashv]);
283 }
284
285 /* del_from_id_hash()
286 *
287 * removes an id from the id hash table
288 */
289 void
290 del_from_id_hash(const char *id, struct Client *client_p)
291 {
292 unsigned int hashv;
293
294 s_assert(id != NULL);
295 s_assert(client_p != NULL);
296 if(EmptyString(id) || client_p == NULL)
297 return;
298
299 hashv = hash_id(id);
300 rb_dlinkFindDestroy(client_p, &idTable[hashv]);
301 }
302
303 /* del_from_client_hash()
304 *
305 * removes a client/server from the client hash table
306 */
307 void
308 del_from_client_hash(const char *name, struct Client *client_p)
309 {
310 unsigned int hashv;
311
312 /* no s_asserts, this can happen when removing a client that
313 * is unregistered.
314 */
315 if(EmptyString(name) || client_p == NULL)
316 return;
317
318 hashv = hash_nick(name);
319 rb_dlinkFindDestroy(client_p, &clientTable[hashv]);
320 }
321
322 /* del_from_channel_hash()
323 *
324 * removes a channel from the channel hash table
325 */
326 void
327 del_from_channel_hash(const char *name, struct Channel *chptr)
328 {
329 unsigned int hashv;
330
331 s_assert(name != NULL);
332 s_assert(chptr != NULL);
333
334 if(EmptyString(name) || chptr == NULL)
335 return;
336
337 hashv = hash_channel(name);
338 rb_dlinkFindDestroy(chptr, &channelTable[hashv]);
339 }
340
341 /* del_from_hostname_hash()
342 *
343 * removes a client entry from the hostname hash table
344 */
345 void
346 del_from_hostname_hash(const char *hostname, struct Client *client_p)
347 {
348 unsigned int hashv;
349
350 if(hostname == NULL || client_p == NULL)
351 return;
352
353 hashv = hash_hostname(hostname);
354
355 rb_dlinkFindDestroy(client_p, &hostTable[hashv]);
356 }
357
358 /* del_from_resv_hash()
359 *
360 * removes a resv entry from the resv hash table
361 */
362 void
363 del_from_resv_hash(const char *name, struct ConfItem *aconf)
364 {
365 unsigned int hashv;
366
367 s_assert(name != NULL);
368 s_assert(aconf != NULL);
369 if(EmptyString(name) || aconf == NULL)
370 return;
371
372 hashv = hash_resv(name);
373
374 rb_dlinkFindDestroy(aconf, &resvTable[hashv]);
375 }
376
377 /* find_id()
378 *
379 * finds a client entry from the id hash table
380 */
381 struct Client *
382 find_id(const char *name)
383 {
384 struct Client *target_p;
385 rb_dlink_node *ptr;
386 unsigned int hashv;
387
388 if(EmptyString(name))
389 return NULL;
390
391 hashv = hash_id(name);
392
393 RB_DLINK_FOREACH(ptr, idTable[hashv].head)
394 {
395 target_p = ptr->data;
396
397 if(strcmp(name, target_p->id) == 0)
398 return target_p;
399 }
400
401 return NULL;
402 }
403
404 /* find_client()
405 *
406 * finds a client/server entry from the client hash table
407 */
408 struct Client *
409 find_client(const char *name)
410 {
411 struct Client *target_p;
412 rb_dlink_node *ptr;
413 unsigned int hashv;
414
415 s_assert(name != NULL);
416 if(EmptyString(name))
417 return NULL;
418
419 /* hunting for an id, not a nick */
420 if(IsDigit(*name))
421 return (find_id(name));
422
423 hashv = hash_nick(name);
424
425 RB_DLINK_FOREACH(ptr, clientTable[hashv].head)
426 {
427 target_p = ptr->data;
428
429 if(irccmp(name, target_p->name) == 0)
430 return target_p;
431 }
432
433 return NULL;
434 }
435
436 /* find_named_client()
437 *
438 * finds a client/server entry from the client hash table
439 */
440 struct Client *
441 find_named_client(const char *name)
442 {
443 struct Client *target_p;
444 rb_dlink_node *ptr;
445 unsigned int hashv;
446
447 s_assert(name != NULL);
448 if(EmptyString(name))
449 return NULL;
450
451 hashv = hash_nick(name);
452
453 RB_DLINK_FOREACH(ptr, clientTable[hashv].head)
454 {
455 target_p = ptr->data;
456
457 if(irccmp(name, target_p->name) == 0)
458 return target_p;
459 }
460
461 return NULL;
462 }
463
464 /* find_server()
465 *
466 * finds a server from the client hash table
467 */
468 struct Client *
469 find_server(struct Client *source_p, const char *name)
470 {
471 struct Client *target_p;
472 rb_dlink_node *ptr;
473 unsigned int hashv;
474
475 if(EmptyString(name))
476 return NULL;
477
478 if((source_p == NULL || !MyClient(source_p)) &&
479 IsDigit(*name) && strlen(name) == 3)
480 {
481 target_p = find_id(name);
482 return(target_p);
483 }
484
485 hashv = hash_nick(name);
486
487 RB_DLINK_FOREACH(ptr, clientTable[hashv].head)
488 {
489 target_p = ptr->data;
490
491 if((IsServer(target_p) || IsMe(target_p)) &&
492 irccmp(name, target_p->name) == 0)
493 return target_p;
494 }
495
496 return NULL;
497 }
498
499 /* find_hostname()
500 *
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
504 */
505 rb_dlink_node *
506 find_hostname(const char *hostname)
507 {
508 unsigned int hashv;
509
510 if(EmptyString(hostname))
511 return NULL;
512
513 hashv = hash_hostname(hostname);
514
515 return hostTable[hashv].head;
516 }
517
518 /* find_channel()
519 *
520 * finds a channel from the channel hash table
521 */
522 struct Channel *
523 find_channel(const char *name)
524 {
525 struct Channel *chptr;
526 rb_dlink_node *ptr;
527 unsigned int hashv;
528
529 s_assert(name != NULL);
530 if(EmptyString(name))
531 return NULL;
532
533 hashv = hash_channel(name);
534
535 RB_DLINK_FOREACH(ptr, channelTable[hashv].head)
536 {
537 chptr = ptr->data;
538
539 if(irccmp(name, chptr->chname) == 0)
540 return chptr;
541 }
542
543 return NULL;
544 }
545
546 /*
547 * get_or_create_channel
548 * inputs - client pointer
549 * - channel name
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
553 *
554 * Get Channel block for chname (and allocate a new channel
555 * block, if it didn't exist before).
556 */
557 struct Channel *
558 get_or_create_channel(struct Client *client_p, const char *chname, int *isnew)
559 {
560 struct Channel *chptr;
561 rb_dlink_node *ptr;
562 unsigned int hashv;
563 int len;
564 const char *s = chname;
565
566 if(EmptyString(s))
567 return NULL;
568
569 len = strlen(s);
570 if(len > CHANNELLEN)
571 {
572 char *t;
573 if(IsServer(client_p))
574 {
575 sendto_realops_snomask(SNO_DEBUG, L_ALL,
576 "*** Long channel name from %s (%d > %d): %s",
577 client_p->name, len, CHANNELLEN, s);
578 }
579 len = CHANNELLEN;
580 t = LOCAL_COPY(s);
581 *(t + CHANNELLEN) = '\0';
582 s = t;
583 }
584
585 hashv = hash_channel(s);
586
587 RB_DLINK_FOREACH(ptr, channelTable[hashv].head)
588 {
589 chptr = ptr->data;
590
591 if(irccmp(s, chptr->chname) == 0)
592 {
593 if(isnew != NULL)
594 *isnew = 0;
595 return chptr;
596 }
597 }
598
599 if(isnew != NULL)
600 *isnew = 1;
601
602 chptr = allocate_channel(s);
603
604 rb_dlinkAdd(chptr, &chptr->node, &global_channel_list);
605
606 chptr->channelts = rb_current_time(); /* doesn't hurt to set it here */
607
608 rb_dlinkAddAlloc(chptr, &channelTable[hashv]);
609
610 return chptr;
611 }
612
613 /* hash_find_resv()
614 *
615 * hunts for a resv entry in the resv hash table
616 */
617 struct ConfItem *
618 hash_find_resv(const char *name)
619 {
620 struct ConfItem *aconf;
621 rb_dlink_node *ptr;
622 unsigned int hashv;
623
624 s_assert(name != NULL);
625 if(EmptyString(name))
626 return NULL;
627
628 hashv = hash_resv(name);
629
630 RB_DLINK_FOREACH(ptr, resvTable[hashv].head)
631 {
632 aconf = ptr->data;
633
634 if(!irccmp(name, aconf->host))
635 {
636 aconf->port++;
637 return aconf;
638 }
639 }
640
641 return NULL;
642 }
643
644 void
645 clear_resv_hash(void)
646 {
647 struct ConfItem *aconf;
648 rb_dlink_node *ptr;
649 rb_dlink_node *next_ptr;
650 int i;
651
652 HASH_WALK_SAFE(i, R_MAX, ptr, next_ptr, resvTable)
653 {
654 aconf = ptr->data;
655
656 /* skip temp resvs */
657 if(aconf->hold)
658 continue;
659
660 free_conf(ptr->data);
661 rb_dlinkDestroy(ptr, &resvTable[i]);
662 }
663 HASH_WALK_END
664 }
665
666 void
667 add_to_cli_fd_hash(struct Client *client_p)
668 {
669 rb_dlinkAddAlloc(client_p, &clientbyfdTable[hash_cli_fd(rb_get_fd(client_p->localClient->F))]);
670 }
671
672
673 void
674 del_from_cli_fd_hash(struct Client *client_p)
675 {
676 unsigned int hashv;
677 hashv = hash_cli_fd(rb_get_fd(client_p->localClient->F));
678 rb_dlinkFindDestroy(client_p, &clientbyfdTable[hashv]);
679 }
680
681 struct Client *
682 find_cli_fd_hash(int fd)
683 {
684 struct Client *target_p;
685 rb_dlink_node *ptr;
686 unsigned int hashv;
687 hashv = hash_cli_fd(fd);
688 RB_DLINK_FOREACH(ptr, clientbyfdTable[hashv].head)
689 {
690 target_p = ptr->data;
691 if(rb_get_fd(target_p->localClient->F) == fd)
692 return target_p;
693 }
694 return NULL;
695 }
696
697 static void
698 output_hash(struct Client *source_p, const char *name, int length, int *counts, int deepest)
699 {
700 unsigned long total = 0;
701 int i;
702 char buf[128];
703
704 sendto_one_numeric(source_p, RPL_STATSDEBUG,
705 "B :%s Hash Statistics", name);
706
707 snprintf(buf, sizeof buf, "%.3f%%",
708 (float) ((counts[0]*100) / (float) length));
709 sendto_one_numeric(source_p, RPL_STATSDEBUG,
710 "B :Size: %d Empty: %d (%s)",
711 length, counts[0], buf);
712
713 for(i = 1; i < 11; i++)
714 {
715 total += (counts[i] * i);
716 }
717
718 /* dont want to divide by 0! --fl */
719 if(counts[0] != length)
720 {
721 snprintf(buf, sizeof buf, "%.3f/%.3f",
722 (float) (total / (length - counts[0])),
723 (float) (total / length));
724 sendto_one_numeric(source_p, RPL_STATSDEBUG,
725 "B :Average depth: %s Highest depth: %d",
726 buf, deepest);
727 }
728
729 for(i = 0; i < 11; i++)
730 {
731 sendto_one_numeric(source_p, RPL_STATSDEBUG,
732 "B :Nodes with %d entries: %d",
733 i, counts[i]);
734 }
735 }
736
737
738 static void
739 count_hash(struct Client *source_p, rb_dlink_list *table, int length, const char *name)
740 {
741 int counts[11];
742 int deepest = 0;
743 int i;
744
745 memset(counts, 0, sizeof(counts));
746
747 for(i = 0; i < length; i++)
748 {
749 if(rb_dlink_list_length(&table[i]) >= 10)
750 counts[10]++;
751 else
752 counts[rb_dlink_list_length(&table[i])]++;
753
754 if(rb_dlink_list_length(&table[i]) > deepest)
755 deepest = rb_dlink_list_length(&table[i]);
756 }
757
758 output_hash(source_p, name, length, counts, deepest);
759 }
760
761 void
762 hash_stats(struct Client *source_p)
763 {
764 count_hash(source_p, channelTable, CH_MAX, "Channel");
765 sendto_one_numeric(source_p, RPL_STATSDEBUG, "B :--");
766 count_hash(source_p, clientTable, U_MAX, "Client");
767 sendto_one_numeric(source_p, RPL_STATSDEBUG, "B :--");
768 count_hash(source_p, idTable, U_MAX, "ID");
769 sendto_one_numeric(source_p, RPL_STATSDEBUG, "B :--");
770 count_hash(source_p, hostTable, HOST_MAX, "Hostname");
771 }