]>
git.saurik.com Git - apple/network_cmds.git/blob - unbound/iterator/iter_donotq.c
2 * iterator/iter_donotq.c - iterative resolver donotqueryaddresses storage.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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.
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.
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.
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.
45 #include "iterator/iter_donotq.h"
46 #include "util/regional.h"
48 #include "util/config_file.h"
49 #include "util/net_help.h"
54 struct iter_donotq
* dq
= (struct iter_donotq
*)calloc(1,
55 sizeof(struct iter_donotq
));
58 dq
->region
= regional_create();
67 donotq_delete(struct iter_donotq
* dq
)
71 regional_destroy(dq
->region
);
75 /** insert new address into donotq structure */
77 donotq_insert(struct iter_donotq
* dq
, struct sockaddr_storage
* addr
,
78 socklen_t addrlen
, int net
)
80 struct addr_tree_node
* node
= (struct addr_tree_node
*)regional_alloc(
81 dq
->region
, sizeof(*node
));
84 if(!addr_tree_insert(&dq
->tree
, node
, addr
, addrlen
, net
)) {
85 verbose(VERB_QUERY
, "duplicate donotquery address ignored.");
90 /** apply donotq string */
92 donotq_str_cfg(struct iter_donotq
* dq
, const char* str
)
94 struct sockaddr_storage addr
;
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
);
102 if(!donotq_insert(dq
, &addr
, addrlen
, net
)) {
103 log_err("out of memory");
109 /** read donotq config */
111 read_donotq(struct iter_donotq
* dq
, struct config_file
* cfg
)
113 struct config_strlist
* p
;
114 for(p
= cfg
->donotqueryaddrs
; p
; p
= p
->next
) {
116 if(!donotq_str_cfg(dq
, p
->str
))
123 donotq_apply_cfg(struct iter_donotq
* dq
, struct config_file
* cfg
)
125 regional_free_all(dq
->region
);
126 addr_tree_init(&dq
->tree
);
127 if(!read_donotq(dq
, cfg
))
129 if(cfg
->donotquery_localhost
) {
130 if(!donotq_str_cfg(dq
, "127.0.0.0/8"))
133 if(!donotq_str_cfg(dq
, "::1"))
137 addr_tree_init_parents(&dq
->tree
);
142 donotq_lookup(struct iter_donotq
* donotq
, struct sockaddr_storage
* addr
,
145 return addr_tree_lookup(&donotq
->tree
, addr
, addrlen
) != NULL
;
149 donotq_get_mem(struct iter_donotq
* donotq
)
151 if(!donotq
) return 0;
152 return sizeof(*donotq
) + regional_get_mem(donotq
->region
);