1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 1997-2021 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 |
#include "patricia.h" |
35 |
|
36 |
|
37 |
struct ClassItem *class_default; |
38 |
|
39 |
static dlink_list class_list; |
40 |
|
41 |
|
42 |
const dlink_list * |
43 |
class_get_list(void) |
44 |
{ |
45 |
return &class_list; |
46 |
} |
47 |
|
48 |
struct ClassItem * |
49 |
class_make(void) |
50 |
{ |
51 |
struct ClassItem *class = xcalloc(sizeof(*class)); |
52 |
|
53 |
class->active = true; |
54 |
class->con_freq = DEFAULT_CONNECTFREQUENCY; |
55 |
class->ping_freq = DEFAULT_PINGFREQUENCY; |
56 |
class->max_sendq = DEFAULT_SENDQ; |
57 |
class->max_recvq = DEFAULT_RECVQ; |
58 |
class->ip_tree_v6 = patricia_new(128); |
59 |
class->ip_tree_v4 = patricia_new( 32); |
60 |
|
61 |
dlinkAdd(class, &class->node, &class_list); |
62 |
|
63 |
return class; |
64 |
} |
65 |
|
66 |
void |
67 |
class_free(struct ClassItem *const class) |
68 |
{ |
69 |
assert(class != class_default); |
70 |
assert(class->active == false); |
71 |
assert(class->ref_count == 0); |
72 |
|
73 |
if (class->ip_tree_v6) |
74 |
patricia_destroy(class->ip_tree_v6, NULL); |
75 |
if (class->ip_tree_v4) |
76 |
patricia_destroy(class->ip_tree_v4, NULL); |
77 |
|
78 |
dlinkDelete(&class->node, &class_list); |
79 |
xfree(class->name); |
80 |
xfree(class); |
81 |
} |
82 |
|
83 |
void |
84 |
class_init(void) |
85 |
{ |
86 |
class_default = class_make(); |
87 |
class_default->name = xstrdup("default"); |
88 |
} |
89 |
|
90 |
const struct ClassItem * |
91 |
class_get_ptr(const dlink_list *const list) |
92 |
{ |
93 |
const dlink_node *const node = list->head; |
94 |
|
95 |
if (node) |
96 |
{ |
97 |
const struct MaskItem *const conf = node->data; |
98 |
|
99 |
assert(conf->class); |
100 |
assert(conf->type & (CONF_OPER | CONF_CLIENT | CONF_SERVER)); |
101 |
|
102 |
return conf->class; |
103 |
} |
104 |
|
105 |
return class_default; |
106 |
} |
107 |
|
108 |
const char * |
109 |
get_client_class(const dlink_list *const list) |
110 |
{ |
111 |
return class_get_ptr(list)->name; |
112 |
} |
113 |
|
114 |
unsigned int |
115 |
get_client_ping(const dlink_list *const list) |
116 |
{ |
117 |
return class_get_ptr(list)->ping_freq; |
118 |
} |
119 |
|
120 |
unsigned int |
121 |
get_sendq(const dlink_list *const list) |
122 |
{ |
123 |
return class_get_ptr(list)->max_sendq; |
124 |
} |
125 |
|
126 |
unsigned int |
127 |
get_recvq(const dlink_list *const list) |
128 |
{ |
129 |
return class_get_ptr(list)->max_recvq; |
130 |
} |
131 |
|
132 |
/* |
133 |
* inputs - Integer (Number of class) |
134 |
* output - Pointer to ClassItem struct. Non-NULL expected |
135 |
* side effects - NONE |
136 |
*/ |
137 |
struct ClassItem * |
138 |
class_find(const char *name, bool active) |
139 |
{ |
140 |
dlink_node *node; |
141 |
|
142 |
DLINK_FOREACH(node, class_list.head) |
143 |
{ |
144 |
struct ClassItem *class = node->data; |
145 |
|
146 |
if (irccmp(class->name, name) == 0) |
147 |
{ |
148 |
if (active == true && class->active == false) |
149 |
return NULL; |
150 |
|
151 |
return class; |
152 |
} |
153 |
} |
154 |
|
155 |
return NULL; |
156 |
} |
157 |
|
158 |
/* |
159 |
* We don't delete the class table, rather mark all entries for deletion. |
160 |
* The table is cleaned up by delete_marked_classes. - avalon |
161 |
*/ |
162 |
void |
163 |
class_mark_for_deletion(void) |
164 |
{ |
165 |
dlink_node *node; |
166 |
|
167 |
DLINK_FOREACH_PREV(node, class_list.tail->prev) |
168 |
{ |
169 |
struct ClassItem *class = node->data; |
170 |
class->active = false; |
171 |
} |
172 |
} |
173 |
|
174 |
void |
175 |
class_delete_marked(void) |
176 |
{ |
177 |
dlink_node *node, *node_next; |
178 |
|
179 |
DLINK_FOREACH_SAFE(node, node_next, class_list.head) |
180 |
{ |
181 |
struct ClassItem *class = node->data; |
182 |
|
183 |
if (class->active == false && class->ref_count == 0) |
184 |
class_free(class); |
185 |
} |
186 |
} |
187 |
|
188 |
static void * |
189 |
class_ip_limit_trie(struct ClassItem *class, void *addr) |
190 |
{ |
191 |
if (((struct sockaddr *)addr)->sa_family == AF_INET6) |
192 |
return class->ip_tree_v6; |
193 |
else |
194 |
return class->ip_tree_v4; |
195 |
} |
196 |
|
197 |
bool |
198 |
class_ip_limit_add(struct ClassItem *class, void *addr, bool over_rule) |
199 |
{ |
200 |
int bitlen; |
201 |
|
202 |
if (((struct sockaddr *)addr)->sa_family == AF_INET6) |
203 |
bitlen = class->cidr_bitlen_ipv6; |
204 |
else |
205 |
bitlen = class->cidr_bitlen_ipv4; |
206 |
|
207 |
if (class->number_per_cidr == 0 || bitlen == 0) |
208 |
return false; |
209 |
|
210 |
patricia_node_t *pnode = patricia_make_and_lookup_addr(class_ip_limit_trie(class, addr), addr, bitlen); |
211 |
if (((uintptr_t)pnode->data) >= class->number_per_cidr && over_rule == false) |
212 |
return true; |
213 |
|
214 |
PATRICIA_DATA_SET(pnode, (((uintptr_t)pnode->data) + 1)); |
215 |
return false; |
216 |
} |
217 |
|
218 |
bool |
219 |
class_ip_limit_remove(struct ClassItem *class, void *addr) |
220 |
{ |
221 |
int bitlen; |
222 |
|
223 |
if (((struct sockaddr *)addr)->sa_family == AF_INET6) |
224 |
bitlen = class->cidr_bitlen_ipv6; |
225 |
else |
226 |
bitlen = class->cidr_bitlen_ipv4; |
227 |
|
228 |
if (class->number_per_cidr == 0 || bitlen == 0) |
229 |
return false; |
230 |
|
231 |
patricia_node_t *pnode = patricia_try_search_best_addr(class_ip_limit_trie(class, addr), addr, 0); |
232 |
if (pnode == NULL) |
233 |
return false; |
234 |
|
235 |
PATRICIA_DATA_SET(pnode, (((uintptr_t)pnode->data) - 1)); |
236 |
|
237 |
if (((uintptr_t)pnode->data) == 0) |
238 |
{ |
239 |
patricia_remove(class_ip_limit_trie(class, addr), pnode); |
240 |
return true; |
241 |
} |
242 |
|
243 |
return false; |
244 |
} |
245 |
|
246 |
void |
247 |
class_ip_limit_rebuild(struct ClassItem *class) |
248 |
{ |
249 |
dlink_node *node; |
250 |
|
251 |
patricia_clear(class->ip_tree_v6, NULL); |
252 |
patricia_clear(class->ip_tree_v4, NULL); |
253 |
|
254 |
DLINK_FOREACH(node, local_client_list.head) |
255 |
{ |
256 |
struct Client *client = node->data; |
257 |
struct MaskItem *conf = client->connection->confs.tail->data; |
258 |
|
259 |
if (conf->type == CONF_CLIENT) |
260 |
if (conf->class == class) |
261 |
class_ip_limit_add(class, &client->ip, true); |
262 |
} |
263 |
} |