f3772478347dddfa6893f8adc48240d257a08561
[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  * $Id$
20  */
21 #include "config.h"
22
23 #include "class.h"
24 #include "client.h"
25 #include "ircd.h"
26 #include "ircd_alloc.h"
27 #include "ircd_features.h"
28 #include "ircd_reply.h"
29 #include "list.h"
30 #include "numeric.h"
31 #include "s_conf.h"
32 #include "s_debug.h"
33 #include "send.h"
34
35 #include <assert.h>
36
37 #define BAD_CONF_CLASS          ((unsigned int)-1)
38 #define BAD_PING                ((unsigned int)-2)
39 #define BAD_CLIENT_CLASS        ((unsigned int)-3)
40
41 static struct ConnectionClass* connClassList = 0;
42 static unsigned int connClassAllocCount;
43
44 const struct ConnectionClass* get_class_list(void)
45 {
46   return connClassList;
47 }
48
49 struct ConnectionClass* make_class(void)
50 {
51   struct ConnectionClass *tmp;
52
53   tmp = (struct ConnectionClass*) MyMalloc(sizeof(struct ConnectionClass));
54   assert(0 != tmp);
55   ++connClassAllocCount;
56   return tmp;
57 }
58
59 void free_class(struct ConnectionClass* p)
60 {
61   if (p) {
62     assert(0 == p->valid);
63     MyFree(p);
64     --connClassAllocCount;
65   }
66 }
67
68 /*
69  * init_class - initialize the class list
70  */
71 void init_class(void)
72 {
73   if (!connClassList)
74     connClassList = (struct ConnectionClass*) make_class();
75
76   ConClass(connClassList) = 0;
77   PingFreq(connClassList) = feature_int(FEAT_PINGFREQUENCY);
78   ConFreq(connClassList)  = feature_int(FEAT_CONNECTFREQUENCY);
79   MaxLinks(connClassList) = feature_int(FEAT_MAXIMUM_LINKS);
80   MaxSendq(connClassList) = feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
81   connClassList->valid    = 1;
82   Links(connClassList)    = 0;
83   connClassList->next     = 0;
84 }
85
86 /*
87  * class_mark_delete - mark classes for delete
88  * We don't delete the class table, rather mark all entries
89  * for deletion. The table is cleaned up by class_delete_marked(). - avalon
90  * XXX - This destroys data
91  */
92 void class_mark_delete(void)
93 {
94   struct ConnectionClass* p;
95   assert(0 != connClassList);
96
97   for (p = connClassList->next; p; p = p->next)
98     p->valid = 0;
99 }
100
101 /*
102  * class_delete_marked
103  * delete classes marked for deletion
104  * XXX - memory leak, no one deletes classes that become unused
105  * later
106  */
107 void class_delete_marked(void)
108 {
109   struct ConnectionClass* cl;
110   struct ConnectionClass* prev;
111
112   Debug((DEBUG_DEBUG, "Class check:"));
113
114   for (prev = cl = connClassList; cl; cl = prev->next) {
115     Debug((DEBUG_DEBUG, "Class %d : CF: %d PF: %d ML: %d LI: %d SQ: %d",
116            ConClass(cl), ConFreq(cl), PingFreq(cl), MaxLinks(cl), Links(cl), MaxSendq(cl)));
117     /*
118      * unlink marked classes, delete unreferenced ones
119      */
120     if (cl->valid)
121       prev = cl;
122     else {
123       prev->next = cl->next;
124       if (0 == cl->ref_count)
125         free_class(cl);
126     }
127   }
128 }
129
130 unsigned int get_conf_class(const struct ConfItem* aconf)
131 {
132   if ((aconf) && (aconf->conn_class))
133     return (ConfClass(aconf));
134
135   Debug((DEBUG_DEBUG, "No Class For %s", (aconf) ? aconf->name : "*No Conf*"));
136
137   return (BAD_CONF_CLASS);
138 }
139
140 int get_conf_ping(const struct ConfItem* aconf)
141 {
142   assert(0 != aconf);
143   if (aconf->conn_class)
144     return (ConfPingFreq(aconf));
145
146   Debug((DEBUG_DEBUG, "No Ping For %s", aconf->name));
147
148   return -1;
149 }
150
151 unsigned int get_client_class(struct Client *acptr)
152 {
153   struct SLink *tmp;
154   struct ConnectionClass *cl;
155   unsigned int retc = BAD_CLIENT_CLASS;
156
157   if (acptr && !IsMe(acptr) && (cli_confs(acptr)))
158     for (tmp = cli_confs(acptr); tmp; tmp = tmp->next)
159     {
160       if (!tmp->value.aconf || !(cl = tmp->value.aconf->conn_class))
161         continue;
162       if (ConClass(cl) > retc || retc == BAD_CLIENT_CLASS)
163         retc = ConClass(cl);
164     }
165
166   Debug((DEBUG_DEBUG, "Returning Class %d For %s", retc, cli_name(acptr)));
167
168   return (retc);
169 }
170
171 unsigned int get_client_ping(struct Client *acptr)
172 {
173   unsigned int ping = 0;
174   unsigned int ping2;
175   struct ConfItem *aconf;
176   struct SLink *link;
177
178   link = cli_confs(acptr);
179
180   if (link) {
181     while (link) {
182       aconf = link->value.aconf;
183       if (aconf->status & (CONF_CLIENT | CONF_SERVER)) {
184         ping2 = get_conf_ping(aconf);
185         if ((ping2 != BAD_PING) && ((ping > ping2) || !ping))
186           ping = ping2;
187       }
188       link = link->next;
189     }
190   }
191   else {
192     ping = feature_int(FEAT_PINGFREQUENCY);
193     Debug((DEBUG_DEBUG, "No Attached Confs for: %s", cli_name(acptr)));
194   }
195   if (ping <= 0)
196     ping = feature_int(FEAT_PINGFREQUENCY);
197   Debug((DEBUG_DEBUG, "Client %s Ping %d", cli_name(acptr), ping));
198   return (ping);
199 }
200
201 unsigned int get_con_freq(struct ConnectionClass * clptr)
202 {
203   if (clptr)
204     return (ConFreq(clptr));
205   else
206     return feature_int(FEAT_CONNECTFREQUENCY);
207 }
208
209 /*
210  * When adding a class, check to see if it is already present first.
211  * if so, then update the information for that class, rather than create
212  * a new entry for it and later delete the old entry.
213  * if no present entry is found, then create a new one and add it in
214  * immeadiately after the first one (class 0).
215  */
216 void add_class(unsigned int conClass, unsigned int ping, unsigned int confreq,
217                unsigned int maxli, unsigned int sendq)
218 {
219   struct ConnectionClass* t;
220   struct ConnectionClass* p;
221
222   t = find_class(conClass);
223   if ((t == connClassList) && (conClass != 0))
224   {
225     p = (struct ConnectionClass *) make_class();
226     p->next = t->next;
227     t->next = p;
228   }
229   else
230     p = t;
231   Debug((DEBUG_DEBUG, "Add Class %u: cf: %u pf: %u ml: %u sq: %d",
232          conClass, confreq, ping, maxli, sendq));
233   ConClass(p) = conClass;
234   ConFreq(p) = confreq;
235   PingFreq(p) = ping;
236   MaxLinks(p) = maxli;
237   MaxSendq(p) = (sendq > 0) ? sendq : feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
238   p->valid = 1;
239   if (p != t)
240     Links(p) = 0;
241 }
242
243 struct ConnectionClass* find_class(unsigned int cclass)
244 {
245   struct ConnectionClass *cltmp;
246
247   for (cltmp = connClassList; cltmp; cltmp = cltmp->next) {
248     if (ConClass(cltmp) == cclass)
249       return cltmp;
250   }
251   return connClassList;
252 }
253
254 void report_classes(struct Client *sptr)
255 {
256   struct ConnectionClass *cltmp;
257
258   for (cltmp = connClassList; cltmp; cltmp = cltmp->next)
259     send_reply(sptr, RPL_STATSYLINE, 'Y', ConClass(cltmp), PingFreq(cltmp),
260                ConFreq(cltmp), MaxLinks(cltmp), MaxSendq(cltmp),
261                Links(cltmp));
262 }
263
264 unsigned int get_sendq(struct Client *cptr)
265 {
266   assert(0 != cptr);
267   assert(0 != cli_local(cptr));
268
269   if (cli_max_sendq(cptr))
270     return cli_max_sendq(cptr);
271
272   else if (cli_confs(cptr)) {
273     struct SLink*     tmp;
274     struct ConnectionClass* cl;
275
276     for (tmp = cli_confs(cptr); tmp; tmp = tmp->next) {
277       if (!tmp->value.aconf || !(cl = tmp->value.aconf->conn_class))
278         continue;
279       if (ConClass(cl) != BAD_CLIENT_CLASS) {
280         cli_max_sendq(cptr) = MaxSendq(cl);
281         return cli_max_sendq(cptr);
282       }
283     }
284   }
285   return feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
286 }
287
288 void class_send_meminfo(struct Client* cptr)
289 {
290   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes: inuse: %d(%d)",
291              connClassAllocCount, connClassAllocCount * sizeof(struct ConnectionClass));
292 }
293
294