]> jfr.im git - irc/rqf/shadowircd.git/blobdiff - src/hash.c
Disable autoconnect for a server with excessive TS delta.
[irc/rqf/shadowircd.git] / src / hash.c
index 86b22bcd2c4c1464e3f81904a8f0651983d9d915..10e35066d83ef9088e2a4644a43e796aa9223235 100644 (file)
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
  *  USA
  *
- *  $Id: hash.c 1321 2006-05-13 23:49:14Z nenolod $
+ *  $Id: hash.c 3177 2007-02-01 00:19:14Z jilles $
  */
 
 #include "stdinc.h"
 #include "ircd_defs.h"
-#include "tools.h"
 #include "s_conf.h"
 #include "channel.h"
 #include "client.h"
 #include "common.h"
 #include "hash.h"
-#include "irc_string.h"
+#include "match.h"
 #include "ircd.h"
 #include "numeric.h"
 #include "send.h"
-#include "memory.h"
 #include "msg.h"
 #include "cache.h"
 #include "s_newconf.h"
 
-dlink_list *clientTable;
-dlink_list *channelTable;
-dlink_list *idTable;
-dlink_list *resvTable;
-dlink_list *hostTable;
-dlink_list *helpTable;
-dlink_list *ndTable;
+#define hash_cli_fd(x) (x % CLI_FD_MAX)
+
+static rb_dlink_list clientbyfdTable[U_MAX];
+
+rb_dlink_list *clientTable;
+rb_dlink_list *channelTable;
+rb_dlink_list *idTable;
+rb_dlink_list *resvTable;
+rb_dlink_list *hostTable; 
 
 /*
  * look in whowas.c for the missing ...[WW_MAX]; entry
@@ -93,13 +93,11 @@ dlink_list *ndTable;
 void
 init_hash(void)
 {
-       clientTable = MyMalloc(sizeof(dlink_list) * U_MAX);
-       idTable = MyMalloc(sizeof(dlink_list) * U_MAX);
-       ndTable = MyMalloc(sizeof(dlink_list) * U_MAX);
-       channelTable = MyMalloc(sizeof(dlink_list) * CH_MAX);
-       hostTable = MyMalloc(sizeof(dlink_list) * HOST_MAX);
-       resvTable = MyMalloc(sizeof(dlink_list) * R_MAX);
-       helpTable = MyMalloc(sizeof(dlink_list) * HELP_MAX);
+       clientTable = rb_malloc(sizeof(rb_dlink_list) * U_MAX);
+       idTable = rb_malloc(sizeof(rb_dlink_list) * U_MAX);
+       channelTable = rb_malloc(sizeof(rb_dlink_list) * CH_MAX);
+       hostTable = rb_malloc(sizeof(rb_dlink_list) * HOST_MAX);
+       resvTable = rb_malloc(sizeof(rb_dlink_list) * R_MAX);
 }
 
 #ifndef RICER_HASHING
@@ -113,7 +111,8 @@ fnv_hash_upper(const unsigned char *s, int bits)
                h ^= ToUpper(*s++);
                h += (h<<1) + (h<<4) + (h<<7) + (h << 8) + (h << 24);
        }
-        h = (h >> bits) ^ (h & ((2^bits)-1));
+       if (bits < 32)
+               h = ((h >> bits) ^ h) & ((1<<bits)-1);
        return h;
 }
 
@@ -127,7 +126,8 @@ fnv_hash(const unsigned char *s, int bits)
                h ^= *s++;
                h += (h<<1) + (h<<4) + (h<<7) + (h << 8) + (h << 24);
        }
-        h = (h >> bits) ^ (h & ((2^bits)-1));
+       if (bits < 32)
+               h = ((h >> bits) ^ h) & ((1<<bits)-1);
        return h;
 }
 
@@ -141,7 +141,8 @@ fnv_hash_len(const unsigned char *s, int bits, int len)
                h ^= *s++;
                h += (h<<1) + (h<<4) + (h<<7) + (h << 8) + (h << 24);
        }
-        h = (h >> bits) ^ (h & ((2^bits)-1));
+       if (bits < 32)
+               h = ((h >> bits) ^ h) & ((1<<bits)-1);
        return h;
 }
 
@@ -155,7 +156,8 @@ fnv_hash_upper_len(const unsigned char *s, int bits, int len)
                h ^= ToUpper(*s++);
                h += (h<<1) + (h<<4) + (h<<7) + (h << 8) + (h << 24);
        }
-        h = (h >> bits) ^ (h & ((2^bits)-1));
+       if (bits < 32)
+               h = ((h >> bits) ^ h) & ((1<<bits)-1);
        return h;
 }
 #endif
@@ -211,19 +213,6 @@ hash_resv(const char *name)
        return fnv_hash_upper_len((const unsigned char *) name, R_MAX_BITS, 30);
 }
 
-static unsigned int
-hash_help(const char *name)
-{
-       unsigned int h = 0;
-
-       while(*name)
-       {
-               h += (unsigned int) (ToLower(*name++) & 0xDF);
-       }
-
-       return (h % HELP_MAX);
-}
-
 /* add_to_id_hash()
  *
  * adds an entry to the id hash table
@@ -237,7 +226,7 @@ add_to_id_hash(const char *name, struct Client *client_p)
                return;
 
        hashv = hash_id(name);
-       dlinkAddAlloc(client_p, &idTable[hashv]);
+       rb_dlinkAddAlloc(client_p, &idTable[hashv]);
 }
 
 /* add_to_client_hash()
@@ -255,7 +244,7 @@ add_to_client_hash(const char *name, struct Client *client_p)
                return;
 
        hashv = hash_nick(name);
-       dlinkAddAlloc(client_p, &clientTable[hashv]);
+       rb_dlinkAddAlloc(client_p, &clientTable[hashv]);
 }
 
 /* add_to_hostname_hash()
@@ -273,7 +262,7 @@ add_to_hostname_hash(const char *hostname, struct Client *client_p)
                return;
 
        hashv = hash_hostname(hostname);
-       dlinkAddAlloc(client_p, &hostTable[hashv]);
+       rb_dlinkAddAlloc(client_p, &hostTable[hashv]);
 }
 
 /* add_to_resv_hash()
@@ -291,26 +280,7 @@ add_to_resv_hash(const char *name, struct ConfItem *aconf)
                return;
 
        hashv = hash_resv(name);
-       dlinkAddAlloc(aconf, &resvTable[hashv]);
-}
-
-void
-add_to_help_hash(const char *name, struct cachefile *hptr)
-{
-       unsigned int hashv;
-
-       if(EmptyString(name) || hptr == NULL)
-               return;
-
-       hashv = hash_help(name);
-       dlinkAddAlloc(hptr, &helpTable[hashv]);
-}
-
-void
-add_to_nd_hash(const char *name, struct nd_entry *nd)
-{
-       nd->hashv = hash_nick(name);
-       dlinkAdd(nd, &nd->hnode, &ndTable[nd->hashv]);
+       rb_dlinkAddAlloc(aconf, &resvTable[hashv]);
 }
 
 /* del_from_id_hash()
@@ -328,7 +298,7 @@ del_from_id_hash(const char *id, struct Client *client_p)
                return;
 
        hashv = hash_id(id);
-       dlinkFindDestroy(client_p, &idTable[hashv]);
+       rb_dlinkFindDestroy(client_p, &idTable[hashv]);
 }
 
 /* del_from_client_hash()
@@ -347,7 +317,7 @@ del_from_client_hash(const char *name, struct Client *client_p)
                return;
 
        hashv = hash_nick(name);
-       dlinkFindDestroy(client_p, &clientTable[hashv]);
+       rb_dlinkFindDestroy(client_p, &clientTable[hashv]);
 }
 
 /* del_from_channel_hash()
@@ -366,7 +336,7 @@ del_from_channel_hash(const char *name, struct Channel *chptr)
                return;
 
        hashv = hash_channel(name);
-       dlinkFindDestroy(chptr, &channelTable[hashv]);
+       rb_dlinkFindDestroy(chptr, &channelTable[hashv]);
 }
 
 /* del_from_hostname_hash()
@@ -383,7 +353,7 @@ del_from_hostname_hash(const char *hostname, struct Client *client_p)
 
        hashv = hash_hostname(hostname);
 
-       dlinkFindDestroy(client_p, &hostTable[hashv]);
+       rb_dlinkFindDestroy(client_p, &hostTable[hashv]);
 }
 
 /* del_from_resv_hash()
@@ -402,22 +372,7 @@ del_from_resv_hash(const char *name, struct ConfItem *aconf)
 
        hashv = hash_resv(name);
 
-       dlinkFindDestroy(aconf, &resvTable[hashv]);
-}
-
-void
-clear_help_hash(void)
-{
-       dlink_node *ptr;
-       dlink_node *next_ptr;
-       int i;
-
-       HASH_WALK_SAFE(i, HELP_MAX, ptr, next_ptr, helpTable)
-       {
-               free_cachefile(ptr->data);
-               dlinkDestroy(ptr, &helpTable[i]);
-       }
-       HASH_WALK_END
+       rb_dlinkFindDestroy(aconf, &resvTable[hashv]);
 }
 
 /* find_id()
@@ -428,7 +383,7 @@ struct Client *
 find_id(const char *name)
 {
        struct Client *target_p;
-       dlink_node *ptr;
+       rb_dlink_node *ptr;
        unsigned int hashv;
 
        if(EmptyString(name))
@@ -436,7 +391,7 @@ find_id(const char *name)
 
        hashv = hash_id(name);
 
-       DLINK_FOREACH(ptr, idTable[hashv].head)
+       RB_DLINK_FOREACH(ptr, idTable[hashv].head)
        {
                target_p = ptr->data;
 
@@ -447,79 +402,6 @@ find_id(const char *name)
        return NULL;
 }
 
-/* hash_find_masked_server()
- * 
- * 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(struct Client *source_p, const char *name)
-{
-       char buf[HOSTLEN + 1];
-       char *p = buf;
-       char *s;
-       struct Client *server;
-
-       if('*' == *name || '.' == *name)
-               return NULL;
-
-       /* copy it across to give us a buffer to work on */
-       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_server(source_p, s)))
-                       return server;
-               p = s + 2;
-       }
-
-       return NULL;
-}
-
-/* find_any_client()
- *
- * finds a client/server/masked server entry from the hash
- */
-struct Client *
-find_any_client(const char *name)
-{
-       struct Client *target_p;
-       dlink_node *ptr;
-       unsigned int hashv;
-
-       s_assert(name != NULL);
-       if(EmptyString(name))
-               return NULL;
-
-       /* hunting for an id, not a nick */
-       if(IsDigit(*name))
-               return (find_id(name));
-
-       hashv = hash_nick(name);
-
-       DLINK_FOREACH(ptr, clientTable[hashv].head)
-       {
-               target_p = ptr->data;
-
-               if(irccmp(name, target_p->name) == 0)
-                       return target_p;
-       }
-
-       /* wasnt found, look for a masked server */
-       return hash_find_masked_server(NULL, name);
-}
-
 /* find_client()
  *
  * finds a client/server entry from the client hash table
@@ -528,7 +410,7 @@ struct Client *
 find_client(const char *name)
 {
        struct Client *target_p;
-       dlink_node *ptr;
+       rb_dlink_node *ptr;
        unsigned int hashv;
 
        s_assert(name != NULL);
@@ -541,7 +423,7 @@ find_client(const char *name)
 
        hashv = hash_nick(name);
 
-       DLINK_FOREACH(ptr, clientTable[hashv].head)
+       RB_DLINK_FOREACH(ptr, clientTable[hashv].head)
        {
                target_p = ptr->data;
 
@@ -560,7 +442,7 @@ struct Client *
 find_named_client(const char *name)
 {
        struct Client *target_p;
-       dlink_node *ptr;
+       rb_dlink_node *ptr;
        unsigned int hashv;
 
        s_assert(name != NULL);
@@ -569,7 +451,7 @@ find_named_client(const char *name)
 
        hashv = hash_nick(name);
 
-       DLINK_FOREACH(ptr, clientTable[hashv].head)
+       RB_DLINK_FOREACH(ptr, clientTable[hashv].head)
        {
                target_p = ptr->data;
 
@@ -588,7 +470,7 @@ struct Client *
 find_server(struct Client *source_p, const char *name)
 {
        struct Client *target_p;
-       dlink_node *ptr;
+       rb_dlink_node *ptr;
        unsigned int hashv;
   
        if(EmptyString(name))
@@ -603,7 +485,7 @@ find_server(struct Client *source_p, const char *name)
 
        hashv = hash_nick(name);
 
-       DLINK_FOREACH(ptr, clientTable[hashv].head)
+       RB_DLINK_FOREACH(ptr, clientTable[hashv].head)
        {
                target_p = ptr->data;
 
@@ -612,17 +494,16 @@ find_server(struct Client *source_p, const char *name)
                                return target_p;
        }
 
-       /* wasnt found, look for a masked server */
-       return hash_find_masked_server(source_p, name);
+       return NULL;
 }
 
 /* find_hostname()
  *
- * finds a hostname dlink list from the hostname hash table.
- * we return the full dlink list, because you can have multiple
+ * finds a hostname rb_dlink list from the hostname hash table.
+ * we return the full rb_dlink list, because you can have multiple
  * entries with the same hostname
  */
