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