ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ipcache.c
Revision: 4977
Committed: Thu Dec 4 15:12:24 2014 UTC (10 years, 8 months ago) by michael
Content type: text/x-csrc
File size: 6332 byte(s)
Log Message:
- Style corrections; constifications

File Contents

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

Properties

Name Value
svn:keywords Id Revision