A
download hash.c
Language: C
License: GPL
Copyright: (C) 2002 by the past and present ircd coders, and others.
LOC: 504
Project Info
NeoStats
Server: SourceForge
Type: cvs
...stats\neostats\NeoIRCd\src\
   .cvsignore
   .depend
   adns.c
   balloc.c
   channel.c
   channel_mode.c
   class.c
   client.c
   cloak.c
   crypt.c
   descrip.mms
   dynlink.c
   event.c
   fdlist.c
   fileio.c
   getopt.c
   hash.c
   hook.c
   hostmask.c
   irc_string.c
   ircd.c
   ircd_parser.y
   ircd_signal.c
   ircdauth.c
   kdparse.c
   linebuf.c
   list.c
   listener.c
   m_error.c
   Makefile.in
   match.c
   md5.c
   memory.c
   messages.tab
   modules.c
   motd.c
   numeric.c
   packet.c
   parse.c
   restart.c
   resv.c
   rsa.c
   s_auth.c
   s_bsd.c
   s_bsd_devpoll.c
   s_bsd_kqueue.c
   s_bsd_poll.c
   s_bsd_select.c
   s_bsd_sigio.c
   s_conf.c
   s_debug.c
   s_gline.c
   s_log.c
   s_misc.c
   s_serv.c
   s_stats.c
   s_user.c
   scache.c
   send.c
   snprintf.c
   sprintf_irc.c
   ssl.c
   tools.c
   version.c.SH
   whowas.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
/*
 *  NeoIRCd: NeoStats Group. Based on Hybird7
 *  hash.c: Maintains hashtables.
 *
 *  Copyright (C) 2002 by the past and present ircd coders, and others.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 *  USA
 *
 *  $Id: hash.c,v 1.6 2002/10/31 13:01:58 fishwaldo Exp $
 */

#include "stdinc.h"

#include "tools.h"
#include "s_conf.h"
#include "channel.h"
#include "client.h"
#include "common.h"
#include "hash.h"
#include "resv.h"
#include "irc_string.h"
#include "ircd.h"
#include "numeric.h"
#include "send.h"
#include "s_debug.h"
#include "fdlist.h"
#include "fileio.h"
#include "memory.h"

/* New hash code */
/*
 * Contributed by James L. Davis
 */

static unsigned int hash_channel_name(const char* name);

#ifdef DEBUGMODE
static struct HashEntry* clientTable = NULL;
static struct HashEntry* channelTable = NULL;
static struct HashEntry* idTable = NULL;
static struct HashEntry* resvTable = NULL;
static int clhits;
static int clmiss;
static int chhits;
static int chmiss;
static int rhits;
static int rmiss;
#else

static struct HashEntry clientTable[U_MAX];
static struct HashEntry channelTable[CH_MAX];
static struct HashEntry idTable[U_MAX];
static struct HashEntry resvTable[R_MAX];

#endif

/* XXX move channel hash into channel.c or hash channel stuff in channel.c
 * into here eventually -db
 */
extern BlockHeap *channel_heap;

struct HashEntry hash_get_channel_block(int i)
{
  return channelTable[i];
}

size_t hash_get_channel_table_size(void)
{
  return sizeof(struct HashEntry) * CH_MAX;
}

size_t hash_get_client_table_size(void)
{
  return sizeof(struct HashEntry) * U_MAX;
}

size_t hash_get_resv_table_size(void)
{
  return sizeof(struct HashEntry) * R_MAX;
}

/*
 *
 * look in whowas.c for the missing ...[WW_MAX]; entry
 */

/*
 * Hashing.
 *
 *   The server uses a chained hash table to provide quick and efficient
 * hash table maintenance (providing the hash function works evenly over
 * the input range).  The hash table is thus not susceptible to problems
 * of filling all the buckets or the need to rehash.
 *    It is expected that the hash table would look something like this
 * during use:
 *                   +-----+    +-----+    +-----+   +-----+
 *                ---| 224 |----| 225 |----| 226 |---| 227 |---
 *                   +-----+    +-----+    +-----+   +-----+
 *                      |          |          |
 *                   +-----+    +-----+    +-----+
 *                   |  A  |    |  C  |    |  D  |
 *                   +-----+    +-----+    +-----+
 *                      |
 *                   +-----+
 *                   |  B  |
 *                   +-----+
 *
 * A - GOPbot, B - chang, C - hanuaway, D - *.mu.OZ.AU
 *
 * The order shown above is just one instant of the server. 
 */

