ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/conf_class.c
Revision: 8310
Committed: Wed Feb 28 16:46:13 2018 UTC (7 years, 5 months ago) by michael
Content type: text/x-csrc
File size: 7685 byte(s)
Log Message:
- Clean up remaining sizeof() to sizeof(variable)

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 1997-2018 ircd-hybrid development team
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 * USA
20 */
21
22 /*! \file conf_class.c
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;
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 *const class = xcalloc(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 *const class)
66 {
67 assert(class != class_default);
68 assert(class->active == 0);
69 assert(class->ref_count == 0);
70
71 dlinkDelete(&class->node, &class_list);
72 xfree(class->name);
73 xfree(class);
74 }
75
76 void
77 class_init(void)
78 {
79 class_default = class_make();
80 class_default->name = xstrdup("default");
81 }
82
83 const struct ClassItem *
84 get_class_ptr(const dlink_list *const list)
85 {
86 const dlink_node *const node = list->head;
87
88 if (node)
89 {
90 const struct MaskItem *const conf = node->data;
91
92 assert(conf->class);
93 assert(conf->type & (CONF_OPER | CONF_CLIENT | CONF_SERVER));
94
95 return conf->class;
96 }
97
98 return class_default;
99 }
100
101 const char *
102 get_client_class(const dlink_list *const list)
103 {
104 return get_class_ptr(list)->name;
105 }
106
107 unsigned int
108 get_client_ping(const dlink_list *const list)
109 {
110 return get_class_ptr(list)->ping_freq;
111 }
112
113 unsigned int
114 get_sendq(const dlink_list *const list)
115 {
116 return get_class_ptr(list)->max_sendq;
117 }
118
119 unsigned int
120 get_recvq(const dlink_list *const list)
121 {
122 return get_class_ptr(list)->max_recvq;
123 }
124
125 /*
126 * inputs - Integer (Number of class)
127 * output - Pointer to ClassItem struct. Non-NULL expected
128 * side effects - NONE
129 */
130 struct ClassItem *
131 class_find(const char *name, int active)
132 {
133 dlink_node *node;
134
135 DLINK_FOREACH(node, class_list.head)
136 {
137 struct ClassItem *class = node->data;
138
139 if (!irccmp(class->name, name))
140 return active && !class->active ? NULL : class;
141 }
142
143 return NULL;
144 }
145
146 /*
147 * We don't delete the class table, rather mark all entries for deletion.
148 * The table is cleaned up by delete_marked_classes. - avalon
149 */
150 void
151 class_mark_for_deletion(void)
152 {
153 dlink_node *node;
154
155 DLINK_FOREACH_PREV(node, class_list.tail->prev)
156 ((struct ClassItem *)node->data)->active = 0;
157 }
158
159 void
160 class_delete_marked(void)
161 {
162 dlink_node *node, *node_next;
163
164 DLINK_FOREACH_SAFE(node, node_next, class_list.head)
165 {
166 struct ClassItem *class = node->data;
167
168 if (!class->active && !class->ref_count)
169 {
170 destroy_cidr_class(class);
171 class_free(class);
172 }
173 }
174 }
175
176 /*
177 * cidr_limit_reached
178 *
179 * inputs - int flag allowing over_rule of limits
180 * - pointer to the ip to be added
181 * - pointer to the class
182 * output - non zero if limit reached
183 * 0 if limit not reached
184 * side effects -
185 */
186 int
187 cidr_limit_reached(int over_rule, struct irc_ssaddr *ip, struct ClassItem *class)
188 {
189 dlink_node *node;
190
191 if (class->number_per_cidr == 0)
192 return 0;
193
194 if (ip->ss.ss_family == AF_INET)
195 {
196 if (class->cidr_bitlen_ipv4 == 0)
197 return 0;
198
199 DLINK_FOREACH(node, class->list_ipv4.head)
200 {
201 struct CidrItem *cidr = node->data;
202
203 if (match_ipv4(ip, &cidr->mask, class->cidr_bitlen_ipv4))
204 {
205 if (!over_rule && (cidr->number_on_this_cidr >= class->number_per_cidr))
206 return -1;
207
208 cidr->number_on_this_cidr++;
209 return 0;
210 }
211 }
212
213 struct CidrItem *cidr = xcalloc(sizeof(*cidr));
214 cidr->number_on_this_cidr = 1;
215 cidr->mask = *ip;
216 mask_addr(&cidr->mask, class->cidr_bitlen_ipv4);
217 dlinkAdd(cidr, &cidr->node, &class->list_ipv4);
218 }
219 else if (class->cidr_bitlen_ipv6 > 0)
220 {
221 DLINK_FOREACH(node, class->list_ipv6.head)
222 {
223 struct CidrItem *cidr = node->data;
224
225 if (match_ipv6(ip, &cidr->mask, class->cidr_bitlen_ipv6))
226 {
227 if (!over_rule && (cidr->number_on_this_cidr >= class->number_per_cidr))
228 return -1;
229
230 cidr->number_on_this_cidr++;
231 return 0;
232 }
233 }
234
235 struct CidrItem *cidr = xcalloc(sizeof(*cidr));
236 cidr->number_on_this_cidr = 1;
237 cidr->mask = *ip;
238 mask_addr(&cidr->mask, class->cidr_bitlen_ipv6);
239 dlinkAdd(cidr, &cidr->node, &class->list_ipv6);
240 }
241
242 return 0;
243 }
244
245 /*
246 * remove_from_cidr_check
247 *
248 * inputs - pointer to the ip to be removed
249 * - pointer to the class
250 * output - NONE
251 * side effects -
252 */
253 void
254 remove_from_cidr_check(struct irc_ssaddr *ip, struct ClassItem *aclass)
255 {
256 dlink_node *node, *node_next;
257
258 if (aclass->number_per_cidr == 0)
259 return;
260
261 if (ip->ss.ss_family == AF_INET)
262 {
263 if (aclass->cidr_bitlen_ipv4 == 0)
264 return;
265
266 DLINK_FOREACH_SAFE(node, node_next, aclass->list_ipv4.head)
267 {
268 struct CidrItem *cidr = node->data;
269
270 if (match_ipv4(ip, &cidr->mask, aclass->cidr_bitlen_ipv4))
271 {
272 cidr->number_on_this_cidr--;
273
274 if (cidr->number_on_this_cidr == 0)
275 {
276 dlinkDelete(node, &aclass->list_ipv4);
277 xfree(cidr);
278 return;
279 }
280 }
281 }
282 }
283 else if (aclass->cidr_bitlen_ipv6 > 0)
284 {
285 DLINK_FOREACH_SAFE(node, node_next, aclass->list_ipv6.head)
286 {
287 struct CidrItem *cidr = node->data;
288
289 if (match_ipv6(ip, &cidr->mask, aclass->cidr_bitlen_ipv6))
290 {
291 cidr->number_on_this_cidr--;
292
293 if (cidr->number_on_this_cidr == 0)
294 {
295 dlinkDelete(node, &aclass->list_ipv6);
296 xfree(cidr);
297 return;
298 }
299 }
300 }
301 }
302 }
303
304 void
305 rebuild_cidr_list(struct ClassItem *class)
306 {
307 dlink_node *node;
308
309 destroy_cidr_class(class);
310
311 DLINK_FOREACH(node, local_client_list.head)
312 {
313 struct Client *client_p = node->data;
314 struct MaskItem *conf = client_p->connection->confs.tail->data;
315
316 if (conf && (conf->type == CONF_CLIENT))
317 if (conf->class == class)
318 cidr_limit_reached(1, &client_p->connection->ip, class);
319 }
320 }
321
322 /*
323 * destroy_cidr_list
324 *
325 * inputs - pointer to class dlink list of cidr blocks
326 * output - none
327 * side effects - completely destroys the class link list of cidr blocks
328 */
329 static void
330 destroy_cidr_list(dlink_list *list)
331 {
332 while (list->head)
333 {
334 struct CidrItem *cidr = list->head->data;
335 dlinkDelete(&cidr->node, list);
336 xfree(cidr);
337 }
338 }
339
340 /*
341 * destroy_cidr_class
342 *
343 * inputs - pointer to class
344 * output - none
345 * side effects - completely destroys the class link list of cidr blocks
346 */
347 void
348 destroy_cidr_class(struct ClassItem *class)
349 {
350 destroy_cidr_list(&class->list_ipv4);
351 destroy_cidr_list(&class->list_ipv6);
352 }

Properties

Name Value
svn:eol-style native
svn:keywords Id