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"
30 #include "ircd_reply.h"
31 #include "ircd_string.h"
40 /** List of all connection classes. */
41 static struct ConnectionClass* connClassList = 0;
42 /** Number of allocated connection classes. */
43 static unsigned int connClassAllocCount;
45 /** Get start of connection class linked list. */
46 const struct ConnectionClass* get_class_list(void)
51 /** Allocate a new connection class.
52 * @return Newly allocated connection class structure.
54 struct ConnectionClass* make_class(void)
56 struct ConnectionClass *tmp;
58 tmp = (struct ConnectionClass*) MyMalloc(sizeof(struct ConnectionClass));
61 ++connClassAllocCount;
65 /** Dereference a connection class.
66 * @param[in] p Connection class to dereference.
68 void free_class(struct ConnectionClass* p)
72 assert(0 == p->valid);
76 --connClassAllocCount;
80 /** Initialize the connection class list.
81 * A connection class named "default" is created, with ping frequency,
82 * connection frequency, maximum links and max SendQ values from the
83 * corresponding configuration features.
88 connClassList = (struct ConnectionClass*) make_class();
90 /* We had better not try and free this... */
91 ConClass(connClassList) = "default";
92 PingFreq(connClassList) = feature_int(FEAT_PINGFREQUENCY);
93 ConFreq(connClassList) = feature_int(FEAT_CONNECTFREQUENCY);
94 MaxLinks(connClassList) = feature_int(FEAT_MAXIMUM_LINKS);
95 MaxSendq(connClassList) = feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
96 connClassList->valid = 1;
97 Links(connClassList) = 0;
98 connClassList->next = 0;
101 /** Mark current connection classes as invalid.
103 void class_mark_delete(void)
105 struct ConnectionClass* p;
106 assert(0 != connClassList);
108 for (p = connClassList->next; p; p = p->next)
112 /** Unlink (and dereference) invalid connection classes.
113 * This is used in combination with class_mark_delete() during rehash
114 * to get rid of connection classes that are no longer in the
117 void class_delete_marked(void)
119 struct ConnectionClass* cl;
120 struct ConnectionClass* prev;
122 Debug((DEBUG_DEBUG, "Class check:"));
124 for (prev = cl = connClassList; cl; cl = prev->next) {
125 Debug((DEBUG_DEBUG, "Class %s : CF: %d PF: %d ML: %d LI: %d SQ: %d",
126 ConClass(cl), ConFreq(cl), PingFreq(cl), MaxLinks(cl),
127 Links(cl), MaxSendq(cl)));
129 * unlink marked classes, delete unreferenced ones
135 prev->next = cl->next;
136 if (0 == --cl->ref_count)
142 /** Get connection class name for a configuration item.
143 * @param[in] aconf Configuration item to check.
144 * @return Name of connection class associated with \a aconf.
147 get_conf_class(const struct ConfItem* aconf)
149 if ((aconf) && (aconf->conn_class))
150 return (ConfClass(aconf));
152 Debug((DEBUG_DEBUG, "No Class For %s", (aconf) ? aconf->name : "*No Conf*"));
157 /** Get ping time for a configuration item.
158 * @param[in] aconf Configuration item to check.
159 * @return Ping time for connection class associated with \a aconf.
161 int get_conf_ping(const struct ConfItem* aconf)
164 if (aconf->conn_class)
165 return (ConfPingFreq(aconf));
167 Debug((DEBUG_DEBUG, "No Ping For %s", aconf->name));
172 /** Get connection class name for a particular client.
173 * @param[in] acptr Client to check.
174 * @return Name of connection class to which \a acptr belongs.
177 get_client_class(struct Client *acptr)
180 struct ConnectionClass *cl;
182 /* Return the most recent(first on LL) client class... */
183 if (acptr && !IsMe(acptr) && (cli_confs(acptr)))
184 for (tmp = cli_confs(acptr); tmp; tmp = tmp->next)
186 if (tmp->value.aconf && (cl = tmp->value.aconf->conn_class))
189 return "(null-class)";
192 /** Get connection interval for a connection class.
193 * @param[in] clptr Connection class to check (or NULL).
194 * @return If \a clptr != NULL, its connection frequency; else default
195 * connection frequency.
197 unsigned int get_con_freq(struct ConnectionClass * clptr)
200 return (ConFreq(clptr));
202 return feature_int(FEAT_CONNECTFREQUENCY);
205 /** Make sure we have a connection class named \a name.
206 * If one does not exist, create it. Then set its ping frequency,
207 * connection frequency, maximum link count, and max SendQ according
209 * @param[in] name Connection class name.
210 * @param[in] ping Ping frequency for clients in this class.
211 * @param[in] confreq Connection frequency for clients.
212 * @param[in] maxli Maximum link count for class.
213 * @param[in] sendq Max SendQ for clients.
215 void add_class(char *name, unsigned int ping, unsigned int confreq,
216 unsigned int maxli, unsigned int sendq)
218 struct ConnectionClass* t;
219 struct ConnectionClass* p;
221 t = find_class(name);
222 if ((t == connClassList) && (name != NULL))
224 p = (struct ConnectionClass *) make_class();
230 if (ConClass(t) != NULL)
234 Debug((DEBUG_DEBUG, "Add Class %s: cf: %u pf: %u ml: %u sq: %d",
235 name, confreq, ping, maxli, sendq));
237 ConFreq(p) = confreq;
240 MaxSendq(p) = (sendq > 0) ?
241 sendq : feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
247 /** Find a connection class by name.
248 * @param[in] name Name of connection class to search for.
249 * @return Pointer to connection class structure (or NULL if none match).
251 struct ConnectionClass* find_class(const char *name)
253 struct ConnectionClass *cltmp;
255 for (cltmp = connClassList; cltmp; cltmp = cltmp->next) {
256 if (!ircd_strcmp(ConClass(cltmp), name))
259 return connClassList;
262 /** Report connection classes to a client.
263 * @param[in] sptr Client requesting statistics.
264 * @param[in] sd Stats descriptor for request (ignored).
265 * @param[in] param Extra parameter from user (ignored).
268 report_classes(struct Client *sptr, const struct StatDesc *sd,
271 struct ConnectionClass *cltmp;
273 for (cltmp = connClassList; cltmp; cltmp = cltmp->next)
274 send_reply(sptr, RPL_STATSYLINE, 'Y', ConClass(cltmp), PingFreq(cltmp),
275 ConFreq(cltmp), MaxLinks(cltmp), MaxSendq(cltmp),
279 /** Return maximum SendQ length for a client.
280 * @param[in] cptr Local client to check.
281 * @return Number of bytes allowed in SendQ for \a cptr.
284 get_sendq(struct Client *cptr)
287 assert(0 != cli_local(cptr));
289 if (cli_max_sendq(cptr))
290 return cli_max_sendq(cptr);
292 else if (cli_confs(cptr)) {
294 struct ConnectionClass* cl;
296 for (tmp = cli_confs(cptr); tmp; tmp = tmp->next) {
297 if (!tmp->value.aconf || !(cl = tmp->value.aconf->conn_class))
299 if (ConClass(cl) != NULL) {
300 cli_max_sendq(cptr) = MaxSendq(cl);
301 return cli_max_sendq(cptr);
305 return feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
308 /** Report connection class memory statistics to a client.
309 * Send number of classes and number of bytes allocated for them.
310 * @param[in] cptr Client requesting statistics.
312 void class_send_meminfo(struct Client* cptr)
314 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes: inuse: %d(%d)",
316 connClassAllocCount * sizeof(struct ConnectionClass));