Author: Thomas Helvey <tom.helvey@cox.net> Message: Add hasher, fix bugster
[ircu2.10.12-pk.git] / ircd / class.c
index 2f302161b7965dda4bb1b0b5137c214af66fdd12..bf099f964f5a66ed8ca05478e5a1129bc8654eb8 100644 (file)
  *
  * $Id$
  */
+#include "config.h"
+
 #include "class.h"
 #include "client.h"
 #include "ircd.h"
 #include "ircd_alloc.h"
+#include "ircd_features.h"
 #include "ircd_reply.h"
+#include "ircd_string.h"
 #include "list.h"
 #include "numeric.h"
 #include "s_conf.h"
@@ -35,7 +39,7 @@
 #define BAD_PING                ((unsigned int)-2)
 #define BAD_CLIENT_CLASS        ((unsigned int)-3)
 
-static struct ConnectionClass* connClassList;
+static struct ConnectionClass* connClassList = 0;
 static unsigned int connClassAllocCount;
 
 const struct ConnectionClass* get_class_list(void)
@@ -48,6 +52,7 @@ struct ConnectionClass* make_class(void)
   struct ConnectionClass *tmp;
 
   tmp = (struct ConnectionClass*) MyMalloc(sizeof(struct ConnectionClass));
+  tmp->ref_count = 1;
   assert(0 != tmp);
   ++connClassAllocCount;
   return tmp;
