xref: /freebsd/contrib/unbound/iterator/iter_donotq.c (revision 3c4ba5f55438f7afd4f4b0b56f88f2bb505fd6a6)
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 {
54 	struct iter_donotq* dq = (struct iter_donotq*)calloc(1,
55 		sizeof(struct iter_donotq));
56 	if(!dq)
57 		return NULL;
58 	dq->region = regional_create();
59 	if(!dq->region) {
60 		donotq_delete(dq);
61 		return NULL;
62 	}
63 	return dq;
64 }
65 
66 void
67 donotq_delete(struct iter_donotq* dq)
68 {
69 	if(!dq)
70 		return;
71 	regional_destroy(dq->region);
72 	free(dq);
73 }
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 {
80 	struct addr_tree_node* node = (struct addr_tree_node*)regional_alloc(
81 		dq->region, sizeof(*node));
82 	if(!node)
83 		return 0;
84 	if(!addr_tree_insert(&dq->tree, node, addr, addrlen, net)) {
85 		verbose(VERB_QUERY, "duplicate donotquery address ignored.");
86 	}
87 	return 1;
88 }
89 
90 /** apply donotq string */
91 static int
92 donotq_str_cfg(struct iter_donotq* dq, const char* str)
93 {
94 	struct sockaddr_storage addr;
95 	int net;
96 	socklen_t addrlen;
97 	verbose(VERB_ALGO, "donotq: %s", str);
98 	if(!netblockstrtoaddr(str, UNBOUND_DNS_PORT, &addr, &addrlen, &net)) {
99 		log_err("cannot parse donotquery netblock: %s", str);
100 		return 0;
101 	}
102 	if(!donotq_insert(dq, &addr, addrlen, net)) {
103 		log_err("out of memory");
104 		return 0;
105 	}
106 	return 1;
107 }
108 
109 /** read donotq config */
110 static int
111 read_donotq(struct iter_donotq* dq, struct config_file* cfg)
112 {
113 	struct config_strlist* p;
114 	for(p = cfg->donotqueryaddrs; p; p = p->next) {
115 		log_assert(p->str);
116 		if(!donotq_str_cfg(dq, p->str))
117 			return 0;
118 	}
119 	return 1;
120 }
121 
122 int
123 donotq_apply_cfg(struct iter_donotq* dq, struct config_file* cfg)
124 {
125 	regional_free_all(dq->region);
126 	addr_tree_init(&dq->tree);
127 	if(!read_donotq(dq, cfg))
128 		return 0;
129 	if(cfg->donotquery_localhost) {
130 		if(!donotq_str_cfg(dq, "127.0.0.0/8"))
131 			return 0;
132 		if(cfg->do_ip6) {
133 			if(!donotq_str_cfg(dq, "::1"))
134 				return 0;
135 		}
136 	}
137 	addr_tree_init_parents(&dq->tree);
138 	return 1;
139 }
140 
141 int
142 donotq_lookup(struct iter_donotq* donotq, struct sockaddr_storage* addr,
143         socklen_t addrlen)
144 {
145 	return addr_tree_lookup(&donotq->tree, addr, addrlen) != NULL;
146 }
147 
148 size_t
149 donotq_get_mem(struct iter_donotq* donotq)
150 {
151 	if(!donotq) return 0;
152 	return sizeof(*donotq) + regional_get_mem(donotq->region);
153 }
154