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