static unsigned
int hash_nick_name(const char* name)
{
  unsigned int h = 0;

  while (*name)
    {
      h = (h << 4) - (h + (unsigned char)ToLower(*name++));
    }

  return(h & (U_MAX - 1));
}

/*
 * hash_id
 *
 * IDs are a easy to hash -- they're already evenly distributed,
 * and they are always case sensitive.   -orabidoo
 */
static  unsigned int 
hash_id(const char *nname)
{
	unsigned int h = 0;
	
	while (*nname) {
		h = (h << 4) - (h + (unsigned char)*nname++);
	}

	return (h & (U_MAX - 1));
}
/*
 * hash_channel_name
 *
 * calculate a hash value on at most the first 30 characters of the channel
 * name. Most names are short than this or dissimilar in this range. There
 * is little or no point hashing on a full channel name which maybe 255 chars
 * long.
 */
static unsigned
int hash_channel_name(const char* name)
{
  int i = 30;
  unsigned int h = 0;

  while (*name && --i)
    {
      h = (h << 4) - (h + (unsigned char)ToLower(*name++));
    }

  return (h & (CH_MAX - 1));
}


/*
 * hash_resv_channel()
 *
 * calculate a hash value on at most the first 30 characters and add
 * it to the resv hash
 */
static unsigned
int hash_resv_channel(const char *name)
{
  int i = 30;
  unsigned int h = 0;

  while (*name && --i)
  {
    h = (h << 4) - (h + (unsigned char)ToLower(*name++));
  }

  return (h & (R_MAX -1));
}

/*
 * clear_client_hash_table
 *
 * Nullify the hashtable and its contents so it is completely empty.
 */
static void clear_client_hash_table()
{
#ifdef DEBUGMODE
  clhits = 0;
  clmiss = 0;
  if(!clientTable)
    clientTable = (struct HashEntry*) MyMalloc(U_MAX * sizeof(struct HashEntry));
#endif
  memset(clientTable, 0, sizeof(struct HashEntry) * U_MAX);
}

/*
 * clear_id_hash_table
 *
 * Nullify the hashtable and its contents so it is completely empty.
 */
static void clear_id_hash_table()
{
#ifdef DEBUGMODE
  /* XXX -
   * idhits = 0;
   * idmiss = 0;
   * -cosine
   */
  if(!idTable)
    idTable = (struct HashEntry*) MyMalloc(U_MAX * sizeof(struct HashEntry));
#endif
  memset(idTable, 0, sizeof(struct HashEntry) * U_MAX);
}

static void
clear_channel_hash_table()
{
#ifdef DEBUGMODE
  chmiss = 0;
  chhits = 0;
  if (!channelTable)
    channelTable = (struct HashEntry*) MyMalloc(CH_MAX *
                                                sizeof(struct HashEntry));
#endif
  memset(channelTable, 0, sizeof(struct HashEntry) * CH_MAX);
}

static void
clear_resv_hash_table()
{
#ifdef DEBUGMODE
  rmiss = 0;
  rhits = 0;
  if(!resvTable)
    resvTable = (struct HashEntry*) MyMalloc(R_MAX *
                                             sizeof(struct HashEntry));
#endif
  memset(resvTable, 0, sizeof(struct HashEntry) * R_MAX);
}

void 
init_hash(void)
{
  clear_client_hash_table();
  clear_channel_hash_table();
  clear_id_hash_table();
  clear_resv_hash_table();
}

/*
 * add_to_id_hash_table
 */
int
add_to_id_hash_table(char *name, struct Client *client_p)
{
  unsigned int     hashv;

  hashv = hash_id(name);
  client_p->idhnext = (struct Client *)idTable[hashv].list;
  idTable[hashv].list = (void *)client_p;
  idTable[hashv].links++;
  idTable[hashv].hits++;
  return 0;
}

/*
 * add_to_client_hash_table
 */
