added gnutls backend and moved backend code into new files
[ircu2.10.12-pk.git] / ircd / class.c
index 41ed4e62aa501f51324424fa2fe593fe9fcadf3d..216960534d2df75d48bd0e3e55173f4e7a40771a 100644 (file)
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- * $Id$
+ */
+/** @file
+ * @brief Implementation of connection class handling functions.
+ * @version $Id$
  */
 #include "config.h"
 
@@ -25,6 +27,7 @@
 #include "ircd.h"
 #include "ircd_alloc.h"
 #include "ircd_features.h"
+#include "ircd_log.h"
 #include "ircd_reply.h"
 #include "ircd_string.h"
 #include "list.h"
 #include "s_debug.h"
 #include "send.h"
 
-#include <assert.h>
-
-#define BAD_CONF_CLASS          ((unsigned int)-1)
-#define BAD_PING                ((unsigned int)-2)
-#define BAD_CLIENT_CLASS        ((unsigned int)-3)
+/* #include <assert.h> -- Now using assert in ircd_log.h */
 
-static struct ConnectionClass* connClassList = 0;
+/** List of all connection classes. */
+static struct ConnectionClass* connClassList;
+/** Number of allocated connection classes. */
 static unsigned int connClassAllocCount;
 
+/** Get start of connection class linked list. */
 const struct ConnectionClass* get_class_list(void)
 {
   return connClassList;
 }
 
+/** Allocate a new connection class.
+ * If #connClassList is not null, insert the new class just after it.
+ * @return Newly allocated connection class structure.
+ */
 struct ConnectionClass* make_class(void)
 {
   struct ConnectionClass *tmp;
 
-  tmp = (struct ConnectionClass*) MyMalloc(sizeof(struct ConnectionClass));
-  tmp->ref_count = 1;
+  tmp = (struct ConnectionClass*) MyCalloc(1, sizeof(struct ConnectionClass));
   assert(0 != tmp);
+  tmp->ref_count = 1;
+  if (connClassList)
+  {
+    tmp->next = connClassList->next;
+    connClassList->next = tmp;
+  }
   ++connClassAllocCount;
   return tmp;
 }
 
+/** Dereference a connection class.
+ * @param[in] p Connection class to dereference.
+ */
 void free_class(struct ConnectionClass* p)
 {
   if (p)
   {
     assert(0 == p->valid);
-    if (p->cc_name)
-      MyFree(p->cc_name);
+    MyFree(p->cc_name);
+    MyFree(p->default_umode);
     MyFree(p);
     --connClassAllocCount;
   }
 }
 
-/*
- * init_class - initialize the class list
+/** Initialize the connection class list.
+ * A connection class named "default" is created, with ping frequency,
+ * connection frequency, maximum links and max SendQ values from the
+ * corresponding configuration features.
  */
 void init_class(void)
 {
-  if (!connClassList)
+  if (!connClassList) {
     connClassList = (struct ConnectionClass*) make_class();
+    connClassList->next   = 0;
+  }
 
   /* We had better not try and free this... */
   ConClass(connClassList) = "default";
@@ -85,15 +103,10 @@ void init_class(void)
   MaxLinks(connClassList) = feature_int(FEAT_MAXIMUM_LINKS);
   MaxSendq(connClassList) = feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
   connClassList->valid    = 1;
-  Links(connClassList)    = 0;
-  connClassList->next     = 0;
+  Links(connClassList)    = 1;
 }
 
-/*
- * class_mark_delete - mark classes for delete
- * We don't delete the class table, rather mark all entries
- * for deletion. The table is cleaned up by class_delete_marked(). - avalon
- * XXX - This destroys data
+/** Mark current connection classes as invalid.
  */
 void class_mark_delete(void)
 {
@@ -104,11 +117,10 @@ void class_mark_delete(void)
     p->valid = 0;
 }
 
-/*
- * class_delete_marked
- * delete classes marked for deletion
- * XXX - memory leak, no one deletes classes that become unused
- * later
+/** Unlink (and dereference) invalid connection classes.
+ * This is used in combination with class_mark_delete() during rehash
+ * to get rid of connection classes that are no longer in the
+ * configuration.
  */
 void class_delete_marked(void)
 {
@@ -124,17 +136,20 @@ void class_delete_marked(void)
     /*
      * unlink marked classes, delete unreferenced ones
      */
-    if (cl->valid)
+    if (cl->valid || Links(cl) > 1)
       prev = cl;
     else
     {
       prev->next = cl->next;
-      if (0 == --cl->ref_count)
-        free_class(cl);
+      free_class(cl);
     }
   }
 }
 
+/** Get connection class name for a configuration item.
+ * @param[in] aconf Configuration item to check.
+ * @return Name of connection class associated with \a aconf.
+ */
 char*
 get_conf_class(const struct ConfItem* aconf)
 {
@@ -146,6 +161,10 @@ get_conf_class(const struct ConfItem* aconf)
   return NULL;
 }
 
