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