1 |
/* |
2 |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
* |
4 |
* Copyright (C) 2002 by the past and present ircd coders, and others. |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
19 |
* USA |
20 |
*/ |
21 |
|
22 |
/*! \file |
23 |
* \brief Configuration managment for class{} blocks |
24 |
* \version $Id$ |
25 |
*/ |
26 |
|
27 |
#include "stdinc.h" |
28 |
#include "list.h" |
29 |
#include "ircd.h" |
30 |
#include "conf.h" |
31 |
#include "hostmask.h" |
32 |
#include "irc_string.h" |
33 |
#include "memory.h" |
34 |
|
35 |
|
36 |
struct ClassItem *class_default; |
37 |
|
38 |
static dlink_list class_list = { NULL, NULL, 0 }; |
39 |
|
40 |
|
41 |
const dlink_list * |
42 |
class_get_list(void) |
43 |
{ |
44 |
return &class_list; |
45 |
} |
46 |
|
47 |
struct ClassItem * |
48 |
class_make(void) |
49 |
{ |
50 |
struct ClassItem *class = MyMalloc(sizeof(*class)); |
51 |
|
52 |
class->active = 1; |
53 |
class->con_freq = DEFAULT_CONNECTFREQUENCY; |
54 |
class->ping_freq = DEFAULT_PINGFREQUENCY; |
55 |
class->max_total = MAXIMUM_LINKS_DEFAULT; |
56 |
class->max_sendq = DEFAULT_SENDQ; |
57 |
class->max_recvq = DEFAULT_RECVQ; |
58 |
|
59 |
dlinkAdd(class, &class->node, &class_list); |
60 |
|
61 |
return class; |
62 |
} |
63 |
|
64 |
void |
65 |
class_free(struct ClassItem *class) |
66 |
{ |
67 |
assert(class); |
68 |
assert(class->active == 0); |
69 |
assert(class->ref_count == 0); |
70 |
|
71 |
dlinkDelete(&class->node, &class_list); |
72 |
MyFree(class->name); |
73 |
MyFree(class); |
74 |
} |
75 |
|
76 |
void |
77 |
class_init(void) |
78 |
{ |
79 |
(class_default = class_make())->name = xstrdup("default"); |
80 |
} |
81 |
|
82 |
const char * |
83 |
get_client_class(const dlink_list *const list) |
84 |
{ |
85 |
const dlink_node *ptr = NULL; |
86 |
|
87 |
if ((ptr = list->head)) { |
88 |
const struct MaskItem *conf = ptr->data; |
89 |
|
90 |
assert(conf->class); |
91 |
assert(conf->type & (CONF_OPER | CONF_CLIENT | CONF_SERVER)); |
92 |
|
93 |
return conf->class->name; |
94 |
} |
95 |
|
96 |
return class_default->name; |
97 |
} |
98 |
|
99 |
unsigned int |
100 |
get_client_ping(const dlink_list *const list) |
101 |
{ |
102 |
const dlink_node *ptr = NULL; |
103 |
|
104 |
if ((ptr = list->head)) { |
105 |
const struct MaskItem *conf = ptr->data; |
106 |
|
107 |
assert(conf->class); |
108 |
assert(conf->type & (CONF_OPER | CONF_CLIENT | CONF_SERVER)); |
109 |
|
110 |
return conf->class->ping_freq; |
111 |
} |
112 |
|
113 |
return class_default->ping_freq; |
114 |
} |
115 |
|
116 |
unsigned int |
117 |
get_sendq(const dlink_list *const list) |
118 |
{ |
119 |
const dlink_node *ptr = NULL; |
120 |
|
121 |
if ((ptr = list->head)) { |
122 |
const struct MaskItem *conf = ptr->data; |
123 |
|
124 |
assert(conf->class); |
125 |
assert(conf->type & (CONF_OPER | CONF_CLIENT | CONF_SERVER)); |
126 |
|
127 |
return conf->class->max_sendq; |
128 |
} |
129 |
|
130 |
return class_default->max_sendq; |
131 |
} |
132 |
|
133 |
unsigned int |
134 |
get_recvq(const dlink_list *const list) |
135 |
{ |
136 |
const dlink_node *ptr = NULL; |
137 |
|
138 |
if ((ptr = list->head)) { |
139 |
const struct MaskItem *conf = ptr->data; |
140 |
|
141 |
assert(conf->class); |
142 |
assert(conf->type & (CONF_OPER | CONF_CLIENT | CONF_SERVER)); |
143 |
|
144 |
return conf->class->max_recvq; |
145 |
} |
146 |
|
147 |
return class_default->max_recvq; |
148 |
} |
149 |
|
150 |
/* |
151 |
* inputs - Integer (Number of class) |
152 |
* output - Pointer to ClassItem struct. Non-NULL expected |
153 |
* side effects - NONE |
154 |
*/ |
155 |
struct ClassItem * |
156 |
class_find(const char *name, int active) |
157 |
{ |
158 |
dlink_node *ptr = NULL; |
159 |
|
160 |
DLINK_FOREACH(ptr, class_list.head) { |
161 |
struct ClassItem *class = ptr->data; |
162 |
|
163 |
if (!irccmp(class->name, name)) |
164 |
return active && !class->active ? NULL : class; |
165 |
} |
166 |
|
167 |
return NULL; |
168 |
} |
169 |
|
170 |
/* |
171 |
* We don't delete the class table, rather mark all entries for deletion. |
172 |
* The table is cleaned up by delete_marked_classes. - avalon |
173 |
*/ |
174 |
void |
175 |
class_mark_for_deletion(void) |
176 |
{ |
177 |
dlink_node *ptr = NULL; |
178 |
|
179 |
DLINK_FOREACH_PREV(ptr, class_list.tail->prev) |
180 |
((struct ClassItem *)ptr->data)->active = 0; |
181 |
} |
182 |
|
183 |
void |
184 |
class_delete_marked(void) |
185 |
{ |
186 |
dlink_node *ptr = NULL, *ptr_next = NULL; |
187 |
|
188 |
DLINK_FOREACH_SAFE(ptr, ptr_next, class_list.head) { |
189 |
struct ClassItem *class = ptr->data; |
190 |
|
191 |
if (!class->active && !class->ref_count) |
192 |
{ |
193 |
destroy_cidr_class(class); |
194 |
class_free(class); |
195 |
} |
196 |
} |
197 |
} |
198 |
|
199 |
/* |
200 |
* cidr_limit_reached |
201 |
* |
202 |
* inputs - int flag allowing over_rule of limits |
203 |
* - pointer to the ip to be added |
204 |
* - pointer to the class |
205 |
* output - non zero if limit reached |
206 |
* 0 if limit not reached |
207 |
* side effects - |
208 |
*/ |
209 |
int |
210 |
cidr_limit_reached(int over_rule, |
211 |
struct irc_ssaddr *ip, struct ClassItem *class) |
212 |
{ |
213 |
dlink_node *ptr = NULL; |
214 |
struct CidrItem *cidr = NULL; |
215 |
|
216 |
if (class->number_per_cidr == 0) |
217 |
return 0; |
218 |
|
219 |
if (ip->ss.ss_family == AF_INET) |
220 |
{ |
221 |
if (class->cidr_bitlen_ipv4 == 0) |
222 |
return 0; |
223 |
|
224 |
DLINK_FOREACH(ptr, class->list_ipv4.head) |
225 |
{ |
226 |
cidr = ptr->data; |
227 |
if (match_ipv4(ip, &cidr->mask, class->cidr_bitlen_ipv4)) |
228 |
{ |
229 |
if (!over_rule && (cidr->number_on_this_cidr >= class->number_per_cidr)) |
230 |
return -1; |
231 |
cidr->number_on_this_cidr++; |
232 |
return 0; |
233 |
} |
234 |
} |
235 |
cidr = MyMalloc(sizeof(struct CidrItem)); |
236 |
cidr->number_on_this_cidr = 1; |
237 |
cidr->mask = *ip; |
238 |
mask_addr(&cidr->mask, class->cidr_bitlen_ipv4); |
239 |
dlinkAdd(cidr, &cidr->node, &class->list_ipv4); |
240 |
} |
241 |
#ifdef IPV6 |
242 |
else if (class->cidr_bitlen_ipv6 > 0) |
243 |
{ |
244 |
DLINK_FOREACH(ptr, class->list_ipv6.head) |
245 |
{ |
246 |
cidr = ptr->data; |
247 |
if (match_ipv6(ip, &cidr->mask, class->cidr_bitlen_ipv6)) |
248 |
{ |
249 |
if (!over_rule && (cidr->number_on_this_cidr >= class->number_per_cidr)) |
250 |
return -1; |
251 |
cidr->number_on_this_cidr++; |
252 |
return 0; |
253 |
} |
254 |
} |
255 |
cidr = MyMalloc(sizeof(struct CidrItem)); |
256 |
cidr->number_on_this_cidr = 1; |
257 |
cidr->mask = *ip; |
258 |
mask_addr(&cidr->mask, class->cidr_bitlen_ipv6); |
259 |
dlinkAdd(cidr, &cidr->node, &class->list_ipv6); |
260 |
} |
261 |
#endif |
262 |
return 0; |
263 |
} |
264 |
|
265 |
/* |
266 |
* remove_from_cidr_check |
267 |
* |
268 |
* inputs - pointer to the ip to be removed |
269 |
* - pointer to the class |
270 |
* output - NONE |
271 |
* side effects - |
272 |
*/ |
273 |
void |
274 |
remove_from_cidr_check(struct irc_ssaddr *ip, struct ClassItem *aclass) |
275 |
{ |
276 |
dlink_node *ptr = NULL; |
277 |
dlink_node *next_ptr = NULL; |
278 |
struct CidrItem *cidr; |
279 |
|
280 |
if (aclass->number_per_cidr == 0) |
281 |
return; |
282 |
|
283 |
if (ip->ss.ss_family == AF_INET) |
284 |
{ |
285 |
if (aclass->cidr_bitlen_ipv4 == 0) |
286 |
return; |
287 |
|
288 |
DLINK_FOREACH_SAFE(ptr, next_ptr, aclass->list_ipv4.head) |
289 |
{ |
290 |
cidr = ptr->data; |
291 |
if (match_ipv4(ip, &cidr->mask, aclass->cidr_bitlen_ipv4)) |
292 |
{ |
293 |
cidr->number_on_this_cidr--; |
294 |
if (cidr->number_on_this_cidr == 0) |
295 |
{ |
296 |
dlinkDelete(ptr, &aclass->list_ipv4); |
297 |
MyFree(cidr); |
298 |
return; |
299 |
} |
300 |
} |
301 |
} |
302 |
} |
303 |
#ifdef IPV6 |
304 |
else if (aclass->cidr_bitlen_ipv6 > 0) |
305 |
{ |
306 |
DLINK_FOREACH_SAFE(ptr, next_ptr, aclass->list_ipv6.head) |
307 |
{ |
308 |
cidr = ptr->data; |
309 |
if (match_ipv6(ip, &cidr->mask, aclass->cidr_bitlen_ipv6)) |
310 |
{ |
311 |
cidr->number_on_this_cidr--; |
312 |
if (cidr->number_on_this_cidr == 0) |
313 |
{ |
314 |
dlinkDelete(ptr, &aclass->list_ipv6); |
315 |
MyFree(cidr); |
316 |
return; |
317 |
} |
318 |
} |
319 |
} |
320 |
} |
321 |
#endif |
322 |
} |
323 |
|
324 |
void |
325 |
rebuild_cidr_list(struct ClassItem *class) |
326 |
{ |
327 |
dlink_node *ptr; |
328 |
|
329 |
destroy_cidr_class(class); |
330 |
|
331 |
DLINK_FOREACH(ptr, local_client_list.head) |
332 |
{ |
333 |
struct Client *client_p = ptr->data; |
334 |
struct MaskItem *conf = client_p->localClient->confs.tail->data; |
335 |
|
336 |
if (conf && (conf->type == CONF_CLIENT)) |
337 |
if (conf->class == class) |
338 |
cidr_limit_reached(1, &client_p->localClient->ip, class); |
339 |
} |
340 |
} |
341 |
|
342 |
/* |
343 |
* destroy_cidr_list |
344 |
* |
345 |
* inputs - pointer to class dlink list of cidr blocks |
346 |
* output - none |
347 |
* side effects - completely destroys the class link list of cidr blocks |
348 |
*/ |
349 |
static void |
350 |
destroy_cidr_list(dlink_list *list) |
351 |
{ |
352 |
dlink_node *ptr = NULL, *next_ptr = NULL; |
353 |
|
354 |
DLINK_FOREACH_SAFE(ptr, next_ptr, list->head) |
355 |
{ |
356 |
dlinkDelete(ptr, list); |
357 |
MyFree(ptr->data); |
358 |
} |
359 |
} |
360 |
|
361 |
/* |
362 |
* destroy_cidr_class |
363 |
* |
364 |
* inputs - pointer to class |
365 |
* output - none |
366 |
* side effects - completely destroys the class link list of cidr blocks |
367 |
*/ |
368 |
void |
369 |
destroy_cidr_class(struct ClassItem *class) |
370 |
{ |
371 |
destroy_cidr_list(&class->list_ipv4); |
372 |
destroy_cidr_list(&class->list_ipv6); |
373 |
} |