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