2 * iterator/iter_utils.h - iterative resolver module utility functions.
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 LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
39 * This file contains functions to assist the iterator module.
40 * Configuration options. Forward zones.
43 #ifndef ITERATOR_ITER_UTILS_H
44 #define ITERATOR_ITER_UTILS_H
45 #include "iterator/iter_resptype.h"
46 #include <ldns/buffer.h>
61 struct ub_packed_rrset_key;
64 * Process config options and set iterator module state.
65 * Sets default values if no config is found.
66 * @param iter_env: iterator module state.
67 * @param cfg: config options.
70 int iter_apply_cfg(struct iter_env* iter_env, struct config_file* cfg);
73 * Select a valid, nice target to send query to.
74 * Sorting and removing unsuitable targets is combined.
76 * @param iter_env: iterator module global state, with ip6 enabled and
77 * do-not-query-addresses.
78 * @param env: environment with infra cache (lameness, rtt info).
79 * @param dp: delegation point with result list.
80 * @param name: zone name (for lameness check).
81 * @param namelen: length of name.
82 * @param qtype: query type that we want to send.
83 * @param dnssec_lame: set to 1, if a known dnssec-lame server is selected
84 * these are not preferred, but are used as a last resort.
85 * @param chase_to_rd: set to 1 if a known recursion lame server is selected
86 * these are not preferred, but are used as a last resort.
87 * @param open_target: number of currently outstanding target queries.
88 * If we wait for these, perhaps more server addresses become available.
89 * @param blacklist: the IP blacklist to use.
90 * @return best target or NULL if no target.
91 * if not null, that target is removed from the result list in the dp.
93 struct delegpt_addr* iter_server_selection(struct iter_env* iter_env,
94 struct module_env* env, struct delegpt* dp, uint8_t* name,
95 size_t namelen, uint16_t qtype, int* dnssec_lame,
96 int* chase_to_rd, int open_target, struct sock_list* blacklist);
99 * Allocate dns_msg from parsed msg, in regional.
100 * @param pkt: packet.
101 * @param msg: parsed message (cleaned and ready for regional allocation).
102 * @param regional: regional to use for allocation.
103 * @return newly allocated dns_msg, or NULL on memory error.
105 struct dns_msg* dns_alloc_msg(ldns_buffer* pkt, struct msg_parse* msg,
106 struct regional* regional);
109 * Copy a dns_msg to this regional.
110 * @param from: dns message, also in regional.
111 * @param regional: regional to use for allocation.
112 * @return newly allocated dns_msg, or NULL on memory error.
114 struct dns_msg* dns_copy_msg(struct dns_msg* from, struct regional* regional);
117 * Allocate a dns_msg with malloc/alloc structure and store in dns cache.
118 * @param env: environment, with alloc structure and dns cache.
119 * @param qinf: query info, the query for which answer is stored.
120 * @param rep: reply in dns_msg from dns_alloc_msg for example.
121 * @param is_referral: If true, then the given message to be stored is a
122 * referral. The cache implementation may use this as a hint.
123 * @param leeway: prefetch TTL leeway to expire old rrsets quicker.
124 * @param pside: true if dp is parentside, thus message is 'fresh' and NS
125 * can be prefetch-updates.
126 * @param region: to copy modified (cache is better) rrs back to.
127 * @return 0 on alloc error (out of memory).
129 int iter_dns_store(struct module_env* env, struct query_info* qinf,
130 struct reply_info* rep, int is_referral, uint32_t leeway, int pside,
131 struct regional* region);
134 * Select randomly with n/m probability.
135 * For shuffle NS records for address fetching.
136 * @param rnd: random table
137 * @param n: probability.
138 * @param m: divisor for probability.
139 * @return true with n/m probability.
141 int iter_ns_probability(struct ub_randstate* rnd, int n, int m);
144 * Mark targets that result in a dependency cycle as done, so they
145 * will not get selected as targets.
146 * @param qstate: query state.
147 * @param dp: delegpt to mark ns in.
149 void iter_mark_cycle_targets(struct module_qstate* qstate, struct delegpt* dp);
152 * Mark targets that result in a dependency cycle as done, so they
153 * will not get selected as targets. For the parent-side lookups.
154 * @param qstate: query state.
155 * @param dp: delegpt to mark ns in.
157 void iter_mark_pside_cycle_targets(struct module_qstate* qstate,
161 * See if delegation is useful or offers immediately no targets for
163 * @param qinfo: query name and type
164 * @param qflags: query flags with RD flag
165 * @param dp: delegpt to check.
166 * @return true if dp is useless.
168 int iter_dp_is_useless(struct query_info* qinfo, uint16_t qflags,
172 * See if delegation is expected to have DNSSEC information (RRSIGs) in
173 * its answers, or not. Inspects delegation point (name), trust anchors,
174 * and delegation message (DS RRset) to determine this.
175 * @param env: module env with trust anchors.
176 * @param dp: delegation point.
177 * @param msg: delegation message, with DS if a secure referral.
178 * @param dclass: class of query.
179 * @return 1 if dnssec is expected, 0 if not.
181 int iter_indicates_dnssec(struct module_env* env, struct delegpt* dp,
182 struct dns_msg* msg, uint16_t dclass);
185 * See if a message contains DNSSEC.
186 * This is examined by looking for RRSIGs. With DNSSEC a valid answer,
187 * nxdomain, nodata, referral or cname reply has RRSIGs in answer or auth
188 * sections, sigs on answer data, SOA, DS, or NSEC/NSEC3 records.
189 * @param msg: message to examine.
190 * @return true if DNSSEC information was found.
192 int iter_msg_has_dnssec(struct dns_msg* msg);
195 * See if a message is known to be from a certain zone.
196 * This looks for SOA or NS rrsets, for answers.
197 * For referrals, when one label is delegated, the zone is detected.
198 * Does not look at signatures.
199 * @param msg: the message to inspect.
200 * @param dp: delegation point with zone name to look for.
201 * @param type: type of message.
202 * @param dclass: class of query.
203 * @return true if message is certain to be from zone in dp->name.
204 * false if not sure (empty msg), or not from the zone.
206 int iter_msg_from_zone(struct dns_msg* msg, struct delegpt* dp,
207 enum response_type type, uint16_t dclass);
210 * Check if two replies are equal
211 * For fallback procedures
212 * @param p: reply one. The reply has rrset data pointers in region.
213 * Does not check rrset-IDs
214 * @param q: reply two
215 * @param buf: scratch buffer.
216 * @return if one and two are equal.
218 int reply_equal(struct reply_info* p, struct reply_info* q, ldns_buffer* buf);
221 * Store parent-side rrset in seperate rrset cache entries for later
222 * last-resort * lookups in case the child-side versions of this information
224 * @param env: environment with cache, time, ...
225 * @param rrset: the rrset to store (copied).
226 * Failure to store is logged, but otherwise ignored.
228 void iter_store_parentside_rrset(struct module_env* env,
229 struct ub_packed_rrset_key* rrset);
232 * Store parent-side NS records from a referral message
233 * @param env: environment with cache, time, ...
234 * @param rep: response with NS rrset.
235 * Failure to store is logged, but otherwise ignored.
237 void iter_store_parentside_NS(struct module_env* env, struct reply_info* rep);
240 * Store parent-side negative element, the parentside rrset does not exist,
241 * creates an rrset with empty rdata in the rrset cache with PARENTSIDE flag.
242 * @param env: environment with cache, time, ...
243 * @param qinfo: the identity of the rrset that is missing.
244 * @param rep: delegation response or answer response, to glean TTL from.
245 * (malloc) failure is logged but otherwise ignored.
247 void iter_store_parentside_neg(struct module_env* env,
248 struct query_info* qinfo, struct reply_info* rep);
251 * Add parent NS record if that exists in the cache. This is both new
252 * information and acts like a timeout throttle on retries.
253 * @param env: query env with rrset cache and time.
254 * @param dp: delegation point to store result in. Also this dp is used to
255 * see which NS name is needed.
256 * @param region: region to alloc result in.
257 * @param qinfo: pertinent information, the qclass.
258 * @return false on malloc failure.
259 * if true, the routine worked and if such cached information
260 * existed dp->has_parent_side_NS is set true.
262 int iter_lookup_parent_NS_from_cache(struct module_env* env,
263 struct delegpt* dp, struct regional* region, struct query_info* qinfo);
266 * Add parent-side glue if that exists in the cache. This is both new
267 * information and acts like a timeout throttle on retries to fetch them.
268 * @param env: query env with rrset cache and time.
269 * @param dp: delegation point to store result in. Also this dp is used to
270 * see which NS name is needed.
271 * @param region: region to alloc result in.
272 * @param qinfo: pertinent information, the qclass.
273 * @return: true, it worked, no malloc failures, and new addresses (lame)
274 * have been added, giving extra options as query targets.
276 int iter_lookup_parent_glue_from_cache(struct module_env* env,
277 struct delegpt* dp, struct regional* region, struct query_info* qinfo);
280 * Lookup next root-hint or root-forward entry.
281 * @param hints: the hints.
282 * @param fwd: the forwards.
283 * @param c: the class to start searching at. 0 means find first one.
284 * @return false if no classes found, true if found and returned in c.
286 int iter_get_next_root(struct iter_hints* hints, struct iter_forwards* fwd,
290 * Remove DS records that are inappropriate before they are cached.
291 * @param msg: the response to scrub.
292 * @param ns: RRSET that is the NS record for the referral.
293 * if NULL, then all DS records are removed from the authority section.
294 * @param z: zone name that the response is from.
296 void iter_scrub_ds(struct dns_msg* msg, struct ub_packed_rrset_key* ns,
300 * Remove query attempts from all available ips. For 0x20.
301 * @param dp: delegpt.
302 * @param d: decrease.
304 void iter_dec_attempts(struct delegpt* dp, int d);
307 * Add retry counts from older delegpt to newer delegpt.
308 * Does not waste time on timeout'd (or other failing) addresses.
309 * @param dp: new delegationpoint.
310 * @param old: old delegationpoint.
312 void iter_merge_retry_counts(struct delegpt* dp, struct delegpt* old);
315 * See if a DS response (type ANSWER) is too low: a nodata answer with
316 * a SOA record in the authority section at-or-below the qchase.qname.
317 * Also returns true if we are not sure (i.e. empty message, CNAME nosig).
318 * @param msg: the response.
319 * @param dp: the dp name is used to check if the RRSIG gives a clue that
320 * it was originated from the correct nameserver.
321 * @return true if too low.
323 int iter_ds_toolow(struct dns_msg* msg, struct delegpt* dp);
326 * See if delegpt can go down a step to the qname or not
327 * @param qinfo: the query name looked up.
328 * @param dp: checked if the name can go lower to the qname
329 * @return true if can go down, false if that would not be possible.
330 * the current response seems to be the one and only, best possible, response.
332 int iter_dp_cangodown(struct query_info* qinfo, struct delegpt* dp);
334 #endif /* ITERATOR_ITER_UTILS_H */