ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ipcache.c
Revision: 7924
Committed: Sat Dec 31 13:57:08 2016 UTC (8 years, 7 months ago) by michael
Content type: text/x-csrc
File size: 6413 byte(s)
Log Message:
- Update copyright years

File Contents

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

Properties

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