]> jfr.im git - solanum.git/blobdiff - src/hash.c
Merge pull request #53 from ShadowNinja/clarify_U+R
[solanum.git] / src / hash.c
index bf17b06661ee29400f8bbe7681a168c1f677b245..0b90aaa57569ac1d8495917d142600c2c8482b60 100644 (file)
 #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 "msg.h"
 #include "cache.h"
 #include "s_newconf.h"
+#include "s_assert.h"
+
+#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; 
+rb_dlink_list *hostTable;
 
 /*
  * look in whowas.c for the missing ...[WW_MAX]; entry
@@ -72,14 +77,14 @@ rb_dlink_list *hostTable;
  *
  * A - GOPbot, B - chang, C - hanuaway, D - *.mu.OZ.AU
  *
- * The order shown above is just one instant of the server. 
+ * The order shown above is just one instant of the server.
  *
  *
  * The hash functions currently used are based Fowler/Noll/Vo hashes
  * which work amazingly well and have a extremely low collision rate
  * For more info see http://www.isthe.com/chongo/tech/comp/fnv/index.html
  *
- * 
+ *
  */
 
 /* init_hash()
@@ -89,11 +94,11 @@ rb_dlink_list *hostTable;
 void
 init_hash(void)
 {
-       clientTable = MyMalloc(sizeof(rb_dlink_list) * U_MAX);
-       idTable = MyMalloc(sizeof(rb_dlink_list) * U_MAX);
-       channelTable = MyMalloc(sizeof(rb_dlink_list) * CH_MAX);
-       hostTable = MyMalloc(sizeof(rb_dlink_list) * HOST_MAX);
-       resvTable = MyMalloc(sizeof(rb_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
@@ -317,7 +322,7 @@ del_from_client_hash(const char *name, struct Client *client_p)
 }
 
 /* del_from_channel_hash()
- * 
+ *
  * removes a channel from the channel hash table
  */
 void
@@ -468,11 +473,11 @@ find_server(struct Client *source_p, const char *name)
        struct Client *target_p;
        rb_dlink_node *ptr;
        unsigned int hashv;
-  
+
        if(EmptyString(name))
                return NULL;
 
-       if((source_p == NULL || !MyClient(source_p)) && 
+       if((source_p == NULL || !MyClient(source_p)) &&
           IsDigit(*name) && strlen(name) == 3)
        {
                target_p = find_id(name);
@@ -600,7 +605,7 @@ get_or_create_channel(struct Client *client_p, const char *chname, int *isnew)
 
        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 */
 
        rb_dlinkAddAlloc(chptr, &channelTable[hashv]);
 
@@ -628,7 +633,7 @@ hash_find_resv(const char *name)
        {
                aconf = ptr->data;
 
-               if(!irccmp(name, aconf->name))
+               if(!irccmp(name, aconf->host))
                {
                        aconf->port++;
                        return aconf;
@@ -660,19 +665,52 @@ clear_resv_hash(void)
        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++)
        {
@@ -681,10 +719,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++)
        {
@@ -693,7 +735,7 @@ output_hash(struct Client *source_p, const char *name, int length, int *counts,
                                i, counts[i]);
        }
 }
-       
+
 
 static void
 count_hash(struct Client *source_p, rb_dlink_list *table, int length, const char *name)
@@ -703,7 +745,7 @@ count_hash(struct Client *source_p, rb_dlink_list *table, int length, const char
        int i;
 
        memset(counts, 0, sizeof(counts));
-       
+
        for(i = 0; i < length; i++)
        {
                if(rb_dlink_list_length(&table[i]) >= 10)
@@ -728,4 +770,4 @@ hash_stats(struct Client *source_p)
        count_hash(source_p, idTable, U_MAX, "ID");
        sendto_one_numeric(source_p, RPL_STATSDEBUG, "B :--");
        count_hash(source_p, hostTable, HOST_MAX, "Hostname");
-}      
+}