Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / numnicks.c
index cb3ae19cd98ed2c7ff2e01f3912586e0d0ff7b8a..cbba746a1d7b1d8b3c24497628a1074194fecad2 100644 (file)
@@ -18,6 +18,7 @@
  *
  * $Id$
  */
+#include "config.h"
 
 #include "numnicks.h"
 #include "client.h"
@@ -32,6 +33,7 @@
 
 #include <assert.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 
 
  * just-disconnected-client aren't confused with just-connected ones.
  */
 
-/*
- * when n2k comes, define this for more capacity
- */
-#undef  EXTENDED_NUMERICS
 
 /* These must be the same on ALL servers ! Do not change ! */
 
@@ -67,6 +65,7 @@
 #define NUMNICKBASE 64          /* (2 << NUMNICKLOG) */
 #define NUMNICKMASK 63          /* (NUMNICKBASE-1) */
 #define NN_MAX_SERVER 4096      /* (NUMNICKBASE * NUMNICKBASE) */
+#define NN_MAX_CLIENT 262144    /* NUMNICKBASE ^ 3 */
 
 /*
  * The internal counter for the 'XX' of local clients
@@ -169,19 +168,17 @@ struct Client* FindNServer(const char* numeric)
 struct Client* findNUser(const char* yxx)
 {
   struct Client* server = 0;
-  unsigned int index    = 0;
   if (5 == strlen(yxx)) {
     if (0 != (server = FindXNServer(yxx))) {
-      Debug((DEBUG_DEBUG, "findNUser: %s(%d) (%p)", yxx, 
-             base64toint(yxx + 2), server));
-      if ((index = base64toint(yxx + 2)) <= server->serv->nn_mask)
-        return server->serv->client_list[index];
+      Debug((DEBUG_DEBUG, "findNUser: %s(%d)", yxx, 
+             base64toint(yxx + 2) & cli_serv(server)->nn_mask));
+      return cli_serv(server)->client_list[base64toint(yxx + 2) & cli_serv(server)->nn_mask];
     }
   }
   else if (0 != (server = FindNServer(yxx))) {
-    index  = base64toint(yxx + 1) & server->serv->nn_mask;
-    Debug((DEBUG_DEBUG, "findNUser: %s(%d) (%p)", yxx, index, server));
-    return server->serv->client_list[index];
+    Debug((DEBUG_DEBUG, "findNUser: %s(%d)",
+           yxx, base64toint(yxx + 1) & cli_serv(server)->nn_mask));
+    return cli_serv(server)->client_list[base64toint(yxx + 1) & cli_serv(server)->nn_mask];
   }
   return 0;
 }
@@ -191,17 +188,9 @@ void RemoveYXXClient(struct Client* server, const char* yxx)
   assert(0 != server);
   assert(0 != yxx);
   if (*yxx) {
-    unsigned int index = 0;
-
-    if (strlen(yxx) < 3)
-      index = base64toint(yxx) & server->serv->nn_mask;
-    else
-      index = base64toint(yxx);
-
-    Debug((DEBUG_DEBUG, "RemoveYXXClient: %s(%d)", yxx, index));
-
-    if (index < (server->serv->nn_mask + 1))
-      server->serv->client_list[index] = NULL;
+    Debug((DEBUG_DEBUG, "RemoveYXXClient: %s(%d)", yxx,
+           base64toint(yxx) & cli_serv(server)->nn_mask));
+    cli_serv(server)->client_list[base64toint(yxx) & cli_serv(server)->nn_mask] = 0;
   }
 }
 
@@ -209,17 +198,17 @@ void SetServerYXX(struct Client* cptr, struct Client* server, const char* yxx)
 {
   unsigned int index;
   if (5 == strlen(yxx)) {
-    ircd_strncpy(server->yxx, yxx, 2);
-    ircd_strncpy(server->serv->nn_capacity, yxx + 2, 3);
+    ircd_strncpy(cli_yxx(server), yxx, 2);
+    ircd_strncpy(cli_serv(server)->nn_capacity, yxx + 2, 3);
   }
   else {
-    server->yxx[0]               = yxx[0];
-    server->serv->nn_capacity[0] = yxx[1];
-    server->serv->nn_capacity[1] = yxx[2];
+    (cli_yxx(server))[0]               = yxx[0];
+    cli_serv(server)->nn_capacity[0] = yxx[1];
+    cli_serv(server)->nn_capacity[1] = yxx[2];
   }
-  server->serv->nn_mask = base64toint(server->serv->nn_capacity);
+  cli_serv(server)->nn_mask = base64toint(cli_serv(server)->nn_capacity);
 
-  index = base64toint(server->yxx);
+  index = base64toint(cli_yxx(server));
   if (index >= lastNNServer)
     lastNNServer = index + 1;
   server_list[index] = server;
@@ -227,40 +216,33 @@ void SetServerYXX(struct Client* cptr, struct Client* server, const char* yxx)
   /* Note, exit_one_client uses the fact that `client_list' != NULL to
    * determine that SetServerYXX has been called - and then calls
    * ClearServerYXX. However, freeing the allocation happens in free_client() */