void 
add_to_client_hash_table(const char* name, struct Client* client_p)
{
  unsigned int hashv;
  assert(name != NULL);
  assert(client_p != NULL);
  if(name == NULL || client_p == NULL)
    return;
  
  hashv = hash_nick_name(name);
  client_p->hnext = (struct Client*) clientTable[hashv].list;
  clientTable[hashv].list = (void*) client_p;
  ++clientTable[hashv].links;
  ++clientTable[hashv].hits;
}

/*
 * add_to_resv_hash_table
 */
void
add_to_resv_hash_table(const char *name, struct ResvChannel *resv_p)
{
  unsigned int hashv;
  assert(name != NULL);
  assert(resv_p != NULL);
  
  if(name == NULL || resv_p == NULL)
    return;

  hashv = hash_resv_channel(name);

  resv_p->hnext = (struct ResvChannel *) resvTable[hashv].list;
  resvTable[hashv].list = (void*)resv_p;
  ++resvTable[hashv].links;
  ++resvTable[hashv].hits;
}


/*
 * del_from_id_hash_table - remove a client/server from the id
 * hash table
 */
void
del_from_id_hash_table(const char* id, struct Client* client_p)
{
  struct Client* found_client;
  struct Client* prev = NULL;
  unsigned int   hashv;

  assert(id != NULL);
  assert(client_p != NULL);
  
  if(id == NULL || client_p == NULL)
    return;

  hashv = hash_id(id);
  found_client = (struct Client*) idTable[hashv].list;

  for ( ; found_client; found_client = found_client->idhnext)
    {
      if (found_client == client_p)
        {
          if (prev)
            prev->idhnext = found_client->idhnext;
          else
            idTable[hashv].list = (void*) found_client->idhnext;
          found_client->idhnext = NULL;

          assert(idTable[hashv].links > 0);
          if (idTable[hashv].links > 0)
            --idTable[hashv].links;
          return;
        }
      prev = found_client;
    }
  Debug((DEBUG_ERROR, "%#x !in tab %s[%s] %#x %#x %#x %d %d %#x",
         client_p, client_p->name, client_p->from ? client_p->from->host : "??host",
         client_p->from, client_p->next, client_p->prev, client_p->localClient->fd, 
         client_p->status, client_p->user));
}

/*
 * del_from_client_hash_table - remove a client/server from the client
 * hash table
 */
void
del_from_client_hash_table(const char* name, struct Client* client_p)
{
  struct Client* found_client;
  struct Client* prev = NULL;
  unsigned int   hashv;
  assert(name != NULL);
  assert(client_p != NULL);
  
  if(name == NULL || client_p == NULL)
    return;

  hashv = hash_nick_name(name);
  found_client = (struct Client*) clientTable[hashv].list;

  for ( ; found_client; found_client = found_client->hnext)
    {
      if (found_client == client_p)
        {
          if (prev)
            prev->hnext = found_client->hnext;
          else
            clientTable[hashv].list = (void*) found_client->hnext;
          found_client->hnext = NULL;

          assert(clientTable[hashv].links > 0);
          if (clientTable[hashv].links > 0)
            --clientTable[hashv].links;
          return;
        }
      prev = found_client;
    }
  Debug((DEBUG_ERROR, "%#x !in tab %s[%s] %#x %#x %#x %d %d %#x",
         client_p, client_p->name, client_p->from ? client_p->from->host : "??host",
         client_p->from, client_p->next, client_p->prev, client_p->localClient->fd, 
         client_p->status, client_p->user));
}

/*
 * del_from_channel_hash_table
 */
void 
del_from_channel_hash_table(const char* name, struct Channel* chptr)
{
  struct Channel* found_chptr;
  struct Channel* prev = NULL;
  unsigned int    hashv;

#ifdef INVARIANTS
  assert(name != NULL);
  assert(chptr != NULL);
#else
  if(name == NULL || chptr == NULL)
    return;
#endif
    
  hashv = hash_channel_name(name);
  found_chptr = (struct Channel*) channelTable[hashv].list;

  for ( ; found_chptr; found_chptr = found_chptr->hnextch)
    {
      if (found_chptr == chptr)
        {
          if (prev)
            prev->hnextch = found_chptr->hnextch;
          else
            channelTable[hashv].list = (void*) found_chptr->hnextch;
          found_chptr->hnextch = NULL;

          assert(channelTable[hashv].links > 0);
          if (channelTable[hashv].links > 0)
            --channelTable[hashv].links;
          return;
        }
      prev = found_chptr;
    }
}