+/** Get ping time for a configuration item.
+ * @param[in] aconf Configuration item to check.
+ * @return Ping time for connection class associated with \a aconf.
+ */
 int get_conf_ping(const struct ConfItem* aconf)
 {
   assert(0 != aconf);
@@ -157,6 +176,10 @@ int get_conf_ping(const struct ConfItem* aconf)
   return -1;
 }
 
+/** Get connection class name for a particular client.
+ * @param[in] acptr Client to check.
+ * @return Name of connection class to which \a acptr belongs.
+ */
 char*
 get_client_class(struct Client *acptr)
 {
@@ -173,42 +196,29 @@ get_client_class(struct Client *acptr)
   return "(null-class)";
 }
 
-unsigned int get_con_freq(struct ConnectionClass * clptr)
-{
-  if (clptr)
-    return (ConFreq(clptr));
-  else
-    return feature_int(FEAT_CONNECTFREQUENCY);
-}
-
-/*
- * When adding a class, check to see if it is already present first.
- * if so, then update the information for that class, rather than create
- * a new entry for it and later delete the old entry.
- * if no present entry is found, then create a new one and add it in
- * immeadiately after the first one (class 0).
+/** Make sure we have a connection class named \a name.
+ * If one does not exist, create it.  Then set its ping frequency,
+ * connection frequency, maximum link count, and max SendQ according
+ * to the parameters.
+ * @param[in] name Connection class name.
+ * @param[in] ping Ping frequency for clients in this class.
+ * @param[in] confreq Connection frequency for clients.
+ * @param[in] maxli Maximum link count for class.
+ * @param[in] sendq Max SendQ for clients.
  */
 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(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 %s: cf: %u pf: %u ml: %u sq: %d",
          name, confreq, ping, maxli, sendq));
+  assert(name != NULL);
+  p = do_find_class(name, 1);
+  if (!p)
+    p = make_class();
+  else
+    MyFree(ConClass(p));
   ConClass(p) = name;
   ConFreq(p) = confreq;
   PingFreq(p) = ping;
@@ -216,21 +226,31 @@ void add_class(char *name, unsigned int ping, unsigned int confreq,
   MaxSendq(p) = (sendq > 0) ?
      sendq : feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
   p->valid = 1;
-  if (p != t)
-    Links(p) = 0;
 }
 
-struct ConnectionClass* find_class(const char *name)
+/** Find a connection class by name.
+ * @param[in] name Name of connection class to search for.
+ * @param[in] extras If non-zero, include unreferenced classes.
+ * @return Pointer to connection class structure (or NULL if none match).
+ */
+struct ConnectionClass* do_find_class(const char *name, int extras)
 {
   struct ConnectionClass *cltmp;
 
   for (cltmp = connClassList; cltmp; cltmp = cltmp->next) {
+    if (!cltmp->valid && !extras)
+      continue;
     if (!ircd_strcmp(ConClass(cltmp), name))
       return cltmp;
   }
-  return connClassList;
+  return NULL;
 }
 
+/** Report connection classes to a client.
+ * @param[in] sptr Client requesting statistics.
+ * @param[in] sd Stats descriptor for request (ignored).
+ * @param[in] param Extra parameter from user (ignored).
+ */
 void
 report_classes(struct Client *sptr, const struct StatDesc *sd,
                char *param)
@@ -238,11 +258,15 @@ report_classes(struct Client *sptr, const struct StatDesc *sd,
   struct ConnectionClass *cltmp;
 
   for (cltmp = connClassList; cltmp; cltmp = cltmp->next)
-    send_reply(sptr, RPL_STATSYLINE, 'Y', ConClass(cltmp), PingFreq(cltmp),
-              ConFreq(cltmp), MaxLinks(cltmp), MaxSendq(cltmp),
-              Links(cltmp));
+    send_reply(sptr, RPL_STATSYLINE, (cltmp->valid ? 'Y' : 'y'),
+               ConClass(cltmp), PingFreq(cltmp), ConFreq(cltmp),
+               MaxLinks(cltmp), MaxSendq(cltmp), Links(cltmp) - 1);
 }
 
+/** Return maximum SendQ length for a client.
+ * @param[in] cptr Local client to check.
+ * @return Number of bytes allowed in SendQ for \a cptr.
+ */
 unsigned int
 get_sendq(struct Client *cptr)
 {
@@ -268,6 +292,10 @@ get_sendq(struct Client *cptr)
   return feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
 }
 
+/** Report connection class memory statistics to a client.
+ * Send number of classes and number of bytes allocated for them.
+ * @param[in] cptr Client requesting statistics.
+ */
 void class_send_meminfo(struct Client* cptr)
 {
   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes: inuse: %d(%d)",