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