/*
 * del_from_resv_hash_table()
 */
void 
del_from_resv_hash_table(const char *name, struct ResvChannel *rptr)
{
  struct ResvChannel *found_chptr;
  struct ResvChannel *prev=NULL;
  unsigned int hashv;

  assert(name != NULL);
  assert(rptr != NULL);

  if(name == NULL || rptr == NULL)
    return;
    
  hashv = hash_resv_channel(name);

  found_chptr = (struct ResvChannel *) resvTable[hashv].list;

  for( ; found_chptr; found_chptr = found_chptr->hnext)
  {
    if(found_chptr == rptr)
    {
      if(prev)
        prev->hnext = found_chptr->hnext;
      else
        resvTable[hashv].list = (void*)found_chptr->hnext;

      found_chptr->hnext=NULL;

      assert(resvTable[hashv].links > 0);
      --resvTable[hashv].links;

      return;
    }
  }
}  
 
/*
 * find_id
 */
struct Client *
find_id(const char *name)
{
  struct Client *found_client;
  unsigned int hashv;
	
  if (name == NULL)
    return NULL;

  hashv = hash_id(name);
  found_client = (struct Client *)idTable[hashv].list;

  /*
   * Got the bucket, now search the chain.
   */
  for (; found_client; found_client = found_client->idhnext)
  {
    if (found_client->user && strcmp(name, found_client->user->id) == 0)
    {
      return(found_client);
    }
  }
	
  return (NULL);
}


/*
 * find_client
 *
 * inputs	- name of either server or client
 * output	- pointer to client pointer
 * side effects	- none
 */
struct Client* 
find_client(const char* name)
{
  struct Client* found_client;
  unsigned int   hashv;

  assert(name != NULL);
  if(name == NULL)
    return NULL;

  if (*name == '.') /* it's an ID .. */
    return (find_id(name));

  hashv = hash_nick_name(name);
  found_client = (struct Client*) clientTable[hashv].list;

  for ( ; found_client; found_client = found_client->hnext)
    if (irccmp(name, found_client->name) == 0)
      {
#ifdef DEBUGMODE
        ++clhits;
#endif
        return (found_client);
      }
#ifdef DEBUGMODE
  ++clmiss;
#endif
  
  return (NULL);
}


/*
 * Whats happening in this next loop ? Well, it takes a name like
 * foo.bar.edu and proceeds to earch for *.edu and then *.bar.edu.
 * This is for checking full server names against masks although
 * it isnt often done this way in lieu of using matches().
 *
 * Rewrote to do *.bar.edu first, which is the most likely case,
 * also made const correct
 * --Bleep
 */
static struct Client* 
hash_find_masked_server(const char* name)
{
  char           buf[HOSTLEN + 1];
  char*          p = buf;
  char*          s;
  struct Client* server;

  if ('*' == *name || '.' == *name)
    return 0;

  /*
   * copy the damn thing and be done with it
   */
  strlcpy(buf, name, sizeof(buf));

  while ((s = strchr(p, '.')) != 0)
    {
       *--s = '*';
      /*
       * Dont need to check IsServer() here since nicknames cant
       * have *'s in them anyway.
       */
       if ((server = find_client(s)))
	 return server;
       p = s + 2;
    }
  return 0;
}

/*
 * find_server
 *
 * inputs	- pointer to server name
 * output	- NULL if given name is NULL or
 *		  given server not found
 * side effects	-
 */
struct Client* 
find_server(const char* name)
{
  struct Client* found_server;
  unsigned int   hashv;

  if (name == NULL)
    return(NULL);

  hashv = hash_nick_name(name);
  found_server = (struct Client*) clientTable[hashv].list;

  for ( ; found_server; found_server = found_server->hnext)
    {
      if (!IsServer(found_server) && !IsMe(found_server))
        continue;
      if (irccmp(name, found_server->name) == 0)
        {
#ifdef DEBUGMODE
          ++clhits;
#endif
          return (found_server);
        }
    }
  
#ifndef DEBUGMODE
  return hash_find_masked_server(name);

#else /* DEBUGMODE */
  if (!(found_server = hash_find_masked_server(name)))
    ++clmiss;
  return (found_server);
#endif
}

