]> jfr.im git - solanum.git/blame - ircd/class.c
ircd: rework sendq limits a bit.
[solanum.git] / ircd / class.c
CommitLineData
212380e3
AC
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
212380e3
AC
30#include "class.h"
31#include "client.h"
32#include "common.h"
33#include "ircd.h"
34#include "numeric.h"
35#include "s_conf.h"
36#include "s_newconf.h"
37#include "send.h"
4562c604 38#include "match.h"
212380e3
AC
39
40#define BAD_CONF_CLASS -1
41#define BAD_PING -2
42#define BAD_CLIENT_CLASS -3
43
330fc5c1 44rb_dlink_list class_list;
212380e3
AC
45struct Class *default_class;
46
1087485c
JT
47struct Class *
48make_class(void)
49{
50 struct Class *tmp;
51
52 tmp = rb_malloc(sizeof(struct Class));
53
54 ConFreq(tmp) = DEFAULT_CONNECTFREQUENCY;
55 PingFreq(tmp) = DEFAULT_PINGFREQUENCY;
56 MaxUsers(tmp) = 1;
57 MaxSendq(tmp) = DEFAULT_SENDQ;
9a595811 58 MaxSendqHard(tmp) = DEFAULT_SENDQ * 4;
1087485c
JT
59
60 tmp->ip_limits = rb_new_patricia(PATRICIA_BITS);
61 return tmp;
62}
63
64void
65free_class(struct Class *tmp)
66{
67 if(tmp->ip_limits)
68 rb_destroy_patricia(tmp->ip_limits, NULL);
69
70 rb_free(tmp->class_name);
71 rb_free(tmp);
72
212380e3
AC
73}
74
75/*
76 * get_conf_ping
77 *
78 * inputs - pointer to struct ConfItem
79 * output - ping frequency
80 * side effects - NONE
81 */
82static int
83get_conf_ping(struct ConfItem *aconf)
84{
85 if((aconf) && ClassPtr(aconf))
86 return (ConfPingFreq(aconf));
87
88 return (BAD_PING);
89}
90
91/*
92 * get_client_class
93 *
94 * inputs - pointer to client struct
95 * output - pointer to name of class
96 * side effects - NONE
97 */
98const char *
99get_client_class(struct Client *target_p)
100{
101 const char *retc = "unknown";
102
103 if(target_p == NULL || IsMe(target_p))
104 return retc;
105
106 if(IsServer(target_p))
107 {
108 struct server_conf *server_p = target_p->localClient->att_sconf;
109 return server_p->class_name;
110 }
111 else
112 {
113 struct ConfItem *aconf;
114 aconf = target_p->localClient->att_conf;
115
116 if((aconf == NULL) || (aconf->className == NULL))
117 retc = "default";
118 else
119 retc = aconf->className;
120 }
121
122 return (retc);
123}
124
125/*
126 * get_client_ping
127 *
128 * inputs - pointer to client struct
129 * output - ping frequency
130 * side effects - NONE
131 */
132int
133get_client_ping(struct Client *target_p)
134{
135 int ping = 0;
136
137 if(IsServer(target_p))
138 {
139 struct server_conf *server_p = target_p->localClient->att_sconf;
140 ping = PingFreq(server_p->class);
141 }
142 else
143 {
144 struct ConfItem *aconf;
145
146 aconf = target_p->localClient->att_conf;
147
148 if(aconf != NULL)
149 ping = get_conf_ping(aconf);
150 else
151 ping = DEFAULT_PINGFREQUENCY;
152 }
153
154 if(ping <= 0)
155 ping = DEFAULT_PINGFREQUENCY;
156
157 return ping;
158}
159
160/*
161 * get_con_freq
162 *
163 * inputs - pointer to class struct
164 * output - connection frequency
165 * side effects - NONE
166 */
167int
168get_con_freq(struct Class *clptr)
169{
170 if(clptr)
171 return (ConFreq(clptr));
172 return (DEFAULT_CONNECTFREQUENCY);
173}
174
175/* add_class()
176 *
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.
181 */
182void
183add_class(struct Class *classptr)
184{
185 struct Class *tmpptr;
186
187 tmpptr = find_class(classptr->class_name);
188
189 if(tmpptr == default_class)
190 {
330fc5c1 191 rb_dlinkAddAlloc(classptr, &class_list);
212380e3
AC
192 CurrUsers(classptr) = 0;
193 }
194 else
195 {
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);
9a595811 202 MaxSendqHard(tmpptr) = MaxSendq(classptr) * 4;
212380e3 203 ConFreq(tmpptr) = ConFreq(classptr);
e33e589c
JT
204 CidrIpv4Bitlen(tmpptr) = CidrIpv4Bitlen(classptr);
205 CidrIpv6Bitlen(tmpptr) = CidrIpv6Bitlen(classptr);
212380e3
AC
206 CidrAmount(tmpptr) = CidrAmount(classptr);
207
208 free_class(classptr);
209 }
210}
211
212
213/*
214 * find_class
215 *
216 * inputs - string name of class
217 * output - corresponding class pointer
218 * side effects - NONE
219 */
220struct Class *
221find_class(const char *classname)
222{
223 struct Class *cltmp;
330fc5c1 224 rb_dlink_node *ptr;
212380e3
AC
225
226 if(classname == NULL)
227 return default_class;
228
5cefa1d6 229 RB_DLINK_FOREACH(ptr, class_list.head)
212380e3
AC
230 {
231 cltmp = ptr->data;
232
233 if(!strcmp(ClassName(cltmp), classname))
234 return cltmp;
235 }
236
237 return default_class;
238}
239
240/*
241 * check_class
242 *
243 * inputs - NONE
244 * output - NONE
55abcbb2 245 * side effects -
212380e3
AC
246 */
247void
248check_class()
249{
250 struct Class *cltmp;
330fc5c1 251 rb_dlink_node *ptr;
637c4932 252 rb_dlink_node *next_ptr;
212380e3 253
637c4932 254 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, class_list.head)
212380e3
AC
255 {
256 cltmp = ptr->data;
257
258 if(MaxUsers(cltmp) < 0)
259 {
330fc5c1 260 rb_dlinkDestroy(ptr, &class_list);
212380e3
AC
261 if(CurrUsers(cltmp) <= 0)
262 free_class(cltmp);
263 }
264 }
265}
266
267/*
268 * initclass
269 *
270 * inputs - NONE
271 * output - NONE
55abcbb2 272 * side effects -
212380e3
AC
273 */
274void
275initclass()
276{
277 default_class = make_class();
47a03750 278 ClassName(default_class) = rb_strdup("default");
212380e3
AC
279}
280
281/*
282 * report_classes
283 *
284 * inputs - pointer to client to report to
285 * output - NONE
286 * side effects - class report is done to this client
287 */
288void
289report_classes(struct Client *source_p)
290{
291 struct Class *cltmp;
330fc5c1 292 rb_dlink_node *ptr;
212380e3 293
5cefa1d6 294 RB_DLINK_FOREACH(ptr, class_list.head)
212380e3
AC
295 {
296 cltmp = ptr->data;
297
55abcbb2 298 sendto_one_numeric(source_p, RPL_STATSYLINE,
212380e3 299 form_str(RPL_STATSYLINE),
55abcbb2
KB
300 ClassName(cltmp), PingFreq(cltmp),
301 ConFreq(cltmp), MaxUsers(cltmp),
302 MaxSendq(cltmp),
212380e3
AC
303 MaxLocal(cltmp), MaxIdent(cltmp),
304 MaxGlobal(cltmp), MaxIdent(cltmp),
305 CurrUsers(cltmp));
306 }
307
308 /* also output the default class */
309 sendto_one_numeric(source_p, RPL_STATSYLINE, form_str(RPL_STATSYLINE),
55abcbb2
KB
310 ClassName(default_class), PingFreq(default_class),
311 ConFreq(default_class), MaxUsers(default_class),
312 MaxSendq(default_class),
212380e3
AC
313 MaxLocal(default_class), MaxIdent(default_class),
314 MaxGlobal(default_class), MaxIdent(default_class),
315 CurrUsers(default_class));
316}
317
318/*
319 * get_sendq
320 *
321 * inputs - pointer to client
322 * output - sendq for this client as found from its class
323 * side effects - NONE
324 */
325long
326get_sendq(struct Client *client_p)
327{
328 if(client_p == NULL || IsMe(client_p))
329 return DEFAULT_SENDQ;
330
331 if(IsServer(client_p))
332 {
333 struct server_conf *server_p;
334 server_p = client_p->localClient->att_sconf;
335 return MaxSendq(server_p->class);
336 }
337 else
338 {
339 struct ConfItem *aconf = client_p->localClient->att_conf;
340
341 if(aconf != NULL && aconf->status & CONF_CLIENT)
342 return ConfMaxSendq(aconf);
343 }
344
345 return DEFAULT_SENDQ;
346}
9a595811
AC
347
348/*
349 * get_sendq_hard
350 *
351 * inputs - pointer to client
352 * output - hard sendq limit for this client as found from its class
353 * side effects - NONE
354 */
355long
356get_sendq_hard(struct Client *client_p)
357{
358 if(client_p == NULL || IsMe(client_p))
359 return DEFAULT_SENDQ;
360
361 if(IsServer(client_p))
362 {
363 struct server_conf *server_p;
364 server_p = client_p->localClient->att_sconf;
365 return MaxSendqHard(server_p->class);
366 }
367 else
368 {
369 struct ConfItem *aconf = client_p->localClient->att_conf;
370
371 if(aconf != NULL && aconf->status & CONF_CLIENT)
372 return ConfMaxSendqHard(aconf);
373 }
374
375 return DEFAULT_SENDQ;
376}