]> jfr.im git - irc/rqf/shadowircd.git/blame - src/class.c
Various changes for libratbox.
[irc/rqf/shadowircd.git] / src / class.c
CommitLineData
212380e3 1/*
2 * ircd-ratbox: A slightly useful ircd.
3 * class.c: Controls connection classes.
4 *
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
8 *
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.
13 *
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.
18 *
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
23 *
24 * $Id: class.c 254 2005-09-21 23:35:12Z nenolod $
25 */
26
27#include "stdinc.h"
28#include "config.h"
29
30#include "tools.h"
31#include "class.h"
32#include "client.h"
33#include "common.h"
34#include "ircd.h"
35#include "numeric.h"
36#include "s_conf.h"
37#include "s_newconf.h"
38#include "send.h"
39#include "irc_string.h"
40#include "memory.h"
212380e3 41
42#define BAD_CONF_CLASS -1
43#define BAD_PING -2
44#define BAD_CLIENT_CLASS -3
45
af81d5a0 46rb_dlink_list class_list;
212380e3 47struct Class *default_class;
48
49struct Class *
50make_class(void)
51{
52 struct Class *tmp;
53
54 tmp = (struct Class *) MyMalloc(sizeof(struct Class));
55
56 ConFreq(tmp) = DEFAULT_CONNECTFREQUENCY;
57 PingFreq(tmp) = DEFAULT_PINGFREQUENCY;
58 MaxUsers(tmp) = 1;
59 MaxSendq(tmp) = DEFAULT_SENDQ;
60
f14bcab7 61 tmp->ip_limits = rb_new_patricia(PATRICIA_BITS);
212380e3 62 return tmp;
63}
64
65void
66free_class(struct Class *tmp)
67{
68 if(tmp->ip_limits)
f14bcab7 69 rb_destroy_patricia(tmp->ip_limits, NULL);
212380e3 70
71 MyFree(tmp->class_name);
72 MyFree(tmp);
73
74}
75
76/*
77 * get_conf_ping
78 *
79 * inputs - pointer to struct ConfItem
80 * output - ping frequency
81 * side effects - NONE
82 */
83static int
84get_conf_ping(struct ConfItem *aconf)
85{
86 if((aconf) && ClassPtr(aconf))
87 return (ConfPingFreq(aconf));
88
89 return (BAD_PING);
90}
91
92/*
93 * get_client_class
94 *
95 * inputs - pointer to client struct
96 * output - pointer to name of class
97 * side effects - NONE
98 */
99const char *
100get_client_class(struct Client *target_p)
101{
102 const char *retc = "unknown";
103
104 if(target_p == NULL || IsMe(target_p))
105 return retc;
106
107 if(IsServer(target_p))
108 {
109 struct server_conf *server_p = target_p->localClient->att_sconf;
110 return server_p->class_name;
111 }
112 else
113 {
114 struct ConfItem *aconf;
115 aconf = target_p->localClient->att_conf;
116
117 if((aconf == NULL) || (aconf->className == NULL))
118 retc = "default";
119 else
120 retc = aconf->className;
121 }
122
123 return (retc);
124}
125
126/*
127 * get_client_ping
128 *
129 * inputs - pointer to client struct
130 * output - ping frequency
131 * side effects - NONE
132 */
133int
134get_client_ping(struct Client *target_p)
135{
136 int ping = 0;
137
138 if(IsServer(target_p))
139 {
140 struct server_conf *server_p = target_p->localClient->att_sconf;
141 ping = PingFreq(server_p->class);
142 }
143 else
144 {
145 struct ConfItem *aconf;
146
147 aconf = target_p->localClient->att_conf;
148
149 if(aconf != NULL)
150 ping = get_conf_ping(aconf);
151 else
152 ping = DEFAULT_PINGFREQUENCY;
153 }
154
155 if(ping <= 0)
156 ping = DEFAULT_PINGFREQUENCY;
157
158 return ping;
159}
160
161/*
162 * get_con_freq
163 *
164 * inputs - pointer to class struct
165 * output - connection frequency
166 * side effects - NONE
167 */
168int
169get_con_freq(struct Class *clptr)
170{
171 if(clptr)
172 return (ConFreq(clptr));
173 return (DEFAULT_CONNECTFREQUENCY);
174}
175
176/* add_class()
177 *
178 * input - class to add
179 * output -
180 * side effects - class is added to class_list if new, else old class
181 * is updated with new values.
182 */
183void
184add_class(struct Class *classptr)
185{
186 struct Class *tmpptr;
187
188 tmpptr = find_class(classptr->class_name);
189
190 if(tmpptr == default_class)
191 {
af81d5a0 192 rb_dlinkAddAlloc(classptr, &class_list);
212380e3 193 CurrUsers(classptr) = 0;
194 }
195 else
196 {
197 MaxUsers(tmpptr) = MaxUsers(classptr);
198 MaxLocal(tmpptr) = MaxLocal(classptr);
199 MaxGlobal(tmpptr) = MaxGlobal(classptr);
200 MaxIdent(tmpptr) = MaxIdent(classptr);
201 PingFreq(tmpptr) = PingFreq(classptr);
202 MaxSendq(tmpptr) = MaxSendq(classptr);
203 ConFreq(tmpptr) = ConFreq(classptr);
204 CidrBitlen(tmpptr) = CidrBitlen(classptr);
205 CidrAmount(tmpptr) = CidrAmount(classptr);
206
207 free_class(classptr);
208 }
209}
210
211
212/*
213 * find_class
214 *
215 * inputs - string name of class
216 * output - corresponding class pointer
217 * side effects - NONE
218 */
219struct Class *
220find_class(const char *classname)
221{
222 struct Class *cltmp;
af81d5a0 223 rb_dlink_node *ptr;
212380e3 224
225 if(classname == NULL)
226 return default_class;
227
8e69bb4e 228 RB_DLINK_FOREACH(ptr, class_list.head)
212380e3 229 {
230 cltmp = ptr->data;
231
232 if(!strcmp(ClassName(cltmp), classname))
233 return cltmp;
234 }
235
236 return default_class;
237}
238
239/*
240 * check_class
241 *
242 * inputs - NONE
243 * output - NONE
244 * side effects -
245 */
246void
247check_class()
248{
249 struct Class *cltmp;
af81d5a0
WP
250 rb_dlink_node *ptr;
251 rb_dlink_node *next_ptr;
212380e3 252
8e69bb4e 253 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, class_list.head)
212380e3 254 {
255 cltmp = ptr->data;
256
257 if(MaxUsers(cltmp) < 0)
258 {
af81d5a0 259 rb_dlinkDestroy(ptr, &class_list);
212380e3 260 if(CurrUsers(cltmp) <= 0)
261 free_class(cltmp);
262 }
263 }
264}
265
266/*
267 * initclass
268 *
269 * inputs - NONE
270 * output - NONE
271 * side effects -
272 */
273void
274initclass()
275{
276 default_class = make_class();
277 DupString(ClassName(default_class), "default");
278}
279
280/*
281 * report_classes
282 *
283 * inputs - pointer to client to report to
284 * output - NONE
285 * side effects - class report is done to this client
286 */
287void
288report_classes(struct Client *source_p)
289{
290 struct Class *cltmp;
af81d5a0 291 rb_dlink_node *ptr;
212380e3 292
8e69bb4e 293 RB_DLINK_FOREACH(ptr, class_list.head)
212380e3 294 {
295 cltmp = ptr->data;
296
297 sendto_one_numeric(source_p, RPL_STATSYLINE,
298 form_str(RPL_STATSYLINE),
299 ClassName(cltmp), PingFreq(cltmp),
300 ConFreq(cltmp), MaxUsers(cltmp),
301 MaxSendq(cltmp),
302 MaxLocal(cltmp), MaxIdent(cltmp),
303 MaxGlobal(cltmp), MaxIdent(cltmp),
304 CurrUsers(cltmp));
305 }
306
307 /* also output the default class */
308 sendto_one_numeric(source_p, RPL_STATSYLINE, form_str(RPL_STATSYLINE),
309 ClassName(default_class), PingFreq(default_class),
310 ConFreq(default_class), MaxUsers(default_class),
311 MaxSendq(default_class),
312 MaxLocal(default_class), MaxIdent(default_class),
313 MaxGlobal(default_class), MaxIdent(default_class),
314 CurrUsers(default_class));
315}
316
317/*
318 * get_sendq
319 *
320 * inputs - pointer to client
321 * output - sendq for this client as found from its class
322 * side effects - NONE
323 */
324long
325get_sendq(struct Client *client_p)
326{
327 if(client_p == NULL || IsMe(client_p))
328 return DEFAULT_SENDQ;
329
330 if(IsServer(client_p))
331 {
332 struct server_conf *server_p;
333 server_p = client_p->localClient->att_sconf;
334 return MaxSendq(server_p->class);
335 }
336 else
337 {
338 struct ConfItem *aconf = client_p->localClient->att_conf;
339
340 if(aconf != NULL && aconf->status & CONF_CLIENT)
341 return ConfMaxSendq(aconf);
342 }
343
344 return DEFAULT_SENDQ;
345}