@@ -55,8 +60,11 @@ struct ConnectionClass* make_class(void)
 
 void free_class(struct ConnectionClass* p)
 {
-  if (p) {
+  if (p)
+  {
     assert(0 == p->valid);
+    if (p->cc_name)
+      MyFree(p->cc_name);
     MyFree(p);
     --connClassAllocCount;
   }
@@ -67,13 +75,15 @@ void free_class(struct ConnectionClass* p)
  */
 void init_class(void)
 {
-  connClassList = (struct ConnectionClass*) make_class();
-
-  ConClass(connClassList) = 0;
-  PingFreq(connClassList) = PINGFREQUENCY;
-  ConFreq(connClassList)  = CONNECTFREQUENCY;
-  MaxLinks(connClassList) = MAXIMUM_LINKS;
-  MaxSendq(connClassList) = DEFAULTMAXSENDQLENGTH;
+  if (!connClassList)
+    connClassList = (struct ConnectionClass*) make_class();
+
+  /* We had better not try and free this... */
+  ConClass(connClassList) = "default";
+  PingFreq(connClassList) = feature_int(FEAT_PINGFREQUENCY);
+  ConFreq(connClassList)  = feature_int(FEAT_CONNECTFREQUENCY);
+  MaxLinks(connClassList) = feature_int(FEAT_MAXIMUM_LINKS);
+  MaxSendq(connClassList) = feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
   connClassList->valid    = 1;
   Links(connClassList)    = 0;
   connClassList->next     = 0;
@@ -108,29 +118,32 @@ void class_delete_marked(void)
   Debug((DEBUG_DEBUG, "Class check:"));
 
   for (prev = cl = connClassList; cl; cl = prev->next) {
-    Debug((DEBUG_DEBUG, "Class %d : CF: %d PF: %d ML: %d LI: %d SQ: %d",
-           ConClass(cl), ConFreq(cl), PingFreq(cl), MaxLinks(cl), Links(cl), MaxSendq(cl)));
+    Debug((DEBUG_DEBUG, "Class %s : CF: %d PF: %d ML: %d LI: %d SQ: %d",
+           ConClass(cl), ConFreq(cl), PingFreq(cl), MaxLinks(cl),
+           Links(cl), MaxSendq(cl)));
     /*
      * unlink marked classes, delete unreferenced ones
      */
     if (cl->valid)
       prev = cl;
-    else {
+    else
+    {
       prev->next = cl->next;
-      if (0 == cl->ref_count)
+      if (0 == --cl->ref_count)
         free_class(cl);
     }
   }
 }
 
-unsigned int get_conf_class(const struct ConfItem* aconf)
+char*
+get_conf_class(const struct ConfItem* aconf)
 {
   if ((aconf) && (aconf->conn_class))
     return (ConfClass(aconf));
 
   Debug((DEBUG_DEBUG, "No Class For %s", (aconf) ? aconf->name : "*No Conf*"));
 
-  return (BAD_CONF_CLASS);
+  return NULL;
 }
 
 int get_conf_ping(const struct ConfItem* aconf)
@@ -144,54 +157,20 @@ int get_conf_ping(const struct ConfItem* aconf)
   return -1;
 }
 
-unsigned int get_client_class(struct Client *acptr)
+char*
+get_client_class(struct Client *acptr)
 {
   struct SLink *tmp;
   struct ConnectionClass *cl;
-  unsigned int retc = BAD_CLIENT_CLASS;
 
-  if (acptr && !IsMe(acptr) && (acptr->confs))
-    for (tmp = acptr->confs; tmp; tmp = tmp->next)
+  /* Return the most recent(first on LL) client class... */
+  if (acptr && !IsMe(acptr) && (cli_confs(acptr)))
+    for (tmp = cli_confs(acptr); tmp; tmp = tmp->next)
     {
-      if (!tmp->value.aconf || !(cl = tmp->value.aconf->conn_class))
-        continue;
-      if (ConClass(cl) > retc || retc == BAD_CLIENT_CLASS)
-        retc = ConClass(cl);
+      if (tmp->value.aconf && (cl = tmp->value.aconf->conn_class))
+        return ConClass(cl);
     }
-
-  Debug((DEBUG_DEBUG, "Returning Class %d For %s", retc, acptr->name));
-
-  return (retc);
-}
-
-unsigned int get_client_ping(struct Client *acptr)
-{
-  unsigned int ping = 0;
-  unsigned int ping2;
-  struct ConfItem *aconf;
-  struct SLink *link;
-
-  link = acptr->confs;
-
-  if (link) {
-    while (link) {
-      aconf = link->value.aconf;
-      if (aconf->status & (CONF_CLIENT | CONF_SERVER)) {
-        ping2 = get_conf_ping(aconf);
-        if ((ping2 != BAD_PING) && ((ping > ping2) || !ping))
-          ping = ping2;
-      }
-      link = link->next;
-    }
-  }
-  else {
-    ping = PINGFREQUENCY;
-    Debug((DEBUG_DEBUG, "No Attached Confs for: %s", acptr->name));
-  }
-  if (ping <= 0)
-    ping = PINGFREQUENCY;
-  Debug((DEBUG_DEBUG, "Client %s Ping %d", acptr->name, ping));
-  return (ping);
+  return "(null-class)";
 }
 
 unsigned int get_con_freq(struct ConnectionClass * clptr)
@@ -199,7 +178,7 @@ unsigned int get_con_freq(struct ConnectionClass * clptr)
   if (clptr)
     return (ConFreq(clptr));
   else
-    return (CONNECTFREQUENCY);
+    return feature_int(FEAT_CONNECTFREQUENCY);
 }
 
 /*
@@ -209,45 +188,52 @@ unsigned int get_con_freq(struct ConnectionClass * clptr)
  * if no present entry is found, then create a new one and add it in
  * immeadiately after the first one (class 0).
  */
-void add_class(unsigned int conClass, unsigned int ping, unsigned int confreq,
+void add_class(char *name, unsigned int ping, unsigned int confreq,
                unsigned int maxli, unsigned int sendq)
 {
   struct ConnectionClass* t;
   struct ConnectionClass* p;
 
-  t = find_class(conClass);
-  if ((t == connClassList) && (conClass != 0))
+  t = find_class(name);
+  if ((t == connClassList) && (name != NULL))
   {
     p = (struct ConnectionClass *) make_class();
     p->next = t->next;
     t->next = p;
   }
   else
+  {
+    if (ConClass(t) != NULL)
+      MyFree(ConClass(t));
     p = t;
-  Debug((DEBUG_DEBUG, "Add Class %u: cf: %u pf: %u ml: %u sq: %d",
-         conClass, confreq, ping, maxli, sendq));
-  ConClass(p) = conClass;
+  }
+  Debug((DEBUG_DEBUG, "Add Class %s: cf: %u pf: %u ml: %u sq: %d",
+         name, confreq, ping, maxli, sendq));
+  ConClass(p) = name;
   ConFreq(p) = confreq;
   PingFreq(p) = ping;
   MaxLinks(p) = maxli;
-  MaxSendq(p) = (sendq > 0) ? sendq : DEFAULTMAXSENDQLENGTH;
+  MaxSendq(p) = (sendq > 0) ?
+     sendq : feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
   p->valid = 1;
   if (p != t)
     Links(p) = 0;
 }
 
-struct ConnectionClass* find_class(unsigned int cclass)
+struct ConnectionClass* find_class(const char *name)
 {
   struct ConnectionClass *cltmp;
 
   for (cltmp = connClassList; cltmp; cltmp = cltmp->next) {
-    if (ConClass(cltmp) == cclass)
+    if (!ircd_strcmp(ConClass(cltmp), name))
       return cltmp;
   }
   return connClassList;
 }
 
-void report_classes(struct Client *sptr)
+void
+report_classes(struct Client *sptr, struct StatDesc *sd, int stat,
+               char *param)
 {
   struct ConnectionClass *cltmp;
 
@@ -257,34 +243,36 @@ void report_classes(struct Client *sptr)
               Links(cltmp));
 }
 
-unsigned int get_sendq(struct Client *cptr)
+unsigned int
+get_sendq(struct Client *cptr)
 {
   assert(0 != cptr);
-  assert(0 != cptr->local);
+  assert(0 != cli_local(cptr));
 
-  if (cptr->max_sendq)
-    return cptr->max_sendq;
+  if (cli_max_sendq(cptr))
+    return cli_max_sendq(cptr);
 
-  else if (cptr->confs) {
+  else if (cli_confs(cptr)) {
     struct SLink*     tmp;
     struct ConnectionClass* cl;
 
-    for (tmp = cptr->confs; tmp; tmp = tmp->next) {
+    for (tmp = cli_confs(cptr); tmp; tmp = tmp->next) {
       if (!tmp->value.aconf || !(cl = tmp->value.aconf->conn_class))
         continue;
-      if (ConClass(cl) != BAD_CLIENT_CLASS) {
-        cptr->max_sendq = MaxSendq(cl);
-        return cptr->max_sendq;
+      if (ConClass(cl) != NULL) {
+        cli_max_sendq(cptr) = MaxSendq(cl);
+        return cli_max_sendq(cptr);
       }
     }
   }
-  return DEFAULTMAXSENDQLENGTH;
+  return feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
 }
 
 void class_send_meminfo(struct Client* cptr)
 {
   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes: inuse: %d(%d)",
-             connClassAllocCount, connClassAllocCount * sizeof(struct ConnectionClass));
+             connClassAllocCount,
+             connClassAllocCount * sizeof(struct ConnectionClass));
 }