-dlink_node *
+rb_dlink_node *
 find_hostname(const char *hostname)
 {
        unsigned int hashv;
@@ -643,7 +524,7 @@ struct Channel *
 find_channel(const char *name)
 {
        struct Channel *chptr;
-       dlink_node *ptr;
+       rb_dlink_node *ptr;
        unsigned int hashv;
 
        s_assert(name != NULL);
@@ -652,7 +533,7 @@ find_channel(const char *name)
 
        hashv = hash_channel(name);
 
-       DLINK_FOREACH(ptr, channelTable[hashv].head)
+       RB_DLINK_FOREACH(ptr, channelTable[hashv].head)
        {
                chptr = ptr->data;
 
@@ -678,7 +559,7 @@ struct Channel *
 get_or_create_channel(struct Client *client_p, const char *chname, int *isnew)
 {
        struct Channel *chptr;
-       dlink_node *ptr;
+       rb_dlink_node *ptr;
        unsigned int hashv;
        int len;
        const char *s = chname;
@@ -704,7 +585,7 @@ get_or_create_channel(struct Client *client_p, const char *chname, int *isnew)
 
        hashv = hash_channel(s);
 
-       DLINK_FOREACH(ptr, channelTable[hashv].head)
+       RB_DLINK_FOREACH(ptr, channelTable[hashv].head)
        {
                chptr = ptr->data;
 
@@ -721,11 +602,11 @@ get_or_create_channel(struct Client *client_p, const char *chname, int *isnew)
 
        chptr = allocate_channel(s);
 
-       dlinkAdd(chptr, &chptr->node, &global_channel_list);
+       rb_dlinkAdd(chptr, &chptr->node, &global_channel_list);
 
-       chptr->channelts = CurrentTime; /* doesn't hurt to set it here */
+       chptr->channelts = rb_current_time();   /* doesn't hurt to set it here */
 
-       dlinkAddAlloc(chptr, &channelTable[hashv]);
+       rb_dlinkAddAlloc(chptr, &channelTable[hashv]);
 
        return chptr;
 }
@@ -738,7 +619,7 @@ struct ConfItem *
 hash_find_resv(const char *name)
 {
        struct ConfItem *aconf;
-       dlink_node *ptr;
+       rb_dlink_node *ptr;
        unsigned int hashv;
 
        s_assert(name != NULL);
@@ -747,7 +628,7 @@ hash_find_resv(const char *name)
 
        hashv = hash_resv(name);
 
-       DLINK_FOREACH(ptr, resvTable[hashv].head)
+       RB_DLINK_FOREACH(ptr, resvTable[hashv].head)
        {
                aconf = ptr->data;
 
@@ -761,36 +642,12 @@ hash_find_resv(const char *name)
        return NULL;
 }
 
-struct cachefile *
-hash_find_help(const char *name, int flags)
-{
-       struct cachefile *hptr;
-       dlink_node *ptr;
-       unsigned int hashv;
-
-       if(EmptyString(name))
-               return NULL;
-
-       hashv = hash_help(name);
-
-       DLINK_FOREACH(ptr, helpTable[hashv].head)
-       {
-               hptr = ptr->data;
-
-               if((irccmp(name, hptr->name) == 0) &&
-                  (hptr->flags & flags))
-                       return hptr;
-       }
-
-       return NULL;
-}
-
 void
 clear_resv_hash(void)
 {
        struct ConfItem *aconf;
-       dlink_node *ptr;
-       dlink_node *next_ptr;
+       rb_dlink_node *ptr;
+       rb_dlink_node *next_ptr;
        int i;
 
        HASH_WALK_SAFE(i, R_MAX, ptr, next_ptr, resvTable)
@@ -802,32 +659,40 @@ clear_resv_hash(void)
                        continue;
 
                free_conf(ptr->data);
-               dlinkDestroy(ptr, &resvTable[i]);
+               rb_dlinkDestroy(ptr, &resvTable[i]);
        }
        HASH_WALK_END
 }
 
-struct nd_entry *
-hash_find_nd(const char *name)
+void
+add_to_cli_fd_hash(struct Client *client_p)
 {
-       struct nd_entry *nd;
-       dlink_node *ptr;
-       unsigned int hashv;
+       rb_dlinkAddAlloc(client_p, &clientbyfdTable[hash_cli_fd(rb_get_fd(client_p->localClient->F))]);
+}
 
-       if(EmptyString(name))
-               return NULL;
 
-       hashv = hash_nick(name);
+void
+del_from_cli_fd_hash(struct Client *client_p)
+{
+       unsigned int hashv;
+       hashv = hash_cli_fd(rb_get_fd(client_p->localClient->F));
+       rb_dlinkFindDestroy(client_p, &clientbyfdTable[hashv]);
+}
 
-       DLINK_FOREACH(ptr, ndTable[hashv].head)
+struct Client *
+find_cli_fd_hash(int fd)
+{
+       struct Client *target_p;
+       rb_dlink_node *ptr;
+       unsigned int hashv;
+       hashv = hash_cli_fd(fd);
+       RB_DLINK_FOREACH(ptr, clientbyfdTable[hashv].head)
        {
-               nd = ptr->data;
-
-               if(!irccmp(name, nd->name))
-                       return nd;
+               target_p = ptr->data;
+               if(rb_get_fd(target_p->localClient->F) == fd)
+                       return target_p;
        }
-
-       return NULL;
+       return  NULL;   
 }
 
 static void
@@ -835,14 +700,16 @@ output_hash(struct Client *source_p, const char *name, int length, int *counts,
 {
        unsigned long total = 0;
        int i;
+       char buf[128];
 
        sendto_one_numeric(source_p, RPL_STATSDEBUG,
                        "B :%s Hash Statistics", name);
 
-       sendto_one_numeric(source_p, RPL_STATSDEBUG,
-                       "B :Size: %d Empty: %d (%.3f%%)",
-                       length, counts[0], 
+       snprintf(buf, sizeof buf, "%.3f%%",
                        (float) ((counts[0]*100) / (float) length));
+       sendto_one_numeric(source_p, RPL_STATSDEBUG,
+                       "B :Size: %d Empty: %d (%s)",
+                       length, counts[0], buf);
 
        for(i = 1; i < 11; i++)
        {
@@ -851,10 +718,14 @@ output_hash(struct Client *source_p, const char *name, int length, int *counts,
 
        /* dont want to divide by 0! --fl */
        if(counts[0] != length)
-               sendto_one_numeric(source_p, RPL_STATSDEBUG,
-                               "B :Average depth: %.3f/%.3f Highest depth: %d",
+       {
+               snprintf(buf, sizeof buf, "%.3f/%.3f",
                                (float) (total / (length - counts[0])),
-                               (float) (total / length), deepest);
+                               (float) (total / length));
+               sendto_one_numeric(source_p, RPL_STATSDEBUG,
+                               "B :Average depth: %s Highest depth: %d",
+                               buf, deepest);
+       }
 
        for(i = 0; i < 11; i++)
        {
@@ -866,7 +737,7 @@ output_hash(struct Client *source_p, const char *name, int length, int *counts,
        
 
 static void
-count_hash(struct Client *source_p, dlink_list *table, int length, const char *name)
+count_hash(struct Client *source_p, rb_dlink_list *table, int length, const char *name)
 {
        int counts[11];
        int deepest = 0;
@@ -876,13 +747,13 @@ count_hash(struct Client *source_p, dlink_list *table, int length, const char *n
        
        for(i = 0; i < length; i++)
        {
-               if(dlink_list_length(&table[i]) >= 10)
+               if(rb_dlink_list_length(&table[i]) >= 10)
                        counts[10]++;
                else
-                       counts[dlink_list_length(&table[i])]++;
+                       counts[rb_dlink_list_length(&table[i])]++;
 
-               if(dlink_list_length(&table[i]) > deepest)
-                       deepest = dlink_list_length(&table[i]);
+               if(rb_dlink_list_length(&table[i]) > deepest)
+                       deepest = rb_dlink_list_length(&table[i]);
        }
 
        output_hash(source_p, name, length, counts, deepest);