]> 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 6f2bd6531532089160702c0ddd9bf42fd10d9be4..10e35066d83ef9088e2a4644a43e796aa9223235 100644 (file)
 
 #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; 
+#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
@@ -91,11 +93,11 @@ dlink_list *hostTable;
 void
 init_hash(void)
 {
-       clientTable = MyMalloc(sizeof(dlink_list) * U_MAX);
-       idTable = 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);
+       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
@@ -109,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;
 }
 
@@ -123,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;
 }
 
@@ -137,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;
 }
 
@@ -151,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
@@ -220,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()
@@ -238,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()
@@ -256,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()
@@ -274,7 +280,7 @@ add_to_resv_hash(const char *name, struct ConfItem *aconf)
                return;
 
        hashv = hash_resv(name);
-       dlinkAddAlloc(aconf, &resvTable[hashv]);
+       rb_dlinkAddAlloc(aconf, &resvTable[hashv]);
 }
 
 /* del_from_id_hash()
@@ -292,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()
@@ -311,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()
@@ -330,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()
@@ -347,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()
@@ -366,7 +372,7 @@ del_from_resv_hash(const char *name, struct ConfItem *aconf)
 
        hashv = hash_resv(name);
 
-       dlinkFindDestroy(aconf, &resvTable[hashv]);
+       rb_dlinkFindDestroy(aconf, &resvTable[hashv]);
 }
 
 /* find_id()
@@ -377,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))
@@ -385,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;
 
@@ -404,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);
@@ -417,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;
 
@@ -436,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);
@@ -445,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;
 
@@ -464,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))
@@ -479,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;
 
@@ -493,11 +499,11 @@ find_server(struct Client *source_p, const char *name)
 
 /* 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;
@@ -518,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);
@@ -527,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;
 
@@ -553,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;
@@ -579,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;
 
@@ -596,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;
 }
@@ -613,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);
@@ -622,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;
 
@@ -640,8 +646,8 @@ 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)
@@ -653,24 +659,57 @@ clear_resv_hash(void)
                        continue;
 
                free_conf(ptr->data);
-               dlinkDestroy(ptr, &resvTable[i]);
+               rb_dlinkDestroy(ptr, &resvTable[i]);
        }
        HASH_WALK_END
 }
 
+void
+add_to_cli_fd_hash(struct Client *client_p)
+{
+       rb_dlinkAddAlloc(client_p, &clientbyfdTable[hash_cli_fd(rb_get_fd(client_p->localClient->F))]);
+}
+
+
+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]);
+}
+
+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)
+       {
+               target_p = ptr->data;
+               if(rb_get_fd(target_p->localClient->F) == fd)
+                       return target_p;
+       }
+       return  NULL;   
+}
+
 static void
 output_hash(struct Client *source_p, const char *name, int length, int *counts, int deepest)
 {
        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++)
        {
@@ -679,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++)
        {
@@ -694,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;
@@ -704,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);