Author: ZenShadow
[ircu2.10.12-pk.git] / ircd / IPcheck.c
index f97a49efbe016e949b45ebd90d753b77b65928f2..6a5b5585e1f18ef0c4332a0edf8b4e1b6c2cfe9a 100644 (file)
  *
  * $Id$
  *
- * 
- * This file should be edited in a window with a width of 141 characters
- * ick
  */
+
+/*----------------------------------------------------------------------------
+ * Platform Includes
+ *--------------------------------------------------------------------------*/
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+
+
+/*----------------------------------------------------------------------------
+ * Application Includes
+ *--------------------------------------------------------------------------*/
 #include "IPcheck.h"
 #include "client.h"
 #include "ircd.h"
-#include "numnicks.h"       /* NumNick, NumServ (GODMODE) */
-#include "querycmds.h"      /* UserStats */
+#include "numnicks.h"
 #include "ircd_alloc.h"
-#include "s_bsd.h"          /* SetIPChecked */
-#include "s_user.h"         /* TARGET_DELAY */
+#include "msg.h"
+#include "s_bsd.h"
+#include "s_debug.h"
+#include "s_user.h"
 #include "send.h"
-#include "struct.h"
 
-#include <assert.h>
-#include <stdio.h>          /* NULL ... bleah */
 
-/*
- * IP number and last targets of a user that just disconnected.
- * Used to allow a user that shortly disconnected to rejoin
- * the channels he/she was on.
- */
-struct ip_targets_st {
-  struct in_addr ip;
-  unsigned char free_targets;
+/*----------------------------------------------------------------------------
+ * Data Structures (should be moved to IPcheck.h)
+ *--------------------------------------------------------------------------*/
+typedef struct IPTargetEntry {
+  int           count;
   unsigned char targets[MAXTARGETS];
-};
-
-/* We keep one IPregistry for each IP number (for both, remote and local clients) */
-struct IPregistry {
-  union {
-    struct in_addr ip;          /* The IP number of the registry entry. */
-    struct ip_targets_st *ptr;  /* a list of targets */
-  } ip_targets;
-  unsigned int last_connect:16; /* Time of last connect (attempt), see BITMASK
-                                 * below, or time of last disconnect when 
-                                 * `connected' is zero. */
-  unsigned int connected:8;     /* Used for IP# throttling: Number of
-                                 * currently on-line clients with this IP */
-  unsigned int connect_attempts:4;      /* Used for connect speed throttling: Number of clients that connected with this IP number
-                                           or `15' when then real value is >= 15.  This value is only valid when the last connect
-                                           was less then IPCHECK_CLONE_PERIOD seconds ago, it should considered to be 0 otherwise. */
-  unsigned int free_targets:4;  /* Number of free targets that the next local client will inherit on connect,
-                                   or HAS_TARGETS_MAGIC when ip_targets.ptr is a pointer to a ip_targets_st. */
-};
-
-struct IPregistry_vector {
-  unsigned short length;
-  unsigned short allocated_length;
-  struct IPregistry *vector;
-};
-
-#define HASHTABSIZE 0x2000      /* Must be power of 2 */
-static struct IPregistry_vector IPregistry_hashtable[HASHTABSIZE];
+} iptarget_entry_t;
+
+typedef struct IPRegistryEntry {
+  struct IPRegistryEntry *next;
+  struct IPTargetEntry   *target;
+  unsigned int             addr;
+  time_t                  last_connect;
+  unsigned char            connected;
+  unsigned char            attempts;
+} ip_reg_entry_t;
 
-/*
- * Calculate a `hash' value between 0 and HASHTABSIZE, from the internet address `in_addr'.
- * Apply it immedeately to the table, effectively hiding the table itself.
- */
-#define CALCULATE_HASH(in_addr) \
-  struct IPregistry_vector *hash; \
-  do { unsigned int ip = (in_addr).s_addr; \
-       hash = &IPregistry_hashtable[((ip >> 14) + (ip >> 7) + ip) & (HASHTABSIZE - 1)]; } while(0)
 
 /*
- * Fit `now' in an unsigned short, the advantage is that we use less memory
- * `struct IPregistry::last_connect' can be smaller while the only disadvantage 
- * is that if someone reconnects after exactly 18 hours and 12 minutes, and NOBODY with the
- * same _hash_ value for this IP-number did disconnect in the meantime, then the server
- * will think he reconnected immedeately. In other words: No disadvantage at all.
+ * Hash table for IPv4 address registry
+ *
+ * Hash table size must be a power of 2
+ * Use 64K hash table to conserve memory
  */
