Add support for CIDR-based MOTD masks.
[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;
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  * If #connClassList is not null, insert the new class just after it.
53  * @return Newly allocated connection class structure.
54  */
55 struct ConnectionClass* make_class(void)
56 {
57   struct ConnectionClass *tmp;
58
59   tmp = (struct ConnectionClass*) MyCalloc(1, sizeof(struct ConnectionClass));
60   assert(0 != tmp);
61   tmp->ref_count = 1;
62   if (connClassList)
63   {
64     tmp->next = connClassList->next;
65     connClassList->next = tmp;
66   }
67   ++connClassAllocCount;
68   return tmp;
69 }
70
71 /** Dereference a connection class.
72  * @param[in] p Connection class to dereference.
73  */
74 void free_class(struct ConnectionClass* p)
75 {
76   if (p)
77   {
78     assert(0 == p->valid);
79     if (p->cc_name)
80       MyFree(p->cc_name);
81     MyFree(p);
82     --connClassAllocCount;
83   }
84 }
85
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.
90  */
91 void init_class(void)
92 {
93   if (!connClassList)
94     connClassList = (struct ConnectionClass*) make_class();
95
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;
105 }
106
107 /** Mark current connection classes as invalid.
108  */
109 void class_mark_delete(void)
110 {
111   struct ConnectionClass* p;
112   assert(0 != connClassList);
113
114   for (p = connClassList->next; p; p = p->next)
115     p->valid = 0;
116 }
117
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
121  * configuration.
122  */
123 void class_delete_marked(void)
124 {
125   struct ConnectionClass* cl;
126   struct ConnectionClass* prev;
127
128   Debug((DEBUG_DEBUG, "Class check:"));
129
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)));
134     /*
135      * unlink marked classes, delete unreferenced ones
136      */
137     if (cl->valid)
138       prev = cl;
139     else
140     {
141       prev->next = cl->next;
142       if (0 == --cl->ref_count)
143         free_class(cl);
144     }
145   }
146 }
147
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.
151  */
152 char*
153 get_conf_class(const struct ConfItem* aconf)
154 {
155   if ((aconf) && (aconf->conn_class))
156     return (ConfClass(aconf));
157
158   Debug((DEBUG_DEBUG, "No Class For %s", (aconf) ? aconf->name : "*No Conf*"));
159
160   return NULL;
161 }
162
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.
166  */
167 int get_conf_ping(const struct ConfItem* aconf)
168 {
169   assert(0 != aconf);
170   if (aconf->conn_class)
171     return (ConfPingFreq(aconf));
172
173   Debug((DEBUG_DEBUG, "No Ping For %s", aconf->name));
174
175   return -1;
176 }
177
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.
181  */
182 char*
183 get_client_class(struct Client *acptr)
184 {
185   struct SLink *tmp;
186   struct ConnectionClass *cl;
187
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)
191     {
192       if (tmp->value.aconf && (cl = tmp->value.aconf->conn_class))
193         return ConClass(cl);
194     }
195   return "(null-class)";
196 }
197
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.
202  */
203 unsigned int get_con_freq(struct ConnectionClass * clptr)
204 {
205   if (clptr)
206     return (ConFreq(clptr));
207   else
208     return feature_int(FEAT_CONNECTFREQUENCY);
209 }
210
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
214  * to the parameters.
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.
220  */
221 void add_class(char *name, unsigned int ping, unsigned int confreq,
222                unsigned int maxli, unsigned int sendq)
223 {
224   struct ConnectionClass* p;
225
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);
230   if (!p)
231     p = make_class();
232   else
233     MyFree(ConClass(p));
234   ConClass(p) = name;
235   ConFreq(p) = confreq;
236   PingFreq(p) = ping;
237   MaxLinks(p) = maxli;
238   MaxSendq(p) = (sendq > 0) ?
239      sendq : feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
240   p->valid = 1;
241 }
242
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).
246  */
247 struct ConnectionClass* find_class(const char *name)
248 {
249   struct ConnectionClass *cltmp;
250
251   for (cltmp = connClassList; cltmp; cltmp = cltmp->next) {
252     if (!ircd_strcmp(ConClass(cltmp), name))
253       return cltmp;
254   }
255   return NULL;
256 }
257
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).
262  */
263 void
264 report_classes(struct Client *sptr, const struct StatDesc *sd,
265                char *param)
266 {
267   struct ConnectionClass *cltmp;
268
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),
272                Links(cltmp));
273 }
274
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.
278  */
279 unsigned int
280 get_sendq(struct Client *cptr)
281 {
282   assert(0 != cptr);
283   assert(0 != cli_local(cptr));
284
285   if (cli_max_sendq(cptr))
286     return cli_max_sendq(cptr);
287
288   else if (cli_confs(cptr)) {
289     struct SLink*     tmp;
290     struct ConnectionClass* cl;
291
292     for (tmp = cli_confs(cptr); tmp; tmp = tmp->next) {
293       if (!tmp->value.aconf || !(cl = tmp->value.aconf->conn_class))
294         continue;
295       if (ConClass(cl) != NULL) {
296         cli_max_sendq(cptr) = MaxSendq(cl);
297         return cli_max_sendq(cptr);
298       }
299     }
300   }
301   return feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
302 }
303
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.
307  */
308 void class_send_meminfo(struct Client* cptr)
309 {
310   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes: inuse: %d(%d)",
311              connClassAllocCount,
312              connClassAllocCount * sizeof(struct ConnectionClass));
313 }
314
315