]>
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" | |
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 AC |
36 | |
37 | #define BAD_CONF_CLASS -1 | |
38 | #define BAD_PING -2 | |
39 | #define BAD_CLIENT_CLASS -3 | |
40 | ||
330fc5c1 | 41 | rb_dlink_list class_list; |
212380e3 AC |
42 | struct Class *default_class; |
43 | ||
1087485c JT |
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 | ||
212380e3 AC |
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 | { | |
330fc5c1 | 187 | rb_dlinkAddAlloc(classptr, &class_list); |
212380e3 AC |
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); | |
e33e589c JT |
199 | CidrIpv4Bitlen(tmpptr) = CidrIpv4Bitlen(classptr); |
200 | CidrIpv6Bitlen(tmpptr) = CidrIpv6Bitlen(classptr); | |
212380e3 AC |
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; | |
330fc5c1 | 219 | rb_dlink_node *ptr; |
212380e3 AC |
220 | |
221 | if(classname == NULL) | |
222 | return default_class; | |
223 | ||
5cefa1d6 | 224 | RB_DLINK_FOREACH(ptr, class_list.head) |
212380e3 AC |
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 | |
55abcbb2 | 240 | * side effects - |
212380e3 AC |
241 | */ |
242 | void | |
243 | check_class() | |
244 | { | |
245 | struct Class *cltmp; | |
330fc5c1 | 246 | rb_dlink_node *ptr; |
637c4932 | 247 | rb_dlink_node *next_ptr; |
212380e3 | 248 | |
637c4932 | 249 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, class_list.head) |
212380e3 AC |
250 | { |
251 | cltmp = ptr->data; | |
252 | ||
253 | if(MaxUsers(cltmp) < 0) | |
254 | { | |
330fc5c1 | 255 | rb_dlinkDestroy(ptr, &class_list); |
212380e3 AC |
256 | if(CurrUsers(cltmp) <= 0) |
257 | free_class(cltmp); | |
258 | } | |
259 | } | |
260 | } | |
261 | ||
262 | /* | |
263 | * initclass | |
264 | * | |
265 | * inputs - NONE | |
266 | * output - NONE | |
55abcbb2 | 267 | * side effects - |
212380e3 AC |
268 | */ |
269 | void | |
270 | initclass() | |
271 | { | |
272 | default_class = make_class(); | |
47a03750 | 273 | ClassName(default_class) = rb_strdup("default"); |
212380e3 AC |
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; | |
330fc5c1 | 287 | rb_dlink_node *ptr; |
212380e3 | 288 | |
5cefa1d6 | 289 | RB_DLINK_FOREACH(ptr, class_list.head) |
212380e3 AC |
290 | { |
291 | cltmp = ptr->data; | |
292 | ||
55abcbb2 | 293 | sendto_one_numeric(source_p, RPL_STATSYLINE, |
212380e3 | 294 | form_str(RPL_STATSYLINE), |
55abcbb2 KB |
295 | ClassName(cltmp), PingFreq(cltmp), |
296 | ConFreq(cltmp), MaxUsers(cltmp), | |
297 | MaxSendq(cltmp), | |
212380e3 AC |
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), | |
55abcbb2 KB |
305 | ClassName(default_class), PingFreq(default_class), |
306 | ConFreq(default_class), MaxUsers(default_class), | |
307 | MaxSendq(default_class), | |
212380e3 AC |
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 | } |