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 (8 years, 7 months ago) by michael
Content type: text/x-csrc
File size: 6387 byte(s)
Log Message:
- Style corrections

File Contents

# User Rev Content
1 michael 4325 /*
2     * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3     *
4 michael 8279 * Copyright (c) 1997-2018 ircd-hybrid development team
5 michael 4325 *
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 michael 4565 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 michael 4325 * USA
20     */
21    
22     /*! \file ipcache.c
23     * \brief Routines to count connections from particular IP addresses.
24 michael 4327 * \version $Id$
25 michael 4325 */
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 michael 4977 const struct sockaddr_in *const v4 = (const struct sockaddr_in *)addr;
52 michael 4325 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 michael 4977 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 michael 4325
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 michael 6651 ipcache_find_or_add_address(const struct irc_ssaddr *addr)
81 michael 4325 {
82 michael 8349 dlink_node *node;
83 michael 4977 const uint32_t hash_index = ipcache_hash_address(addr);
84 michael 6651 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 michael 4325
87 michael 4800 DLINK_FOREACH(node, ip_hash_table[hash_index].head)
88 michael 4325 {
89 michael 8349 struct ip_entry *iptr = node->data;
90 michael 4415
91 michael 4325 if (iptr->ip.ss.ss_family != addr->ss.ss_family)
92     continue;
93    
94     if (addr->ss.ss_family == AF_INET6)
95     {
96 michael 6651 ptr_v6 = (const struct sockaddr_in6 *)&iptr->ip;
97 michael 4367 if (!memcmp(&v6->sin6_addr, &ptr_v6->sin6_addr, sizeof(struct in6_addr)))
98     return iptr; /* Found entry already in hash, return it. */
99 michael 4325 }
100     else
101     {
102 michael 6651 ptr_v4 = (const struct sockaddr_in *)&iptr->ip;
103 michael 4367 if (!memcmp(&v4->sin_addr, &ptr_v4->sin_addr, sizeof(struct in_addr)))
104     return iptr; /* Found entry already in hash, return it. */
105 michael 4325 }
106     }
107    
108 michael 8349 struct ip_entry *iptr = mp_pool_get(ip_entry_pool);
109 michael 4325 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 michael 6651 ipcache_remove_address(const struct irc_ssaddr *addr)
127 michael 4325 {
128 michael 8349 dlink_node *node;
129 michael 4977 const uint32_t hash_index = ipcache_hash_address(addr);
130 michael 6651 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 michael 4325
133 michael 4800 DLINK_FOREACH(node, ip_hash_table[hash_index].head)
134 michael 4325 {
135 michael 4800 struct ip_entry *iptr = node->data;
136 michael 4415
137 michael 4325 if (iptr->ip.ss.ss_family != addr->ss.ss_family)
138     continue;
139 michael 4367
140 michael 4325 if (addr->ss.ss_family == AF_INET6)
141     {
142 michael 6651 ptr_v6 = (const struct sockaddr_in6 *)&iptr->ip;
143 michael 4367 if (memcmp(&v6->sin6_addr, &ptr_v6->sin6_addr, sizeof(struct in6_addr)))
144     continue;
145 michael 4325 }
146     else
147     {
148 michael 6651 ptr_v4 = (const struct sockaddr_in *)&iptr->ip;
149 michael 4367 if (memcmp(&v4->sin_addr, &ptr_v4->sin_addr, sizeof(struct in_addr)))
150     continue;
151 michael 4325 }
152    
153     assert(iptr->count > 0);
154    
155     if (--iptr->count == 0 &&
156 michael 4340 (CurrentTime - iptr->last_attempt) >= ConfigGeneral.throttle_time)
157 michael 4325 {
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 michael 8349 dlink_node *node, *node_next;
175 michael 4325
176     for (unsigned int i = 0; i < IP_HASH_SIZE; ++i)
177     {
178 michael 4800 DLINK_FOREACH_SAFE(node, node_next, ip_hash_table[i].head)
179 michael 4325 {
180 michael 4800 struct ip_entry *iptr = node->data;
181 michael 4325
182     if (iptr->count == 0 &&
183 michael 4340 (CurrentTime - iptr->last_attempt) >= ConfigGeneral.throttle_time)
184 michael 4325 {
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 michael 6719 ipcache_get_stats(unsigned int *const number_ips_stored, size_t *const mem_ips_stored)
204 michael 4325 {
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