/*
 * hash_find_channel
 * inputs	- pointer to name
 * output	- 
 * side effects	-
 */
struct Channel* 
hash_find_channel(const char* name)
{
  struct Channel* found_chptr;
  unsigned int hashv;
  
  assert(name != NULL);
  if(name == NULL)
    return NULL;
  hashv = hash_channel_name(name);

  found_chptr = (struct Channel*) channelTable[hashv].list;

  for ( ; found_chptr; found_chptr = found_chptr->hnextch)
  
    if (irccmp(name, found_chptr->chname) == 0)
      {
#ifdef DEBUGMODE
        ++chhits;
#endif
        return(found_chptr);
      }
#ifdef DEBUGMODE
  ++chmiss;
#endif
  return(NULL);
}

/*
 * get_or_create_channel
 * inputs       - client pointer
 *              - channel name
 *              - pointer to int flag whether channel was newly created or not
 * output       - returns channel block or NULL if illegal name
 *		- also modifies *isnew
 *
 *  Get Channel block for chname (and allocate a new channel
 *  block, if it didn't exist before).
 */
struct Channel *
get_or_create_channel(struct Client *client_p, char *chname, int *isnew)
{
  struct Channel *chptr;
  struct Channel *found_chptr;
  unsigned int hashv;
  int len;

  if (BadPtr(chname))
    return NULL;

  len = strlen(chname);
  if (len > CHANNELLEN)
    {
      if (IsServer(client_p))
	{
	  sendto_realops_flags(FLAGS_DEBUG|FLAGS_REMOTE, L_ALL,
			       "*** Long channel name from %s (%d > %d): %s",
			       client_p->name,
			       len,
			       CHANNELLEN,
			       chname);
	}
      len = CHANNELLEN;
      *(chname + CHANNELLEN) = '\0';
    }

  hashv = hash_channel_name(chname);

  for ( found_chptr = (struct Channel*) channelTable[hashv].list;
	found_chptr; found_chptr = found_chptr->hnextch)
    {
      if (irccmp(chname, found_chptr->chname) == 0)
	{
#ifdef DEBUGMODE
	  ++chhits;
#endif
	  if(isnew != NULL)
	    *isnew = 0;
	  return(found_chptr);
	}
#ifdef DEBUGMODE
      ++chmiss;
#endif
    }

  if(isnew != NULL)
    *isnew = 1;

  chptr = BlockHeapAlloc(channel_heap);
  memset(chptr, 0, sizeof(struct Channel));
  strlcpy(chptr->chname, chname, sizeof(chptr->chname));

  if (GlobalChannelList)
    GlobalChannelList->prevch = chptr;

  chptr->prevch = NULL;
  chptr->nextch = GlobalChannelList;
  GlobalChannelList = chptr;
  chptr->channelts = CurrentTime;     /* doesn't hurt to set it here */

  chptr->hnextch = (struct Channel*) channelTable[hashv].list;
  channelTable[hashv].list = (void*) chptr;
  ++channelTable[hashv].links;
  ++channelTable[hashv].hits;

  Count.chan++;
  return chptr;
}

/*
 * hash_find_resv()
 */
struct ResvChannel *
hash_find_resv(const char *name)
{
  struct ResvChannel *found_chptr;
  unsigned int hashv;

  assert(name != NULL);
  if(name == NULL)
    return NULL;
  hashv = hash_resv_channel(name);

  found_chptr = (struct ResvChannel *) resvTable[hashv].list;

  for( ; found_chptr; found_chptr = found_chptr->hnext)
  {
    if(!irccmp(name, found_chptr->name))
    {
#ifdef DEBUGMODE
      ++rhits;
#endif      
      return(found_chptr);
    }
  }
#ifdef DEBUGMODE
  ++rmiss;
#endif
  
  return(NULL);
}  






About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us