Author: Stephane Thiell <mbuna@undernet.org>
[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_con_freq(struct ConnectionClass * clptr)
173 {
174   if (clptr)
175     return (ConFreq(clptr));
176   else
177     return feature_int(FEAT_CONNECTFREQUENCY);
178 }
179
180 /*
181  * When adding a class, check to see if it is already present first.
182  * if so, then update the information for that class, rather than create
183  * a new entry for it and later delete the old entry.
184  * if no present entry is found, then create a new one and add it in
185  * immeadiately after the first one (class 0).
186  */
187 void add_class(char *name, unsigned int ping, unsigned int confreq,
188                unsigned int maxli, unsigned int sendq)
189 {
190   struct ConnectionClass* t;
191   struct ConnectionClass* p;
192
193   t = find_class(name);
194   if ((t == connClassList) && (name != NULL))
195   {
196     p = (struct ConnectionClass *) make_class();
197     p->next = t->next;
198     t->next = p;
199   }
200   else
201     p = t;
202   Debug((DEBUG_DEBUG, "Add Class %s: cf: %u pf: %u ml: %u sq: %d",
203          name, confreq, ping, maxli, sendq));
204   ConClass(p) = name;
205   ConFreq(p) = confreq;
206   PingFreq(p) = ping;
207   MaxLinks(p) = maxli;
208   MaxSendq(p) = (sendq > 0) ?
209      sendq : feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
210   p->valid = 1;
211   if (p != t)
212     Links(p) = 0;
213 }
214
215 struct ConnectionClass* find_class(const char *name)
216 {
217   struct ConnectionClass *cltmp;
218
219   for (cltmp = connClassList; cltmp; cltmp = cltmp->next) {
220     if (!ircd_strcmp(ConClass(cltmp), name))
221       return cltmp;
222   }
223   return connClassList;
224 }
225
226 void
227 report_classes(struct Client *sptr)
228 {
229   struct ConnectionClass *cltmp;
230
231   for (cltmp = connClassList; cltmp; cltmp = cltmp->next)
232     send_reply(sptr, RPL_STATSYLINE, 'Y', ConClass(cltmp), PingFreq(cltmp),
233                ConFreq(cltmp), MaxLinks(cltmp), MaxSendq(cltmp),
234                Links(cltmp));
235 }
236
237 unsigned int
238 get_sendq(struct Client *cptr)
239 {
240   assert(0 != cptr);
241   assert(0 != cli_local(cptr));
242
243   if (cli_max_sendq(cptr))
244     return cli_max_sendq(cptr);
245
246   else if (cli_confs(cptr)) {
247     struct SLink*     tmp;
248     struct ConnectionClass* cl;
249
250     for (tmp = cli_confs(cptr); tmp; tmp = tmp->next) {
251       if (!tmp->value.aconf || !(cl = tmp->value.aconf->conn_class))
252         continue;
253       if (ConClass(cl) != NULL) {
254         cli_max_sendq(cptr) = MaxSendq(cl);
255         return cli_max_sendq(cptr);
256       }
257     }
258   }
259   return feature_int(FEAT_DEFAULTMAXSENDQLENGTH);
260 }
261
262 void class_send_meminfo(struct Client* cptr)
263 {
264   send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Classes: inuse: %d(%d)",
265              connClassAllocCount, connClassAllocCount * sizeof(struct ConnectionClass));
266 }
267
268