Restore stats_spy hook that was removed in commit 401f2454671ca233e35b0e6e4f3fa4c43cd...
[seven-1.x.git] / src / class.c
blob54da1158fffd48c33e085b30b249023f8c4809e0
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * class.c: Controls connection classes.
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
25 #include "stdinc.h"
26 #include "config.h"
28 #include "tools.h"
29 #include "class.h"
30 #include "client.h"
31 #include "common.h"
32 #include "ircd.h"
33 #include "numeric.h"
34 #include "s_conf.h"
35 #include "s_newconf.h"
36 #include "send.h"
37 #include "irc_string.h"
38 #include "memory.h"
39 #include "patricia.h"
41 #define BAD_CONF_CLASS -1
42 #define BAD_PING -2
43 #define BAD_CLIENT_CLASS -3
45 dlink_list class_list;
46 struct Class *default_class;
48 struct Class *
49 make_class(void)
51 struct Class *tmp;
53 tmp = (struct Class *) MyMalloc(sizeof(struct Class));
55 ConFreq(tmp) = DEFAULT_CONNECTFREQUENCY;
56 PingFreq(tmp) = DEFAULT_PINGFREQUENCY;
57 MaxUsers(tmp) = 1;
58 MaxSendq(tmp) = DEFAULT_SENDQ;
60 tmp->ip_limits = New_Patricia(PATRICIA_BITS);
61 return tmp;
64 void
65 free_class(struct Class *tmp)
67 if(tmp->ip_limits)
68 Destroy_Patricia(tmp->ip_limits, NULL);
70 MyFree(tmp->class_name);
71 MyFree(tmp);
76 * get_conf_ping
78 * inputs - pointer to struct ConfItem
79 * output - ping frequency
80 * side effects - NONE
82 static int
83 get_conf_ping(struct ConfItem *aconf)
85 if((aconf) && ClassPtr(aconf))
86 return (ConfPingFreq(aconf));
88 return (BAD_PING);
92 * get_client_class
94 * inputs - pointer to client struct
95 * output - pointer to name of class
96 * side effects - NONE
98 const char *
99 get_client_class(struct Client *target_p)
101 const char *retc = "unknown";
103 if(target_p == NULL || IsMe(target_p))
104 return retc;
106 if(IsServer(target_p))
108 struct server_conf *server_p = target_p->localClient->att_sconf;
109 return server_p->class_name;
111 else
113 struct ConfItem *aconf;
114 aconf = target_p->localClient->att_conf;
116 if((aconf == NULL) || (aconf->className == NULL))
117 retc = "default";
118 else
119 retc = aconf->className;
122 return (retc);
126 * get_client_ping
128 * inputs - pointer to client struct
129 * output - ping frequency
130 * side effects - NONE
133 get_client_ping(struct Client *target_p)
135 int ping = 0;
137 if(IsServer(target_p))
139 struct server_conf *server_p = target_p->localClient->att_sconf;
140 ping = PingFreq(server_p->class);
142 else
144 struct ConfItem *aconf;
146 aconf = target_p->localClient->att_conf;
148 if(aconf != NULL)
149 ping = get_conf_ping(aconf);
150 else
151 ping = DEFAULT_PINGFREQUENCY;
154 if(ping <= 0)
155 ping = DEFAULT_PINGFREQUENCY;
157 return ping;
161 * get_con_freq
163 * inputs - pointer to class struct
164 * output - connection frequency
165 * side effects - NONE
168 get_con_freq(struct Class *clptr)
170 if(clptr)
171 return (ConFreq(clptr));
172 return (DEFAULT_CONNECTFREQUENCY);
175 /* add_class()
177 * input - class to add
178 * output -
179 * side effects - class is added to class_list if new, else old class
180 * is updated with new values.
182 void
183 add_class(struct Class *classptr)
185 struct Class *tmpptr;
187 tmpptr = find_class(classptr->class_name);
189 if(tmpptr == default_class)
191 dlinkAddAlloc(classptr, &class_list);
192 CurrUsers(classptr) = 0;
194 else
196 MaxUsers(tmpptr) = MaxUsers(classptr);
197 MaxLocal(tmpptr) = MaxLocal(classptr);
198 MaxGlobal(tmpptr) = MaxGlobal(classptr);
199 MaxIdent(tmpptr) = MaxIdent(classptr);
200 PingFreq(tmpptr) = PingFreq(classptr);
201 MaxSendq(tmpptr) = MaxSendq(classptr);
202 ConFreq(tmpptr) = ConFreq(classptr);
203 CidrBitlen(tmpptr) = CidrBitlen(classptr);
204 CidrAmount(tmpptr) = CidrAmount(classptr);
206 free_class(classptr);
212 * find_class
214 * inputs - string name of class
215 * output - corresponding class pointer
216 * side effects - NONE
218 struct Class *
219 find_class(const char *classname)
221 struct Class *cltmp;
222 dlink_node *ptr;
224 if(classname == NULL)
225 return default_class;
227 DLINK_FOREACH(ptr, class_list.head)
229 cltmp = ptr->data;
231 if(!strcmp(ClassName(cltmp), classname))
232 return cltmp;
235 return default_class;
239 * check_class
241 * inputs - NONE
242 * output - NONE
243 * side effects -
245 void
246 check_class()
248 struct Class *cltmp;
249 dlink_node *ptr;
250 dlink_node *next_ptr;
252 DLINK_FOREACH_SAFE(ptr, next_ptr, class_list.head)
254 cltmp = ptr->data;
256 if(MaxUsers(cltmp) < 0)
258 dlinkDestroy(ptr, &class_list);
259 if(CurrUsers(cltmp) <= 0)
260 free_class(cltmp);
266 * initclass
268 * inputs - NONE
269 * output - NONE
270 * side effects -
272 void
273 initclass()
275 default_class = make_class();
276 DupString(ClassName(default_class), "default");
280 * report_classes
282 * inputs - pointer to client to report to
283 * output - NONE
284 * side effects - class report is done to this client
286 void
287 report_classes(struct Client *source_p)
289 struct Class *cltmp;
290 dlink_node *ptr;
292 DLINK_FOREACH(ptr, class_list.head)
294 cltmp = ptr->data;
296 sendto_one_numeric(source_p, RPL_STATSYLINE,
297 form_str(RPL_STATSYLINE),
298 ClassName(cltmp), PingFreq(cltmp),
299 ConFreq(cltmp), MaxUsers(cltmp),
300 MaxSendq(cltmp),
301 MaxLocal(cltmp), MaxIdent(cltmp),
302 MaxGlobal(cltmp), MaxIdent(cltmp),
303 CurrUsers(cltmp));
306 /* also output the default class */
307 sendto_one_numeric(source_p, RPL_STATSYLINE, form_str(RPL_STATSYLINE),
308 ClassName(default_class), PingFreq(default_class),
309 ConFreq(default_class), MaxUsers(default_class),
310 MaxSendq(default_class),
311 MaxLocal(default_class), MaxIdent(default_class),
312 MaxGlobal(default_class), MaxIdent(default_class),
313 CurrUsers(default_class));
317 * get_sendq
319 * inputs - pointer to client
320 * output - sendq for this client as found from its class
321 * side effects - NONE
323 long
324 get_sendq(struct Client *client_p)
326 if(client_p == NULL || IsMe(client_p))
327 return DEFAULT_SENDQ;
329 if(IsServer(client_p))
331 struct server_conf *server_p;
332 server_p = client_p->localClient->att_sconf;
333 return MaxSendq(server_p->class);
335 else
337 struct ConfItem *aconf = client_p->localClient->att_conf;
339 if(aconf != NULL && aconf->status & CONF_CLIENT)
340 return ConfMaxSendq(aconf);
343 return DEFAULT_SENDQ;