* 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"
+
#include "class.h"
#include "client.h"
#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 "numeric.h"
#include "s_conf.h"
#include "s_debug.h"
#include "send.h"
-#include "struct.h"
-#include <assert.h>
+/* #include <assert.h> -- Now using assert in ircd_log.h */
-#define BAD_CONF_CLASS ((unsigned int)-1)
-#define BAD_PING ((unsigned int)-2)
-#define BAD_CLIENT_CLASS ((unsigned int)-3)
+/** List of all connection classes. */
+static struct ConnectionClass* connClassList;
+/** Number of allocated connection classes. */
+static unsigned int connClassAllocCount;
-struct ConfClass *classes;
+/** Get start of connection class linked list. */
+const struct ConnectionClass* get_class_list(void)
+{
+ return connClassList;
+}
-unsigned int get_conf_class(const struct ConfItem* aconf)
+/** 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)
{
- if ((aconf) && (aconf->confClass))
- return (ConfClass(aconf));
+ struct ConnectionClass *tmp;
- Debug((DEBUG_DEBUG, "No Class For %s", (aconf) ? aconf->name : "*No Conf*"));
+ 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;
+}
- return (BAD_CONF_CLASS);
+/** Dereference a connection class.
+ * @param[in] p Connection class to dereference.
+ */
+void free_class(struct ConnectionClass* p)
+{
+ if (p)
+ {
+ assert(0 == p->valid);
+ MyFree(p->cc_name);
+ MyFree(p->default_umode);
+ MyFree(p);
+ --connClassAllocCount;
+ }
}
-unsigned int get_conf_ping(struct ConfItem *aconf)
+/** 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 ((aconf) && (aconf->confClass))
- return (ConfPingFreq(aconf));
+ if (!connClassList) {
+ connClassList = (struct ConnectionClass*) make_class();
+ connClassList->next = 0;
+ }
- Debug((DEBUG_DEBUG, "No Ping For %s", (aconf) ? aconf->name : "*No Conf*"));
+ /* 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) = 1;
+}
- return (BAD_PING);
+/** Mark current connection classes as invalid.
+ */
+void class_mark_delete(void)
+{
+ struct ConnectionClass* p;
+ assert(0 != connClassList);
+
+ for (p = connClassList->next; p; p = p->next)
+ p->valid = 0;
}
-unsigned int get_client_class(struct Client *acptr)
+/** 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)
{
- struct SLink *tmp;
- struct ConfClass *cl;
- unsigned int retc = BAD_CLIENT_CLASS;
+ struct ConnectionClass* cl;
+ struct ConnectionClass* prev;
- if (acptr && !IsMe(acptr) && (acptr->confs))
- for (tmp = acptr->confs; tmp; tmp = tmp->next)
+ Debug((DEBUG_DEBUG, "Class check:"));
+
+ for (prev = cl = connClassList; cl; cl = prev->next) {
+ 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 || Links(cl) > 1)
+ prev = cl;
+ else
{
- if (!tmp->value.aconf || !(cl = tmp->value.aconf->confClass))
- continue;
- if (ConClass(cl) > retc || retc == BAD_CLIENT_CLASS)
- retc = ConClass(cl);
+ prev->next = cl->next;
+ free_class(cl);
}
+ }
+}
- Debug((DEBUG_DEBUG, "Returning Class %d For %s", retc, acptr->name));
+/** 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)
+{
+ if ((aconf) && (aconf->conn_class))
+ return (ConfClass(aconf));
- return (retc);
+ Debug((DEBUG_DEBUG, "No Class For %s", (aconf) ? aconf->name : "*No Conf*"));
+
+ return NULL;
}
-unsigned int get_client_ping(struct Client *acptr)
+/** 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)
{
- 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);
+ assert(0 != aconf);
+ if (aconf->conn_class)
+ return (ConfPingFreq(aconf));
+
+ Debug((DEBUG_DEBUG, "No Ping For %s", aconf->name));
+
+ return -1;
}
-unsigned int get_con_freq(struct ConfClass * clptr)
+/** 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)
{
- if (clptr)
- return (ConFreq(clptr));
- else
- return (CONNECTFREQUENCY);
+ struct SLink *tmp;
+ struct ConnectionClass *cl;
+
+ /* 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))
+ return ConClass(cl);
+ }
+ return "(null-class)";
}
-/*
- * 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(unsigned int conClass, unsigned int ping, unsigned int confreq,
- unsigned int maxli, unsigned int sendq)
+void add_class(char *name, unsigned int ping, unsigned int confreq,
+ unsigned int maxli, unsigned int sendq)
{
- struct ConfClass *t, *p;
-
- t = find_class(conClass);
- if ((t == classes) && (conClass != 0))
- {
- p = (struct ConfClass *) make_class();
- NextClass(p) = NextClass(t);
- NextClass(t) = p;
- }
+ struct ConnectionClass* p;
+
+ 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
- p = t;
- Debug((DEBUG_DEBUG, "Add Class %u: p %p t %p - cf: %u pf: %u ml: %u sq: %d",
- conClass, p, t, confreq, ping, maxli, sendq));
- ConClass(p) = conClass;
+ MyFree(ConClass(p));
+ ConClass(p) = name;
ConFreq(p) = confreq;
PingFreq(p) = ping;
MaxLinks(p) = maxli;
- MaxSendq(p) = (sendq > 0) ? sendq : DEFAULTMAXSENDQLENGTH;
- if (p != t)
- Links(p) = 0;
+ MaxSendq(p) = (sendq > 0) ?
+ sendq : feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
+ p->valid = 1;
}
-struct ConfClass *find_class(unsigned int cclass)
+/** 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 ConfClass *cltmp;
+ struct ConnectionClass *cltmp;
- for (cltmp = FirstClass(); cltmp; cltmp = NextClass(cltmp))
- if (ConClass(cltmp) == cclass)
+ for (cltmp = connClassList; cltmp; cltmp = cltmp->next) {
+ if (!cltmp->valid && !extras)
+ continue;
+ if (!ircd_strcmp(ConClass(cltmp), name))
return cltmp;
- return classes;
-}
-
-void check_class(void)
-{
- struct ConfClass *cltmp, *cltmp2;
-
- Debug((DEBUG_DEBUG, "Class check:"));
-
- for (cltmp2 = cltmp = FirstClass(); cltmp; cltmp = NextClass(cltmp2))
- {
- Debug((DEBUG_DEBUG,
- "Class %d : CF: %d PF: %d ML: %d LI: %d SQ: %d",
- ConClass(cltmp), ConFreq(cltmp), PingFreq(cltmp),
- MaxLinks(cltmp), Links(cltmp), MaxSendq(cltmp)));
- if (IsMarkedDelete(cltmp))
- {
- NextClass(cltmp2) = NextClass(cltmp);
- if (Links(cltmp) == 0)
- free_class(cltmp);
- }
- else
- cltmp2 = cltmp;
}
+ return NULL;
}
-void initclass(void)
-{
- classes = (struct ConfClass *) make_class();
-
- ConClass(FirstClass()) = 0;
- ConFreq(FirstClass()) = CONNECTFREQUENCY;
- PingFreq(FirstClass()) = PINGFREQUENCY;
- MaxLinks(FirstClass()) = MAXIMUM_LINKS;
- MaxSendq(FirstClass()) = DEFAULTMAXSENDQLENGTH;
- Links(FirstClass()) = 0;
- NextClass(FirstClass()) = NULL;
-}
-
-void report_classes(struct Client *sptr)
+/** 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)
{
- struct ConfClass *cltmp;
+ struct ConnectionClass *cltmp;
- for (cltmp = FirstClass(); cltmp; cltmp = NextClass(cltmp))
- send_reply(sptr, RPL_STATSYLINE, 'Y', ConClass(cltmp), PingFreq(cltmp),
- ConFreq(cltmp), MaxLinks(cltmp), MaxSendq(cltmp));
+ for (cltmp = connClassList; cltmp; cltmp = cltmp->next)
+ send_reply(sptr, RPL_STATSYLINE, (cltmp->valid ? 'Y' : 'y'),
+ ConClass(cltmp), PingFreq(cltmp), ConFreq(cltmp),
+ MaxLinks(cltmp), MaxSendq(cltmp), Links(cltmp) - 1);
}
-unsigned int get_sendq(struct Client *cptr)
+/** 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)
{
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 ConfClass* cl;
+ struct ConnectionClass* cl;
- for (tmp = cptr->confs; tmp; tmp = tmp->next) {
- if (!tmp->value.aconf || !(cl = tmp->value.aconf->confClass))
+ 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);
}
+/** 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)",
+ connClassAllocCount,
+ connClassAllocCount * sizeof(struct ConnectionClass));
+}
+
+