-  server->serv->client_list =
-      (struct Client**) MyCalloc(server->serv->nn_mask + 1, 
-                                   sizeof(struct Client*));
+  cli_serv(server)->client_list =
+      (struct Client**) MyCalloc(cli_serv(server)->nn_mask + 1, sizeof(struct Client*));
 }
 
 void SetYXXCapacity(struct Client* c, unsigned int capacity)
 {
-  unsigned int max_clients;
-#if defined(EXTENDED_NUMERICS)
-  max_clients = capacity - 1;
-  inttobase64(c->serv->nn_capacity, max_clients, 3); 
-#else
-  max_clients = 16;
+  unsigned int max_clients = 16;
   /* 
    * Calculate mask to be used for the maximum number of clients
    */
-  while (capacity >= max_clients)
-    max_clients = max_clients << 1;
+  while (max_clients < capacity)
+    max_clients <<= 1;
   /*
    * Sanity checks
    */
-  if (max_clients > NN_MAX_SERVER) {
+  if (max_clients > NN_MAX_CLIENT) {
     fprintf(stderr, "MAXCLIENTS (or MAXCONNECTIONS) is (at least) %d "
             "too large ! Please decrease this value.\n",
-             max_clients - NN_MAX_SERVER);
+             max_clients - NN_MAX_CLIENT);
     exit(-1);
   }
   --max_clients;
-  inttobase64(c->serv->nn_capacity, max_clients, 2); 
-#endif
-  c->serv->nn_mask = max_clients;       /* Our Numeric Nick mask */
-  c->serv->client_list = (struct Client**) MyCalloc(max_clients + 1, 
+  inttobase64(cli_serv(c)->nn_capacity, max_clients, 3); 
+  cli_serv(c)->nn_mask = max_clients;       /* Our Numeric Nick mask */
+  cli_serv(c)->client_list = (struct Client**) MyCalloc(max_clients + 1, 
                                                      sizeof(struct Client*));
-  server_list[base64toint(c->yxx)] = c;
+  server_list[base64toint(cli_yxx(c))] = c;
 }
 
 void SetYXXServerName(struct Client* c, unsigned int numeric)
@@ -268,12 +250,7 @@ void SetYXXServerName(struct Client* c, unsigned int numeric)
   assert(0 != c);
   assert(numeric < NN_MAX_SERVER);
 
-#if defined(EXTENDED_NUMERICS)
-  inttobase64(c->yxx, numeric, 2);
-#else
-  assert(numeric < NUMNICKBASE);
-  c->yxx[0] = convert2y[numeric];
-#endif
+  inttobase64(cli_yxx(c), numeric, 2);
   if (numeric >= lastNNServer)
     lastNNServer = numeric + 1;
   server_list[numeric] = c;
@@ -281,9 +258,9 @@ void SetYXXServerName(struct Client* c, unsigned int numeric)
 
 void ClearServerYXX(const struct Client *server)
 {
-  unsigned int index = base64toint(server->yxx);
+  unsigned int index = base64toint(cli_yxx(server));
   if (server_list[index] == server)     /* Sanity check */
-    server_list[index] = NULL;
+    server_list[index] = 0;
 }
 
 /*
@@ -292,39 +269,31 @@ void ClearServerYXX(const struct Client *server)
  * Register numeric of new, remote, client.
  * Add it to the appropriate client_list.
  */
-int SetRemoteNumNick(struct Client* acptr, const char *yxx)
+void SetRemoteNumNick(struct Client* acptr, const char *yxx)
 {
   struct Client** acptrp;
-  struct Client*  server = acptr->user->server;
-  unsigned int index = 0;
+  struct Client*  server = cli_user(acptr)->server;
  
   if (5 == strlen(yxx)) {
-    strcpy(acptr->yxx, yxx + 2);
-    index = base64toint(acptr->yxx);
+    strcpy(cli_yxx(acptr), yxx + 2);
   }
   else {
-    acptr->yxx[0] = *++yxx;
-    acptr->yxx[1] = *++yxx;
-    acptr->yxx[2] = 0;
-    index = base64toint(acptr->yxx) & server->serv->nn_mask;
+    (cli_yxx(acptr))[0] = *++yxx;
+    (cli_yxx(acptr))[1] = *++yxx;
+    (cli_yxx(acptr))[2] = 0;
   }
+  Debug((DEBUG_DEBUG, "SetRemoteNumNick: %s(%d)", cli_yxx(acptr), 
+         base64toint(cli_yxx(acptr)) & cli_serv(server)->nn_mask));
 
-  Debug((DEBUG_DEBUG, "SetRemoteNumNick: %s(%d)", acptr->yxx, index));
-  if (index > server->serv->nn_mask)
-    return 0;
-
-  assert(index <= server->serv->nn_mask);
-
-  acptrp = &server->serv->client_list[index];
+  acptrp = &(cli_serv(server))->client_list[base64toint(cli_yxx(acptr)) & cli_serv(server)->nn_mask];
   if (*acptrp) {
     /*
      * this exits the old client in the array, not the client
      * that is being set
      */
-    exit_client(acptr->from, *acptrp, server, "Numeric nick collision (Ghost)");
+    exit_client(cli_from(acptr), *acptrp, server, "Numeric nick collision (Ghost)");
   }
   *acptrp = acptr;
-  return 1;
 }
 
 
@@ -332,33 +301,31 @@ int SetRemoteNumNick(struct Client* acptr, const char *yxx)
  * SetLocalNumNick()
  *
  * Register numeric of new, local, client. Add it to our client_list.
+ * Muxtex needed if threaded
  */
-void SetLocalNumNick(struct Client *cptr)
+int SetLocalNumNick(struct Client *cptr)
 {
   static unsigned int last_nn     = 0;
-  struct Client**     client_list = me.serv->client_list;
-  unsigned int        capacity    = me.serv->nn_mask + 1;
+  struct Client**     client_list = cli_serv(&me)->client_list;
+  unsigned int        mask        = cli_serv(&me)->nn_mask;
   unsigned int        count       = 0;
 
-  assert(cptr->user->server == &me);
+  assert(cli_user(cptr)->server == &me);
 
-  while (client_list[last_nn]) {
-    if (++count == capacity) {
-      assert(count < capacity);
-      return;
+  while (client_list[last_nn & mask]) {
+    if (++count == NN_MAX_CLIENT) {
+      assert(count < NN_MAX_CLIENT);
+      return 0;
     }
-    if (++last_nn == capacity)
+    if (++last_nn == NN_MAX_CLIENT)
       last_nn = 0;
   }
-  client_list[last_nn] = cptr;  /* Reserve the numeric ! */
-
-#if defined(EXTENDED_NUMERICS)
-  inttobase64(cptr->yxx, last_nn, 3);
-#else
-  inttobase64(cptr->yxx, last_nn, 2);
-#endif
-  if (++last_nn == capacity)
+  client_list[last_nn & mask] = cptr;  /* Reserve the numeric ! */
+
+  inttobase64(cli_yxx(cptr), last_nn, 3);
+  if (++last_nn == NN_MAX_CLIENT)
     last_nn = 0;
+  return 1;
 }
 
 /* 
@@ -374,10 +341,10 @@ int markMatchexServer(const char *cmask, int minlen)
 
   for (i = 0; i < lastNNServer; i++) {
     if ((acptr = server_list[i])) {
-      if (matchexec(acptr->name, cmask, minlen))
-        acptr->flags &= ~FLAGS_MAP;
+      if (matchexec(cli_name(acptr), cmask, minlen))
+        cli_flags(acptr) &= ~FLAGS_MAP;
       else {
-        acptr->flags |= FLAGS_MAP;
+        cli_flags(acptr) |= FLAGS_MAP;
         cnt++;
       }
     }
@@ -393,7 +360,7 @@ struct Client* find_match_server(char *mask)
   if (!(BadPtr(mask))) {
     collapse(mask);
     for (i = 0; i < lastNNServer; i++) {
-      if ((acptr = server_list[i]) && (!match(mask, acptr->name)))
+      if ((acptr = server_list[i]) && (!match(mask, cli_name(acptr))))
         return acptr;
     }
   }