9c00e848bc145f9c615eac23744c1c0308f1c0d1
[ircu2.10.12-pk.git] / ircd / class.c
1 /*
2  * IRC - Internet Relay Chat, ircd/class.c
3  * Copyright (C) 1990 Darren Reed
4  *
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)
8  * any later version.
9  *
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.
14  *
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.
18  */
19 /** @file
20  * @brief Implementation of connection class handling functions.
21  * @version $Id$
22  */
23 #include "config.h"
24
25 #include "class.h"
26 #include "client.h"
27 #include "ircd.h"
28 #include "ircd_alloc.h"
29 #include "ircd_features.h"
30 #include "ircd_reply.h"
31 #include "ircd_string.h"
32 #include "list.h"
33 #include "numeric.h"
34 #include "s_conf.h"
35 #include "s_debug.h"
36 #include "send.h"
37
38 #include <assert.h>
39
40 /** List of all connection classes. */
41 static struct ConnectionClass* connClassList = 0;
42 /** Number of allocated connection classes. */
43 static unsigned int connClassAllocCount;
44
45 /** Get start of connection class linked list. */
46 const struct ConnectionClass* get_class_list(void)
47 {
48   return connClassList;
49 }
50
51 /** Allocate a new connection class.
52  * @return Newly allocated connection class structure.
53  */
54 struct ConnectionClass* make_class(void)
55 {
56   struct ConnectionClass *tmp;
57
58   tmp = (struct ConnectionClass*) MyMalloc(sizeof(struct ConnectionClass));
59   tmp->ref_count = 1;
60   assert(0 != tmp);
61   ++connClassAllocCount;
62   return tmp;
63 }
64
65 /** Dereference a connection class.
66  * @param[in] p Connection class to dereference.
67  */
68 void free_class(struct ConnectionClass* p)
69 {
70   if (p)
71   {
72     assert(0 == p->valid);
73     if (p->cc_name)
74       MyFree(p->cc_name);
75     MyFree(p);
76     --connClassAllocCount;
77   }
78 }
79
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.
84  */
85 void init_class(void)
86 {
87   if (!connClassList)
88     connClassList = (struct ConnectionClass*) make_class();
89
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;
99 }
100
101 /** Mark current connection classes as invalid.
102  */
103 void class_mark_delete(void)
104 {
105   struct ConnectionClass* p;
106   assert(0 != connClassList);
107
108   for (p = connClassList->next; p; p = p->next)
109     p->valid = 0;
110 }
111
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
115  * configuration.
116  */
117 void class_delete_marked(void)
118 {
119   struct ConnectionClass* cl;
120   struct ConnectionClass* prev;
121
122   Debug((DEBUG_DEBUG, "Class check:"));
123
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)));
128     /*
129      * unlink marked classes, delete unreferenced ones
130      */
131     if (cl->valid)
132       prev = cl;
133     else
134     {
135       prev->next = cl->next;
136       if (0 == --cl->ref_count)
137         free_class(cl);
138     }
139   }
140 }
141
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.
145  */
146 char*
147 get_conf_class(const struct ConfItem* aconf)
148 {
149   if ((aconf) && (aconf->conn_class))
150     return (ConfClass(aconf));
151
152   Debug((DEBUG_DEBUG, "No Class For %s", (aconf) ? aconf->name : "*No Conf*"));
153
154   return NULL;
155 }
156
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.
160  */
161 int get_conf_ping(const struct ConfItem* aconf)
162 {
163   assert(0 != aconf);
164   if (aconf->conn_class)
165     return (ConfPingFreq(aconf));
166
167   Debug((DEBUG_DEBUG, "No Ping For %s", aconf->name));
168
169   return -1;
170 }
171
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.
175  */
176 char*
177 get_client_class(struct Client *acptr)
178 {
179   struct SLink *tmp;
180   struct ConnectionClass *cl;
181
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)
185     {
186       if (tmp->value.aconf && (cl = tmp->value.aconf->conn_class))
187         return ConClass(cl);
188     }
189   return "(null-class)";
190 }
191
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.
196  */
197 unsigned int get_con_freq(struct ConnectionClass * clptr)
198 {
199   if (clptr)
200     return (ConFreq(clptr));
201   else
202     return feature_int(FEAT_CONNECTFREQUENCY);
203 }
204
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
208  * to the parameters.
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.
214  */
215 void add_class(char *name, unsigned int ping, unsigned int confreq,
216                unsigned int maxli, unsigned int sendq)
217 {
218   struct ConnectionClass* t;
219   struct ConnectionClass* p;
220
221   t = find_class(name);
222   if ((t == connClassList) && (name != NULL))
223   {
224     p = (struct ConnectionClass *) make_class();
225     p->next = t->next;
226     t->next = p;
227   }
228   else
229   {
230     if (ConClass(t) != NULL)
231       MyFree(ConClass(t));
232     p = t;
233   }
234   Debug((DEBUG_DEBUG, "Add Class %s: cf: %u pf: %u ml: %u sq: %d",
235          name, confreq, ping, maxli, sendq));
236   ConClass(p) = name;
237   ConFreq(p) = confreq;
238   PingFreq(p) = ping;
239   MaxLinks(p) = maxli;
240   MaxSendq(p) = (sendq > 0) ?
241      sendq : feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
242   p->valid = 1;
243   if (p != t)
244     Links(p) = 0;
245 }
246
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).
250  */
251 struct ConnectionClass* find_class(const char *name)
252 {
253   struct ConnectionClass *cltmp;
254
255   for (cltmp = connClassList; cltmp; cltmp = cltmp->next) {
256     if (!ircd_strcmp(ConClass(cltmp), name))
257       return cltmp;
258   }
259   return connClassList;
260 }
261
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).
266  */
267 void
268 report_classes(struct Client *sptr, const struct StatDesc *sd,
269                char *param)
270 {
271   struct ConnectionClass *cltmp;
272
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),
276                Links(cltmp));
277 }
278
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.
282  */
283 unsigned int
284 get_sendq(struct Client *cptr)
285 {
286   assert(0 != cptr);
287   assert(0 != cli_local(cptr));
288
289   if (cli_max_sendq(cptr))
290     return cli_max_sendq(cptr);
291
292   else if (cli_confs(cptr)) {
293     struct SLink*     tmp;
294     struct ConnectionClass* cl;
295
296     for (tmp = cli_confs(cptr); tmp; tmp = tmp->next) {
297       if (!tmp->value.aconf || !(cl = tmp->value.aconf->conn_class))
298         continue;
299       if (ConClass(cl) != NULL) {
300         cli_max_sendq(cptr) = MaxSendq(cl);
301         return cli_max_sendq(cptr);
302       }
303     }
304   }
305   return feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
306 }
307
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.
311  */
312 void class_send_meminfo(struct Client* cptr)
313 {
314   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes: inuse: %d(%d)",
315              connClassAllocCount,
316              connClassAllocCount * sizeof(struct ConnectionClass));
317 }
318
319