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