ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/hopm/trunk/src/negcache.c
Revision: 7819
Committed: Wed Oct 19 21:20:27 2016 UTC (7 years, 5 months ago) by michael
Content type: text/x-csrc
File size: 3457 byte(s)
Log Message:
- negcache.c:negcache_insert(): remove deprecated inet_pton call no longer needed

File Contents

# Content
1 /*
2 * Copyright (c) 2002-2003 Andy Smith
3 * Copyright (c) 2014-2016 ircd-hybrid development team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18 * USA
19 */
20
21 /*
22 * A Negative caching implementation for IPv4/IPv6 addresses. The idea is that
23 * every time an IP address is seen, it is checked against a patricia trie. If
24 * the IP address was previously seen and within an acceptable period of time,
25 * it is not scanned again. Otherwise, the address is scanned as normal. If
26 * it is proven to be OK (i.e. it doesn't run an open proxy) then it is added
27 * to the trie.
28 */
29
30 #include "setup.h"
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <sys/time.h>
35 #include <time.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
40
41 #include "irc.h"
42 #include "negcache.h"
43 #include "config.h"
44 #include "memory.h"
45 #include "log.h"
46 #include "patricia.h"
47 #include "list.h"
48
49
50 extern unsigned int OPT_DEBUG;
51
52 static list_t *negcache_list;
53 static patricia_tree_t *negcache_trie;
54
55
56 /*
57 * Initialise the patricia trie we use for storing our negative cache.
58 */
59 void
60 negcache_init(void)
61 {
62 if (negcache_trie)
63 /* Cache already exists */
64 return;
65
66 negcache_trie = New_Patricia(PATRICIA_MAXBITS);
67 negcache_list = list_create();
68 }
69
70 /*
71 * Check whether an IP address is in our negative cache and was added
72 * recently enough. Return a pointer to its node if so, NULL otherwise.
73 */
74 struct negcache_item *
75 check_neg_cache(const char *ipstr)
76 {
77 if (OptionsItem->negcache == 0)
78 return NULL;
79
80 patricia_node_t *pnode = try_search_exact(negcache_trie, (char *)ipstr);
81 if (pnode)
82 {
83 struct negcache_item *n = pnode->data;
84
85 if (time(NULL) - n->seen <= OptionsItem->negcache)
86 return n;
87 }
88
89 return NULL;
90 }
91
92 /*
93 * Prepare an ASCII string representing an IPv4/IPv6 address for inserting into
94 * our negative cache.
95 */
96 void
97 negcache_insert(const char *ipstr)
98 {
99 patricia_node_t *pnode = make_and_lookup(negcache_trie, (char *)ipstr);
100 if (!pnode)
101 return;
102
103 struct negcache_item *n = xcalloc(sizeof(*n));
104 n->seen = time(NULL);
105
106 pnode->data = n;
107 list_add(negcache_list, node_create(pnode));
108 }
109
110 /*
111 * Wrapper for recursive rebuild function.
112 */
113 void
114 negcache_rebuild(void)
115 {
116 node_t *node, *node_next;
117
118 LIST_FOREACH_SAFE(node, node_next, negcache_list->head)
119 {
120 patricia_node_t *pnode = node->data;
121 struct negcache_item *n = pnode->data;
122
123 if (n->seen + OptionsItem->negcache < time(NULL))
124 {
125 if (OPT_DEBUG >= 2)
126 log_printf("NEGCACHE -> Deleting expired negcache node for %s added at %lu",
127 prefix_toa(pnode->prefix), n->seen);
128
129 list_remove(negcache_list, node);
130 node_free(node);
131 xfree(n);
132 patricia_remove(negcache_trie, pnode);
133 }
134 }
135 }

Properties

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