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;
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 * If #connClassList is not null, insert the new class just after it.
53 * @return Newly allocated connection class structure.
55 struct ConnectionClass* make_class(void)
57 struct ConnectionClass *tmp;
59 tmp = (struct ConnectionClass*) MyCalloc(1, sizeof(struct ConnectionClass));
64 tmp->next = connClassList->next;
65 connClassList->next = tmp;
67 ++connClassAllocCount;
71 /** Dereference a connection class.
72 * @param[in] p Connection class to dereference.
74 void free_class(struct ConnectionClass* p)
78 assert(0 == p->valid);
82 --connClassAllocCount;
86 /** Initialize the connection class list.
87 * A connection class named "default" is created, with ping frequency,
88 * connection frequency, maximum links and max SendQ values from the
89 * corresponding configuration features.
94 connClassList = (struct ConnectionClass*) make_class();
96 /* We had better not try and free this... */
97 ConClass(connClassList) = "default";
98 PingFreq(connClassList) = feature_int(FEAT_PINGFREQUENCY);
99 ConFreq(connClassList) = feature_int(FEAT_CONNECTFREQUENCY);
100 MaxLinks(connClassList) = feature_int(FEAT_MAXIMUM_LINKS);
101 MaxSendq(connClassList) = feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
102 connClassList->valid = 1;
103 Links(connClassList) = 0;
104 connClassList->next = 0;
107 /** Mark current connection classes as invalid.
109 void class_mark_delete(void)
111 struct ConnectionClass* p;
112 assert(0 != connClassList);
114 for (p = connClassList->next; p; p = p->next)
118 /** Unlink (and dereference) invalid connection classes.
119 * This is used in combination with class_mark_delete() during rehash
120 * to get rid of connection classes that are no longer in the
123 void class_delete_marked(void)
125 struct ConnectionClass* cl;
126 struct ConnectionClass* prev;
128 Debug((DEBUG_DEBUG, "Class check:"));
130 for (prev = cl = connClassList; cl; cl = prev->next) {
131 Debug((DEBUG_DEBUG, "Class %s : CF: %d PF: %d ML: %d LI: %d SQ: %d",
132 ConClass(cl), ConFreq(cl), PingFreq(cl), MaxLinks(cl),
133 Links(cl), MaxSendq(cl)));
135 * unlink marked classes, delete unreferenced ones
141 prev->next = cl->next;
142 if (0 == --cl->ref_count)
148 /** Get connection class name for a configuration item.
149 * @param[in] aconf Configuration item to check.
150 * @return Name of connection class associated with \a aconf.
153 get_conf_class(const struct ConfItem* aconf)
155 if ((aconf) && (aconf->conn_class))
156 return (ConfClass(aconf));
158 Debug((DEBUG_DEBUG, "No Class For %s", (aconf) ? aconf->name : "*No Conf*"));
163 /** Get ping time for a configuration item.
164 * @param[in] aconf Configuration item to check.
165 * @return Ping time for connection class associated with \a aconf.
167 int get_conf_ping(const struct ConfItem* aconf)
170 if (aconf->conn_class)
171 return (ConfPingFreq(aconf));
173 Debug((DEBUG_DEBUG, "No Ping For %s", aconf->name));
178 /** Get connection class name for a particular client.
179 * @param[in] acptr Client to check.
180 * @return Name of connection class to which \a acptr belongs.
183 get_client_class(struct Client *acptr)
186 struct ConnectionClass *cl;
188 /* Return the most recent(first on LL) client class... */
189 if (acptr && !IsMe(acptr) && (cli_confs(acptr)))
190 for (tmp = cli_confs(acptr); tmp; tmp = tmp->next)
192 if (tmp->value.aconf && (cl = tmp->value.aconf->conn_class))
195 return "(null-class)";
198 /** Get connection interval for a connection class.
199 * @param[in] clptr Connection class to check (or NULL).
200 * @return If \a clptr != NULL, its connection frequency; else default
201 * connection frequency.
203 unsigned int get_con_freq(struct ConnectionClass * clptr)
206 return (ConFreq(clptr));
208 return feature_int(FEAT_CONNECTFREQUENCY);
211 /** Make sure we have a connection class named \a name.
212 * If one does not exist, create it. Then set its ping frequency,
213 * connection frequency, maximum link count, and max SendQ according
215 * @param[in] name Connection class name.
216 * @param[in] ping Ping frequency for clients in this class.
217 * @param[in] confreq Connection frequency for clients.
218 * @param[in] maxli Maximum link count for class.
219 * @param[in] sendq Max SendQ for clients.
221 void add_class(char *name, unsigned int ping, unsigned int confreq,
222 unsigned int maxli, unsigned int sendq)
224 struct ConnectionClass* p;
226 Debug((DEBUG_DEBUG, "Add Class %s: cf: %u pf: %u ml: %u sq: %d",
227 name, confreq, ping, maxli, sendq));
228 assert(name != NULL);
229 p = find_class(name);
235 ConFreq(p) = confreq;
238 MaxSendq(p) = (sendq > 0) ?
239 sendq : feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
243 /** Find a connection class by name.
244 * @param[in] name Name of connection class to search for.
245 * @return Pointer to connection class structure (or NULL if none match).
247 struct ConnectionClass* find_class(const char *name)
249 struct ConnectionClass *cltmp;
251 for (cltmp = connClassList; cltmp; cltmp = cltmp->next) {
252 if (!ircd_strcmp(ConClass(cltmp), name))
258 /** Report connection classes to a client.
259 * @param[in] sptr Client requesting statistics.
260 * @param[in] sd Stats descriptor for request (ignored).
261 * @param[in] param Extra parameter from user (ignored).
264 report_classes(struct Client *sptr, const struct StatDesc *sd,
267 struct ConnectionClass *cltmp;
269 for (cltmp = connClassList; cltmp; cltmp = cltmp->next)
270 send_reply(sptr, RPL_STATSYLINE, 'Y', ConClass(cltmp), PingFreq(cltmp),
271 ConFreq(cltmp), MaxLinks(cltmp), MaxSendq(cltmp),
275 /** Return maximum SendQ length for a client.
276 * @param[in] cptr Local client to check.
277 * @return Number of bytes allowed in SendQ for \a cptr.
280 get_sendq(struct Client *cptr)
283 assert(0 != cli_local(cptr));
285 if (cli_max_sendq(cptr))
286 return cli_max_sendq(cptr);
288 else if (cli_confs(cptr)) {
290 struct ConnectionClass* cl;
292 for (tmp = cli_confs(cptr); tmp; tmp = tmp->next) {
293 if (!tmp->value.aconf || !(cl = tmp->value.aconf->conn_class))
295 if (ConClass(cl) != NULL) {
296 cli_max_sendq(cptr) = MaxSendq(cl);
297 return cli_max_sendq(cptr);
301 return feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
304 /** Report connection class memory statistics to a client.
305 * Send number of classes and number of bytes allocated for them.
306 * @param[in] cptr Client requesting statistics.
308 void class_send_meminfo(struct Client* cptr)
310 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes: inuse: %d(%d)",
312 connClassAllocCount * sizeof(struct ConnectionClass));