/[svn]/hopm/trunk/src/negcache.c
ViewVC logotype

Contents of /hopm/trunk/src/negcache.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 7821 - (show annotations)
Wed Oct 19 21:40:43 2016 UTC (4 years ago) by michael
File MIME type: text/x-chdr
File size: 3352 byte(s)
- negcache.h: remove unused header includes

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 <time.h>
35
36 #include "list.h"
37 #include "patricia.h"
38 #include "negcache.h"
39 #include "config.h"
40 #include "memory.h"
41 #include "log.h"
42
43
44 extern unsigned int OPT_DEBUG;
45
46 static list_t *negcache_list;
47 static patricia_tree_t *negcache_trie;
48
49
50 /*
51 * Initialise the patricia trie we use for storing our negative cache.
52 */
53 void
54 negcache_init(void)
55 {
56 if (negcache_trie)
57 /* Cache already exists */
58 return;
59
60 negcache_trie = New_Patricia(PATRICIA_MAXBITS);
61 negcache_list = list_create();
62 }
63
64 /*
65 * Check whether an IP address is in our negative cache and was added
66 * recently enough. Return a pointer to its node if so, NULL otherwise.
67 */
68 struct negcache_item *
69 check_neg_cache(const char *ipstr)
70 {
71 if (OptionsItem->negcache == 0)
72 return NULL;
73
74 patricia_node_t *pnode = try_search_exact(negcache_trie, (char *)ipstr);
75 if (pnode)
76 {
77 struct negcache_item *n = pnode->data;
78
79 if (time(NULL) - n->seen <= OptionsItem->negcache)
80 return n;
81 }
82
83 return NULL;
84 }
85
86 /*
87 * Prepare an ASCII string representing an IPv4/IPv6 address for inserting into
88 * our negative cache.
89 */
90 void
91 negcache_insert(const char *ipstr)
92 {
93 patricia_node_t *pnode = make_and_lookup(negcache_trie, (char *)ipstr);
94 if (!pnode)
95 return; /* Malformed IP address */
96
97 struct negcache_item *n = xcalloc(sizeof(*n));
98 n->seen = time(NULL);
99
100 pnode->data = n;
101 list_add(negcache_list, node_create(pnode));
102 }
103
104 /*
105 * Wrapper for recursive rebuild function.
106 */
107 void
108 negcache_rebuild(void)
109 {
110 node_t *node, *node_next;
111
112 LIST_FOREACH_SAFE(node, node_next, negcache_list->head)
113 {
114 patricia_node_t *pnode = node->data;
115 struct negcache_item *n = pnode->data;
116
117 if (n->seen + OptionsItem->negcache < time(NULL))
118 {
119 if (OPT_DEBUG >= 2)
120 log_printf("NEGCACHE -> Deleting expired negcache node for %s added at %lu",
121 prefix_toa(pnode->prefix), n->seen);
122
123 list_remove(negcache_list, node);
124 node_free(node);
125 xfree(n);
126 patricia_remove(negcache_trie, pnode);
127 }
128 }
129 }

Properties

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

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28