-#define BITMASK 0xffff          /* Same number of bits as `struct IPregistry::last_connect' */
-#define NOW ((unsigned short)(CurrentTime & BITMASK))
-#define CONNECTED_SINCE(x) ((unsigned short)((CurrentTime & BITMASK) - (x)->last_connect))
+/*----------------------------------------------------------------------------
+ * Compile-time Configuration
+ *--------------------------------------------------------------------------*/
+#define IP_REGISTRY_TABLE_SIZE 0x10000
+#define MASK_16                0xffff
 
 #define IPCHECK_CLONE_LIMIT 2
 #define IPCHECK_CLONE_PERIOD 20
-#define IPCHECK_CLONE_DELAY 600
+#define IPCHECK_CLONE_DELAY  600
 
-#define HAS_TARGETS_MAGIC 15
-#define HAS_TARGETS(entry) ((entry)->free_targets == HAS_TARGETS_MAGIC)
 
-#if STARTTARGETS >= HAS_TARGETS_MAGIC
-#error "That doesn't fit in 4 bits, does it?"
-#endif
+/*----------------------------------------------------------------------------
+ * Handy Macros
+ *--------------------------------------------------------------------------*/
+#define NOW (CurrentTime)
+#define CONNECTED_SINCE(x) (NOW - (x->last_connect))
 
-/* IP(entry) returns the `struct in_addr' of the IPregistry. */
-#define IP(entry) (HAS_TARGETS(entry) ? (entry)->ip_targets.ptr->ip : (entry)->ip_targets.ip)
-#define FREE_TARGETS(entry) (HAS_TARGETS(entry) ? (entry)->ip_targets.ptr->free_targets : (entry)->free_targets)
 
-static unsigned short count = 10000, average_length = 4;
+/*----------------------------------------------------------------------------
+ * Global Data (ugly!)
+ *--------------------------------------------------------------------------*/
+static ip_reg_entry_t *hashTable[IP_REGISTRY_TABLE_SIZE];
+static ip_reg_entry_t *freeList = 0;
 
