/src/unbound/iterator/iter_donotq.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * iterator/iter_donotq.c - iterative resolver donotqueryaddresses storage. |
3 | | * |
4 | | * Copyright (c) 2007, NLnet Labs. All rights reserved. |
5 | | * |
6 | | * This software is open source. |
7 | | * |
8 | | * Redistribution and use in source and binary forms, with or without |
9 | | * modification, are permitted provided that the following conditions |
10 | | * are met: |
11 | | * |
12 | | * Redistributions of source code must retain the above copyright notice, |
13 | | * this list of conditions and the following disclaimer. |
14 | | * |
15 | | * Redistributions in binary form must reproduce the above copyright notice, |
16 | | * this list of conditions and the following disclaimer in the documentation |
17 | | * and/or other materials provided with the distribution. |
18 | | * |
19 | | * Neither the name of the NLNET LABS nor the names of its contributors may |
20 | | * be used to endorse or promote products derived from this software without |
21 | | * specific prior written permission. |
22 | | * |
23 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
24 | | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
25 | | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
26 | | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
27 | | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
28 | | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
29 | | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
30 | | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
31 | | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
32 | | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
33 | | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
34 | | */ |
35 | | |
36 | | /** |
37 | | * \file |
38 | | * |
39 | | * This file contains functions to assist the iterator module. |
40 | | * The donotqueryaddresses are stored and looked up. These addresses |
41 | | * (like 127.0.0.1) must not be used to send queries to, and can be |
42 | | * discarded immediately from the server selection. |
43 | | */ |
44 | | #include "config.h" |
45 | | #include "iterator/iter_donotq.h" |
46 | | #include "util/regional.h" |
47 | | #include "util/log.h" |
48 | | #include "util/config_file.h" |
49 | | #include "util/net_help.h" |
50 | | |
51 | | struct iter_donotq* |
52 | | donotq_create(void) |
53 | 0 | { |
54 | 0 | struct iter_donotq* dq = (struct iter_donotq*)calloc(1, |
55 | 0 | sizeof(struct iter_donotq)); |
56 | 0 | if(!dq) |
57 | 0 | return NULL; |
58 | 0 | dq->region = regional_create(); |
59 | 0 | if(!dq->region) { |
60 | 0 | donotq_delete(dq); |
61 | 0 | return NULL; |
62 | 0 | } |
63 | 0 | return dq; |
64 | 0 | } |
65 | | |
66 | | void |
67 | | donotq_delete(struct iter_donotq* dq) |
68 | 0 | { |
69 | 0 | if(!dq) |
70 | 0 | return; |
71 | 0 | regional_destroy(dq->region); |
72 | 0 | free(dq); |
73 | 0 | } |
74 | | |
75 | | /** insert new address into donotq structure */ |
76 | | static int |
77 | | donotq_insert(struct iter_donotq* dq, struct sockaddr_storage* addr, |
78 | | socklen_t addrlen, int net) |
79 | 0 | { |
80 | 0 | struct addr_tree_node* node = (struct addr_tree_node*)regional_alloc( |
81 | 0 | dq->region, sizeof(*node)); |
82 | 0 | if(!node) |
83 | 0 | return 0; |
84 | 0 | if(!addr_tree_insert(&dq->tree, node, addr, addrlen, net)) { |
85 | 0 | verbose(VERB_QUERY, "duplicate donotquery address ignored."); |
86 | 0 | } |
87 | 0 | return 1; |
88 | 0 | } |
89 | | |
90 | | /** apply donotq string */ |
91 | | static int |
92 | | donotq_str_cfg(struct iter_donotq* dq, const char* str) |
93 | 0 | { |
94 | 0 | struct sockaddr_storage addr; |
95 | 0 | int net; |
96 | 0 | socklen_t addrlen; |
97 | 0 | verbose(VERB_ALGO, "donotq: %s", str); |
98 | 0 | if(!netblockstrtoaddr(str, UNBOUND_DNS_PORT, &addr, &addrlen, &net)) { |
99 | 0 | log_err("cannot parse donotquery netblock: %s", str); |
100 | 0 | return 0; |
101 | 0 | } |
102 | 0 | if(!donotq_insert(dq, &addr, addrlen, net)) { |
103 | 0 | log_err("out of memory"); |
104 | 0 | return 0; |
105 | 0 | } |
106 | 0 | return 1; |
107 | 0 | } |
108 | | |
109 | | /** read donotq config */ |
110 | | static int |
111 | | read_donotq(struct iter_donotq* dq, struct config_file* cfg) |
112 | 0 | { |
113 | 0 | struct config_strlist* p; |
114 | 0 | for(p = cfg->donotqueryaddrs; p; p = p->next) { |
115 | 0 | log_assert(p->str); |
116 | 0 | if(!donotq_str_cfg(dq, p->str)) |
117 | 0 | return 0; |
118 | 0 | } |
119 | 0 | return 1; |
120 | 0 | } |
121 | | |
122 | | int |
123 | | donotq_apply_cfg(struct iter_donotq* dq, struct config_file* cfg) |
124 | 0 | { |
125 | 0 | regional_free_all(dq->region); |
126 | 0 | addr_tree_init(&dq->tree); |
127 | 0 | if(!read_donotq(dq, cfg)) |
128 | 0 | return 0; |
129 | 0 | if(cfg->donotquery_localhost) { |
130 | 0 | if(!donotq_str_cfg(dq, "127.0.0.0/8")) |
131 | 0 | return 0; |
132 | 0 | if(cfg->do_ip6) { |
133 | 0 | if(!donotq_str_cfg(dq, "::1")) |
134 | 0 | return 0; |
135 | 0 | } |
136 | 0 | } |
137 | 0 | addr_tree_init_parents(&dq->tree); |
138 | 0 | return 1; |
139 | 0 | } |
140 | | |
141 | | int |
142 | | donotq_lookup(struct iter_donotq* donotq, struct sockaddr_storage* addr, |
143 | | socklen_t addrlen) |
144 | 0 | { |
145 | 0 | return addr_tree_lookup(&donotq->tree, addr, addrlen) != NULL; |
146 | 0 | } |
147 | | |
148 | | size_t |
149 | | donotq_get_mem(struct iter_donotq* donotq) |
150 | 0 | { |
151 | 0 | if(!donotq) return 0; |
152 | 0 | return sizeof(*donotq) + regional_get_mem(donotq->region); |
153 | 0 | } |