2509f6577be1e7b58fd5f924a2218cf5b263a946
[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_log.h"
31 #include "ircd_reply.h"
32 #include "ircd_string.h"
33 #include "list.h"
34 #include "numeric.h"
35 #include "s_conf.h"
36 #include "s_debug.h"
37 #include "send.h"
38
39 /* #include <assert.h> -- Now using assert in ircd_log.h */
40
41 /** List of all connection classes. */
42 static struct ConnectionClass* connClassList;
43 /** Number of allocated connection classes. */
44 static unsigned int connClassAllocCount;
45
46 /** Get start of connection class linked list. */
47 const struct ConnectionClass* get_class_list(void)
48 {
49   return connClassList;
50 }
51
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.
55  */
56 struct ConnectionClass* make_class(void)
57 {
58   struct ConnectionClass *tmp;
59
60   tmp = (struct ConnectionClass*) MyCalloc(1, sizeof(struct ConnectionClass));
61   assert(0 != tmp);
62   tmp->ref_count = 1;
63   if (connClassList)
64   {
65     tmp->next = connClassList->next;
66     connClassList->next = tmp;
67   }
68   ++connClassAllocCount;
69   return tmp;
70 }
71
72 /** Dereference a connection class.
73  * @param[in] p Connection class to dereference.
74  */
75 void free_class(struct ConnectionClass* p)
76 {
77   if (p)
78   {
79     assert(0 == p->valid);
80     MyFree(p->cc_name);
81     MyFree(p->default_umode);
82     MyFree(p);
83     --connClassAllocCount;
84   }
85 }
86
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.
91  */
92 void init_class(void)
93 {
94   if (!connClassList)
95     connClassList = (struct ConnectionClass*) make_class();
96
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;
106 }
107
108 /** Mark current connection classes as invalid.
109  */
110 void class_mark_delete(void)
111 {
112   struct ConnectionClass* p;
113   assert(0 != connClassList);
114
115   for (p = connClassList->next; p; p = p->next)
116     p->valid = 0;
117 }
118
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
122  * configuration.
123  */
124 void class_delete_marked(void)
125 {
126   struct ConnectionClass* cl;
127   struct ConnectionClass* prev;
128
129   Debug((DEBUG_DEBUG, "Class check:"));
130
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)));
135     /*
136      * unlink marked classes, delete unreferenced ones
137      */
138     if (cl->valid)
139       prev = cl;
140     else
141     {
142       prev->next = cl->next;
143       if (0 == --cl->ref_count)
144         free_class(cl);
145     }
146   }
147 }
148
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.
152  */
153 char*
154 get_conf_class(const struct ConfItem* aconf)
155 {
156   if ((aconf) && (aconf->conn_class))
157     return (ConfClass(aconf));
158
159   Debug((DEBUG_DEBUG, "No Class For %s", (aconf) ? aconf->name : "*No Conf*"));
160
161   return NULL;
162 }
163
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.
167  */
168 int get_conf_ping(const struct ConfItem* aconf)
169 {
170   assert(0 != aconf);
171   if (aconf->conn_class)
172     return (ConfPingFreq(aconf));
173
174   Debug((DEBUG_DEBUG, "No Ping For %s", aconf->name));
175
176   return -1;
177 }
178
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.
182  */
183 char*
184 get_client_class(struct Client *acptr)
185 {
186   struct SLink *tmp;
187   struct ConnectionClass *cl;
188
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)
192     {
193       if (tmp->value.aconf && (cl = tmp->value.aconf->conn_class))
194         return ConClass(cl);
195     }
196   return "(null-class)";
197 }
198
199 /** Get connection interval for a connection class.
200  * @param[in] clptr Connection class to check (or NULL).
201  * @return If \a clptr != NULL, its connection frequency; else default
202  * connection frequency.
203  */
204 unsigned int get_con_freq(struct ConnectionClass * clptr)
205 {
206   if (clptr)
207     return (ConFreq(clptr));
208   else
209     return feature_int(FEAT_CONNECTFREQUENCY);
210 }
211
212 /** Make sure we have a connection class named \a name.
213  * If one does not exist, create it.  Then set its ping frequency,
214  * connection frequency, maximum link count, and max SendQ according
215  * to the parameters.
216  * @param[in] name Connection class name.
217  * @param[in] ping Ping frequency for clients in this class.
218  * @param[in] confreq Connection frequency for clients.
219  * @param[in] maxli Maximum link count for class.
220  * @param[in] sendq Max SendQ for clients.
221  */
222 void add_class(char *name, unsigned int ping, unsigned int confreq,
223                unsigned int maxli, unsigned int sendq)
224 {
225   struct ConnectionClass* p;
226
227   Debug((DEBUG_DEBUG, "Add Class %s: cf: %u pf: %u ml: %u sq: %d",
228          name, confreq, ping, maxli, sendq));
229   assert(name != NULL);
230   p = find_class(name);
231   if (!p)
232     p = make_class();
233   else
234     MyFree(ConClass(p));
235   ConClass(p) = name;
236   ConFreq(p) = confreq;
237   PingFreq(p) = ping;
238   MaxLinks(p) = maxli;
239   MaxSendq(p) = (sendq > 0) ?
240      sendq : feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
241   p->valid = 1;
242 }
243
244 /** Find a connection class by name.
245  * @param[in] name Name of connection class to search for.
246  * @return Pointer to connection class structure (or NULL if none match).
247  */
248 struct ConnectionClass* find_class(const char *name)
249 {
250   struct ConnectionClass *cltmp;
251
252   for (cltmp = connClassList; cltmp; cltmp = cltmp->next) {
253     if (!ircd_strcmp(ConClass(cltmp), name))
254       return cltmp;
255   }
256   return NULL;
257 }
258
259 /** Report connection classes to a client.
260  * @param[in] sptr Client requesting statistics.
261  * @param[in] sd Stats descriptor for request (ignored).
262  * @param[in] param Extra parameter from user (ignored).
263  */
264 void
265 report_classes(struct Client *sptr, const struct StatDesc *sd,
266                char *param)
267 {
268   struct ConnectionClass *cltmp;
269
270   for (cltmp = connClassList; cltmp; cltmp = cltmp->next)
271     send_reply(sptr, RPL_STATSYLINE, 'Y', ConClass(cltmp), PingFreq(cltmp),
272                ConFreq(cltmp), MaxLinks(cltmp), MaxSendq(cltmp),
273                Links(cltmp));
274 }
275
276 /** Return maximum SendQ length for a client.
277  * @param[in] cptr Local client to check.
278  * @return Number of bytes allowed in SendQ for \a cptr.
279  */
280 unsigned int
281 get_sendq(struct Client *cptr)
282 {
283   assert(0 != cptr);
284   assert(0 != cli_local(cptr));
285
286   if (cli_max_sendq(cptr))
287     return cli_max_sendq(cptr);
288
289   else if (cli_confs(cptr)) {
290     struct SLink*     tmp;
291     struct ConnectionClass* cl;
292
293     for (tmp = cli_confs(cptr); tmp; tmp = tmp->next) {
294       if (!tmp->value.aconf || !(cl = tmp->value.aconf->conn_class))
295         continue;
296       if (ConClass(cl) != NULL) {
297         cli_max_sendq(cptr) = MaxSendq(cl);
298         return cli_max_sendq(cptr);
299       }
300     }
301   }
302   return feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
303 }
304
305 /** Report connection class memory statistics to a client.
306  * Send number of classes and number of bytes allocated for them.
307  * @param[in] cptr Client requesting statistics.
308  */
309 void class_send_meminfo(struct Client* cptr)
310 {
311   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes: inuse: %d(%d)",
312              connClassAllocCount,
313              connClassAllocCount * sizeof(struct ConnectionClass));
314 }
315
316