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