-static struct IPregistry *IPregistry_add(struct IPregistry_vector *iprv)
-{
-  assert(0 != iprv);
-  if (iprv->length == iprv->allocated_length)
-  {
-    iprv->allocated_length += 4;
-    if (iprv->vector) {
-      iprv->vector = 
-              (struct IPregistry*) MyRealloc(iprv->vector,
-                       iprv->allocated_length * sizeof(struct IPregistry));
-    }
-    else {
-      iprv->vector = 
-              (struct IPregistry*) MyMalloc(
-                       iprv->allocated_length * sizeof(struct IPregistry));
-    }
-  }
-  return &iprv->vector[iprv->length++];
+
+/*----------------------------------------------------------------------------
+ * ip_registry_hash:  Create a hash key for an IP registry entry and return
+ *                    the value.  (Is unsigned int really a good type to give
+ *                    to the IP argument?  Ugly.  This should probably be a
+ *                    struct in_addr.  This is asking for trouble.  --ZS)
+ *--------------------------------------------------------------------------*/
+static unsigned int ip_registry_hash(unsigned int ip) {
+  return ((ip >> 16) ^ ip) & (IP_REGISTRY_TABLE_SIZE - 1);
 }
 
-static struct IPregistry *IPregistry_find(struct IPregistry_vector *iprv,
-    struct in_addr ip)
-{
-  if (iprv->length > 0)
-  {
-    struct IPregistry *i, *end = &iprv->vector[iprv->length];
-    for (i = &iprv->vector[0]; i < end; ++i)
-      if (IP(i).s_addr == ip.s_addr)
-        return i;
+
+/*----------------------------------------------------------------------------
+ * ip_registry_find:  Find a given IP registry entry and return it.
+ *--------------------------------------------------------------------------*/
+static ip_reg_entry_t *ip_registry_find(unsigned int ip) {
+  ip_reg_entry_t *entry;
+
+  for (entry = hashTable[ip_registry_hash(ip)]; entry; entry = entry->next) {
+    if (entry->addr == ip)
+      break;
   }
-  return NULL;
+
+  return entry;
 }
 
-static struct IPregistry *IPregistry_find_with_expire(struct IPregistry_vector
-    *iprv, struct in_addr ip)
-{
-  struct IPregistry *last;
-  struct IPregistry *curr;
-  struct IPregistry *retval = NULL;
 
-  /*
-   * if the vector is empty, IPcheck_disconnect will cause the server
-   * to core when NDEBUG is defined
-   */
-  if (iprv->length < 1)
-    return retval;
-
-  last = &iprv->vector[iprv->length - 1];
-
-  for (curr = &iprv->vector[0]; curr < last;)
-  {
-    if (IP(curr).s_addr == ip.s_addr)
-      /* `curr' is element we looked for */
-      retval = curr;
-    else if (curr->connected == 0)
-    {
-      if (CONNECTED_SINCE(curr) > 600U) /* Don't touch this number, it has statistical significance */
-      {
-        /* `curr' expired */
-        if (HAS_TARGETS(curr))
-          MyFree(curr->ip_targets.ptr);
-        *curr = *last--;
-        iprv->length--;
-        if (--count == 0)
-        {
-          /* Make ever 10000 disconnects an estimation of the average vector length */
-          count = 10000;
-          average_length =
-              (UserStats.clients + UserStats.unknowns + UserStats.local_servers) / HASHTABSIZE;
-        }
-        /* Now check the new element (last) that was moved to this position */
-        continue;
-      }
-      else if (CONNECTED_SINCE(curr) > 120U && HAS_TARGETS(curr))
-      {
-        /* Expire storage of targets */
-        struct in_addr ip1 = curr->ip_targets.ptr->ip;
-        curr->free_targets = curr->ip_targets.ptr->free_targets;
-        MyFree(curr->ip_targets.ptr);
-        curr->ip_targets.ip = ip1;
+/*----------------------------------------------------------------------------
+ * ip_registry_add:  Add an entry to the IP registry
+ *--------------------------------------------------------------------------*/
+static void ip_registry_add(ip_reg_entry_t *entry) {
+  unsigned int bucket = ip_registry_hash(entry->addr);
+
+  entry->next = hashTable[bucket];
+  hashTable[bucket] = entry;
+}
+  
+
+/*----------------------------------------------------------------------------
+ * ip_registry_remove:  Remove an entry from the IP registry
+ *--------------------------------------------------------------------------*/
+static void ip_registry_remove(ip_reg_entry_t *entry) {
+  unsigned int bucket = ip_registry_hash(entry->addr);
+
+  if (hashTable[bucket] == entry)
+    hashTable[bucket] = entry->next;
+  else {
+    ip_reg_entry_t *prev;
+
+    for (prev = hashTable[bucket]; prev; prev = prev->next) {
+      if (prev->next == entry) {
+        prev->next = entry->next;
+        break;
       }
     }
-    /* Did not expire, check next element */
-    ++curr;
   }
-  /* Now check the last element in the list (curr == last) */
-  if (IP(curr).s_addr == ip.s_addr)
-    /* `curr' is element we looked for */
-    retval = curr;
-  else if (curr->connected == 0)
-  {
-    if (CONNECTED_SINCE(curr) > 600U)   /* Don't touch this number, it has statistical significance */
-    {
-      /* `curr' expired */
-      if (HAS_TARGETS(curr))
-        MyFree(curr->ip_targets.ptr);
-      iprv->length--;
-      if (--count == 0)
-      {
-        /* Make ever 10000 disconnects an estimation of the average vector length */
-        count = 10000;
-        average_length =
-            (UserStats.clients + UserStats.unknowns + UserStats.local_servers) / HASHTABSIZE;
-      }
-    }
-    else if (CONNECTED_SINCE(curr) > 120U && HAS_TARGETS(curr))
-    {
-      /* Expire storage of targets */
-      struct in_addr ip1 = curr->ip_targets.ptr->ip;
-      curr->free_targets = curr->ip_targets.ptr->free_targets;
-      MyFree(curr->ip_targets.ptr);
-      curr->ip_targets.ip = ip1;
-    }
+}
+
+/*----------------------------------------------------------------------------
+ * ip_registry_new_entry():  Creates and initializes an IP Registry entry.
+ *                           NOW ALSO ADDS IT TO THE LIST! --ZS
+ *--------------------------------------------------------------------------*/
+static ip_reg_entry_t *ip_registry_new_entry(unsigned int addr, int attempt) {
+  ip_reg_entry_t *entry = freeList;
+
+  if (entry)
+    freeList = entry->next;
+  else
+    entry = (ip_reg_entry_t *)MyMalloc(sizeof(ip_reg_entry_t));
+
+  assert(0 != entry);
+
+  memset(entry, 0, sizeof(ip_reg_entry_t));
+  entry->last_connect = NOW;     /* Seconds since last connect attempt */
+  entry->connected    = 1;       /* connected clients for this IP */
+  entry->attempts     = attempt; /* Number attempts for this IP        */
+  entry->addr         = addr;    /* Entry's IP Address                 */
+
+  ip_registry_add(entry);
+
+  return entry;
+}
+
+
+/*----------------------------------------------------------------------------
+ * ip_registry_delete_entry:  Frees an entry and adds the structure to a list
+ *                            of free structures.  (We should probably reclaim
+ *                            the freelist every once in a while!  This is
+ *                            potentially a way to DoS the server...  -ZS)
+ *--------------------------------------------------------------------------*/
+static void ip_registry_delete_entry(ip_reg_entry_t *entry) {
+  if (entry->target)
+    MyFree(entry->target);
+
+  entry->next = freeList;
+  freeList = entry;
+}
+
+
+/*----------------------------------------------------------------------------
+ * ip_registry_update_free_targets:  
+ *--------------------------------------------------------------------------*/
+static unsigned int ip_registry_update_free_targets(ip_reg_entry_t  *entry) {
+  unsigned int free_targets = STARTTARGETS;
+
+  if (entry->target) {
+    free_targets = (entry->target->count +
+                   (CONNECTED_SINCE(entry) / TARGET_DELAY));
+
+    if (free_targets > STARTTARGETS)
+      free_targets = STARTTARGETS;
+
+    entry->target->count = free_targets;
   }
-  /* Do we need to shrink the vector? */
-  if (iprv->allocated_length > average_length
-      && iprv->allocated_length - iprv->length >= 4)
-  {
-    struct IPregistry *newpos;
-    iprv->allocated_length = iprv->length;
-    newpos =
-        (struct IPregistry *)MyRealloc(iprv->vector,
-        iprv->allocated_length * sizeof(struct IPregistry));
-    if (newpos != iprv->vector) /* Is this ever true? */
-    {
-      retval =
-          (struct IPregistry *)((char *)retval + ((char *)newpos -
-          (char *)iprv->vector));
-      iprv->vector = newpos;
-    }
+
+  return free_targets;
+}
+
+
+/*----------------------------------------------------------------------------
+ * ip_registry_expire_entry:  expire an IP entry if it needs to be.  If an
+ *                            entry isn't expired, then also check the target
+ *                            list to see if it needs to be expired.
+ *--------------------------------------------------------------------------*/
+static void ip_registry_expire_entry(ip_reg_entry_t *entry) {
+  /*
+   * Don't touch this number, it has statistical significance
+   * XXX - blah blah blah
+   * ZS - Just -what- statistical significance does it -have-?
+   */
+  if (CONNECTED_SINCE(entry) > 600) {
+    ip_registry_remove(entry);
+    ip_registry_delete_entry(entry);
+  } else if (CONNECTED_SINCE(entry) > 120 && 0 != entry->target) {
+    MyFree(entry->target);
+    entry->target = 0;
   }
-  return retval;
 }
 
-static void reset_connect_time(struct IPregistry *entry)
-{
-  unsigned int previous_free_targets;
-
-  /* Apply aging */
-  previous_free_targets =
-      FREE_TARGETS(entry) + CONNECTED_SINCE(entry) / TARGET_DELAY;
-  if (previous_free_targets > STARTTARGETS)
-    previous_free_targets = STARTTARGETS;
-  if (HAS_TARGETS(entry))
-    entry->ip_targets.ptr->free_targets = previous_free_targets;
-  else
-    entry->free_targets = previous_free_targets;
 
-  entry->last_connect = NOW;
+/*----------------------------------------------------------------------------
+ * ip_registry_expire:  Expire all of the needed entries in the hash table
+ *--------------------------------------------------------------------------*/
+void ip_registry_expire(void) {
+  ip_reg_entry_t *entry;
+  ip_reg_entry_t *entry_next;
+  static time_t   next_expire = 0;
+  int i;
+
+  /* Only do this if we're ready to */
+  if (next_expire >= CurrentTime)
+    return;
+
+  for (i = 0; i < IP_REGISTRY_TABLE_SIZE; ++i) {
+    for (entry = hashTable[i]; entry; entry = entry_next) {
+      entry_next = entry->next;
+      if (0 == entry->connected)
+        ip_registry_expire_entry(entry);
+    }
+  }
+
+  next_expire = CurrentTime + 60;
 }
 
-/*
+
+/*----------------------------------------------------------------------------
  * IPcheck_local_connect
  *
  * Event:
@@ -276,9 +271,10 @@ static void reset_connect_time(struct IPregistry *entry)
  *
  * Throttling:
  *
- * A connection should be rejected when a connection from the same IP number was
- * received IPCHECK_CLONE_LIMIT times before this connect attempt, with
- * reconnect intervals of IPCHECK_CLONE_PERIOD seconds or less.
+ * A connection should be rejected when a connection from the same IP
+ * number was received IPCHECK_CLONE_LIMIT times before this connect
+ * attempt, with reconnect intervals of IPCHECK_CLONE_PERIOD seconds
+ * or less.
  *
  * Free target inheritance:
  *
@@ -287,53 +283,60 @@ static void reset_connect_time(struct IPregistry *entry)
  * structure, or left at STARTTARGETS.  This can be done by changing
  * cptr->nexttarget to be `now - (TARGET_DELAY * (FREE_TARGETS - 1))',
  * where FREE_TARGETS may range from 0 till STARTTARGETS.
- */
-int IPcheck_local_connect(struct in_addr a, time_t* next_target_out)
+ *--------------------------------------------------------------------------*/
+int ip_registry_check_local(unsigned int addr, time_t *next_target_out)
 {
-  struct IPregistry *entry;
-  CALCULATE_HASH(a);
+  ip_reg_entry_t *entry        = ip_registry_find(addr);
+  unsigned int free_targets = STARTTARGETS;
   assert(0 != next_target_out);
 
-  if (!(entry = IPregistry_find(hash, a)))
-  {
-    entry = IPregistry_add(hash);
-    entry->ip_targets.ip = a;          /* The IP number of registry entry */
-    entry->last_connect = NOW;          /* Seconds since last connect attempt */
-    entry->connected = 1;               /* connected clients for this IP */
-    entry->connect_attempts = 1;        /* Number attempts for this IP */
-    entry->free_targets = STARTTARGETS; /* free targets a client gets */
+  if (0 == entry) {
+    entry = ip_registry_new_entry(addr, 1);
     return 1;
   }
-  /* Note that this also connects server connects.
-   * It is hard and not interesting, to change that.
-   *
-   * Don't allow more then 255 connects from one IP number, ever
-   */
+
+  /* Do not allow more than 255 connects from a single IP, EVER. */
   if (0 == ++entry->connected)
     return 0;
 
+  /* If our threshhold has elapsed, reset the counter so we don't throttle */
   if (CONNECTED_SINCE(entry) > IPCHECK_CLONE_PERIOD)
-    entry->connect_attempts = 0;
-
-  reset_connect_time(entry);
+    entry->attempts = 0;
+  else if (0 == ++entry->attempts)
+    --entry->attempts;  /* Disallow overflow */
 
-  if (0 == ++entry->connect_attempts)   /* Check for overflow */
-    --entry->connect_attempts;
-
-  if (entry->connect_attempts <= IPCHECK_CLONE_LIMIT)
-    *next_target_out = CurrentTime - (TARGET_DELAY * (FREE_TARGETS(entry) - 1));
+  entry->last_connect = NOW;
+  free_targets = ip_registry_update_free_targets(entry);
 
-  /* Don't refuse connection when we just rebooted the server */
-  else if (CurrentTime - me.since > IPCHECK_CLONE_DELAY)
-#ifndef NOTHROTTLE 
-    return 0;
-#else
+  if (entry->attempts < IPCHECK_CLONE_LIMIT && next_target_out)
+      *next_target_out = CurrentTime - (TARGET_DELAY * free_targets - 1);
+  else if ((CurrentTime - me.since) > IPCHECK_CLONE_DELAY) {
+#ifdef NOTHROTTLE 
     return 1;
+#else
+    --entry->connected;
+    return 0;
 #endif        
+  }
+
   return 1;
 }
 
-/*
+
+/*----------------------------------------------------------------------------
+ * ip_registry_local_connect
+ *
+ * Does anything that needs to be done once we actually have a client
+ * structure to play with on a local connection that passed the IPcheck test.
+ *--------------------------------------------------------------------------*/
+void ip_registry_local_connect(struct Client *cptr) {
+  assert(0 != cptr);
+  SetIPChecked(cptr);
+}
+
+
+/*----------------------------------------------------------------------------
  * IPcheck_remote_connect
  *
  * Event:
@@ -342,46 +345,41 @@ int IPcheck_local_connect(struct in_addr a, time_t* next_target_out)
  *
  * Action:
  *   Update the IPcheck registry.
- *   Return -1 on failure, 0 on success.
- */
-int IPcheck_remote_connect(struct Client *cptr, const char *hostname,
-    int is_burst)
-{
-  struct IPregistry *entry;
-  CALCULATE_HASH(cptr->ip);
-  SetIPChecked(cptr);           /* Mark that we did add/update an IPregistry entry */
-  if (!(entry = IPregistry_find(hash, cptr->ip)))
-  {
-    entry = IPregistry_add(hash);
-    entry->ip_targets.ip = cptr->ip;    /* The IP number of registry entry */
-    entry->last_connect = NOW;  /* Seconds since last connect (attempt) */
-    entry->connected = 1;       /* Number of currently connected clients with this IP number */
-    entry->connect_attempts = is_burst ? 1 : 0; /* Number of clients that connected with this IP number */
-    entry->free_targets = STARTTARGETS; /* Number of free targets that a client gets on connect */
-  }
-  else
-  {
-#ifdef GODMODE
-    sendto_one(cptr,
-        "%s NOTICE %s%s :I saw your face before my friend (connected: %u; connect_attempts %u; free_targets %u)",
-        NumServ(&me), NumNick(cptr), entry->connected, entry->connect_attempts,
-        FREE_TARGETS(entry));
-#endif
-    if (++(entry->connected) == 0)      /* Don't allow more then 255 connects from one IP number, ever */
-      return -1;
+ *   Return 0 on failure, 1 on success.
+ *--------------------------------------------------------------------------*/
+int ip_registry_check_remote(struct Client* cptr, int is_burst) {
+  ip_reg_entry_t *entry;
+
+  assert(cptr);
+
+  entry = ip_registry_find(cptr->ip.s_addr);
+
+  SetIPChecked(cptr);
+
+  if (0 == entry)
+    entry = ip_registry_new_entry(cptr->ip.s_addr, (is_burst ? 0 : 1));
+  else {
+    /* NEVER more than 255 connections. */
+    if (0 == ++entry->connected)
+      return 0;
+
+    /* Make sure we don't bounce if our threshhold has expired */
     if (CONNECTED_SINCE(entry) > IPCHECK_CLONE_PERIOD)
-      entry->connect_attempts = 0;
-    if (!is_burst)
-    {
-      if (++(entry->connect_attempts) == 0)     /* Check for overflow */
-        --(entry->connect_attempts);
-      reset_connect_time(entry);
+      entry->attempts = 0;
+
+    /* If we're not part of a burst, go ahead and process the rest */
+    if (!is_burst) {
+      if (0 == ++entry->attempts)
+        --entry->attempts;  /* Overflows are bad, mmmkay? */
+      ip_registry_update_free_targets(entry);
+      entry->last_connect = NOW;
     }
   }
-  return 0;
+
+  return 1;
 }
 
-/*
+/*----------------------------------------------------------------------------
  * IPcheck_connect_fail
  *
  * Event:
@@ -391,40 +389,50 @@ int IPcheck_remote_connect(struct Client *cptr, const char *hostname,
  *   Neutralize the effect of calling IPcheck_local_connect, in such
  *   a way that the client won't be penalized when trying to reconnect
  *   again.
- */
-void IPcheck_connect_fail(struct in_addr a)
-{
-  struct IPregistry *entry;
-  CALCULATE_HASH(a);
-  if ((entry = IPregistry_find(hash, a)))
-    --entry->connect_attempts;
+ *--------------------------------------------------------------------------*/
+void ip_registry_connect_fail(unsigned int addr) {
+  ip_reg_entry_t *entry = ip_registry_find(addr);
+
+  if (entry)
+    --entry->attempts;
 }
 
-/*
+
+/*----------------------------------------------------------------------------
  * IPcheck_connect_succeeded
  *
  * Event:
  *   A client succeeded to finish the registration.
  *
  * Finish IPcheck registration of a successfully, locally connected client.
- */
-void IPcheck_connect_succeeded(struct Client *cptr)
-{
-  struct IPregistry *entry;
-  const char *tr = "";
-  CALCULATE_HASH(cptr->ip);
-  entry = IPregistry_find(hash, cptr->ip);
-  if (HAS_TARGETS(entry))
-  {
-    memcpy(cptr->targets, entry->ip_targets.ptr->targets, MAXTARGETS);
+ *--------------------------------------------------------------------------*/
+void ip_registry_connect_succeeded(struct Client *cptr) {
+  const char     *tr           = "";
+  unsigned int free_targets     = STARTTARGETS;
+  ip_reg_entry_t *entry;
+
+  assert(cptr);
+
+  entry = ip_registry_find(cptr->ip.s_addr);
+
+  if (!entry) {
+    Debug((DEBUG_ERROR, "Missing registry entry for: %s", cptr->sock_ip));
+    return;
+  }
+
+  if (entry->target) {
+    memcpy(cptr->targets, entry->target->targets, MAXTARGETS);
+    free_targets = entry->target->count;
     tr = " tr";
   }
-  sendto_one(cptr, ":%s NOTICE %s :on %u ca %u(%u) ft %u(%u)%s",
-      me.name, cptr->name, entry->connected, entry->connect_attempts,
-      IPCHECK_CLONE_LIMIT, FREE_TARGETS(entry), STARTTARGETS, tr);
+
+  sendcmdto_one(&me, CMD_NOTICE, cptr, "%C :on %u ca %u(%u) ft %u(%u)%s",
+               cptr, entry->connected, entry->attempts, IPCHECK_CLONE_LIMIT,
+               free_targets, STARTTARGETS, tr);
 }
 
-/*
+
+/*----------------------------------------------------------------------------
  * IPcheck_disconnect
  *
  * Event:
@@ -434,94 +442,86 @@ void IPcheck_connect_succeeded(struct Client *cptr)
  *   Update the IPcheck registry.
  *   Remove all expired IPregistry structures from the hash bucket
  *     that belongs to this clients IP number.
- */
-void IPcheck_disconnect(struct Client *cptr)
-{
-  struct IPregistry *entry;
-  CALCULATE_HASH(cptr->ip);
-  entry = IPregistry_find_with_expire(hash, cptr->ip);
-  if (0 == entry) {
-    /*
-     * trying to find an entry for a server causes this to happen,
-     * servers should never have FLAGS_IPCHECK set
-     */
-    assert(0 != entry);
+ *--------------------------------------------------------------------------*/
+void ip_registry_disconnect(struct Client *cptr) {
+  ip_reg_entry_t *entry;
+
+  assert(0 != cptr);
+
+  if (!IsIPChecked(cptr))
     return;
-  }
+
+  entry = ip_registry_find(cptr->ip.s_addr);
+
+  /* Entry is probably a server if this happens. */
+  if (0 == entry)
+    return;
+
+
   /*
-   * If this was the last one, set `last_connect' to disconnect time (used for expiration)
+   * If this was the last one, set `last_connect' to disconnect time
+   * (used for expiration)   Note that we reset attempts here as well if our
+   * threshhold hasn't been crossed.
    */
-  if (--(entry->connected) == 0) {
+  if (0 == --entry->connected) {
     if (CONNECTED_SINCE(entry) > IPCHECK_CLONE_LIMIT * IPCHECK_CLONE_PERIOD)
-      /*
-       * Otherwise we'd penetalize for this old value if the client reconnects within 20 seconds
-       */
-      entry->connect_attempts = 0;
-    reset_connect_time(entry);
+      entry->attempts = 0;
+    ip_registry_update_free_targets(entry);
+    entry->last_connect = NOW;
   }
+
+
   if (MyConnect(cptr)) {
-    unsigned int inheritance;
-    /*
-     * Copy the clients targets
-     */
-    if (HAS_TARGETS(entry)) {
-      entry->free_targets = entry->ip_targets.ptr->free_targets;
-      MyFree(entry->ip_targets.ptr);
+    unsigned int free_targets;
+
+    if (0 == entry->target) {
+      entry->target = (iptarget_entry_t *)MyMalloc(sizeof(iptarget_entry_t));
+      assert(0 != entry->target);
+      entry->target->count = STARTTARGETS;
     }
-    entry->ip_targets.ptr =
-        (struct ip_targets_st*) MyMalloc(sizeof(struct ip_targets_st));
-
-    assert(0 != entry->ip_targets.ptr);
-    entry->ip_targets.ptr->ip = cptr->ip;
-    entry->ip_targets.ptr->free_targets = entry->free_targets;
-    entry->free_targets = HAS_TARGETS_MAGIC;
-    memcpy(entry->ip_targets.ptr->targets, cptr->targets, MAXTARGETS);
+    memcpy(entry->target->targets, cptr->targets, MAXTARGETS);
+
     /*
-     * This calculation can be pretty unfair towards large multi-user hosts, but
-     * there is "nothing" we can do without also allowing spam bots to send more
-     * messages or by drastically increasing the ammount of memory used in the IPregistry.
+     * This calculation can be pretty unfair towards large multi-user hosts,
+     * but there is "nothing" we can do without also allowing spam bots to
+     * send more messages or by drastically increasing the ammount of memory
+     * used in the IPregistry.
      *
-     * The problem is that when a client disconnects, leaving no free targets, then
-     * the next client from that IP number has to pay for it (getting no free targets).
-     * But ALSO the next client, and the next client, and the next client etc - until
-     * another client disconnects that DOES leave free targets.  The reason for this
-     * is that if there are 10 SPAM bots, and they all disconnect at once, then they
-     * ALL should get no free targets when reconnecting.  We'd need to store an entry
-     * per client (instead of per IP number) to avoid this.
-     */
-    if (cptr->nexttarget <= CurrentTime)
-        /*
-         * Number of free targets
+     * The problem is that when a client disconnects, leaving no free targets,
+     * then the next client from that IP number has to pay for it (getting no
+     * free targets).  But ALSO the next client, and the next client, and the
+     * next client etc - until another client disconnects that DOES leave free
+     * targets.  The reason for this is that if there are 10 SPAM bots, and
+     * they all disconnect at once, then they ALL should get no free targets
+     * when reconnecting.  We'd need to store an entry per client (instead of
+     * per IP number) to avoid this.  
          */
-      inheritance = (CurrentTime - cptr->nexttarget) / TARGET_DELAY + 1;
+    if (cptr->nexttarget < CurrentTime)
+      free_targets = (CurrentTime - cptr->nexttarget) / TARGET_DELAY + 1;
     else
-      inheritance = 0;
-    /*
-     * Add bonus, this is pretty fuzzy, but it will help in some cases.
-     */
-    if (CurrentTime - cptr->firsttime > 600)
-      /*
-       * Was longer then 10 minutes online?
-       */
-      inheritance += (CurrentTime - cptr->firsttime - 600) / TARGET_DELAY;
-    /*
-     * Finally, store smallest value for Judgement Day
-     */
-    if (inheritance < entry->ip_targets.ptr->free_targets)
-      entry->ip_targets.ptr->free_targets = inheritance;
+      free_targets = 0;
+
+    /* Add bonus, this is pretty fuzzy, but it will help in some cases. */
+    if ((CurrentTime - cptr->firsttime) > 600)
+      free_targets += (CurrentTime - cptr->firsttime - 600) / TARGET_DELAY;
+
+    /* Finally, store smallest value for Judgement Day */
+    if (free_targets < entry->target->count)
+      entry->target->count = free_targets;
   }
 }
 
-/*
+/*----------------------------------------------------------------------------
  * IPcheck_nr
  *
  * Returns number of clients with the same IP number
- */
-unsigned short IPcheck_nr(struct Client *cptr)
-{
-  struct IPregistry *entry;
-  CALCULATE_HASH(cptr->ip);
-  entry = IPregistry_find(hash, cptr->ip);
-  return (entry ? entry->connected : 0);
+ *--------------------------------------------------------------------------*/
+int ip_registry_count(unsigned int addr) {
+  ip_reg_entry_t *entry = ip_registry_find(addr);
+  return (entry) ? entry->connected : 0;
 }
 
+
+
+
+