2 * IRC - Internet Relay Chat, ircd/class.c
3 * Copyright (C) 1990 Darren Reed
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 1, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * @brief Implementation of connection class handling functions.
28 #include "ircd_alloc.h"
29 #include "ircd_features.h"
31 #include "ircd_reply.h"
32 #include "ircd_string.h"
39 /* #include <assert.h> -- Now using assert in ircd_log.h */
41 /** List of all connection classes. */
42 static struct ConnectionClass* connClassList;
43 /** Number of allocated connection classes. */
44 static unsigned int connClassAllocCount;
46 /** Get start of connection class linked list. */
47 const struct ConnectionClass* get_class_list(void)
52 /** Allocate a new connection class.
53 * If #connClassList is not null, insert the new class just after it.
54 * @return Newly allocated connection class structure.
56 struct ConnectionClass* make_class(void)
58 struct ConnectionClass *tmp;
60 tmp = (struct ConnectionClass*) MyCalloc(1, sizeof(struct ConnectionClass));
65 tmp->next = connClassList->next;
66 connClassList->next = tmp;
68 ++connClassAllocCount;
72 /** Dereference a connection class.
73 * @param[in] p Connection class to dereference.
75 void free_class(struct ConnectionClass* p)
79 assert(0 == p->valid);
81 MyFree(p->default_umode);
83 --connClassAllocCount;
87 /** Initialize the connection class list.
88 * A connection class named "default" is created, with ping frequency,
89 * connection frequency, maximum links and max SendQ values from the
90 * corresponding configuration features.
95 connClassList = (struct ConnectionClass*) make_class();
97 /* We had better not try and free this... */
98 ConClass(connClassList) = "default";
99 PingFreq(connClassList) = feature_int(FEAT_PINGFREQUENCY);
100 ConFreq(connClassList) = feature_int(FEAT_CONNECTFREQUENCY);
101 MaxLinks(connClassList) = feature_int(FEAT_MAXIMUM_LINKS);
102 MaxSendq(connClassList) = feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
103 connClassList->valid = 1;
104 Links(connClassList) = 0;
105 connClassList->next = 0;
108 /** Mark current connection classes as invalid.
110 void class_mark_delete(void)
112 struct ConnectionClass* p;
113 assert(0 != connClassList);
115 for (p = connClassList->next; p; p = p->next)
119 /** Unlink (and dereference) invalid connection classes.
120 * This is used in combination with class_mark_delete() during rehash
121 * to get rid of connection classes that are no longer in the
124 void class_delete_marked(void)
126 struct ConnectionClass* cl;
127 struct ConnectionClass* prev;
129 Debug((DEBUG_DEBUG, "Class check:"));
131 for (prev = cl = connClassList; cl; cl = prev->next) {
132 Debug((DEBUG_DEBUG, "Class %s : CF: %d PF: %d ML: %d LI: %d SQ: %d",
133 ConClass(cl), ConFreq(cl), PingFreq(cl), MaxLinks(cl),
134 Links(cl), MaxSendq(cl)));
136 * unlink marked classes, delete unreferenced ones
142 prev->next = cl->next;
143 if (0 == --cl->ref_count)
149 /** Get connection class name for a configuration item.
150 * @param[in] aconf Configuration item to check.
151 * @return Name of connection class associated with \a aconf.
154 get_conf_class(const struct ConfItem* aconf)
156 if ((aconf) && (aconf->conn_class))
157 return (ConfClass(aconf));
159 Debug((DEBUG_DEBUG, "No Class For %s", (aconf) ? aconf->name : "*No Conf*"));
164 /** Get ping time for a configuration item.
165 * @param[in] aconf Configuration item to check.
166 * @return Ping time for connection class associated with \a aconf.
168 int get_conf_ping(const struct ConfItem* aconf)
171 if (aconf->conn_class)
172 return (ConfPingFreq(aconf));
174 Debug((DEBUG_DEBUG, "No Ping For %s", aconf->name));
179 /** Get connection class name for a particular client.
180 * @param[in] acptr Client to check.
181 * @return Name of connection class to which \a acptr belongs.
184 get_client_class(struct Client *acptr)
187 struct ConnectionClass *cl;
189 /* Return the most recent(first on LL) client class... */
190 if (acptr && !IsMe(acptr) && (cli_confs(acptr)))
191 for (tmp = cli_confs(acptr); tmp; tmp = tmp->next)
193 if (tmp->value.aconf && (cl = tmp->value.aconf->conn_class))
196 return "(null-class)";
199 /** Make sure we have a connection class named \a name.
200 * If one does not exist, create it. Then set its ping frequency,
201 * connection frequency, maximum link count, and max SendQ according
203 * @param[in] name Connection class name.
204 * @param[in] ping Ping frequency for clients in this class.
205 * @param[in] confreq Connection frequency for clients.
206 * @param[in] maxli Maximum link count for class.
207 * @param[in] sendq Max SendQ for clients.
209 void add_class(char *name, unsigned int ping, unsigned int confreq,
210 unsigned int maxli, unsigned int sendq)
212 struct ConnectionClass* p;
214 Debug((DEBUG_DEBUG, "Add Class %s: cf: %u pf: %u ml: %u sq: %d",
215 name, confreq, ping, maxli, sendq));
216 assert(name != NULL);
217 p = find_class(name);
223 ConFreq(p) = confreq;
226 MaxSendq(p) = (sendq > 0) ?
227 sendq : feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
231 /** Find a connection class by name.
232 * @param[in] name Name of connection class to search for.
233 * @return Pointer to connection class structure (or NULL if none match).
235 struct ConnectionClass* find_class(const char *name)
237 struct ConnectionClass *cltmp;
239 for (cltmp = connClassList; cltmp; cltmp = cltmp->next) {
240 if (!ircd_strcmp(ConClass(cltmp), name))
246 /** Report connection classes to a client.
247 * @param[in] sptr Client requesting statistics.
248 * @param[in] sd Stats descriptor for request (ignored).
249 * @param[in] param Extra parameter from user (ignored).
252 report_classes(struct Client *sptr, const struct StatDesc *sd,
255 struct ConnectionClass *cltmp;
257 for (cltmp = connClassList; cltmp; cltmp = cltmp->next)
258 send_reply(sptr, RPL_STATSYLINE, 'Y', ConClass(cltmp), PingFreq(cltmp),
259 ConFreq(cltmp), MaxLinks(cltmp), MaxSendq(cltmp),
263 /** Return maximum SendQ length for a client.
264 * @param[in] cptr Local client to check.
265 * @return Number of bytes allowed in SendQ for \a cptr.
268 get_sendq(struct Client *cptr)
271 assert(0 != cli_local(cptr));
273 if (cli_max_sendq(cptr))
274 return cli_max_sendq(cptr);
276 else if (cli_confs(cptr)) {
278 struct ConnectionClass* cl;
280 for (tmp = cli_confs(cptr); tmp; tmp = tmp->next) {
281 if (!tmp->value.aconf || !(cl = tmp->value.aconf->conn_class))
283 if (ConClass(cl) != NULL) {
284 cli_max_sendq(cptr) = MaxSendq(cl);
285 return cli_max_sendq(cptr);
289 return feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
292 /** Report connection class memory statistics to a client.
293 * Send number of classes and number of bytes allocated for them.
294 * @param[in] cptr Client requesting statistics.
296 void class_send_meminfo(struct Client* cptr)
298 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes: inuse: %d(%d)",
300 connClassAllocCount * sizeof(struct ConnectionClass));