Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / IPcheck.c
index 88c45916def709996fb24332af3f40c166729b8a..e1f052d79c7cd6b4330e5e8a931da8f0fcf917fd 100644 (file)
  * This file should be edited in a window with a width of 141 characters
  * ick
  */
+#include "config.h"
+
 #include "IPcheck.h"
 #include "client.h"
 #include "ircd.h"
 #include "msg.h"
 #include "numnicks.h"       /* NumNick, NumServ (GODMODE) */
 #include "ircd_alloc.h"
+#include "ircd_events.h"
 #include "s_debug.h"        /* Debug */
 #include "s_user.h"         /* TARGET_DELAY */
 #include "send.h"
@@ -69,6 +72,8 @@ struct IPRegistryEntry {
 static struct IPRegistryEntry* hashTable[IP_REGISTRY_TABLE_SIZE];
 static struct IPRegistryEntry* freeList = 0;
 
+static struct Timer expireTimer;
+
 static unsigned int ip_registry_hash(unsigned int ip)
 {
   return ((ip >> 16) ^ ip) & (IP_REGISTRY_TABLE_SIZE - 1);
@@ -166,15 +171,16 @@ static void ip_registry_expire_entry(struct IPRegistryEntry* entry)
   }
 }
 
-/*
- * ip_registry_expire
- */
-static void ip_registry_expire()
+/* Callback to run an expiry of the IPcheck registry */
+static void ip_registry_expire(struct Event* ev)
 {
   int i;
   struct IPRegistryEntry* entry;
   struct IPRegistryEntry* entry_next;
 
+  assert(ET_EXPIRE == ev_type(ev));
+  assert(0 != ev_timer(ev));
+
   for (i = 0; i < IP_REGISTRY_TABLE_SIZE; ++i) {
     for (entry = hashTable[i]; entry; entry = entry_next) {
       entry_next = entry->next;
@@ -184,6 +190,16 @@ static void ip_registry_expire()
   }
 }
 
+/*
+ * IPcheck_init()
+ *
+ * Initializes the registry timer
+ */
+void IPcheck_init(void)
+{
+  timer_add(&expireTimer, ip_registry_expire, 0, TT_PERIODIC, 60);
+}
+
 /*
  * IPcheck_local_connect
  *
@@ -242,7 +258,7 @@ int ip_registry_check_local(unsigned int addr, time_t* next_target_out)
     if (next_target_out)
       *next_target_out = CurrentTime - (TARGET_DELAY * free_targets - 1);
   }
-  else if ((CurrentTime - me.since) > IPCHECK_CLONE_DELAY) {
+  else if ((CurrentTime - cli_since(&me)) > IPCHECK_CLONE_DELAY) {
     /* 
      * Don't refuse connection when we just rebooted the server
      */
@@ -339,16 +355,16 @@ void ip_registry_connect_succeeded(struct Client *cptr)
   struct IPRegistryEntry* entry = ip_registry_find((cli_ip(cptr)).s_addr);
 
   if (!entry) {
-    Debug((DEBUG_ERROR, "Missing registry entry for: %s", con_sock_ip(cptr)));
+    Debug((DEBUG_ERROR, "Missing registry entry for: %s", cli_sock_ip(cptr)));
     return;
   }
   if (entry->target) {
-    memcpy(con_targets(cptr), entry->target->targets, MAXTARGETS);
+    memcpy(cli_targets(cptr), entry->target->targets, MAXTARGETS);
     free_targets = entry->target->count;
     tr = " tr";
   }
-  sendcmdto_one(&me, CMD_NOTICE, cptr, ":on %u ca %u(%u) ft %u(%u)%s",
-               entry->connected, entry->attempts, IPCHECK_CLONE_LIMIT,
+  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);
 }
 
@@ -397,7 +413,7 @@ void ip_registry_disconnect(struct Client *cptr)
     }
     assert(0 != entry->target);
 
-    memcpy(entry->target->targets, con_targets(cptr), MAXTARGETS);
+    memcpy(entry->target->targets, cli_targets(cptr), 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
@@ -411,11 +427,11 @@ void ip_registry_disconnect(struct Client *cptr)
      * 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 (con_nexttarget(cptr) < CurrentTime) {
+    if (cli_nexttarget(cptr) < CurrentTime) {
         /*
          * Number of free targets
          */
-      free_targets = (CurrentTime - con_nexttarget(cptr)) / TARGET_DELAY + 1;
+      free_targets = (CurrentTime - cli_nexttarget(cptr)) / TARGET_DELAY + 1;
     }
     else
       free_targets = 0;
@@ -550,16 +566,5 @@ void IPcheck_disconnect(struct Client *cptr)
 unsigned short IPcheck_nr(struct Client *cptr)
 {
   assert(0 != cptr);
-  return ip_registry_count(cptr->ip.s_addr);
-}
-
-void IPcheck_expire()
-{
-  static time_t next_expire = 0;
-  if (next_expire < CurrentTime) {
-    ip_registry_expire();
-    next_expire = CurrentTime + 60;
-  }
+  return ip_registry_count(cli_ip(cptr).s_addr);
 }
-
-