ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ipcache.c
Revision: 8593
Committed: Sun Oct 21 18:11:04 2018 UTC (6 years, 10 months ago) by michael
Content type: text/x-csrc
File size: 4312 byte(s)
Log Message:
- ipcache: rewrite to use patricia

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 "memory.h"
32 #include "conf.h"
33 #include "ircd.h"
34 #include "patricia.h"
35
36
37 static dlink_list ipcache_list;
38 static patricia_tree_t *ipcache_trie;
39
40
41 /* ipcache_find_or_add_address()
42 *
43 * inputs - pointer to struct irc_ssaddr
44 * output - pointer to a struct ip_entry
45 * side effects -
46 *
47 * If the ip # was not found, a new struct ip_entry is created, and the ip
48 * count set to 0.
49 */
50 struct ip_entry *
51 ipcache_record_find_or_add(void *addr)
52 {
53 patricia_node_t *pnode = patricia_make_and_lookup_addr(ipcache_trie, addr, 0);
54
55 if (pnode->data) /* Deliberate crash if 'pnode' is NULL */
56 return pnode->data; /* Already added to the trie */
57
58 struct ip_entry *iptr = xcalloc(sizeof(*iptr));
59 dlinkAdd(pnode, &iptr->node, &ipcache_list);
60
61 PATRICIA_DATA_SET(pnode, iptr);
62
63 return iptr;
64 }
65
66 static void
67 ipcache_record_delete(patricia_node_t *pnode)
68 {
69 struct ip_entry *iptr = PATRICIA_DATA_GET(pnode, struct ip_entry);
70
71 if (iptr->count_local == 0 && iptr->count_remote == 0 &&
72 (CurrentTime - iptr->last_attempt) >= ConfigGeneral.throttle_time)
73 {
74 dlinkDelete(&iptr->node, &ipcache_list);
75 xfree(iptr);
76
77 patricia_remove(ipcache_trie, pnode);
78 }
79 }
80
81 /* ipcache_remove_addres()
82 *
83 * inputs - unsigned long IP address value
84 * output - NONE
85 * side effects - The ip address given, is looked up in ip hash table
86 * and number of ip#'s for that ip decremented.
87 * If ip # count reaches 0 and has expired,
88 * the struct ip_entry is returned to the ip_entry_heap
89 */
90 void
91 ipcache_record_remove(void *addr, int local)
92 {
93 patricia_node_t *pnode = patricia_try_search_exact_addr(ipcache_trie, addr, 0);
94
95 if (pnode == NULL)
96 return;
97
98 struct ip_entry *iptr = PATRICIA_DATA_GET(pnode, struct ip_entry);
99 assert(iptr->count_local > 0 || iptr->count_remote > 0);
100
101 if (local)
102 --iptr->count_local;
103 else
104 --iptr->count_remote;
105
106 ipcache_record_delete(pnode);
107 }
108
109 /* ipcache_remove_expired_entries()
110 *
111 * input - NONE
112 * output - NONE
113 * side effects - free up all ip entries with no connections
114 */
115 static void
116 ipcache_remove_expired_records(void *unused)
117 {
118 dlink_node *node, *node_next;
119
120 DLINK_FOREACH_SAFE(node, node_next, ipcache_list.head)
121 ipcache_record_delete(node->data);
122 }
123
124 /* ipcache_get_stats()
125 *
126 * inputs - pointer to counter of number of ips hashed
127 * - pointer to memory used for ip hash
128 * output - returned via pointers input
129 * side effects - NONE
130 *
131 * number of hashed ip #'s is counted up, plus the amount of memory
132 * used in the hash.
133 */
134 void
135 ipcache_get_stats(unsigned int *const number_ips_stored, size_t *const mem_ips_stored)
136 {
137 /* TBD: inaccurate for now as it does only count the amount of memory for struct ip_entry items */
138 (*number_ips_stored) = dlink_list_length(&ipcache_list);
139 (*mem_ips_stored) = dlink_list_length(&ipcache_list) * sizeof(struct ip_entry);
140 }
141
142 void
143 ipcache_init(void)
144 {
145 static struct event event_expire_ipcache =
146 {
147 .name = "ipcache_remove_expired_records",
148 .handler = ipcache_remove_expired_records,
149 .when = 123
150 };
151
152 ipcache_trie = patricia_new(PATRICIA_MAXBITS);
153
154 event_add(&event_expire_ipcache, NULL);
155 }

Properties

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