ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ipcache.c
Revision: 8349
Committed: Sun Mar 4 13:24:03 2018 UTC (7 years, 5 months ago) by michael
Content type: text/x-csrc
File size: 6387 byte(s)
Log Message:
- Style corrections

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 ipcache.c
23 * \brief Routines to count connections from particular IP addresses.
24 * \version $Id$
25 */
26
27 #include "stdinc.h"
28 #include "list.h"
29 #include "ipcache.h"
30 #include "event.h"
31 #include "mempool.h"
32 #include "conf.h"
33 #include "ircd.h"
34
35
36 static dlink_list ip_hash_table[IP_HASH_SIZE];
37 static mp_pool_t *ip_entry_pool;
38
39
40 /* ipcache_hash_address()
41 *
42 * input - pointer to an irc_inaddr
43 * output - integer value used as index into hash table
44 * side effects - hopefully, none
45 */
46 static uint32_t
47 ipcache_hash_address(const struct irc_ssaddr *addr)
48 {
49 if (addr->ss.ss_family == AF_INET)
50 {
51 const struct sockaddr_in *const v4 = (const struct sockaddr_in *)addr;
52 uint32_t hash = 0, ip = ntohl(v4->sin_addr.s_addr);
53
54 hash = ((ip >> 12) + ip) & (IP_HASH_SIZE - 1);
55 return hash;
56 }
57 else
58 {
59 const struct sockaddr_in6 *const v6 = (const struct sockaddr_in6 *)addr;
60 uint32_t hash = 0, *const ip = (uint32_t *)&v6->sin6_addr.s6_addr;
61
62 hash = ip[0] ^ ip[3];
63 hash ^= hash >> 16;
64 hash ^= hash >> 8;
65 hash = hash & (IP_HASH_SIZE - 1);
66 return hash;
67 }
68 }
69
70 /* ipcache_find_or_add_address()
71 *
72 * inputs - pointer to struct irc_ssaddr
73 * output - pointer to a struct ip_entry
74 * side effects -
75 *
76 * If the ip # was not found, a new struct ip_entry is created, and the ip
77 * count set to 0.
78 */
79 struct ip_entry *
80 ipcache_find_or_add_address(const struct irc_ssaddr *addr)
81 {
82 dlink_node *node;
83 const uint32_t hash_index = ipcache_hash_address(addr);
84 const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr, *ptr_v4;
85 const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr, *ptr_v6;
86
87 DLINK_FOREACH(node, ip_hash_table[hash_index].head)
88 {
89 struct ip_entry *iptr = node->data;
90
91 if (iptr->ip.ss.ss_family != addr->ss.ss_family)
92 continue;
93
94 if (addr->ss.ss_family == AF_INET6)
95 {
96 ptr_v6 = (const struct sockaddr_in6 *)&iptr->ip;
97 if (!memcmp(&v6->sin6_addr, &ptr_v6->sin6_addr, sizeof(struct in6_addr)))
98 return iptr; /* Found entry already in hash, return it. */
99 }
100 else
101 {
102 ptr_v4 = (const struct sockaddr_in *)&iptr->ip;
103 if (!memcmp(&v4->sin_addr, &ptr_v4->sin_addr, sizeof(struct in_addr)))
104 return iptr; /* Found entry already in hash, return it. */
105 }
106 }
107
108 struct ip_entry *iptr = mp_pool_get(ip_entry_pool);
109 memcpy(&iptr->ip, addr, sizeof(struct irc_ssaddr));
110
111 dlinkAdd(iptr, &iptr->node, &ip_hash_table[hash_index]);
112
113 return iptr;
114 }
115
116 /* ipcache_remove_addres()
117 *
118 * inputs - unsigned long IP address value
119 * output - NONE
120 * side effects - The ip address given, is looked up in ip hash table
121 * and number of ip#'s for that ip decremented.
122 * If ip # count reaches 0 and has expired,
123 * the struct ip_entry is returned to the ip_entry_heap
124 */
125 void
126 ipcache_remove_address(const struct irc_ssaddr *addr)
127 {
128 dlink_node *node;
129 const uint32_t hash_index = ipcache_hash_address(addr);
130 const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr, *ptr_v4;
131 const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr, *ptr_v6;
132
133 DLINK_FOREACH(node, ip_hash_table[hash_index].head)
134 {
135 struct ip_entry *iptr = node->data;
136
137 if (iptr->ip.ss.ss_family != addr->ss.ss_family)
138 continue;
139
140 if (addr->ss.ss_family == AF_INET6)
141 {
142 ptr_v6 = (const struct sockaddr_in6 *)&iptr->ip;
143 if (memcmp(&v6->sin6_addr, &ptr_v6->sin6_addr, sizeof(struct in6_addr)))
144 continue;
145 }
146 else
147 {
148 ptr_v4 = (const struct sockaddr_in *)&iptr->ip;
149 if (memcmp(&v4->sin_addr, &ptr_v4->sin_addr, sizeof(struct in_addr)))
150 continue;
151 }
152
153 assert(iptr->count > 0);
154
155 if (--iptr->count == 0 &&
156 (CurrentTime - iptr->last_attempt) >= ConfigGeneral.throttle_time)
157 {
158 dlinkDelete(&iptr->node, &ip_hash_table[hash_index]);
159 mp_pool_release(iptr);
160 return;
161 }
162 }
163 }
164
165 /* ipcache_remove_expired_entries()
166 *
167 * input - NONE
168 * output - NONE
169 * side effects - free up all ip entries with no connections
170 */
171 static void
172 ipcache_remove_expired_entries(void *unused)
173 {
174 dlink_node *node, *node_next;
175
176 for (unsigned int i = 0; i < IP_HASH_SIZE; ++i)
177 {
178 DLINK_FOREACH_SAFE(node, node_next, ip_hash_table[i].head)
179 {
180 struct ip_entry *iptr = node->data;
181
182 if (iptr->count == 0 &&
183 (CurrentTime - iptr->last_attempt) >= ConfigGeneral.throttle_time)
184 {
185 dlinkDelete(&iptr->node, &ip_hash_table[i]);
186 mp_pool_release(iptr);
187 }
188 }
189 }
190 }
191
192 /* ipcache_get_stats()
193 *
194 * inputs - pointer to counter of number of ips hashed
195 * - pointer to memory used for ip hash
196 * output - returned via pointers input
197 * side effects - NONE
198 *
199 * number of hashed ip #'s is counted up, plus the amount of memory
200 * used in the hash.
201 */
202 void
203 ipcache_get_stats(unsigned int *const number_ips_stored, size_t *const mem_ips_stored)
204 {
205 for (unsigned int i = 0; i < IP_HASH_SIZE; ++i)
206 *number_ips_stored += dlink_list_length(&ip_hash_table[i]);
207 *mem_ips_stored = *number_ips_stored * sizeof(struct ip_entry);
208 }
209
210 void
211 ipcache_init(void)
212 {
213 static struct event event_expire_ipcache =
214 {
215 .name = "ipcache_remove_expired_entries",
216 .handler = ipcache_remove_expired_entries,
217 .when = 123
218 };
219
220 event_add(&event_expire_ipcache, NULL);
221 ip_entry_pool = mp_pool_new(sizeof(struct ip_entry), MP_CHUNK_SIZE_IP_ENTRY);
222 }

Properties

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