1 |
|
/* |
2 |
|
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
|
* |
4 |
< |
* Copyright (c) 1997-2014 ircd-hybrid development team |
4 |
> |
* Copyright (c) 1997-2015 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 |
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 |
18 |
> |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
|
* USA |
20 |
|
*/ |
21 |
|
|
22 |
|
/*! \file ipcache.c |
23 |
|
* \brief Routines to count connections from particular IP addresses. |
24 |
< |
* \version $Id: ipcache.c 4324 2014-08-01 16:59:35Z michael $ |
24 |
> |
* \version $Id$ |
25 |
|
*/ |
26 |
|
|
27 |
|
#include "stdinc.h" |
49 |
|
{ |
50 |
|
if (addr->ss.ss_family == AF_INET) |
51 |
|
{ |
52 |
< |
const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr; |
52 |
> |
const struct sockaddr_in *const v4 = (const struct sockaddr_in *)addr; |
53 |
|
uint32_t hash = 0, ip = ntohl(v4->sin_addr.s_addr); |
54 |
|
|
55 |
|
hash = ((ip >> 12) + ip) & (IP_HASH_SIZE - 1); |
56 |
|
return hash; |
57 |
|
} |
58 |
– |
#ifdef IPV6 |
58 |
|
else |
59 |
|
{ |
60 |
< |
const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr; |
61 |
< |
uint32_t hash = 0, *ip = (uint32_t *)&v6->sin6_addr.s6_addr; |
60 |
> |
const struct sockaddr_in6 *const v6 = (const struct sockaddr_in6 *)addr; |
61 |
> |
uint32_t hash = 0, *const ip = (uint32_t *)&v6->sin6_addr.s6_addr; |
62 |
|
|
63 |
|
hash = ip[0] ^ ip[3]; |
64 |
|
hash ^= hash >> 16; |
66 |
|
hash = hash & (IP_HASH_SIZE - 1); |
67 |
|
return hash; |
68 |
|
} |
70 |
– |
#else |
71 |
– |
return 0; |
72 |
– |
#endif |
69 |
|
} |
70 |
|
|
71 |
|
/* ipcache_find_or_add_address() |
80 |
|
struct ip_entry * |
81 |
|
ipcache_find_or_add_address(struct irc_ssaddr *addr) |
82 |
|
{ |
83 |
< |
dlink_node *ptr = NULL; |
83 |
> |
dlink_node *node = NULL; |
84 |
|
struct ip_entry *iptr = NULL; |
85 |
< |
uint32_t hash_index = ipcache_hash_address(addr); |
90 |
< |
int res = 0; |
85 |
> |
const uint32_t hash_index = ipcache_hash_address(addr); |
86 |
|
struct sockaddr_in *v4 = (struct sockaddr_in *)addr, *ptr_v4; |
92 |
– |
#ifdef IPV6 |
87 |
|
struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)addr, *ptr_v6; |
94 |
– |
#endif |
88 |
|
|
89 |
< |
DLINK_FOREACH(ptr, ip_hash_table[hash_index].head) |
89 |
> |
DLINK_FOREACH(node, ip_hash_table[hash_index].head) |
90 |
|
{ |
91 |
< |
iptr = ptr->data; |
92 |
< |
#ifdef IPV6 |
91 |
> |
iptr = node->data; |
92 |
> |
|
93 |
|
if (iptr->ip.ss.ss_family != addr->ss.ss_family) |
94 |
|
continue; |
95 |
|
|
96 |
|
if (addr->ss.ss_family == AF_INET6) |
97 |
|
{ |
98 |
|
ptr_v6 = (struct sockaddr_in6 *)&iptr->ip; |
99 |
< |
res = memcmp(&v6->sin6_addr, &ptr_v6->sin6_addr, sizeof(struct in6_addr)); |
99 |
> |
if (!memcmp(&v6->sin6_addr, &ptr_v6->sin6_addr, sizeof(struct in6_addr))) |
100 |
> |
return iptr; /* Found entry already in hash, return it. */ |
101 |
|
} |
102 |
|
else |
109 |
– |
#endif |
103 |
|
{ |
104 |
|
ptr_v4 = (struct sockaddr_in *)&iptr->ip; |
105 |
< |
res = memcmp(&v4->sin_addr, &ptr_v4->sin_addr, sizeof(struct in_addr)); |
105 |
> |
if (!memcmp(&v4->sin_addr, &ptr_v4->sin_addr, sizeof(struct in_addr))) |
106 |
> |
return iptr; /* Found entry already in hash, return it. */ |
107 |
|
} |
114 |
– |
|
115 |
– |
if (res == 0) |
116 |
– |
return iptr; /* Found entry already in hash, return it. */ |
108 |
|
} |
109 |
|
|
110 |
|
iptr = mp_pool_get(ip_entry_pool); |
127 |
|
void |
128 |
|
ipcache_remove_address(struct irc_ssaddr *addr) |
129 |
|
{ |
130 |
< |
dlink_node *ptr = NULL; |
131 |
< |
uint32_t hash_index = ipcache_hash_address(addr); |
141 |
< |
int res = 0; |
130 |
> |
dlink_node *node = NULL; |
131 |
> |
const uint32_t hash_index = ipcache_hash_address(addr); |
132 |
|
struct sockaddr_in *v4 = (struct sockaddr_in *)addr, *ptr_v4; |
143 |
– |
#ifdef IPV6 |
133 |
|
struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)addr, *ptr_v6; |
145 |
– |
#endif |
134 |
|
|
135 |
< |
DLINK_FOREACH(ptr, ip_hash_table[hash_index].head) |
135 |
> |
DLINK_FOREACH(node, ip_hash_table[hash_index].head) |
136 |
|
{ |
137 |
< |
struct ip_entry *iptr = ptr->data; |
138 |
< |
#ifdef IPV6 |
137 |
> |
struct ip_entry *iptr = node->data; |
138 |
> |
|
139 |
|
if (iptr->ip.ss.ss_family != addr->ss.ss_family) |
140 |
|
continue; |
141 |
+ |
|
142 |
|
if (addr->ss.ss_family == AF_INET6) |
143 |
|
{ |
144 |
|
ptr_v6 = (struct sockaddr_in6 *)&iptr->ip; |
145 |
< |
res = memcmp(&v6->sin6_addr, &ptr_v6->sin6_addr, sizeof(struct in6_addr)); |
145 |
> |
if (memcmp(&v6->sin6_addr, &ptr_v6->sin6_addr, sizeof(struct in6_addr))) |
146 |
> |
continue; |
147 |
|
} |
148 |
|
else |
159 |
– |
#endif |
149 |
|
{ |
150 |
|
ptr_v4 = (struct sockaddr_in *)&iptr->ip; |
151 |
< |
res = memcmp(&v4->sin_addr, &ptr_v4->sin_addr, sizeof(struct in_addr)); |
151 |
> |
if (memcmp(&v4->sin_addr, &ptr_v4->sin_addr, sizeof(struct in_addr))) |
152 |
> |
continue; |
153 |
|
} |
154 |
|
|
165 |
– |
if (res) |
166 |
– |
continue; |
155 |
|
assert(iptr->count > 0); |
156 |
|
|
157 |
|
if (--iptr->count == 0 && |
158 |
< |
(CurrentTime - iptr->last_attempt) >= ConfigFileEntry.throttle_time) |
158 |
> |
(CurrentTime - iptr->last_attempt) >= ConfigGeneral.throttle_time) |
159 |
|
{ |
160 |
|
dlinkDelete(&iptr->node, &ip_hash_table[hash_index]); |
161 |
|
mp_pool_release(iptr); |
173 |
|
static void |
174 |
|
ipcache_remove_expired_entries(void *unused) |
175 |
|
{ |
176 |
< |
dlink_node *ptr = NULL, *ptr_next = NULL; |
176 |
> |
dlink_node *node = NULL, *node_next = NULL; |
177 |
|
|
178 |
|
for (unsigned int i = 0; i < IP_HASH_SIZE; ++i) |
179 |
|
{ |
180 |
< |
DLINK_FOREACH_SAFE(ptr, ptr_next, ip_hash_table[i].head) |
180 |
> |
DLINK_FOREACH_SAFE(node, node_next, ip_hash_table[i].head) |
181 |
|
{ |
182 |
< |
struct ip_entry *iptr = ptr->data; |
182 |
> |
struct ip_entry *iptr = node->data; |
183 |
|
|
184 |
|
if (iptr->count == 0 && |
185 |
< |
(CurrentTime - iptr->last_attempt) >= ConfigFileEntry.throttle_time) |
185 |
> |
(CurrentTime - iptr->last_attempt) >= ConfigGeneral.throttle_time) |
186 |
|
{ |
187 |
|
dlinkDelete(&iptr->node, &ip_hash_table[i]); |
188 |
|
mp_pool_release(iptr); |
202 |
|
* used in the hash. |
203 |
|
*/ |
204 |
|
void |
205 |
< |
ipcache_get_stats(unsigned int *number_ips_stored, uint64_t *mem_ips_stored) |
205 |
> |
ipcache_get_stats(unsigned int *const number_ips_stored, uint64_t *const mem_ips_stored) |
206 |
|
{ |
219 |
– |
*number_ips_stored = 0; |
220 |
– |
*mem_ips_stored = 0; |
221 |
– |
|
207 |
|
for (unsigned int i = 0; i < IP_HASH_SIZE; ++i) |
208 |
|
*number_ips_stored += dlink_list_length(&ip_hash_table[i]); |
209 |
|
*mem_ips_stored = *number_ips_stored * sizeof(struct ip_entry); |