fixed ssl.c bug when ssl backend returns IO_BLOCKED but IO engine doesn't get informe...
[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: class.c 1514 2005-10-06 00:37:31Z entrope $
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     connClassList->next   = 0;
97   }
98
99   /* We had better not try and free this... */
100   ConClass(connClassList) = "default";
101   PingFreq(connClassList) = feature_int(FEAT_PINGFREQUENCY);
102   ConFreq(connClassList)  = feature_int(FEAT_CONNECTFREQUENCY);
103   MaxLinks(connClassList) = feature_int(FEAT_MAXIMUM_LINKS);
104   MaxSendq(connClassList) = feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
105   connClassList->max_channels = 0;
106   connClassList->valid    = 1;
107   Links(connClassList)    = 1;
108 }
109
110 /** Mark current connection classes as invalid.
111  */
112 void class_mark_delete(void)
113 {
114   struct ConnectionClass* p;
115   assert(0 != connClassList);
116
117   for (p = connClassList->next; p; p = p->next)
118     p->valid = 0;
119 }
120
121 /** Unlink (and dereference) invalid connection classes.
122  * This is used in combination with class_mark_delete() during rehash
123  * to get rid of connection classes that are no longer in the
124  * configuration.
125  */
126 void class_delete_marked(void)
127 {
128   struct ConnectionClass* cl;
129   struct ConnectionClass* prev;
130
131   Debug((DEBUG_DEBUG, "Class check:"));
132
133   for (prev = cl = connClassList; cl; cl = prev->next) {
134     Debug((DEBUG_DEBUG, "Class %s : CF: %d PF: %d ML: %d LI: %d SQ: %d",
135            ConClass(cl), ConFreq(cl), PingFreq(cl), MaxLinks(cl),
136            Links(cl), MaxSendq(cl)));
137     /*
138      * unlink marked classes, delete unreferenced ones
139      */
140     if (cl->valid || Links(cl) > 1)
141       prev = cl;
142     else
143     {
144       prev->next = cl->next;
145       free_class(cl);
146     }
147   }
148 }
149
150 /** Get connection class name for a configuration item.
151  * @param[in] aconf Configuration item to check.
152  * @return Name of connection class associated with \a aconf.
153  */
154 char*
155 get_conf_class(const struct ConfItem* aconf)
156 {
157   if ((aconf) && (aconf->conn_class))
158     return (ConfClass(aconf));
159
160   Debug((DEBUG_DEBUG, "No Class For %s", (aconf) ? aconf->name : "*No Conf*"));
161
162   return NULL;
163 }
164
165 /** Get ping time for a configuration item.
166  * @param[in] aconf Configuration item to check.
167  * @return Ping time for connection class associated with \a aconf.
168  */
169 int get_conf_ping(const struct ConfItem* aconf)
170 {
171   assert(0 != aconf);
172   if (aconf->conn_class)
173     return (ConfPingFreq(aconf));
174
175   Debug((DEBUG_DEBUG, "No Ping For %s", aconf->name));
176
177   return -1;
178 }
179
180 /** Get connection class name for a particular client.
181  * @param[in] acptr Client to check.
182  * @return Name of connection class to which \a acptr belongs.
183  */
184 char*
185 get_client_class(struct Client *acptr)
186 {
187   struct SLink *tmp;
188   struct ConnectionClass *cl;
189
190   /* Return the most recent(first on LL) client class... */
191   if (acptr && !IsMe(acptr) && (cli_confs(acptr)))
192     for (tmp = cli_confs(acptr); tmp; tmp = tmp->next)
193     {
194       if (tmp->value.aconf && (cl = tmp->value.aconf->conn_class))
195         return ConClass(cl);
196     }
197   return "(null-class)";
198 }
199
200 /** Make sure we have a connection class named \a name.
201  * If one does not exist, create it.  Then set its ping frequency,
202  * connection frequency, maximum link count, and max SendQ according
203  * to the parameters.
204  * @param[in] name Connection class name.
205  * @param[in] ping Ping frequency for clients in this class.
206  * @param[in] confreq Connection frequency for clients.
207  * @param[in] maxli Maximum link count for class.
208  * @param[in] sendq Max SendQ for clients.
209  * @param[in] max_channels Maximum number of channels.
210  */
211 void add_class(char *name, unsigned int ping, unsigned int confreq,
212                unsigned int maxli, unsigned int sendq, unsigned int max_channels)
213 {
214   struct ConnectionClass* p;
215
216   Debug((DEBUG_DEBUG, "Add Class %s: cf: %u pf: %u ml: %u sq: %d mc: %u",
217          name, confreq, ping, maxli, sendq, max_channels));
218   assert(name != NULL);
219   p = do_find_class(name, 1);
220   if (!p)
221     p = make_class();
222   else
223     MyFree(ConClass(p));
224   ConClass(p) = name;
225   ConFreq(p) = confreq;
226   PingFreq(p) = ping;
227   MaxLinks(p) = maxli;
228   p->max_channels = max_channels;
229   MaxSendq(p) = (sendq > 0) ?
230      sendq : feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
231   p->valid = 1;
232 }
233
234 /** Find a connection class by name.
235  * @param[in] name Name of connection class to search for.
236  * @param[in] extras If non-zero, include unreferenced classes.
237  * @return Pointer to connection class structure (or NULL if none match).
238  */
239 struct ConnectionClass* do_find_class(const char *name, int extras)
240 {
241   struct ConnectionClass *cltmp;
242
243   for (cltmp = connClassList; cltmp; cltmp = cltmp->next) {
244     if (!cltmp->valid && !extras)
245       continue;
246     if (!ircd_strcmp(ConClass(cltmp), name))
247       return cltmp;
248   }
249   return NULL;
250 }
251
252 /** Report connection classes to a client.
253  * @param[in] sptr Client requesting statistics.
254  * @param[in] sd Stats descriptor for request (ignored).
255  * @param[in] param Extra parameter from user (ignored).
256  */
257 void
258 report_classes(struct Client *sptr, const struct StatDesc *sd,
259                char *param)
260 {
261   struct ConnectionClass *cltmp;
262
263   for (cltmp = connClassList; cltmp; cltmp = cltmp->next)
264     send_reply(sptr, RPL_STATSYLINE, (cltmp->valid ? 'Y' : 'y'),
265                ConClass(cltmp), PingFreq(cltmp), ConFreq(cltmp),
266                MaxLinks(cltmp), MaxSendq(cltmp), MaxChannels(cltmp), Links(cltmp) - 1);
267 }
268
269 /** Return maximum SendQ length for a client.
270  * @param[in] cptr Local client to check.
271  * @return Number of bytes allowed in SendQ for \a cptr.
272  */
273 unsigned int
274 get_sendq(struct Client *cptr)
275 {
276   assert(0 != cptr);
277   assert(0 != cli_local(cptr));
278
279   if (cli_max_sendq(cptr))
280     return cli_max_sendq(cptr);
281
282   else if (cli_confs(cptr)) {
283     struct SLink*     tmp;
284     struct ConnectionClass* cl;
285
286     for (tmp = cli_confs(cptr); tmp; tmp = tmp->next) {
287       if (!tmp->value.aconf || !(cl = tmp->value.aconf->conn_class))
288         continue;
289       if (ConClass(cl) != NULL) {
290         cli_max_sendq(cptr) = MaxSendq(cl);
291         return cli_max_sendq(cptr);
292       }
293     }
294   }
295   return feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
296 }
297
298 /** Report connection class memory statistics to a client.
299  * Send number of classes and number of bytes allocated for them.
300  * @param[in] cptr Client requesting statistics.
301  */
302 void class_send_meminfo(struct Client* cptr)
303 {
304   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes: inuse: %d(%d)",
305              connClassAllocCount,
306              connClassAllocCount * sizeof(struct ConnectionClass));
307 }
308
309