xref: /freebsd/contrib/unbound/iterator/iterator.c (revision 325151a3)
1 /*
2  * iterator/iterator.c - iterative resolver DNS query response module
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 a module that performs recusive iterative DNS query
40  * processing.
41  */
42 
43 #include "config.h"
44 #include "iterator/iterator.h"
45 #include "iterator/iter_utils.h"
46 #include "iterator/iter_hints.h"
47 #include "iterator/iter_fwd.h"
48 #include "iterator/iter_donotq.h"
49 #include "iterator/iter_delegpt.h"
50 #include "iterator/iter_resptype.h"
51 #include "iterator/iter_scrub.h"
52 #include "iterator/iter_priv.h"
53 #include "validator/val_neg.h"
54 #include "services/cache/dns.h"
55 #include "services/cache/infra.h"
56 #include "util/module.h"
57 #include "util/netevent.h"
58 #include "util/net_help.h"
59 #include "util/regional.h"
60 #include "util/data/dname.h"
61 #include "util/data/msgencode.h"
62 #include "util/fptr_wlist.h"
63 #include "util/config_file.h"
64 #include "util/random.h"
65 #include "sldns/rrdef.h"
66 #include "sldns/wire2str.h"
67 #include "sldns/parseutil.h"
68 #include "sldns/sbuffer.h"
69 
70 int
71 iter_init(struct module_env* env, int id)
72 {
73 	struct iter_env* iter_env = (struct iter_env*)calloc(1,
74 		sizeof(struct iter_env));
75 	if(!iter_env) {
76 		log_err("malloc failure");
77 		return 0;
78 	}
79 	env->modinfo[id] = (void*)iter_env;
80 	if(!iter_apply_cfg(iter_env, env->cfg)) {
81 		log_err("iterator: could not apply configuration settings.");
82 		return 0;
83 	}
84 	return 1;
85 }
86 
87 /** delete caps_whitelist element */
88 static void
89 caps_free(struct rbnode_t* n, void* ATTR_UNUSED(d))
90 {
91 	if(n) {
92 		free(((struct name_tree_node*)n)->name);
93 		free(n);
94 	}
95 }
96 
97 void
98 iter_deinit(struct module_env* env, int id)
99 {
100 	struct iter_env* iter_env;
101 	if(!env || !env->modinfo[id])
102 		return;
103 	iter_env = (struct iter_env*)env->modinfo[id];
104 	free(iter_env->target_fetch_policy);
105 	priv_delete(iter_env->priv);
106 	donotq_delete(iter_env->donotq);
107 	if(iter_env->caps_white) {
108 		traverse_postorder(iter_env->caps_white, caps_free, NULL);
109 		free(iter_env->caps_white);
110 	}
111 	free(iter_env);
112 	env->modinfo[id] = NULL;
113 }
114 
115 /** new query for iterator */
116 static int
117 iter_new(struct module_qstate* qstate, int id)
118 {
119 	struct iter_qstate* iq = (struct iter_qstate*)regional_alloc(
120 		qstate->region, sizeof(struct iter_qstate));
121 	qstate->minfo[id] = iq;
122 	if(!iq)
123 		return 0;
124 	memset(iq, 0, sizeof(*iq));
125 	iq->state = INIT_REQUEST_STATE;
126 	iq->final_state = FINISHED_STATE;
127 	iq->an_prepend_list = NULL;
128 	iq->an_prepend_last = NULL;
129 	iq->ns_prepend_list = NULL;
130 	iq->ns_prepend_last = NULL;
131 	iq->dp = NULL;
132 	iq->depth = 0;
133 	iq->num_target_queries = 0;
134 	iq->num_current_queries = 0;
135 	iq->query_restart_count = 0;
136 	iq->referral_count = 0;
137 	iq->sent_count = 0;
138 	iq->ratelimit_ok = 0;
139 	iq->target_count = NULL;
140 	iq->wait_priming_stub = 0;
141 	iq->refetch_glue = 0;
142 	iq->dnssec_expected = 0;
143 	iq->dnssec_lame_query = 0;
144 	iq->chase_flags = qstate->query_flags;
145 	/* Start with the (current) qname. */
146 	iq->qchase = qstate->qinfo;
147 	outbound_list_init(&iq->outlist);
148 	return 1;
149 }
150 
151 /**
152  * Transition to the next state. This can be used to advance a currently
153  * processing event. It cannot be used to reactivate a forEvent.
154  *
155  * @param iq: iterator query state
156  * @param nextstate The state to transition to.
157  * @return true. This is so this can be called as the return value for the
158  *         actual process*State() methods. (Transitioning to the next state
159  *         implies further processing).
160  */
161 static int
162 next_state(struct iter_qstate* iq, enum iter_state nextstate)
163 {
164 	/* If transitioning to a "response" state, make sure that there is a
165 	 * response */
166 	if(iter_state_is_responsestate(nextstate)) {
167 		if(iq->response == NULL) {
168 			log_err("transitioning to response state sans "
169 				"response.");
170 		}
171 	}
172 	iq->state = nextstate;
173 	return 1;
174 }
175 
176 /**
177  * Transition an event to its final state. Final states always either return
178  * a result up the module chain, or reactivate a dependent event. Which
179  * final state to transtion to is set in the module state for the event when
180  * it was created, and depends on the original purpose of the event.
181  *
182  * The response is stored in the qstate->buf buffer.
183  *
184  * @param iq: iterator query state
185  * @return false. This is so this method can be used as the return value for
186  *         the processState methods. (Transitioning to the final state
187  */
188 static int
189 final_state(struct iter_qstate* iq)
190 {
191 	return next_state(iq, iq->final_state);
192 }
193 
194 /**
195  * Callback routine to handle errors in parent query states
196  * @param qstate: query state that failed.
197  * @param id: module id.
198  * @param super: super state.
199  */
200 static void
201 error_supers(struct module_qstate* qstate, int id, struct module_qstate* super)
202 {
203 	struct iter_qstate* super_iq = (struct iter_qstate*)super->minfo[id];
204 
205 	if(qstate->qinfo.qtype == LDNS_RR_TYPE_A ||
206 		qstate->qinfo.qtype == LDNS_RR_TYPE_AAAA) {
207 		/* mark address as failed. */
208 		struct delegpt_ns* dpns = NULL;
209 		if(super_iq->dp)
210 			dpns = delegpt_find_ns(super_iq->dp,
211 				qstate->qinfo.qname, qstate->qinfo.qname_len);
212 		if(!dpns) {
213 			/* not interested */
214 			verbose(VERB_ALGO, "subq error, but not interested");
215 			log_query_info(VERB_ALGO, "superq", &super->qinfo);
216 			if(super_iq->dp)
217 				delegpt_log(VERB_ALGO, super_iq->dp);
218 			log_assert(0);
219 			return;
220 		} else {
221 			/* see if the failure did get (parent-lame) info */
222 			if(!cache_fill_missing(super->env,
223 				super_iq->qchase.qclass, super->region,
224 				super_iq->dp))
225 				log_err("out of memory adding missing");
226 		}
227 		dpns->resolved = 1; /* mark as failed */
228 		super_iq->num_target_queries--;
229 	}
230 	if(qstate->qinfo.qtype == LDNS_RR_TYPE_NS) {
231 		/* prime failed to get delegation */
232 		super_iq->dp = NULL;
233 	}
234 	/* evaluate targets again */
235 	super_iq->state = QUERYTARGETS_STATE;
236 	/* super becomes runnable, and will process this change */
237 }
238 
239 /**
240  * Return an error to the client
241  * @param qstate: our query state
242  * @param id: module id
243  * @param rcode: error code (DNS errcode).
244  * @return: 0 for use by caller, to make notation easy, like:
245  * 	return error_response(..).
246  */
247 static int
248 error_response(struct module_qstate* qstate, int id, int rcode)
249 {
250 	verbose(VERB_QUERY, "return error response %s",
251 		sldns_lookup_by_id(sldns_rcodes, rcode)?
252 		sldns_lookup_by_id(sldns_rcodes, rcode)->name:"??");
253 	qstate->return_rcode = rcode;
254 	qstate->return_msg = NULL;
255 	qstate->ext_state[id] = module_finished;
256 	return 0;
257 }
258 
259 /**
260  * Return an error to the client and cache the error code in the
261  * message cache (so per qname, qtype, qclass).
262  * @param qstate: our query state
263  * @param id: module id
264  * @param rcode: error code (DNS errcode).
265  * @return: 0 for use by caller, to make notation easy, like:
266  * 	return error_response(..).
267  */
268 static int
269 error_response_cache(struct module_qstate* qstate, int id, int rcode)
270 {
271 	/* store in cache */
272 	struct reply_info err;
273 	if(qstate->prefetch_leeway > NORR_TTL) {
274 		verbose(VERB_ALGO, "error response for prefetch in cache");
275 		/* attempt to adjust the cache entry prefetch */
276 		if(dns_cache_prefetch_adjust(qstate->env, &qstate->qinfo,
277 			NORR_TTL, qstate->query_flags))
278 			return error_response(qstate, id, rcode);
279 		/* if that fails (not in cache), fall through to store err */
280 	}
281 	memset(&err, 0, sizeof(err));
282 	err.flags = (uint16_t)(BIT_QR | BIT_RA);
283 	FLAGS_SET_RCODE(err.flags, rcode);
284 	err.qdcount = 1;
285 	err.ttl = NORR_TTL;
286 	err.prefetch_ttl = PREFETCH_TTL_CALC(err.ttl);
287 	/* do not waste time trying to validate this servfail */
288 	err.security = sec_status_indeterminate;
289 	verbose(VERB_ALGO, "store error response in message cache");
290 	iter_dns_store(qstate->env, &qstate->qinfo, &err, 0, 0, 0, NULL,
291 		qstate->query_flags);
292 	return error_response(qstate, id, rcode);
293 }
294 
295 /** check if prepend item is duplicate item */
296 static int
297 prepend_is_duplicate(struct ub_packed_rrset_key** sets, size_t to,
298 	struct ub_packed_rrset_key* dup)
299 {
300 	size_t i;
301 	for(i=0; i<to; i++) {
302 		if(sets[i]->rk.type == dup->rk.type &&
303 			sets[i]->rk.rrset_class == dup->rk.rrset_class &&
304 			sets[i]->rk.dname_len == dup->rk.dname_len &&
305 			query_dname_compare(sets[i]->rk.dname, dup->rk.dname)
306 			== 0)
307 			return 1;
308 	}
309 	return 0;
310 }
311 
312 /** prepend the prepend list in the answer and authority section of dns_msg */
313 static int
314 iter_prepend(struct iter_qstate* iq, struct dns_msg* msg,
315 	struct regional* region)
316 {
317 	struct iter_prep_list* p;
318 	struct ub_packed_rrset_key** sets;
319 	size_t num_an = 0, num_ns = 0;;
320 	for(p = iq->an_prepend_list; p; p = p->next)
321 		num_an++;
322 	for(p = iq->ns_prepend_list; p; p = p->next)
323 		num_ns++;
324 	if(num_an + num_ns == 0)
325 		return 1;
326 	verbose(VERB_ALGO, "prepending %d rrsets", (int)num_an + (int)num_ns);
327 	if(num_an > RR_COUNT_MAX || num_ns > RR_COUNT_MAX ||
328 		msg->rep->rrset_count > RR_COUNT_MAX) return 0; /* overflow */
329 	sets = regional_alloc(region, (num_an+num_ns+msg->rep->rrset_count) *
330 		sizeof(struct ub_packed_rrset_key*));
331 	if(!sets)
332 		return 0;
333 	/* ANSWER section */
334 	num_an = 0;
335 	for(p = iq->an_prepend_list; p; p = p->next) {
336 		sets[num_an++] = p->rrset;
337 	}
338 	memcpy(sets+num_an, msg->rep->rrsets, msg->rep->an_numrrsets *
339 		sizeof(struct ub_packed_rrset_key*));
340 	/* AUTH section */
341 	num_ns = 0;
342 	for(p = iq->ns_prepend_list; p; p = p->next) {
343 		if(prepend_is_duplicate(sets+msg->rep->an_numrrsets+num_an,
344 			num_ns, p->rrset) || prepend_is_duplicate(
345 			msg->rep->rrsets+msg->rep->an_numrrsets,
346 			msg->rep->ns_numrrsets, p->rrset))
347 			continue;
348 		sets[msg->rep->an_numrrsets + num_an + num_ns++] = p->rrset;
349 	}
350 	memcpy(sets + num_an + msg->rep->an_numrrsets + num_ns,
351 		msg->rep->rrsets + msg->rep->an_numrrsets,
352 		(msg->rep->ns_numrrsets + msg->rep->ar_numrrsets) *
353 		sizeof(struct ub_packed_rrset_key*));
354 
355 	/* NXDOMAIN rcode can stay if we prepended DNAME/CNAMEs, because
356 	 * this is what recursors should give. */
357 	msg->rep->rrset_count += num_an + num_ns;
358 	msg->rep->an_numrrsets += num_an;
359 	msg->rep->ns_numrrsets += num_ns;
360 	msg->rep->rrsets = sets;
361 	return 1;
362 }
363 
364 /**
365  * Add rrset to ANSWER prepend list
366  * @param qstate: query state.
367  * @param iq: iterator query state.
368  * @param rrset: rrset to add.
369  * @return false on failure (malloc).
370  */
371 static int
372 iter_add_prepend_answer(struct module_qstate* qstate, struct iter_qstate* iq,
373 	struct ub_packed_rrset_key* rrset)
374 {
375 	struct iter_prep_list* p = (struct iter_prep_list*)regional_alloc(
376 		qstate->region, sizeof(struct iter_prep_list));
377 	if(!p)
378 		return 0;
379 	p->rrset = rrset;
380 	p->next = NULL;
381 	/* add at end */
382 	if(iq->an_prepend_last)
383 		iq->an_prepend_last->next = p;
384 	else	iq->an_prepend_list = p;
385 	iq->an_prepend_last = p;
386 	return 1;
387 }
388 
389 /**
390  * Add rrset to AUTHORITY prepend list
391  * @param qstate: query state.
392  * @param iq: iterator query state.
393  * @param rrset: rrset to add.
394  * @return false on failure (malloc).
395  */
396 static int
397 iter_add_prepend_auth(struct module_qstate* qstate, struct iter_qstate* iq,
398 	struct ub_packed_rrset_key* rrset)
399 {
400 	struct iter_prep_list* p = (struct iter_prep_list*)regional_alloc(
401 		qstate->region, sizeof(struct iter_prep_list));
402 	if(!p)
403 		return 0;
404 	p->rrset = rrset;
405 	p->next = NULL;
406 	/* add at end */
407 	if(iq->ns_prepend_last)
408 		iq->ns_prepend_last->next = p;
409 	else	iq->ns_prepend_list = p;
410 	iq->ns_prepend_last = p;
411 	return 1;
412 }
413 
414 /**
415  * Given a CNAME response (defined as a response containing a CNAME or DNAME
416  * that does not answer the request), process the response, modifying the
417  * state as necessary. This follows the CNAME/DNAME chain and returns the
418  * final query name.
419  *
420  * sets the new query name, after following the CNAME/DNAME chain.
421  * @param qstate: query state.
422  * @param iq: iterator query state.
423  * @param msg: the response.
424  * @param mname: returned target new query name.
425  * @param mname_len: length of mname.
426  * @return false on (malloc) error.
427  */
428 static int
429 handle_cname_response(struct module_qstate* qstate, struct iter_qstate* iq,
430         struct dns_msg* msg, uint8_t** mname, size_t* mname_len)
431 {
432 	size_t i;
433 	/* Start with the (current) qname. */
434 	*mname = iq->qchase.qname;
435 	*mname_len = iq->qchase.qname_len;
436 
437 	/* Iterate over the ANSWER rrsets in order, looking for CNAMEs and
438 	 * DNAMES. */
439 	for(i=0; i<msg->rep->an_numrrsets; i++) {
440 		struct ub_packed_rrset_key* r = msg->rep->rrsets[i];
441 		/* If there is a (relevant) DNAME, add it to the list.
442 		 * We always expect there to be CNAME that was generated
443 		 * by this DNAME following, so we don't process the DNAME
444 		 * directly.  */
445 		if(ntohs(r->rk.type) == LDNS_RR_TYPE_DNAME &&
446 			dname_strict_subdomain_c(*mname, r->rk.dname)) {
447 			if(!iter_add_prepend_answer(qstate, iq, r))
448 				return 0;
449 			continue;
450 		}
451 
452 		if(ntohs(r->rk.type) == LDNS_RR_TYPE_CNAME &&
453 			query_dname_compare(*mname, r->rk.dname) == 0) {
454 			/* Add this relevant CNAME rrset to the prepend list.*/
455 			if(!iter_add_prepend_answer(qstate, iq, r))
456 				return 0;
457 			get_cname_target(r, mname, mname_len);
458 		}
459 
460 		/* Other rrsets in the section are ignored. */
461 	}
462 	/* add authority rrsets to authority prepend, for wildcarded CNAMEs */
463 	for(i=msg->rep->an_numrrsets; i<msg->rep->an_numrrsets +
464 		msg->rep->ns_numrrsets; i++) {
465 		struct ub_packed_rrset_key* r = msg->rep->rrsets[i];
466 		/* only add NSEC/NSEC3, as they may be needed for validation */
467 		if(ntohs(r->rk.type) == LDNS_RR_TYPE_NSEC ||
468 			ntohs(r->rk.type) == LDNS_RR_TYPE_NSEC3) {
469 			if(!iter_add_prepend_auth(qstate, iq, r))
470 				return 0;
471 		}
472 	}
473 	return 1;
474 }
475 
476 /** see if target name is caps-for-id whitelisted */
477 static int
478 is_caps_whitelisted(struct iter_env* ie, struct iter_qstate* iq)
479 {
480 	if(!ie->caps_white) return 0; /* no whitelist, or no capsforid */
481 	return name_tree_lookup(ie->caps_white, iq->qchase.qname,
482 		iq->qchase.qname_len, dname_count_labels(iq->qchase.qname),
483 		iq->qchase.qclass) != NULL;
484 }
485 
486 /** create target count structure for this query */
487 static void
488 target_count_create(struct iter_qstate* iq)
489 {
490 	if(!iq->target_count) {
491 		iq->target_count = (int*)calloc(2, sizeof(int));
492 		/* if calloc fails we simply do not track this number */
493 		if(iq->target_count)
494 			iq->target_count[0] = 1;
495 	}
496 }
497 
498 static void
499 target_count_increase(struct iter_qstate* iq, int num)
500 {
501 	target_count_create(iq);
502 	if(iq->target_count)
503 		iq->target_count[1] += num;
504 }
505 
506 /**
507  * Generate a subrequest.
508  * Generate a local request event. Local events are tied to this module, and
509  * have a correponding (first tier) event that is waiting for this event to
510  * resolve to continue.
511  *
512  * @param qname The query name for this request.
513  * @param qnamelen length of qname
514  * @param qtype The query type for this request.
515  * @param qclass The query class for this request.
516  * @param qstate The event that is generating this event.
517  * @param id: module id.
518  * @param iq: The iterator state that is generating this event.
519  * @param initial_state The initial response state (normally this
520  *          is QUERY_RESP_STATE, unless it is known that the request won't
521  *          need iterative processing
522  * @param finalstate The final state for the response to this request.
523  * @param subq_ret: if newly allocated, the subquerystate, or NULL if it does
524  * 	not need initialisation.
525  * @param v: if true, validation is done on the subquery.
526  * @return false on error (malloc).
527  */
528 static int
529 generate_sub_request(uint8_t* qname, size_t qnamelen, uint16_t qtype,
530 	uint16_t qclass, struct module_qstate* qstate, int id,
531 	struct iter_qstate* iq, enum iter_state initial_state,
532 	enum iter_state finalstate, struct module_qstate** subq_ret, int v)
533 {
534 	struct module_qstate* subq = NULL;
535 	struct iter_qstate* subiq = NULL;
536 	uint16_t qflags = 0; /* OPCODE QUERY, no flags */
537 	struct query_info qinf;
538 	int prime = (finalstate == PRIME_RESP_STATE)?1:0;
539 	int valrec = 0;
540 	qinf.qname = qname;
541 	qinf.qname_len = qnamelen;
542 	qinf.qtype = qtype;
543 	qinf.qclass = qclass;
544 
545 	/* RD should be set only when sending the query back through the INIT
546 	 * state. */
547 	if(initial_state == INIT_REQUEST_STATE)
548 		qflags |= BIT_RD;
549 	/* We set the CD flag so we can send this through the "head" of
550 	 * the resolution chain, which might have a validator. We are
551 	 * uninterested in validating things not on the direct resolution
552 	 * path.  */
553 	if(!v) {
554 		qflags |= BIT_CD;
555 		valrec = 1;
556 	}
557 
558 	/* attach subquery, lookup existing or make a new one */
559 	fptr_ok(fptr_whitelist_modenv_attach_sub(qstate->env->attach_sub));
560 	if(!(*qstate->env->attach_sub)(qstate, &qinf, qflags, prime, valrec,
561 		&subq)) {
562 		return 0;
563 	}
564 	*subq_ret = subq;
565 	if(subq) {
566 		/* initialise the new subquery */
567 		subq->curmod = id;
568 		subq->ext_state[id] = module_state_initial;
569 		subq->minfo[id] = regional_alloc(subq->region,
570 			sizeof(struct iter_qstate));
571 		if(!subq->minfo[id]) {
572 			log_err("init subq: out of memory");
573 			fptr_ok(fptr_whitelist_modenv_kill_sub(
574 				qstate->env->kill_sub));
575 			(*qstate->env->kill_sub)(subq);
576 			return 0;
577 		}
578 		subiq = (struct iter_qstate*)subq->minfo[id];
579 		memset(subiq, 0, sizeof(*subiq));
580 		subiq->num_target_queries = 0;
581 		target_count_create(iq);
582 		subiq->target_count = iq->target_count;
583 		if(iq->target_count)
584 			iq->target_count[0] ++; /* extra reference */
585 		subiq->num_current_queries = 0;
586 		subiq->depth = iq->depth+1;
587 		outbound_list_init(&subiq->outlist);
588 		subiq->state = initial_state;
589 		subiq->final_state = finalstate;
590 		subiq->qchase = subq->qinfo;
591 		subiq->chase_flags = subq->query_flags;
592 		subiq->refetch_glue = 0;
593 	}
594 	return 1;
595 }
596 
597 /**
598  * Generate and send a root priming request.
599  * @param qstate: the qtstate that triggered the need to prime.
600  * @param iq: iterator query state.
601  * @param id: module id.
602  * @param qclass: the class to prime.
603  * @return 0 on failure
604  */
605 static int
606 prime_root(struct module_qstate* qstate, struct iter_qstate* iq, int id,
607 	uint16_t qclass)
608 {
609 	struct delegpt* dp;
610 	struct module_qstate* subq;
611 	verbose(VERB_DETAIL, "priming . %s NS",
612 		sldns_lookup_by_id(sldns_rr_classes, (int)qclass)?
613 		sldns_lookup_by_id(sldns_rr_classes, (int)qclass)->name:"??");
614 	dp = hints_lookup_root(qstate->env->hints, qclass);
615 	if(!dp) {
616 		verbose(VERB_ALGO, "Cannot prime due to lack of hints");
617 		return 0;
618 	}
619 	/* Priming requests start at the QUERYTARGETS state, skipping
620 	 * the normal INIT state logic (which would cause an infloop). */
621 	if(!generate_sub_request((uint8_t*)"\000", 1, LDNS_RR_TYPE_NS,
622 		qclass, qstate, id, iq, QUERYTARGETS_STATE, PRIME_RESP_STATE,
623 		&subq, 0)) {
624 		verbose(VERB_ALGO, "could not prime root");
625 		return 0;
626 	}
627 	if(subq) {
628 		struct iter_qstate* subiq =
629 			(struct iter_qstate*)subq->minfo[id];
630 		/* Set the initial delegation point to the hint.
631 		 * copy dp, it is now part of the root prime query.
632 		 * dp was part of in the fixed hints structure. */
633 		subiq->dp = delegpt_copy(dp, subq->region);
634 		if(!subiq->dp) {
635 			log_err("out of memory priming root, copydp");
636 			fptr_ok(fptr_whitelist_modenv_kill_sub(
637 				qstate->env->kill_sub));
638 			(*qstate->env->kill_sub)(subq);
639 			return 0;
640 		}
641 		/* there should not be any target queries. */
642 		subiq->num_target_queries = 0;
643 		subiq->dnssec_expected = iter_indicates_dnssec(
644 			qstate->env, subiq->dp, NULL, subq->qinfo.qclass);
645 	}
646 
647 	/* this module stops, our submodule starts, and does the query. */
648 	qstate->ext_state[id] = module_wait_subquery;
649 	return 1;
650 }
651 
652 /**
653  * Generate and process a stub priming request. This method tests for the
654  * need to prime a stub zone, so it is safe to call for every request.
655  *
656  * @param qstate: the qtstate that triggered the need to prime.
657  * @param iq: iterator query state.
658  * @param id: module id.
659  * @param qname: request name.
660  * @param qclass: request class.
661  * @return true if a priming subrequest was made, false if not. The will only
662  *         issue a priming request if it detects an unprimed stub.
663  *         Uses value of 2 to signal during stub-prime in root-prime situation
664  *         that a noprime-stub is available and resolution can continue.
665  */
666 static int
667 prime_stub(struct module_qstate* qstate, struct iter_qstate* iq, int id,
668 	uint8_t* qname, uint16_t qclass)
669 {
670 	/* Lookup the stub hint. This will return null if the stub doesn't
671 	 * need to be re-primed. */
672 	struct iter_hints_stub* stub;
673 	struct delegpt* stub_dp;
674 	struct module_qstate* subq;
675 
676 	if(!qname) return 0;
677 	stub = hints_lookup_stub(qstate->env->hints, qname, qclass, iq->dp);
678 	/* The stub (if there is one) does not need priming. */
679 	if(!stub)
680 		return 0;
681 	stub_dp = stub->dp;
682 
683 	/* is it a noprime stub (always use) */
684 	if(stub->noprime) {
685 		int r = 0;
686 		if(iq->dp == NULL) r = 2;
687 		/* copy the dp out of the fixed hints structure, so that
688 		 * it can be changed when servicing this query */
689 		iq->dp = delegpt_copy(stub_dp, qstate->region);
690 		if(!iq->dp) {
691 			log_err("out of memory priming stub");
692 			(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
693 			return 1; /* return 1 to make module stop, with error */
694 		}
695 		log_nametypeclass(VERB_DETAIL, "use stub", stub_dp->name,
696 			LDNS_RR_TYPE_NS, qclass);
697 		return r;
698 	}
699 
700 	/* Otherwise, we need to (re)prime the stub. */
701 	log_nametypeclass(VERB_DETAIL, "priming stub", stub_dp->name,
702 		LDNS_RR_TYPE_NS, qclass);
703 
704 	/* Stub priming events start at the QUERYTARGETS state to avoid the
705 	 * redundant INIT state processing. */
706 	if(!generate_sub_request(stub_dp->name, stub_dp->namelen,
707 		LDNS_RR_TYPE_NS, qclass, qstate, id, iq,
708 		QUERYTARGETS_STATE, PRIME_RESP_STATE, &subq, 0)) {
709 		verbose(VERB_ALGO, "could not prime stub");
710 		(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
711 		return 1; /* return 1 to make module stop, with error */
712 	}
713 	if(subq) {
714 		struct iter_qstate* subiq =
715 			(struct iter_qstate*)subq->minfo[id];
716 
717 		/* Set the initial delegation point to the hint. */
718 		/* make copy to avoid use of stub dp by different qs/threads */
719 		subiq->dp = delegpt_copy(stub_dp, subq->region);
720 		if(!subiq->dp) {
721 			log_err("out of memory priming stub, copydp");
722 			fptr_ok(fptr_whitelist_modenv_kill_sub(
723 				qstate->env->kill_sub));
724 			(*qstate->env->kill_sub)(subq);
725 			(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
726 			return 1; /* return 1 to make module stop, with error */
727 		}
728 		/* there should not be any target queries -- although there
729 		 * wouldn't be anyway, since stub hints never have
730 		 * missing targets. */
731 		subiq->num_target_queries = 0;
732 		subiq->wait_priming_stub = 1;
733 		subiq->dnssec_expected = iter_indicates_dnssec(
734 			qstate->env, subiq->dp, NULL, subq->qinfo.qclass);
735 	}
736 
737 	/* this module stops, our submodule starts, and does the query. */
738 	qstate->ext_state[id] = module_wait_subquery;
739 	return 1;
740 }
741 
742 /**
743  * Generate A and AAAA checks for glue that is in-zone for the referral
744  * we just got to obtain authoritative information on the adresses.
745  *
746  * @param qstate: the qtstate that triggered the need to prime.
747  * @param iq: iterator query state.
748  * @param id: module id.
749  */
750 static void
751 generate_a_aaaa_check(struct module_qstate* qstate, struct iter_qstate* iq,
752 	int id)
753 {
754 	struct iter_env* ie = (struct iter_env*)qstate->env->modinfo[id];
755 	struct module_qstate* subq;
756 	size_t i;
757 	struct reply_info* rep = iq->response->rep;
758 	struct ub_packed_rrset_key* s;
759 	log_assert(iq->dp);
760 
761 	if(iq->depth == ie->max_dependency_depth)
762 		return;
763 	/* walk through additional, and check if in-zone,
764 	 * only relevant A, AAAA are left after scrub anyway */
765 	for(i=rep->an_numrrsets+rep->ns_numrrsets; i<rep->rrset_count; i++) {
766 		s = rep->rrsets[i];
767 		/* check *ALL* addresses that are transmitted in additional*/
768 		/* is it an address ? */
769 		if( !(ntohs(s->rk.type)==LDNS_RR_TYPE_A ||
770 			ntohs(s->rk.type)==LDNS_RR_TYPE_AAAA)) {
771 			continue;
772 		}
773 		/* is this query the same as the A/AAAA check for it */
774 		if(qstate->qinfo.qtype == ntohs(s->rk.type) &&
775 			qstate->qinfo.qclass == ntohs(s->rk.rrset_class) &&
776 			query_dname_compare(qstate->qinfo.qname,
777 				s->rk.dname)==0 &&
778 			(qstate->query_flags&BIT_RD) &&
779 			!(qstate->query_flags&BIT_CD))
780 			continue;
781 
782 		/* generate subrequest for it */
783 		log_nametypeclass(VERB_ALGO, "schedule addr fetch",
784 			s->rk.dname, ntohs(s->rk.type),
785 			ntohs(s->rk.rrset_class));
786 		if(!generate_sub_request(s->rk.dname, s->rk.dname_len,
787 			ntohs(s->rk.type), ntohs(s->rk.rrset_class),
788 			qstate, id, iq,
789 			INIT_REQUEST_STATE, FINISHED_STATE, &subq, 1)) {
790 			verbose(VERB_ALGO, "could not generate addr check");
791 			return;
792 		}
793 		/* ignore subq - not need for more init */
794 	}
795 }
796 
797 /**
798  * Generate a NS check request to obtain authoritative information
799  * on an NS rrset.
800  *
801  * @param qstate: the qtstate that triggered the need to prime.
802  * @param iq: iterator query state.
803  * @param id: module id.
804  */
805 static void
806 generate_ns_check(struct module_qstate* qstate, struct iter_qstate* iq, int id)
807 {
808 	struct iter_env* ie = (struct iter_env*)qstate->env->modinfo[id];
809 	struct module_qstate* subq;
810 	log_assert(iq->dp);
811 
812 	if(iq->depth == ie->max_dependency_depth)
813 		return;
814 	/* is this query the same as the nscheck? */
815 	if(qstate->qinfo.qtype == LDNS_RR_TYPE_NS &&
816 		query_dname_compare(iq->dp->name, qstate->qinfo.qname)==0 &&
817 		(qstate->query_flags&BIT_RD) && !(qstate->query_flags&BIT_CD)){
818 		/* spawn off A, AAAA queries for in-zone glue to check */
819 		generate_a_aaaa_check(qstate, iq, id);
820 		return;
821 	}
822 
823 	log_nametypeclass(VERB_ALGO, "schedule ns fetch",
824 		iq->dp->name, LDNS_RR_TYPE_NS, iq->qchase.qclass);
825 	if(!generate_sub_request(iq->dp->name, iq->dp->namelen,
826 		LDNS_RR_TYPE_NS, iq->qchase.qclass, qstate, id, iq,
827 		INIT_REQUEST_STATE, FINISHED_STATE, &subq, 1)) {
828 		verbose(VERB_ALGO, "could not generate ns check");
829 		return;
830 	}
831 	if(subq) {
832 		struct iter_qstate* subiq =
833 			(struct iter_qstate*)subq->minfo[id];
834 
835 		/* make copy to avoid use of stub dp by different qs/threads */
836 		/* refetch glue to start higher up the tree */
837 		subiq->refetch_glue = 1;
838 		subiq->dp = delegpt_copy(iq->dp, subq->region);
839 		if(!subiq->dp) {
840 			log_err("out of memory generating ns check, copydp");
841 			fptr_ok(fptr_whitelist_modenv_kill_sub(
842 				qstate->env->kill_sub));
843 			(*qstate->env->kill_sub)(subq);
844 			return;
845 		}
846 	}
847 }
848 
849 /**
850  * Generate a DNSKEY prefetch query to get the DNSKEY for the DS record we
851  * just got in a referral (where we have dnssec_expected, thus have trust
852  * anchors above it).  Note that right after calling this routine the
853  * iterator detached subqueries (because of following the referral), and thus
854  * the DNSKEY query becomes detached, its return stored in the cache for
855  * later lookup by the validator.  This cache lookup by the validator avoids
856  * the roundtrip incurred by the DNSKEY query.  The DNSKEY query is now
857  * performed at about the same time the original query is sent to the domain,
858  * thus the two answers are likely to be returned at about the same time,
859  * saving a roundtrip from the validated lookup.
860  *
861  * @param qstate: the qtstate that triggered the need to prime.
862  * @param iq: iterator query state.
863  * @param id: module id.
864  */
865 static void
866 generate_dnskey_prefetch(struct module_qstate* qstate,
867 	struct iter_qstate* iq, int id)
868 {
869 	struct module_qstate* subq;
870 	log_assert(iq->dp);
871 
872 	/* is this query the same as the prefetch? */
873 	if(qstate->qinfo.qtype == LDNS_RR_TYPE_DNSKEY &&
874 		query_dname_compare(iq->dp->name, qstate->qinfo.qname)==0 &&
875 		(qstate->query_flags&BIT_RD) && !(qstate->query_flags&BIT_CD)){
876 		return;
877 	}
878 
879 	/* if the DNSKEY is in the cache this lookup will stop quickly */
880 	log_nametypeclass(VERB_ALGO, "schedule dnskey prefetch",
881 		iq->dp->name, LDNS_RR_TYPE_DNSKEY, iq->qchase.qclass);
882 	if(!generate_sub_request(iq->dp->name, iq->dp->namelen,
883 		LDNS_RR_TYPE_DNSKEY, iq->qchase.qclass, qstate, id, iq,
884 		INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0)) {
885 		/* we'll be slower, but it'll work */
886 		verbose(VERB_ALGO, "could not generate dnskey prefetch");
887 		return;
888 	}
889 	if(subq) {
890 		struct iter_qstate* subiq =
891 			(struct iter_qstate*)subq->minfo[id];
892 		/* this qstate has the right delegation for the dnskey lookup*/
893 		/* make copy to avoid use of stub dp by different qs/threads */
894 		subiq->dp = delegpt_copy(iq->dp, subq->region);
895 		/* if !subiq->dp, it'll start from the cache, no problem */
896 	}
897 }
898 
899 /**
900  * See if the query needs forwarding.
901  *
902  * @param qstate: query state.
903  * @param iq: iterator query state.
904  * @return true if the request is forwarded, false if not.
905  * 	If returns true but, iq->dp is NULL then a malloc failure occurred.
906  */
907 static int
908 forward_request(struct module_qstate* qstate, struct iter_qstate* iq)
909 {
910 	struct delegpt* dp;
911 	uint8_t* delname = iq->qchase.qname;
912 	size_t delnamelen = iq->qchase.qname_len;
913 	if(iq->refetch_glue) {
914 		delname = iq->dp->name;
915 		delnamelen = iq->dp->namelen;
916 	}
917 	/* strip one label off of DS query to lookup higher for it */
918 	if( (iq->qchase.qtype == LDNS_RR_TYPE_DS || iq->refetch_glue)
919 		&& !dname_is_root(iq->qchase.qname))
920 		dname_remove_label(&delname, &delnamelen);
921 	dp = forwards_lookup(qstate->env->fwds, delname, iq->qchase.qclass);
922 	if(!dp)
923 		return 0;
924 	/* send recursion desired to forward addr */
925 	iq->chase_flags |= BIT_RD;
926 	iq->dp = delegpt_copy(dp, qstate->region);
927 	/* iq->dp checked by caller */
928 	verbose(VERB_ALGO, "forwarding request");
929 	return 1;
930 }
931 
932 /**
933  * Process the initial part of the request handling. This state roughly
934  * corresponds to resolver algorithms steps 1 (find answer in cache) and 2
935  * (find the best servers to ask).
936  *
937  * Note that all requests start here, and query restarts revisit this state.
938  *
939  * This state either generates: 1) a response, from cache or error, 2) a
940  * priming event, or 3) forwards the request to the next state (init2,
941  * generally).
942  *
943  * @param qstate: query state.
944  * @param iq: iterator query state.
945  * @param ie: iterator shared global environment.
946  * @param id: module id.
947  * @return true if the event needs more request processing immediately,
948  *         false if not.
949  */
950 static int
951 processInitRequest(struct module_qstate* qstate, struct iter_qstate* iq,
952 	struct iter_env* ie, int id)
953 {
954 	uint8_t* delname;
955 	size_t delnamelen;
956 	struct dns_msg* msg;
957 
958 	log_query_info(VERB_DETAIL, "resolving", &qstate->qinfo);
959 	/* check effort */
960 
961 	/* We enforce a maximum number of query restarts. This is primarily a
962 	 * cheap way to prevent CNAME loops. */
963 	if(iq->query_restart_count > MAX_RESTART_COUNT) {
964 		verbose(VERB_QUERY, "request has exceeded the maximum number"
965 			" of query restarts with %d", iq->query_restart_count);
966 		return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
967 	}
968 
969 	/* We enforce a maximum recursion/dependency depth -- in general,
970 	 * this is unnecessary for dependency loops (although it will
971 	 * catch those), but it provides a sensible limit to the amount
972 	 * of work required to answer a given query. */
973 	verbose(VERB_ALGO, "request has dependency depth of %d", iq->depth);
974 	if(iq->depth > ie->max_dependency_depth) {
975 		verbose(VERB_QUERY, "request has exceeded the maximum "
976 			"dependency depth with depth of %d", iq->depth);
977 		return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
978 	}
979 
980 	/* If the request is qclass=ANY, setup to generate each class */
981 	if(qstate->qinfo.qclass == LDNS_RR_CLASS_ANY) {
982 		iq->qchase.qclass = 0;
983 		return next_state(iq, COLLECT_CLASS_STATE);
984 	}
985 
986 	/* Resolver Algorithm Step 1 -- Look for the answer in local data. */
987 
988 	/* This either results in a query restart (CNAME cache response), a
989 	 * terminating response (ANSWER), or a cache miss (null). */
990 
991 	if(qstate->blacklist) {
992 		/* if cache, or anything else, was blacklisted then
993 		 * getting older results from cache is a bad idea, no cache */
994 		verbose(VERB_ALGO, "cache blacklisted, going to the network");
995 		msg = NULL;
996 	} else {
997 		msg = dns_cache_lookup(qstate->env, iq->qchase.qname,
998 			iq->qchase.qname_len, iq->qchase.qtype,
999 			iq->qchase.qclass, qstate->query_flags,
1000 			qstate->region, qstate->env->scratch);
1001 		if(!msg && qstate->env->neg_cache) {
1002 			/* lookup in negative cache; may result in
1003 			 * NOERROR/NODATA or NXDOMAIN answers that need validation */
1004 			msg = val_neg_getmsg(qstate->env->neg_cache, &iq->qchase,
1005 				qstate->region, qstate->env->rrset_cache,
1006 				qstate->env->scratch_buffer,
1007 				*qstate->env->now, 1/*add SOA*/, NULL);
1008 		}
1009 		/* item taken from cache does not match our query name, thus
1010 		 * security needs to be re-examined later */
1011 		if(msg && query_dname_compare(qstate->qinfo.qname,
1012 			iq->qchase.qname) != 0)
1013 			msg->rep->security = sec_status_unchecked;
1014 	}
1015 	if(msg) {
1016 		/* handle positive cache response */
1017 		enum response_type type = response_type_from_cache(msg,
1018 			&iq->qchase);
1019 		if(verbosity >= VERB_ALGO) {
1020 			log_dns_msg("msg from cache lookup", &msg->qinfo,
1021 				msg->rep);
1022 			verbose(VERB_ALGO, "msg ttl is %d, prefetch ttl %d",
1023 				(int)msg->rep->ttl,
1024 				(int)msg->rep->prefetch_ttl);
1025 		}
1026 
1027 		if(type == RESPONSE_TYPE_CNAME) {
1028 			uint8_t* sname = 0;
1029 			size_t slen = 0;
1030 			verbose(VERB_ALGO, "returning CNAME response from "
1031 				"cache");
1032 			if(!handle_cname_response(qstate, iq, msg,
1033 				&sname, &slen))
1034 				return error_response(qstate, id,
1035 					LDNS_RCODE_SERVFAIL);
1036 			iq->qchase.qname = sname;
1037 			iq->qchase.qname_len = slen;
1038 			/* This *is* a query restart, even if it is a cheap
1039 			 * one. */
1040 			iq->dp = NULL;
1041 			iq->refetch_glue = 0;
1042 			iq->query_restart_count++;
1043 			iq->sent_count = 0;
1044 			sock_list_insert(&qstate->reply_origin, NULL, 0, qstate->region);
1045 			return next_state(iq, INIT_REQUEST_STATE);
1046 		}
1047 
1048 		/* if from cache, NULL, else insert 'cache IP' len=0 */
1049 		if(qstate->reply_origin)
1050 			sock_list_insert(&qstate->reply_origin, NULL, 0, qstate->region);
1051 		/* it is an answer, response, to final state */
1052 		verbose(VERB_ALGO, "returning answer from cache.");
1053 		iq->response = msg;
1054 		return final_state(iq);
1055 	}
1056 
1057 	/* attempt to forward the request */
1058 	if(forward_request(qstate, iq))
1059 	{
1060 		if(!iq->dp) {
1061 			log_err("alloc failure for forward dp");
1062 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1063 		}
1064 		iq->refetch_glue = 0;
1065 		/* the request has been forwarded.
1066 		 * forwarded requests need to be immediately sent to the
1067 		 * next state, QUERYTARGETS. */
1068 		return next_state(iq, QUERYTARGETS_STATE);
1069 	}
1070 
1071 	/* Resolver Algorithm Step 2 -- find the "best" servers. */
1072 
1073 	/* first, adjust for DS queries. To avoid the grandparent problem,
1074 	 * we just look for the closest set of server to the parent of qname.
1075 	 * When re-fetching glue we also need to ask the parent.
1076 	 */
1077 	if(iq->refetch_glue) {
1078 		if(!iq->dp) {
1079 			log_err("internal or malloc fail: no dp for refetch");
1080 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1081 		}
1082 		delname = iq->dp->name;
1083 		delnamelen = iq->dp->namelen;
1084 	} else {
1085 		delname = iq->qchase.qname;
1086 		delnamelen = iq->qchase.qname_len;
1087 	}
1088 	if(iq->qchase.qtype == LDNS_RR_TYPE_DS || iq->refetch_glue ||
1089 	   (iq->qchase.qtype == LDNS_RR_TYPE_NS && qstate->prefetch_leeway)) {
1090 		/* remove first label from delname, root goes to hints,
1091 		 * but only to fetch glue, not for qtype=DS. */
1092 		/* also when prefetching an NS record, fetch it again from
1093 		 * its parent, just as if it expired, so that you do not
1094 		 * get stuck on an older nameserver that gives old NSrecords */
1095 		if(dname_is_root(delname) && (iq->refetch_glue ||
1096 			(iq->qchase.qtype == LDNS_RR_TYPE_NS &&
1097 			qstate->prefetch_leeway)))
1098 			delname = NULL; /* go to root priming */
1099 		else 	dname_remove_label(&delname, &delnamelen);
1100 	}
1101 	/* delname is the name to lookup a delegation for. If NULL rootprime */
1102 	while(1) {
1103 
1104 		/* Lookup the delegation in the cache. If null, then the
1105 		 * cache needs to be primed for the qclass. */
1106 		if(delname)
1107 		     iq->dp = dns_cache_find_delegation(qstate->env, delname,
1108 			delnamelen, iq->qchase.qtype, iq->qchase.qclass,
1109 			qstate->region, &iq->deleg_msg,
1110 			*qstate->env->now+qstate->prefetch_leeway);
1111 		else iq->dp = NULL;
1112 
1113 		/* If the cache has returned nothing, then we have a
1114 		 * root priming situation. */
1115 		if(iq->dp == NULL) {
1116 			/* if there is a stub, then no root prime needed */
1117 			int r = prime_stub(qstate, iq, id, delname,
1118 				iq->qchase.qclass);
1119 			if(r == 2)
1120 				break; /* got noprime-stub-zone, continue */
1121 			else if(r)
1122 				return 0; /* stub prime request made */
1123 			if(forwards_lookup_root(qstate->env->fwds,
1124 				iq->qchase.qclass)) {
1125 				/* forward zone root, no root prime needed */
1126 				/* fill in some dp - safety belt */
1127 				iq->dp = hints_lookup_root(qstate->env->hints,
1128 					iq->qchase.qclass);
1129 				if(!iq->dp) {
1130 					log_err("internal error: no hints dp");
1131 					return error_response(qstate, id,
1132 						LDNS_RCODE_SERVFAIL);
1133 				}
1134 				iq->dp = delegpt_copy(iq->dp, qstate->region);
1135 				if(!iq->dp) {
1136 					log_err("out of memory in safety belt");
1137 					return error_response(qstate, id,
1138 						LDNS_RCODE_SERVFAIL);
1139 				}
1140 				return next_state(iq, INIT_REQUEST_2_STATE);
1141 			}
1142 			/* Note that the result of this will set a new
1143 			 * DelegationPoint based on the result of priming. */
1144 			if(!prime_root(qstate, iq, id, iq->qchase.qclass))
1145 				return error_response(qstate, id,
1146 					LDNS_RCODE_REFUSED);
1147 
1148 			/* priming creates and sends a subordinate query, with
1149 			 * this query as the parent. So further processing for
1150 			 * this event will stop until reactivated by the
1151 			 * results of priming. */
1152 			return 0;
1153 		}
1154 		if(!iq->ratelimit_ok && qstate->prefetch_leeway)
1155 			iq->ratelimit_ok = 1; /* allow prefetches, this keeps
1156 			otherwise valid data in the cache */
1157 		if(!iq->ratelimit_ok && infra_ratelimit_exceeded(
1158 			qstate->env->infra_cache, iq->dp->name,
1159 			iq->dp->namelen, *qstate->env->now)) {
1160 			/* and increment the rate, so that the rate for time
1161 			 * now will also exceed the rate, keeping cache fresh */
1162 			(void)infra_ratelimit_inc(qstate->env->infra_cache,
1163 				iq->dp->name, iq->dp->namelen,
1164 				*qstate->env->now);
1165 			/* see if we are passed through with slip factor */
1166 			if(qstate->env->cfg->ratelimit_factor != 0 &&
1167 				ub_random_max(qstate->env->rnd,
1168 				    qstate->env->cfg->ratelimit_factor) == 1) {
1169 				iq->ratelimit_ok = 1;
1170 				log_nametypeclass(VERB_ALGO, "ratelimit allowed through for "
1171 					"delegation point", iq->dp->name,
1172 					LDNS_RR_TYPE_NS, LDNS_RR_CLASS_IN);
1173 			} else {
1174 				log_nametypeclass(VERB_ALGO, "ratelimit exceeded with "
1175 					"delegation point", iq->dp->name,
1176 					LDNS_RR_TYPE_NS, LDNS_RR_CLASS_IN);
1177 				return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1178 			}
1179 		}
1180 
1181 		/* see if this dp not useless.
1182 		 * It is useless if:
1183 		 *	o all NS items are required glue.
1184 		 *	  or the query is for NS item that is required glue.
1185 		 *	o no addresses are provided.
1186 		 *	o RD qflag is on.
1187 		 * Instead, go up one level, and try to get even further
1188 		 * If the root was useless, use safety belt information.
1189 		 * Only check cache returns, because replies for servers
1190 		 * could be useless but lead to loops (bumping into the
1191 		 * same server reply) if useless-checked.
1192 		 */
1193 		if(iter_dp_is_useless(&qstate->qinfo, qstate->query_flags,
1194 			iq->dp)) {
1195 			if(dname_is_root(iq->dp->name)) {
1196 				/* use safety belt */
1197 				verbose(VERB_QUERY, "Cache has root NS but "
1198 				"no addresses. Fallback to the safety belt.");
1199 				iq->dp = hints_lookup_root(qstate->env->hints,
1200 					iq->qchase.qclass);
1201 				/* note deleg_msg is from previous lookup,
1202 				 * but RD is on, so it is not used */
1203 				if(!iq->dp) {
1204 					log_err("internal error: no hints dp");
1205 					return error_response(qstate, id,
1206 						LDNS_RCODE_REFUSED);
1207 				}
1208 				iq->dp = delegpt_copy(iq->dp, qstate->region);
1209 				if(!iq->dp) {
1210 					log_err("out of memory in safety belt");
1211 					return error_response(qstate, id,
1212 						LDNS_RCODE_SERVFAIL);
1213 				}
1214 				break;
1215 			} else {
1216 				verbose(VERB_ALGO,
1217 					"cache delegation was useless:");
1218 				delegpt_log(VERB_ALGO, iq->dp);
1219 				/* go up */
1220 				delname = iq->dp->name;
1221 				delnamelen = iq->dp->namelen;
1222 				dname_remove_label(&delname, &delnamelen);
1223 			}
1224 		} else break;
1225 	}
1226 
1227 	verbose(VERB_ALGO, "cache delegation returns delegpt");
1228 	delegpt_log(VERB_ALGO, iq->dp);
1229 
1230 	/* Otherwise, set the current delegation point and move on to the
1231 	 * next state. */
1232 	return next_state(iq, INIT_REQUEST_2_STATE);
1233 }
1234 
1235 /**
1236  * Process the second part of the initial request handling. This state
1237  * basically exists so that queries that generate root priming events have
1238  * the same init processing as ones that do not. Request events that reach
1239  * this state must have a valid currentDelegationPoint set.
1240  *
1241  * This part is primarly handling stub zone priming. Events that reach this
1242  * state must have a current delegation point.
1243  *
1244  * @param qstate: query state.
1245  * @param iq: iterator query state.
1246  * @param id: module id.
1247  * @return true if the event needs more request processing immediately,
1248  *         false if not.
1249  */
1250 static int
1251 processInitRequest2(struct module_qstate* qstate, struct iter_qstate* iq,
1252 	int id)
1253 {
1254 	uint8_t* delname;
1255 	size_t delnamelen;
1256 	log_query_info(VERB_QUERY, "resolving (init part 2): ",
1257 		&qstate->qinfo);
1258 
1259 	if(iq->refetch_glue) {
1260 		if(!iq->dp) {
1261 			log_err("internal or malloc fail: no dp for refetch");
1262 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1263 		}
1264 		delname = iq->dp->name;
1265 		delnamelen = iq->dp->namelen;
1266 	} else {
1267 		delname = iq->qchase.qname;
1268 		delnamelen = iq->qchase.qname_len;
1269 	}
1270 	if(iq->qchase.qtype == LDNS_RR_TYPE_DS || iq->refetch_glue) {
1271 		if(!dname_is_root(delname))
1272 			dname_remove_label(&delname, &delnamelen);
1273 		iq->refetch_glue = 0; /* if CNAME causes restart, no refetch */
1274 	}
1275 	/* Check to see if we need to prime a stub zone. */
1276 	if(prime_stub(qstate, iq, id, delname, iq->qchase.qclass)) {
1277 		/* A priming sub request was made */
1278 		return 0;
1279 	}
1280 
1281 	/* most events just get forwarded to the next state. */
1282 	return next_state(iq, INIT_REQUEST_3_STATE);
1283 }
1284 
1285 /**
1286  * Process the third part of the initial request handling. This state exists
1287  * as a separate state so that queries that generate stub priming events
1288  * will get the tail end of the init process but not repeat the stub priming
1289  * check.
1290  *
1291  * @param qstate: query state.
1292  * @param iq: iterator query state.
1293  * @param id: module id.
1294  * @return true, advancing the event to the QUERYTARGETS_STATE.
1295  */
1296 static int
1297 processInitRequest3(struct module_qstate* qstate, struct iter_qstate* iq,
1298 	int id)
1299 {
1300 	log_query_info(VERB_QUERY, "resolving (init part 3): ",
1301 		&qstate->qinfo);
1302 	/* if the cache reply dp equals a validation anchor or msg has DS,
1303 	 * then DNSSEC RRSIGs are expected in the reply */
1304 	iq->dnssec_expected = iter_indicates_dnssec(qstate->env, iq->dp,
1305 		iq->deleg_msg, iq->qchase.qclass);
1306 
1307 	/* If the RD flag wasn't set, then we just finish with the
1308 	 * cached referral as the response. */
1309 	if(!(qstate->query_flags & BIT_RD)) {
1310 		iq->response = iq->deleg_msg;
1311 		if(verbosity >= VERB_ALGO && iq->response)
1312 			log_dns_msg("no RD requested, using delegation msg",
1313 				&iq->response->qinfo, iq->response->rep);
1314 		if(qstate->reply_origin)
1315 			sock_list_insert(&qstate->reply_origin, NULL, 0, qstate->region);
1316 		return final_state(iq);
1317 	}
1318 	/* After this point, unset the RD flag -- this query is going to
1319 	 * be sent to an auth. server. */
1320 	iq->chase_flags &= ~BIT_RD;
1321 
1322 	/* if dnssec expected, fetch key for the trust-anchor or cached-DS */
1323 	if(iq->dnssec_expected && qstate->env->cfg->prefetch_key &&
1324 		!(qstate->query_flags&BIT_CD)) {
1325 		generate_dnskey_prefetch(qstate, iq, id);
1326 		fptr_ok(fptr_whitelist_modenv_detach_subs(
1327 			qstate->env->detach_subs));
1328 		(*qstate->env->detach_subs)(qstate);
1329 	}
1330 
1331 	/* Jump to the next state. */
1332 	return next_state(iq, QUERYTARGETS_STATE);
1333 }
1334 
1335 /**
1336  * Given a basic query, generate a parent-side "target" query.
1337  * These are subordinate queries for missing delegation point target addresses,
1338  * for which only the parent of the delegation provides correct IP addresses.
1339  *
1340  * @param qstate: query state.
1341  * @param iq: iterator query state.
1342  * @param id: module id.
1343  * @param name: target qname.
1344  * @param namelen: target qname length.
1345  * @param qtype: target qtype (either A or AAAA).
1346  * @param qclass: target qclass.
1347  * @return true on success, false on failure.
1348  */
1349 static int
1350 generate_parentside_target_query(struct module_qstate* qstate,
1351 	struct iter_qstate* iq, int id, uint8_t* name, size_t namelen,
1352 	uint16_t qtype, uint16_t qclass)
1353 {
1354 	struct module_qstate* subq;
1355 	if(!generate_sub_request(name, namelen, qtype, qclass, qstate,
1356 		id, iq, INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0))
1357 		return 0;
1358 	if(subq) {
1359 		struct iter_qstate* subiq =
1360 			(struct iter_qstate*)subq->minfo[id];
1361 		/* blacklist the cache - we want to fetch parent stuff */
1362 		sock_list_insert(&subq->blacklist, NULL, 0, subq->region);
1363 		subiq->query_for_pside_glue = 1;
1364 		if(dname_subdomain_c(name, iq->dp->name)) {
1365 			subiq->dp = delegpt_copy(iq->dp, subq->region);
1366 			subiq->dnssec_expected = iter_indicates_dnssec(
1367 				qstate->env, subiq->dp, NULL,
1368 				subq->qinfo.qclass);
1369 			subiq->refetch_glue = 1;
1370 		} else {
1371 			subiq->dp = dns_cache_find_delegation(qstate->env,
1372 				name, namelen, qtype, qclass, subq->region,
1373 				&subiq->deleg_msg,
1374 				*qstate->env->now+subq->prefetch_leeway);
1375 			/* if no dp, then it's from root, refetch unneeded */
1376 			if(subiq->dp) {
1377 				subiq->dnssec_expected = iter_indicates_dnssec(
1378 					qstate->env, subiq->dp, NULL,
1379 					subq->qinfo.qclass);
1380 				subiq->refetch_glue = 1;
1381 			}
1382 		}
1383 	}
1384 	log_nametypeclass(VERB_QUERY, "new pside target", name, qtype, qclass);
1385 	return 1;
1386 }
1387 
1388 /**
1389  * Given a basic query, generate a "target" query. These are subordinate
1390  * queries for missing delegation point target addresses.
1391  *
1392  * @param qstate: query state.
1393  * @param iq: iterator query state.
1394  * @param id: module id.
1395  * @param name: target qname.
1396  * @param namelen: target qname length.
1397  * @param qtype: target qtype (either A or AAAA).
1398  * @param qclass: target qclass.
1399  * @return true on success, false on failure.
1400  */
1401 static int
1402 generate_target_query(struct module_qstate* qstate, struct iter_qstate* iq,
1403         int id, uint8_t* name, size_t namelen, uint16_t qtype, uint16_t qclass)
1404 {
1405 	struct module_qstate* subq;
1406 	if(!generate_sub_request(name, namelen, qtype, qclass, qstate,
1407 		id, iq, INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0))
1408 		return 0;
1409 	log_nametypeclass(VERB_QUERY, "new target", name, qtype, qclass);
1410 	return 1;
1411 }
1412 
1413 /**
1414  * Given an event at a certain state, generate zero or more target queries
1415  * for it's current delegation point.
1416  *
1417  * @param qstate: query state.
1418  * @param iq: iterator query state.
1419  * @param ie: iterator shared global environment.
1420  * @param id: module id.
1421  * @param maxtargets: The maximum number of targets to query for.
1422  *	if it is negative, there is no maximum number of targets.
1423  * @param num: returns the number of queries generated and processed,
1424  *	which may be zero if there were no missing targets.
1425  * @return false on error.
1426  */
1427 static int
1428 query_for_targets(struct module_qstate* qstate, struct iter_qstate* iq,
1429         struct iter_env* ie, int id, int maxtargets, int* num)
1430 {
1431 	int query_count = 0;
1432 	struct delegpt_ns* ns;
1433 	int missing;
1434 	int toget = 0;
1435 
1436 	if(iq->depth == ie->max_dependency_depth)
1437 		return 0;
1438 	if(iq->depth > 0 && iq->target_count &&
1439 		iq->target_count[1] > MAX_TARGET_COUNT) {
1440 		char s[LDNS_MAX_DOMAINLEN+1];
1441 		dname_str(qstate->qinfo.qname, s);
1442 		verbose(VERB_QUERY, "request %s has exceeded the maximum "
1443 			"number of glue fetches %d", s, iq->target_count[1]);
1444 		return 0;
1445 	}
1446 
1447 	iter_mark_cycle_targets(qstate, iq->dp);
1448 	missing = (int)delegpt_count_missing_targets(iq->dp);
1449 	log_assert(maxtargets != 0); /* that would not be useful */
1450 
1451 	/* Generate target requests. Basically, any missing targets
1452 	 * are queried for here, regardless if it is necessary to do
1453 	 * so to continue processing. */
1454 	if(maxtargets < 0 || maxtargets > missing)
1455 		toget = missing;
1456 	else	toget = maxtargets;
1457 	if(toget == 0) {
1458 		*num = 0;
1459 		return 1;
1460 	}
1461 	/* select 'toget' items from the total of 'missing' items */
1462 	log_assert(toget <= missing);
1463 
1464 	/* loop over missing targets */
1465 	for(ns = iq->dp->nslist; ns; ns = ns->next) {
1466 		if(ns->resolved)
1467 			continue;
1468 
1469 		/* randomly select this item with probability toget/missing */
1470 		if(!iter_ns_probability(qstate->env->rnd, toget, missing)) {
1471 			/* do not select this one, next; select toget number
1472 			 * of items from a list one less in size */
1473 			missing --;
1474 			continue;
1475 		}
1476 
1477 		if(ie->supports_ipv6 && !ns->got6) {
1478 			/* Send the AAAA request. */
1479 			if(!generate_target_query(qstate, iq, id,
1480 				ns->name, ns->namelen,
1481 				LDNS_RR_TYPE_AAAA, iq->qchase.qclass)) {
1482 				*num = query_count;
1483 				if(query_count > 0)
1484 					qstate->ext_state[id] = module_wait_subquery;
1485 				return 0;
1486 			}
1487 			query_count++;
1488 		}
1489 		/* Send the A request. */
1490 		if(ie->supports_ipv4 && !ns->got4) {
1491 			if(!generate_target_query(qstate, iq, id,
1492 				ns->name, ns->namelen,
1493 				LDNS_RR_TYPE_A, iq->qchase.qclass)) {
1494 				*num = query_count;
1495 				if(query_count > 0)
1496 					qstate->ext_state[id] = module_wait_subquery;
1497 				return 0;
1498 			}
1499 			query_count++;
1500 		}
1501 
1502 		/* mark this target as in progress. */
1503 		ns->resolved = 1;
1504 		missing--;
1505 		toget--;
1506 		if(toget == 0)
1507 			break;
1508 	}
1509 	*num = query_count;
1510 	if(query_count > 0)
1511 		qstate->ext_state[id] = module_wait_subquery;
1512 
1513 	return 1;
1514 }
1515 
1516 /** see if last resort is possible - does config allow queries to parent */
1517 static int
1518 can_have_last_resort(struct module_env* env, struct delegpt* dp,
1519 	struct iter_qstate* iq)
1520 {
1521 	struct delegpt* fwddp;
1522 	struct iter_hints_stub* stub;
1523 	/* do not process a last resort (the parent side) if a stub
1524 	 * or forward is configured, because we do not want to go 'above'
1525 	 * the configured servers */
1526 	if(!dname_is_root(dp->name) && (stub = (struct iter_hints_stub*)
1527 		name_tree_find(&env->hints->tree, dp->name, dp->namelen,
1528 		dp->namelabs, iq->qchase.qclass)) &&
1529 		/* has_parent side is turned off for stub_first, where we
1530 		 * are allowed to go to the parent */
1531 		stub->dp->has_parent_side_NS) {
1532 		verbose(VERB_QUERY, "configured stub servers failed -- returning SERVFAIL");
1533 		return 0;
1534 	}
1535 	if((fwddp = forwards_find(env->fwds, dp->name, iq->qchase.qclass)) &&
1536 		/* has_parent_side is turned off for forward_first, where
1537 		 * we are allowed to go to the parent */
1538 		fwddp->has_parent_side_NS) {
1539 		verbose(VERB_QUERY, "configured forward servers failed -- returning SERVFAIL");
1540 		return 0;
1541 	}
1542 	return 1;
1543 }
1544 
1545 /**
1546  * Called by processQueryTargets when it would like extra targets to query
1547  * but it seems to be out of options.  At last resort some less appealing
1548  * options are explored.  If there are no more options, the result is SERVFAIL
1549  *
1550  * @param qstate: query state.
1551  * @param iq: iterator query state.
1552  * @param ie: iterator shared global environment.
1553  * @param id: module id.
1554  * @return true if the event requires more request processing immediately,
1555  *         false if not.
1556  */
1557 static int
1558 processLastResort(struct module_qstate* qstate, struct iter_qstate* iq,
1559 	struct iter_env* ie, int id)
1560 {
1561 	struct delegpt_ns* ns;
1562 	int query_count = 0;
1563 	verbose(VERB_ALGO, "No more query targets, attempting last resort");
1564 	log_assert(iq->dp);
1565 
1566 	if(!can_have_last_resort(qstate->env, iq->dp, iq)) {
1567 		/* fail -- no more targets, no more hope of targets, no hope
1568 		 * of a response. */
1569 		return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
1570 	}
1571 	if(!iq->dp->has_parent_side_NS && dname_is_root(iq->dp->name)) {
1572 		struct delegpt* p = hints_lookup_root(qstate->env->hints,
1573 			iq->qchase.qclass);
1574 		if(p) {
1575 			struct delegpt_ns* ns;
1576 			struct delegpt_addr* a;
1577 			iq->chase_flags &= ~BIT_RD; /* go to authorities */
1578 			for(ns = p->nslist; ns; ns=ns->next) {
1579 				(void)delegpt_add_ns(iq->dp, qstate->region,
1580 					ns->name, ns->lame);
1581 			}
1582 			for(a = p->target_list; a; a=a->next_target) {
1583 				(void)delegpt_add_addr(iq->dp, qstate->region,
1584 					&a->addr, a->addrlen, a->bogus,
1585 					a->lame);
1586 			}
1587 		}
1588 		iq->dp->has_parent_side_NS = 1;
1589 	} else if(!iq->dp->has_parent_side_NS) {
1590 		if(!iter_lookup_parent_NS_from_cache(qstate->env, iq->dp,
1591 			qstate->region, &qstate->qinfo)
1592 			|| !iq->dp->has_parent_side_NS) {
1593 			/* if: malloc failure in lookup go up to try */
1594 			/* if: no parent NS in cache - go up one level */
1595 			verbose(VERB_ALGO, "try to grab parent NS");
1596 			iq->store_parent_NS = iq->dp;
1597 			iq->chase_flags &= ~BIT_RD; /* go to authorities */
1598 			iq->deleg_msg = NULL;
1599 			iq->refetch_glue = 1;
1600 			iq->query_restart_count++;
1601 			iq->sent_count = 0;
1602 			return next_state(iq, INIT_REQUEST_STATE);
1603 		}
1604 	}
1605 	/* see if that makes new names available */
1606 	if(!cache_fill_missing(qstate->env, iq->qchase.qclass,
1607 		qstate->region, iq->dp))
1608 		log_err("out of memory in cache_fill_missing");
1609 	if(iq->dp->usable_list) {
1610 		verbose(VERB_ALGO, "try parent-side-name, w. glue from cache");
1611 		return next_state(iq, QUERYTARGETS_STATE);
1612 	}
1613 	/* try to fill out parent glue from cache */
1614 	if(iter_lookup_parent_glue_from_cache(qstate->env, iq->dp,
1615 		qstate->region, &qstate->qinfo)) {
1616 		/* got parent stuff from cache, see if we can continue */
1617 		verbose(VERB_ALGO, "try parent-side glue from cache");
1618 		return next_state(iq, QUERYTARGETS_STATE);
1619 	}
1620 	/* query for an extra name added by the parent-NS record */
1621 	if(delegpt_count_missing_targets(iq->dp) > 0) {
1622 		int qs = 0;
1623 		verbose(VERB_ALGO, "try parent-side target name");
1624 		if(!query_for_targets(qstate, iq, ie, id, 1, &qs)) {
1625 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1626 		}
1627 		iq->num_target_queries += qs;
1628 		target_count_increase(iq, qs);
1629 		if(qs != 0) {
1630 			qstate->ext_state[id] = module_wait_subquery;
1631 			return 0; /* and wait for them */
1632 		}
1633 	}
1634 	if(iq->depth == ie->max_dependency_depth) {
1635 		verbose(VERB_QUERY, "maxdepth and need more nameservers, fail");
1636 		return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
1637 	}
1638 	if(iq->depth > 0 && iq->target_count &&
1639 		iq->target_count[1] > MAX_TARGET_COUNT) {
1640 		char s[LDNS_MAX_DOMAINLEN+1];
1641 		dname_str(qstate->qinfo.qname, s);
1642 		verbose(VERB_QUERY, "request %s has exceeded the maximum "
1643 			"number of glue fetches %d", s, iq->target_count[1]);
1644 		return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
1645 	}
1646 	/* mark cycle targets for parent-side lookups */
1647 	iter_mark_pside_cycle_targets(qstate, iq->dp);
1648 	/* see if we can issue queries to get nameserver addresses */
1649 	/* this lookup is not randomized, but sequential. */
1650 	for(ns = iq->dp->nslist; ns; ns = ns->next) {
1651 		/* query for parent-side A and AAAA for nameservers */
1652 		if(ie->supports_ipv6 && !ns->done_pside6) {
1653 			/* Send the AAAA request. */
1654 			if(!generate_parentside_target_query(qstate, iq, id,
1655 				ns->name, ns->namelen,
1656 				LDNS_RR_TYPE_AAAA, iq->qchase.qclass))
1657 				return error_response(qstate, id,
1658 					LDNS_RCODE_SERVFAIL);
1659 			ns->done_pside6 = 1;
1660 			query_count++;
1661 		}
1662 		if(ie->supports_ipv4 && !ns->done_pside4) {
1663 			/* Send the A request. */
1664 			if(!generate_parentside_target_query(qstate, iq, id,
1665 				ns->name, ns->namelen,
1666 				LDNS_RR_TYPE_A, iq->qchase.qclass))
1667 				return error_response(qstate, id,
1668 					LDNS_RCODE_SERVFAIL);
1669 			ns->done_pside4 = 1;
1670 			query_count++;
1671 		}
1672 		if(query_count != 0) { /* suspend to await results */
1673 			verbose(VERB_ALGO, "try parent-side glue lookup");
1674 			iq->num_target_queries += query_count;
1675 			target_count_increase(iq, query_count);
1676 			qstate->ext_state[id] = module_wait_subquery;
1677 			return 0;
1678 		}
1679 	}
1680 
1681 	/* if this was a parent-side glue query itself, then store that
1682 	 * failure in cache. */
1683 	if(iq->query_for_pside_glue && !iq->pside_glue)
1684 		iter_store_parentside_neg(qstate->env, &qstate->qinfo,
1685 			iq->deleg_msg?iq->deleg_msg->rep:
1686 			(iq->response?iq->response->rep:NULL));
1687 
1688 	verbose(VERB_QUERY, "out of query targets -- returning SERVFAIL");
1689 	/* fail -- no more targets, no more hope of targets, no hope
1690 	 * of a response. */
1691 	return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
1692 }
1693 
1694 /**
1695  * Try to find the NS record set that will resolve a qtype DS query. Due
1696  * to grandparent/grandchild reasons we did not get a proper lookup right
1697  * away.  We need to create type NS queries until we get the right parent
1698  * for this lookup.  We remove labels from the query to find the right point.
1699  * If we end up at the old dp name, then there is no solution.
1700  *
1701  * @param qstate: query state.
1702  * @param iq: iterator query state.
1703  * @param id: module id.
1704  * @return true if the event requires more immediate processing, false if
1705  *         not. This is generally only true when forwarding the request to
1706  *         the final state (i.e., on answer).
1707  */
1708 static int
1709 processDSNSFind(struct module_qstate* qstate, struct iter_qstate* iq, int id)
1710 {
1711 	struct module_qstate* subq = NULL;
1712 	verbose(VERB_ALGO, "processDSNSFind");
1713 
1714 	if(!iq->dsns_point) {
1715 		/* initialize */
1716 		iq->dsns_point = iq->qchase.qname;
1717 		iq->dsns_point_len = iq->qchase.qname_len;
1718 	}
1719 	/* robustcheck for internal error: we are not underneath the dp */
1720 	if(!dname_subdomain_c(iq->dsns_point, iq->dp->name)) {
1721 		return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
1722 	}
1723 
1724 	/* go up one (more) step, until we hit the dp, if so, end */
1725 	dname_remove_label(&iq->dsns_point, &iq->dsns_point_len);
1726 	if(query_dname_compare(iq->dsns_point, iq->dp->name) == 0) {
1727 		/* there was no inbetween nameserver, use the old delegation
1728 		 * point again.  And this time, because dsns_point is nonNULL
1729 		 * we are going to accept the (bad) result */
1730 		iq->state = QUERYTARGETS_STATE;
1731 		return 1;
1732 	}
1733 	iq->state = DSNS_FIND_STATE;
1734 
1735 	/* spawn NS lookup (validation not needed, this is for DS lookup) */
1736 	log_nametypeclass(VERB_ALGO, "fetch nameservers",
1737 		iq->dsns_point, LDNS_RR_TYPE_NS, iq->qchase.qclass);
1738 	if(!generate_sub_request(iq->dsns_point, iq->dsns_point_len,
1739 		LDNS_RR_TYPE_NS, iq->qchase.qclass, qstate, id, iq,
1740 		INIT_REQUEST_STATE, FINISHED_STATE, &subq, 0)) {
1741 		return error_response_cache(qstate, id, LDNS_RCODE_SERVFAIL);
1742 	}
1743 
1744 	return 0;
1745 }
1746 
1747 /**
1748  * This is the request event state where the request will be sent to one of
1749  * its current query targets. This state also handles issuing target lookup
1750  * queries for missing target IP addresses. Queries typically iterate on
1751  * this state, both when they are just trying different targets for a given
1752  * delegation point, and when they change delegation points. This state
1753  * roughly corresponds to RFC 1034 algorithm steps 3 and 4.
1754  *
1755  * @param qstate: query state.
1756  * @param iq: iterator query state.
1757  * @param ie: iterator shared global environment.
1758  * @param id: module id.
1759  * @return true if the event requires more request processing immediately,
1760  *         false if not. This state only returns true when it is generating
1761  *         a SERVFAIL response because the query has hit a dead end.
1762  */
1763 static int
1764 processQueryTargets(struct module_qstate* qstate, struct iter_qstate* iq,
1765 	struct iter_env* ie, int id)
1766 {
1767 	int tf_policy;
1768 	struct delegpt_addr* target;
1769 	struct outbound_entry* outq;
1770 
1771 	/* NOTE: a request will encounter this state for each target it
1772 	 * needs to send a query to. That is, at least one per referral,
1773 	 * more if some targets timeout or return throwaway answers. */
1774 
1775 	log_query_info(VERB_QUERY, "processQueryTargets:", &qstate->qinfo);
1776 	verbose(VERB_ALGO, "processQueryTargets: targetqueries %d, "
1777 		"currentqueries %d sentcount %d", iq->num_target_queries,
1778 		iq->num_current_queries, iq->sent_count);
1779 
1780 	/* Make sure that we haven't run away */
1781 	/* FIXME: is this check even necessary? */
1782 	if(iq->referral_count > MAX_REFERRAL_COUNT) {
1783 		verbose(VERB_QUERY, "request has exceeded the maximum "
1784 			"number of referrrals with %d", iq->referral_count);
1785 		return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1786 	}
1787 	if(iq->sent_count > MAX_SENT_COUNT) {
1788 		verbose(VERB_QUERY, "request has exceeded the maximum "
1789 			"number of sends with %d", iq->sent_count);
1790 		return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1791 	}
1792 
1793 	/* Make sure we have a delegation point, otherwise priming failed
1794 	 * or another failure occurred */
1795 	if(!iq->dp) {
1796 		verbose(VERB_QUERY, "Failed to get a delegation, giving up");
1797 		return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1798 	}
1799 	if(!ie->supports_ipv6)
1800 		delegpt_no_ipv6(iq->dp);
1801 	if(!ie->supports_ipv4)
1802 		delegpt_no_ipv4(iq->dp);
1803 	delegpt_log(VERB_ALGO, iq->dp);
1804 
1805 	if(iq->num_current_queries>0) {
1806 		/* already busy answering a query, this restart is because
1807 		 * more delegpt addrs became available, wait for existing
1808 		 * query. */
1809 		verbose(VERB_ALGO, "woke up, but wait for outstanding query");
1810 		qstate->ext_state[id] = module_wait_reply;
1811 		return 0;
1812 	}
1813 
1814 	tf_policy = 0;
1815 	/* < not <=, because although the array is large enough for <=, the
1816 	 * generated query will immediately be discarded due to depth and
1817 	 * that servfail is cached, which is not good as opportunism goes. */
1818 	if(iq->depth < ie->max_dependency_depth
1819 		&& iq->sent_count < TARGET_FETCH_STOP) {
1820 		tf_policy = ie->target_fetch_policy[iq->depth];
1821 	}
1822 
1823 	/* if in 0x20 fallback get as many targets as possible */
1824 	if(iq->caps_fallback) {
1825 		int extra = 0;
1826 		size_t naddr, nres, navail;
1827 		if(!query_for_targets(qstate, iq, ie, id, -1, &extra)) {
1828 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1829 		}
1830 		iq->num_target_queries += extra;
1831 		target_count_increase(iq, extra);
1832 		if(iq->num_target_queries > 0) {
1833 			/* wait to get all targets, we want to try em */
1834 			verbose(VERB_ALGO, "wait for all targets for fallback");
1835 			qstate->ext_state[id] = module_wait_reply;
1836 			return 0;
1837 		}
1838 		/* did we do enough fallback queries already? */
1839 		delegpt_count_addr(iq->dp, &naddr, &nres, &navail);
1840 		/* the current caps_server is the number of fallbacks sent.
1841 		 * the original query is one that matched too, so we have
1842 		 * caps_server+1 number of matching queries now */
1843 		if(iq->caps_server+1 >= naddr*3 ||
1844 			iq->caps_server*2+2 >= MAX_SENT_COUNT) {
1845 			/* *2 on sentcount check because ipv6 may fail */
1846 			/* we're done, process the response */
1847 			verbose(VERB_ALGO, "0x20 fallback had %d responses "
1848 				"match for %d wanted, done.",
1849 				(int)iq->caps_server+1, (int)naddr*3);
1850 			iq->response = iq->caps_response;
1851 			iq->caps_fallback = 0;
1852 			iter_dec_attempts(iq->dp, 3); /* space for fallback */
1853 			iq->num_current_queries++; /* RespState decrements it*/
1854 			iq->referral_count++; /* make sure we don't loop */
1855 			iq->sent_count = 0;
1856 			iq->state = QUERY_RESP_STATE;
1857 			return 1;
1858 		}
1859 		verbose(VERB_ALGO, "0x20 fallback number %d",
1860 			(int)iq->caps_server);
1861 
1862 	/* if there is a policy to fetch missing targets
1863 	 * opportunistically, do it. we rely on the fact that once a
1864 	 * query (or queries) for a missing name have been issued,
1865 	 * they will not show up again. */
1866 	} else if(tf_policy != 0) {
1867 		int extra = 0;
1868 		verbose(VERB_ALGO, "attempt to get extra %d targets",
1869 			tf_policy);
1870 		(void)query_for_targets(qstate, iq, ie, id, tf_policy, &extra);
1871 		/* errors ignored, these targets are not strictly necessary for
1872 		 * this result, we do not have to reply with SERVFAIL */
1873 		iq->num_target_queries += extra;
1874 		target_count_increase(iq, extra);
1875 	}
1876 
1877 	/* Add the current set of unused targets to our queue. */
1878 	delegpt_add_unused_targets(iq->dp);
1879 
1880 	/* Select the next usable target, filtering out unsuitable targets. */
1881 	target = iter_server_selection(ie, qstate->env, iq->dp,
1882 		iq->dp->name, iq->dp->namelen, iq->qchase.qtype,
1883 		&iq->dnssec_lame_query, &iq->chase_to_rd,
1884 		iq->num_target_queries, qstate->blacklist);
1885 
1886 	/* If no usable target was selected... */
1887 	if(!target) {
1888 		/* Here we distinguish between three states: generate a new
1889 		 * target query, just wait, or quit (with a SERVFAIL).
1890 		 * We have the following information: number of active
1891 		 * target queries, number of active current queries,
1892 		 * the presence of missing targets at this delegation
1893 		 * point, and the given query target policy. */
1894 
1895 		/* Check for the wait condition. If this is true, then
1896 		 * an action must be taken. */
1897 		if(iq->num_target_queries==0 && iq->num_current_queries==0) {
1898 			/* If there is nothing to wait for, then we need
1899 			 * to distinguish between generating (a) new target
1900 			 * query, or failing. */
1901 			if(delegpt_count_missing_targets(iq->dp) > 0) {
1902 				int qs = 0;
1903 				verbose(VERB_ALGO, "querying for next "
1904 					"missing target");
1905 				if(!query_for_targets(qstate, iq, ie, id,
1906 					1, &qs)) {
1907 					return error_response(qstate, id,
1908 						LDNS_RCODE_SERVFAIL);
1909 				}
1910 				if(qs == 0 &&
1911 				   delegpt_count_missing_targets(iq->dp) == 0){
1912 					/* it looked like there were missing
1913 					 * targets, but they did not turn up.
1914 					 * Try the bad choices again (if any),
1915 					 * when we get back here missing==0,
1916 					 * so this is not a loop. */
1917 					return 1;
1918 				}
1919 				iq->num_target_queries += qs;
1920 				target_count_increase(iq, qs);
1921 			}
1922 			/* Since a target query might have been made, we
1923 			 * need to check again. */
1924 			if(iq->num_target_queries == 0) {
1925 				/* if in capsforid fallback, instead of last
1926 				 * resort, we agree with the current reply
1927 				 * we have (if any) (our count of addrs bad)*/
1928 				if(iq->caps_fallback && iq->caps_reply) {
1929 					/* we're done, process the response */
1930 					verbose(VERB_ALGO, "0x20 fallback had %d responses, "
1931 						"but no more servers except "
1932 						"last resort, done.",
1933 						(int)iq->caps_server+1);
1934 					iq->response = iq->caps_response;
1935 					iq->caps_fallback = 0;
1936 					iter_dec_attempts(iq->dp, 3); /* space for fallback */
1937 					iq->num_current_queries++; /* RespState decrements it*/
1938 					iq->referral_count++; /* make sure we don't loop */
1939 					iq->sent_count = 0;
1940 					iq->state = QUERY_RESP_STATE;
1941 					return 1;
1942 				}
1943 				return processLastResort(qstate, iq, ie, id);
1944 			}
1945 		}
1946 
1947 		/* otherwise, we have no current targets, so submerge
1948 		 * until one of the target or direct queries return. */
1949 		if(iq->num_target_queries>0 && iq->num_current_queries>0) {
1950 			verbose(VERB_ALGO, "no current targets -- waiting "
1951 				"for %d targets to resolve or %d outstanding"
1952 				" queries to respond", iq->num_target_queries,
1953 				iq->num_current_queries);
1954 			qstate->ext_state[id] = module_wait_reply;
1955 		} else if(iq->num_target_queries>0) {
1956 			verbose(VERB_ALGO, "no current targets -- waiting "
1957 				"for %d targets to resolve.",
1958 				iq->num_target_queries);
1959 			qstate->ext_state[id] = module_wait_subquery;
1960 		} else {
1961 			verbose(VERB_ALGO, "no current targets -- waiting "
1962 				"for %d outstanding queries to respond.",
1963 				iq->num_current_queries);
1964 			qstate->ext_state[id] = module_wait_reply;
1965 		}
1966 		return 0;
1967 	}
1968 
1969 	/* if not forwarding, check ratelimits per delegationpoint name */
1970 	if(!(iq->chase_flags & BIT_RD) && !iq->ratelimit_ok) {
1971 		if(!infra_ratelimit_inc(qstate->env->infra_cache, iq->dp->name,
1972 			iq->dp->namelen, *qstate->env->now)) {
1973 			verbose(VERB_ALGO, "query exceeded ratelimits");
1974 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
1975 		}
1976 	}
1977 
1978 	/* We have a valid target. */
1979 	if(verbosity >= VERB_QUERY) {
1980 		log_query_info(VERB_QUERY, "sending query:", &iq->qchase);
1981 		log_name_addr(VERB_QUERY, "sending to target:", iq->dp->name,
1982 			&target->addr, target->addrlen);
1983 		verbose(VERB_ALGO, "dnssec status: %s%s",
1984 			iq->dnssec_expected?"expected": "not expected",
1985 			iq->dnssec_lame_query?" but lame_query anyway": "");
1986 	}
1987 	fptr_ok(fptr_whitelist_modenv_send_query(qstate->env->send_query));
1988 	outq = (*qstate->env->send_query)(
1989 		iq->qchase.qname, iq->qchase.qname_len,
1990 		iq->qchase.qtype, iq->qchase.qclass,
1991 		iq->chase_flags | (iq->chase_to_rd?BIT_RD:0), EDNS_DO|BIT_CD,
1992 		iq->dnssec_expected, iq->caps_fallback || is_caps_whitelisted(
1993 		ie, iq), &target->addr, target->addrlen, iq->dp->name,
1994 		iq->dp->namelen, qstate);
1995 	if(!outq) {
1996 		log_addr(VERB_DETAIL, "error sending query to auth server",
1997 			&target->addr, target->addrlen);
1998 		if(!(iq->chase_flags & BIT_RD) && !iq->ratelimit_ok)
1999 		    infra_ratelimit_dec(qstate->env->infra_cache, iq->dp->name,
2000 			iq->dp->namelen, *qstate->env->now);
2001 		return next_state(iq, QUERYTARGETS_STATE);
2002 	}
2003 	outbound_list_insert(&iq->outlist, outq);
2004 	iq->num_current_queries++;
2005 	iq->sent_count++;
2006 	qstate->ext_state[id] = module_wait_reply;
2007 
2008 	return 0;
2009 }
2010 
2011 /** find NS rrset in given list */
2012 static struct ub_packed_rrset_key*
2013 find_NS(struct reply_info* rep, size_t from, size_t to)
2014 {
2015 	size_t i;
2016 	for(i=from; i<to; i++) {
2017 		if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_NS)
2018 			return rep->rrsets[i];
2019 	}
2020 	return NULL;
2021 }
2022 
2023 
2024 /**
2025  * Process the query response. All queries end up at this state first. This
2026  * process generally consists of analyzing the response and routing the
2027  * event to the next state (either bouncing it back to a request state, or
2028  * terminating the processing for this event).
2029  *
2030  * @param qstate: query state.
2031  * @param iq: iterator query state.
2032  * @param id: module id.
2033  * @return true if the event requires more immediate processing, false if
2034  *         not. This is generally only true when forwarding the request to
2035  *         the final state (i.e., on answer).
2036  */
2037 static int
2038 processQueryResponse(struct module_qstate* qstate, struct iter_qstate* iq,
2039 	int id)
2040 {
2041 	int dnsseclame = 0;
2042 	enum response_type type;
2043 	iq->num_current_queries--;
2044 	if(iq->response == NULL) {
2045 		iq->chase_to_rd = 0;
2046 		iq->dnssec_lame_query = 0;
2047 		verbose(VERB_ALGO, "query response was timeout");
2048 		return next_state(iq, QUERYTARGETS_STATE);
2049 	}
2050 	type = response_type_from_server(
2051 		(int)((iq->chase_flags&BIT_RD) || iq->chase_to_rd),
2052 		iq->response, &iq->qchase, iq->dp);
2053 	iq->chase_to_rd = 0;
2054 	if(type == RESPONSE_TYPE_REFERRAL && (iq->chase_flags&BIT_RD)) {
2055 		/* When forwarding (RD bit is set), we handle referrals
2056 		 * differently. No queries should be sent elsewhere */
2057 		type = RESPONSE_TYPE_ANSWER;
2058 	}
2059 	if(iq->dnssec_expected && !iq->dnssec_lame_query &&
2060 		!(iq->chase_flags&BIT_RD)
2061 		&& type != RESPONSE_TYPE_LAME
2062 		&& type != RESPONSE_TYPE_REC_LAME
2063 		&& type != RESPONSE_TYPE_THROWAWAY
2064 		&& type != RESPONSE_TYPE_UNTYPED) {
2065 		/* a possible answer, see if it is missing DNSSEC */
2066 		/* but not when forwarding, so we dont mark fwder lame */
2067 		if(!iter_msg_has_dnssec(iq->response)) {
2068 			/* Mark this address as dnsseclame in this dp,
2069 			 * because that will make serverselection disprefer
2070 			 * it, but also, once it is the only final option,
2071 			 * use dnssec-lame-bypass if it needs to query there.*/
2072 			if(qstate->reply) {
2073 				struct delegpt_addr* a = delegpt_find_addr(
2074 					iq->dp, &qstate->reply->addr,
2075 					qstate->reply->addrlen);
2076 				if(a) a->dnsseclame = 1;
2077 			}
2078 			/* test the answer is from the zone we expected,
2079 		 	 * otherwise, (due to parent,child on same server), we
2080 		 	 * might mark the server,zone lame inappropriately */
2081 			if(!iter_msg_from_zone(iq->response, iq->dp, type,
2082 				iq->qchase.qclass))
2083 				qstate->reply = NULL;
2084 			type = RESPONSE_TYPE_LAME;
2085 			dnsseclame = 1;
2086 		}
2087 	} else iq->dnssec_lame_query = 0;
2088 	/* see if referral brings us close to the target */
2089 	if(type == RESPONSE_TYPE_REFERRAL) {
2090 		struct ub_packed_rrset_key* ns = find_NS(
2091 			iq->response->rep, iq->response->rep->an_numrrsets,
2092 			iq->response->rep->an_numrrsets
2093 			+ iq->response->rep->ns_numrrsets);
2094 		if(!ns) ns = find_NS(iq->response->rep, 0,
2095 				iq->response->rep->an_numrrsets);
2096 		if(!ns || !dname_strict_subdomain_c(ns->rk.dname, iq->dp->name)
2097 			|| !dname_subdomain_c(iq->qchase.qname, ns->rk.dname)){
2098 			verbose(VERB_ALGO, "bad referral, throwaway");
2099 			type = RESPONSE_TYPE_THROWAWAY;
2100 		} else
2101 			iter_scrub_ds(iq->response, ns, iq->dp->name);
2102 	} else iter_scrub_ds(iq->response, NULL, NULL);
2103 
2104 	/* handle each of the type cases */
2105 	if(type == RESPONSE_TYPE_ANSWER) {
2106 		/* ANSWER type responses terminate the query algorithm,
2107 		 * so they sent on their */
2108 		if(verbosity >= VERB_DETAIL) {
2109 			verbose(VERB_DETAIL, "query response was %s",
2110 				FLAGS_GET_RCODE(iq->response->rep->flags)
2111 				==LDNS_RCODE_NXDOMAIN?"NXDOMAIN ANSWER":
2112 				(iq->response->rep->an_numrrsets?"ANSWER":
2113 				"nodata ANSWER"));
2114 		}
2115 		/* if qtype is DS, check we have the right level of answer,
2116 		 * like grandchild answer but we need the middle, reject it */
2117 		if(iq->qchase.qtype == LDNS_RR_TYPE_DS && !iq->dsns_point
2118 			&& !(iq->chase_flags&BIT_RD)
2119 			&& iter_ds_toolow(iq->response, iq->dp)
2120 			&& iter_dp_cangodown(&iq->qchase, iq->dp)) {
2121 			/* close down outstanding requests to be discarded */
2122 			outbound_list_clear(&iq->outlist);
2123 			iq->num_current_queries = 0;
2124 			fptr_ok(fptr_whitelist_modenv_detach_subs(
2125 				qstate->env->detach_subs));
2126 			(*qstate->env->detach_subs)(qstate);
2127 			iq->num_target_queries = 0;
2128 			return processDSNSFind(qstate, iq, id);
2129 		}
2130 		iter_dns_store(qstate->env, &iq->response->qinfo,
2131 			iq->response->rep, 0, qstate->prefetch_leeway,
2132 			iq->dp&&iq->dp->has_parent_side_NS,
2133 			qstate->region, qstate->query_flags);
2134 		/* close down outstanding requests to be discarded */
2135 		outbound_list_clear(&iq->outlist);
2136 		iq->num_current_queries = 0;
2137 		fptr_ok(fptr_whitelist_modenv_detach_subs(
2138 			qstate->env->detach_subs));
2139 		(*qstate->env->detach_subs)(qstate);
2140 		iq->num_target_queries = 0;
2141 		if(qstate->reply)
2142 			sock_list_insert(&qstate->reply_origin,
2143 				&qstate->reply->addr, qstate->reply->addrlen,
2144 				qstate->region);
2145 		return final_state(iq);
2146 	} else if(type == RESPONSE_TYPE_REFERRAL) {
2147 		/* REFERRAL type responses get a reset of the
2148 		 * delegation point, and back to the QUERYTARGETS_STATE. */
2149 		verbose(VERB_DETAIL, "query response was REFERRAL");
2150 
2151 		if(!(iq->chase_flags & BIT_RD) && !iq->ratelimit_ok) {
2152 			/* we have a referral, no ratelimit, we can send
2153 			 * our queries to the given name */
2154 			infra_ratelimit_dec(qstate->env->infra_cache,
2155 				iq->dp->name, iq->dp->namelen,
2156 				*qstate->env->now);
2157 		}
2158 
2159 		/* if hardened, only store referral if we asked for it */
2160 		if(!qstate->env->cfg->harden_referral_path ||
2161 		    (  qstate->qinfo.qtype == LDNS_RR_TYPE_NS
2162 			&& (qstate->query_flags&BIT_RD)
2163 			&& !(qstate->query_flags&BIT_CD)
2164 			   /* we know that all other NS rrsets are scrubbed
2165 			    * away, thus on referral only one is left.
2166 			    * see if that equals the query name... */
2167 			&& ( /* auth section, but sometimes in answer section*/
2168 			  reply_find_rrset_section_ns(iq->response->rep,
2169 				iq->qchase.qname, iq->qchase.qname_len,
2170 				LDNS_RR_TYPE_NS, iq->qchase.qclass)
2171 			  || reply_find_rrset_section_an(iq->response->rep,
2172 				iq->qchase.qname, iq->qchase.qname_len,
2173 				LDNS_RR_TYPE_NS, iq->qchase.qclass)
2174 			  )
2175 		    )) {
2176 			/* Store the referral under the current query */
2177 			/* no prefetch-leeway, since its not the answer */
2178 			iter_dns_store(qstate->env, &iq->response->qinfo,
2179 				iq->response->rep, 1, 0, 0, NULL, 0);
2180 			if(iq->store_parent_NS)
2181 				iter_store_parentside_NS(qstate->env,
2182 					iq->response->rep);
2183 			if(qstate->env->neg_cache)
2184 				val_neg_addreferral(qstate->env->neg_cache,
2185 					iq->response->rep, iq->dp->name);
2186 		}
2187 		/* store parent-side-in-zone-glue, if directly queried for */
2188 		if(iq->query_for_pside_glue && !iq->pside_glue) {
2189 			iq->pside_glue = reply_find_rrset(iq->response->rep,
2190 				iq->qchase.qname, iq->qchase.qname_len,
2191 				iq->qchase.qtype, iq->qchase.qclass);
2192 			if(iq->pside_glue) {
2193 				log_rrset_key(VERB_ALGO, "found parent-side "
2194 					"glue", iq->pside_glue);
2195 				iter_store_parentside_rrset(qstate->env,
2196 					iq->pside_glue);
2197 			}
2198 		}
2199 
2200 		/* Reset the event state, setting the current delegation
2201 		 * point to the referral. */
2202 		iq->deleg_msg = iq->response;
2203 		iq->dp = delegpt_from_message(iq->response, qstate->region);
2204 		if(!iq->dp)
2205 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2206 		if(!cache_fill_missing(qstate->env, iq->qchase.qclass,
2207 			qstate->region, iq->dp))
2208 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2209 		if(iq->store_parent_NS && query_dname_compare(iq->dp->name,
2210 			iq->store_parent_NS->name) == 0)
2211 			iter_merge_retry_counts(iq->dp, iq->store_parent_NS);
2212 		delegpt_log(VERB_ALGO, iq->dp);
2213 		/* Count this as a referral. */
2214 		iq->referral_count++;
2215 		iq->sent_count = 0;
2216 		/* see if the next dp is a trust anchor, or a DS was sent
2217 		 * along, indicating dnssec is expected for next zone */
2218 		iq->dnssec_expected = iter_indicates_dnssec(qstate->env,
2219 			iq->dp, iq->response, iq->qchase.qclass);
2220 		/* if dnssec, validating then also fetch the key for the DS */
2221 		if(iq->dnssec_expected && qstate->env->cfg->prefetch_key &&
2222 			!(qstate->query_flags&BIT_CD))
2223 			generate_dnskey_prefetch(qstate, iq, id);
2224 
2225 		/* spawn off NS and addr to auth servers for the NS we just
2226 		 * got in the referral. This gets authoritative answer
2227 		 * (answer section trust level) rrset.
2228 		 * right after, we detach the subs, answer goes to cache. */
2229 		if(qstate->env->cfg->harden_referral_path)
2230 			generate_ns_check(qstate, iq, id);
2231 
2232 		/* stop current outstanding queries.
2233 		 * FIXME: should the outstanding queries be waited for and
2234 		 * handled? Say by a subquery that inherits the outbound_entry.
2235 		 */
2236 		outbound_list_clear(&iq->outlist);
2237 		iq->num_current_queries = 0;
2238 		fptr_ok(fptr_whitelist_modenv_detach_subs(
2239 			qstate->env->detach_subs));
2240 		(*qstate->env->detach_subs)(qstate);
2241 		iq->num_target_queries = 0;
2242 		verbose(VERB_ALGO, "cleared outbound list for next round");
2243 		return next_state(iq, QUERYTARGETS_STATE);
2244 	} else if(type == RESPONSE_TYPE_CNAME) {
2245 		uint8_t* sname = NULL;
2246 		size_t snamelen = 0;
2247 		/* CNAME type responses get a query restart (i.e., get a
2248 		 * reset of the query state and go back to INIT_REQUEST_STATE).
2249 		 */
2250 		verbose(VERB_DETAIL, "query response was CNAME");
2251 		if(verbosity >= VERB_ALGO)
2252 			log_dns_msg("cname msg", &iq->response->qinfo,
2253 				iq->response->rep);
2254 		/* if qtype is DS, check we have the right level of answer,
2255 		 * like grandchild answer but we need the middle, reject it */
2256 		if(iq->qchase.qtype == LDNS_RR_TYPE_DS && !iq->dsns_point
2257 			&& !(iq->chase_flags&BIT_RD)
2258 			&& iter_ds_toolow(iq->response, iq->dp)
2259 			&& iter_dp_cangodown(&iq->qchase, iq->dp)) {
2260 			outbound_list_clear(&iq->outlist);
2261 			iq->num_current_queries = 0;
2262 			fptr_ok(fptr_whitelist_modenv_detach_subs(
2263 				qstate->env->detach_subs));
2264 			(*qstate->env->detach_subs)(qstate);
2265 			iq->num_target_queries = 0;
2266 			return processDSNSFind(qstate, iq, id);
2267 		}
2268 		/* Process the CNAME response. */
2269 		if(!handle_cname_response(qstate, iq, iq->response,
2270 			&sname, &snamelen))
2271 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2272 		/* cache the CNAME response under the current query */
2273 		/* NOTE : set referral=1, so that rrsets get stored but not
2274 		 * the partial query answer (CNAME only). */
2275 		/* prefetchleeway applied because this updates answer parts */
2276 		iter_dns_store(qstate->env, &iq->response->qinfo,
2277 			iq->response->rep, 1, qstate->prefetch_leeway,
2278 			iq->dp&&iq->dp->has_parent_side_NS, NULL,
2279 			qstate->query_flags);
2280 		/* set the current request's qname to the new value. */
2281 		iq->qchase.qname = sname;
2282 		iq->qchase.qname_len = snamelen;
2283 		/* Clear the query state, since this is a query restart. */
2284 		iq->deleg_msg = NULL;
2285 		iq->dp = NULL;
2286 		iq->dsns_point = NULL;
2287 		/* Note the query restart. */
2288 		iq->query_restart_count++;
2289 		iq->sent_count = 0;
2290 
2291 		/* stop current outstanding queries.
2292 		 * FIXME: should the outstanding queries be waited for and
2293 		 * handled? Say by a subquery that inherits the outbound_entry.
2294 		 */
2295 		outbound_list_clear(&iq->outlist);
2296 		iq->num_current_queries = 0;
2297 		fptr_ok(fptr_whitelist_modenv_detach_subs(
2298 			qstate->env->detach_subs));
2299 		(*qstate->env->detach_subs)(qstate);
2300 		iq->num_target_queries = 0;
2301 		if(qstate->reply)
2302 			sock_list_insert(&qstate->reply_origin,
2303 				&qstate->reply->addr, qstate->reply->addrlen,
2304 				qstate->region);
2305 		verbose(VERB_ALGO, "cleared outbound list for query restart");
2306 		/* go to INIT_REQUEST_STATE for new qname. */
2307 		return next_state(iq, INIT_REQUEST_STATE);
2308 	} else if(type == RESPONSE_TYPE_LAME) {
2309 		/* Cache the LAMEness. */
2310 		verbose(VERB_DETAIL, "query response was %sLAME",
2311 			dnsseclame?"DNSSEC ":"");
2312 		if(!dname_subdomain_c(iq->qchase.qname, iq->dp->name)) {
2313 			log_err("mark lame: mismatch in qname and dpname");
2314 			/* throwaway this reply below */
2315 		} else if(qstate->reply) {
2316 			/* need addr for lameness cache, but we may have
2317 			 * gotten this from cache, so test to be sure */
2318 			if(!infra_set_lame(qstate->env->infra_cache,
2319 				&qstate->reply->addr, qstate->reply->addrlen,
2320 				iq->dp->name, iq->dp->namelen,
2321 				*qstate->env->now, dnsseclame, 0,
2322 				iq->qchase.qtype))
2323 				log_err("mark host lame: out of memory");
2324 		}
2325 	} else if(type == RESPONSE_TYPE_REC_LAME) {
2326 		/* Cache the LAMEness. */
2327 		verbose(VERB_DETAIL, "query response REC_LAME: "
2328 			"recursive but not authoritative server");
2329 		if(!dname_subdomain_c(iq->qchase.qname, iq->dp->name)) {
2330 			log_err("mark rec_lame: mismatch in qname and dpname");
2331 			/* throwaway this reply below */
2332 		} else if(qstate->reply) {
2333 			/* need addr for lameness cache, but we may have
2334 			 * gotten this from cache, so test to be sure */
2335 			verbose(VERB_DETAIL, "mark as REC_LAME");
2336 			if(!infra_set_lame(qstate->env->infra_cache,
2337 				&qstate->reply->addr, qstate->reply->addrlen,
2338 				iq->dp->name, iq->dp->namelen,
2339 				*qstate->env->now, 0, 1, iq->qchase.qtype))
2340 				log_err("mark host lame: out of memory");
2341 		}
2342 	} else if(type == RESPONSE_TYPE_THROWAWAY) {
2343 		/* LAME and THROWAWAY responses are handled the same way.
2344 		 * In this case, the event is just sent directly back to
2345 		 * the QUERYTARGETS_STATE without resetting anything,
2346 		 * because, clearly, the next target must be tried. */
2347 		verbose(VERB_DETAIL, "query response was THROWAWAY");
2348 	} else {
2349 		log_warn("A query response came back with an unknown type: %d",
2350 			(int)type);
2351 	}
2352 
2353 	/* LAME, THROWAWAY and "unknown" all end up here.
2354 	 * Recycle to the QUERYTARGETS state to hopefully try a
2355 	 * different target. */
2356 	return next_state(iq, QUERYTARGETS_STATE);
2357 }
2358 
2359 /**
2360  * Return priming query results to interested super querystates.
2361  *
2362  * Sets the delegation point and delegation message (not nonRD queries).
2363  * This is a callback from walk_supers.
2364  *
2365  * @param qstate: priming query state that finished.
2366  * @param id: module id.
2367  * @param forq: the qstate for which priming has been done.
2368  */
2369 static void
2370 prime_supers(struct module_qstate* qstate, int id, struct module_qstate* forq)
2371 {
2372 	struct iter_qstate* foriq = (struct iter_qstate*)forq->minfo[id];
2373 	struct delegpt* dp = NULL;
2374 
2375 	log_assert(qstate->is_priming || foriq->wait_priming_stub);
2376 	log_assert(qstate->return_rcode == LDNS_RCODE_NOERROR);
2377 	/* Convert our response to a delegation point */
2378 	dp = delegpt_from_message(qstate->return_msg, forq->region);
2379 	if(!dp) {
2380 		/* if there is no convertable delegation point, then
2381 		 * the ANSWER type was (presumably) a negative answer. */
2382 		verbose(VERB_ALGO, "prime response was not a positive "
2383 			"ANSWER; failing");
2384 		foriq->dp = NULL;
2385 		foriq->state = QUERYTARGETS_STATE;
2386 		return;
2387 	}
2388 
2389 	log_query_info(VERB_DETAIL, "priming successful for", &qstate->qinfo);
2390 	delegpt_log(VERB_ALGO, dp);
2391 	foriq->dp = dp;
2392 	foriq->deleg_msg = dns_copy_msg(qstate->return_msg, forq->region);
2393 	if(!foriq->deleg_msg) {
2394 		log_err("copy prime response: out of memory");
2395 		foriq->dp = NULL;
2396 		foriq->state = QUERYTARGETS_STATE;
2397 		return;
2398 	}
2399 
2400 	/* root priming responses go to init stage 2, priming stub
2401 	 * responses to to stage 3. */
2402 	if(foriq->wait_priming_stub) {
2403 		foriq->state = INIT_REQUEST_3_STATE;
2404 		foriq->wait_priming_stub = 0;
2405 	} else	foriq->state = INIT_REQUEST_2_STATE;
2406 	/* because we are finished, the parent will be reactivated */
2407 }
2408 
2409 /**
2410  * This handles the response to a priming query. This is used to handle both
2411  * root and stub priming responses. This is basically the equivalent of the
2412  * QUERY_RESP_STATE, but will not handle CNAME responses and will treat
2413  * REFERRALs as ANSWERS. It will also update and reactivate the originating
2414  * event.
2415  *
2416  * @param qstate: query state.
2417  * @param id: module id.
2418  * @return true if the event needs more immediate processing, false if not.
2419  *         This state always returns false.
2420  */
2421 static int
2422 processPrimeResponse(struct module_qstate* qstate, int id)
2423 {
2424 	struct iter_qstate* iq = (struct iter_qstate*)qstate->minfo[id];
2425 	enum response_type type;
2426 	iq->response->rep->flags &= ~(BIT_RD|BIT_RA); /* ignore rec-lame */
2427 	type = response_type_from_server(
2428 		(int)((iq->chase_flags&BIT_RD) || iq->chase_to_rd),
2429 		iq->response, &iq->qchase, iq->dp);
2430 	if(type == RESPONSE_TYPE_ANSWER) {
2431 		qstate->return_rcode = LDNS_RCODE_NOERROR;
2432 		qstate->return_msg = iq->response;
2433 	} else {
2434 		qstate->return_rcode = LDNS_RCODE_SERVFAIL;
2435 		qstate->return_msg = NULL;
2436 	}
2437 
2438 	/* validate the root or stub after priming (if enabled).
2439 	 * This is the same query as the prime query, but with validation.
2440 	 * Now that we are primed, the additional queries that validation
2441 	 * may need can be resolved, such as DLV. */
2442 	if(qstate->env->cfg->harden_referral_path) {
2443 		struct module_qstate* subq = NULL;
2444 		log_nametypeclass(VERB_ALGO, "schedule prime validation",
2445 			qstate->qinfo.qname, qstate->qinfo.qtype,
2446 			qstate->qinfo.qclass);
2447 		if(!generate_sub_request(qstate->qinfo.qname,
2448 			qstate->qinfo.qname_len, qstate->qinfo.qtype,
2449 			qstate->qinfo.qclass, qstate, id, iq,
2450 			INIT_REQUEST_STATE, FINISHED_STATE, &subq, 1)) {
2451 			verbose(VERB_ALGO, "could not generate prime check");
2452 		}
2453 		generate_a_aaaa_check(qstate, iq, id);
2454 	}
2455 
2456 	/* This event is finished. */
2457 	qstate->ext_state[id] = module_finished;
2458 	return 0;
2459 }
2460 
2461 /**
2462  * Do final processing on responses to target queries. Events reach this
2463  * state after the iterative resolution algorithm terminates. This state is
2464  * responsible for reactiving the original event, and housekeeping related
2465  * to received target responses (caching, updating the current delegation
2466  * point, etc).
2467  * Callback from walk_supers for every super state that is interested in
2468  * the results from this query.
2469  *
2470  * @param qstate: query state.
2471  * @param id: module id.
2472  * @param forq: super query state.
2473  */
2474 static void
2475 processTargetResponse(struct module_qstate* qstate, int id,
2476 	struct module_qstate* forq)
2477 {
2478 	struct iter_qstate* iq = (struct iter_qstate*)qstate->minfo[id];
2479 	struct iter_qstate* foriq = (struct iter_qstate*)forq->minfo[id];
2480 	struct ub_packed_rrset_key* rrset;
2481 	struct delegpt_ns* dpns;
2482 	log_assert(qstate->return_rcode == LDNS_RCODE_NOERROR);
2483 
2484 	foriq->state = QUERYTARGETS_STATE;
2485 	log_query_info(VERB_ALGO, "processTargetResponse", &qstate->qinfo);
2486 	log_query_info(VERB_ALGO, "processTargetResponse super", &forq->qinfo);
2487 
2488 	/* check to see if parent event is still interested (in orig name).  */
2489 	if(!foriq->dp) {
2490 		verbose(VERB_ALGO, "subq: parent not interested, was reset");
2491 		return; /* not interested anymore */
2492 	}
2493 	dpns = delegpt_find_ns(foriq->dp, qstate->qinfo.qname,
2494 			qstate->qinfo.qname_len);
2495 	if(!dpns) {
2496 		/* If not interested, just stop processing this event */
2497 		verbose(VERB_ALGO, "subq: parent not interested anymore");
2498 		/* could be because parent was jostled out of the cache,
2499 		   and a new identical query arrived, that does not want it*/
2500 		return;
2501 	}
2502 
2503 	/* Tell the originating event that this target query has finished
2504 	 * (regardless if it succeeded or not). */
2505 	foriq->num_target_queries--;
2506 
2507 	/* if iq->query_for_pside_glue then add the pside_glue (marked lame) */
2508 	if(iq->pside_glue) {
2509 		/* if the pside_glue is NULL, then it could not be found,
2510 		 * the done_pside is already set when created and a cache
2511 		 * entry created in processFinished so nothing to do here */
2512 		log_rrset_key(VERB_ALGO, "add parentside glue to dp",
2513 			iq->pside_glue);
2514 		if(!delegpt_add_rrset(foriq->dp, forq->region,
2515 			iq->pside_glue, 1))
2516 			log_err("out of memory adding pside glue");
2517 	}
2518 
2519 	/* This response is relevant to the current query, so we
2520 	 * add (attempt to add, anyway) this target(s) and reactivate
2521 	 * the original event.
2522 	 * NOTE: we could only look for the AnswerRRset if the
2523 	 * response type was ANSWER. */
2524 	rrset = reply_find_answer_rrset(&iq->qchase, qstate->return_msg->rep);
2525 	if(rrset) {
2526 		/* if CNAMEs have been followed - add new NS to delegpt. */
2527 		/* BTW. RFC 1918 says NS should not have got CNAMEs. Robust. */
2528 		if(!delegpt_find_ns(foriq->dp, rrset->rk.dname,
2529 			rrset->rk.dname_len)) {
2530 			/* if dpns->lame then set newcname ns lame too */
2531 			if(!delegpt_add_ns(foriq->dp, forq->region,
2532 				rrset->rk.dname, dpns->lame))
2533 				log_err("out of memory adding cnamed-ns");
2534 		}
2535 		/* if dpns->lame then set the address(es) lame too */
2536 		if(!delegpt_add_rrset(foriq->dp, forq->region, rrset,
2537 			dpns->lame))
2538 			log_err("out of memory adding targets");
2539 		verbose(VERB_ALGO, "added target response");
2540 		delegpt_log(VERB_ALGO, foriq->dp);
2541 	} else {
2542 		verbose(VERB_ALGO, "iterator TargetResponse failed");
2543 		dpns->resolved = 1; /* fail the target */
2544 	}
2545 }
2546 
2547 /**
2548  * Process response for DS NS Find queries, that attempt to find the delegation
2549  * point where we ask the DS query from.
2550  *
2551  * @param qstate: query state.
2552  * @param id: module id.
2553  * @param forq: super query state.
2554  */
2555 static void
2556 processDSNSResponse(struct module_qstate* qstate, int id,
2557 	struct module_qstate* forq)
2558 {
2559 	struct iter_qstate* foriq = (struct iter_qstate*)forq->minfo[id];
2560 
2561 	/* if the finished (iq->response) query has no NS set: continue
2562 	 * up to look for the right dp; nothing to change, do DPNSstate */
2563 	if(qstate->return_rcode != LDNS_RCODE_NOERROR)
2564 		return; /* seek further */
2565 	/* find the NS RRset (without allowing CNAMEs) */
2566 	if(!reply_find_rrset(qstate->return_msg->rep, qstate->qinfo.qname,
2567 		qstate->qinfo.qname_len, LDNS_RR_TYPE_NS,
2568 		qstate->qinfo.qclass)){
2569 		return; /* seek further */
2570 	}
2571 
2572 	/* else, store as DP and continue at querytargets */
2573 	foriq->state = QUERYTARGETS_STATE;
2574 	foriq->dp = delegpt_from_message(qstate->return_msg, forq->region);
2575 	if(!foriq->dp) {
2576 		log_err("out of memory in dsns dp alloc");
2577 		return; /* dp==NULL in QUERYTARGETS makes SERVFAIL */
2578 	}
2579 	/* success, go query the querytargets in the new dp (and go down) */
2580 }
2581 
2582 /**
2583  * Process response for qclass=ANY queries for a particular class.
2584  * Append to result or error-exit.
2585  *
2586  * @param qstate: query state.
2587  * @param id: module id.
2588  * @param forq: super query state.
2589  */
2590 static void
2591 processClassResponse(struct module_qstate* qstate, int id,
2592 	struct module_qstate* forq)
2593 {
2594 	struct iter_qstate* foriq = (struct iter_qstate*)forq->minfo[id];
2595 	struct dns_msg* from = qstate->return_msg;
2596 	log_query_info(VERB_ALGO, "processClassResponse", &qstate->qinfo);
2597 	log_query_info(VERB_ALGO, "processClassResponse super", &forq->qinfo);
2598 	if(qstate->return_rcode != LDNS_RCODE_NOERROR) {
2599 		/* cause servfail for qclass ANY query */
2600 		foriq->response = NULL;
2601 		foriq->state = FINISHED_STATE;
2602 		return;
2603 	}
2604 	/* append result */
2605 	if(!foriq->response) {
2606 		/* allocate the response: copy RCODE, sec_state */
2607 		foriq->response = dns_copy_msg(from, forq->region);
2608 		if(!foriq->response) {
2609 			log_err("malloc failed for qclass ANY response");
2610 			foriq->state = FINISHED_STATE;
2611 			return;
2612 		}
2613 		foriq->response->qinfo.qclass = forq->qinfo.qclass;
2614 		/* qclass ANY does not receive the AA flag on replies */
2615 		foriq->response->rep->authoritative = 0;
2616 	} else {
2617 		struct dns_msg* to = foriq->response;
2618 		/* add _from_ this response _to_ existing collection */
2619 		/* if there are records, copy RCODE */
2620 		/* lower sec_state if this message is lower */
2621 		if(from->rep->rrset_count != 0) {
2622 			size_t n = from->rep->rrset_count+to->rep->rrset_count;
2623 			struct ub_packed_rrset_key** dest, **d;
2624 			/* copy appropriate rcode */
2625 			to->rep->flags = from->rep->flags;
2626 			/* copy rrsets */
2627 			if(from->rep->rrset_count > RR_COUNT_MAX ||
2628 				to->rep->rrset_count > RR_COUNT_MAX) {
2629 				log_err("malloc failed (too many rrsets) in collect ANY");
2630 				foriq->state = FINISHED_STATE;
2631 				return; /* integer overflow protection */
2632 			}
2633 			dest = regional_alloc(forq->region, sizeof(dest[0])*n);
2634 			if(!dest) {
2635 				log_err("malloc failed in collect ANY");
2636 				foriq->state = FINISHED_STATE;
2637 				return;
2638 			}
2639 			d = dest;
2640 			/* copy AN */
2641 			memcpy(dest, to->rep->rrsets, to->rep->an_numrrsets
2642 				* sizeof(dest[0]));
2643 			dest += to->rep->an_numrrsets;
2644 			memcpy(dest, from->rep->rrsets, from->rep->an_numrrsets
2645 				* sizeof(dest[0]));
2646 			dest += from->rep->an_numrrsets;
2647 			/* copy NS */
2648 			memcpy(dest, to->rep->rrsets+to->rep->an_numrrsets,
2649 				to->rep->ns_numrrsets * sizeof(dest[0]));
2650 			dest += to->rep->ns_numrrsets;
2651 			memcpy(dest, from->rep->rrsets+from->rep->an_numrrsets,
2652 				from->rep->ns_numrrsets * sizeof(dest[0]));
2653 			dest += from->rep->ns_numrrsets;
2654 			/* copy AR */
2655 			memcpy(dest, to->rep->rrsets+to->rep->an_numrrsets+
2656 				to->rep->ns_numrrsets,
2657 				to->rep->ar_numrrsets * sizeof(dest[0]));
2658 			dest += to->rep->ar_numrrsets;
2659 			memcpy(dest, from->rep->rrsets+from->rep->an_numrrsets+
2660 				from->rep->ns_numrrsets,
2661 				from->rep->ar_numrrsets * sizeof(dest[0]));
2662 			/* update counts */
2663 			to->rep->rrsets = d;
2664 			to->rep->an_numrrsets += from->rep->an_numrrsets;
2665 			to->rep->ns_numrrsets += from->rep->ns_numrrsets;
2666 			to->rep->ar_numrrsets += from->rep->ar_numrrsets;
2667 			to->rep->rrset_count = n;
2668 		}
2669 		if(from->rep->security < to->rep->security) /* lowest sec */
2670 			to->rep->security = from->rep->security;
2671 		if(from->rep->qdcount != 0) /* insert qd if appropriate */
2672 			to->rep->qdcount = from->rep->qdcount;
2673 		if(from->rep->ttl < to->rep->ttl) /* use smallest TTL */
2674 			to->rep->ttl = from->rep->ttl;
2675 		if(from->rep->prefetch_ttl < to->rep->prefetch_ttl)
2676 			to->rep->prefetch_ttl = from->rep->prefetch_ttl;
2677 	}
2678 	/* are we done? */
2679 	foriq->num_current_queries --;
2680 	if(foriq->num_current_queries == 0)
2681 		foriq->state = FINISHED_STATE;
2682 }
2683 
2684 /**
2685  * Collect class ANY responses and make them into one response.  This
2686  * state is started and it creates queries for all classes (that have
2687  * root hints).  The answers are then collected.
2688  *
2689  * @param qstate: query state.
2690  * @param id: module id.
2691  * @return true if the event needs more immediate processing, false if not.
2692  */
2693 static int
2694 processCollectClass(struct module_qstate* qstate, int id)
2695 {
2696 	struct iter_qstate* iq = (struct iter_qstate*)qstate->minfo[id];
2697 	struct module_qstate* subq;
2698 	/* If qchase.qclass == 0 then send out queries for all classes.
2699 	 * Otherwise, do nothing (wait for all answers to arrive and the
2700 	 * processClassResponse to put them together, and that moves us
2701 	 * towards the Finished state when done. */
2702 	if(iq->qchase.qclass == 0) {
2703 		uint16_t c = 0;
2704 		iq->qchase.qclass = LDNS_RR_CLASS_ANY;
2705 		while(iter_get_next_root(qstate->env->hints,
2706 			qstate->env->fwds, &c)) {
2707 			/* generate query for this class */
2708 			log_nametypeclass(VERB_ALGO, "spawn collect query",
2709 				qstate->qinfo.qname, qstate->qinfo.qtype, c);
2710 			if(!generate_sub_request(qstate->qinfo.qname,
2711 				qstate->qinfo.qname_len, qstate->qinfo.qtype,
2712 				c, qstate, id, iq, INIT_REQUEST_STATE,
2713 				FINISHED_STATE, &subq,
2714 				(int)!(qstate->query_flags&BIT_CD))) {
2715 				return error_response(qstate, id,
2716 					LDNS_RCODE_SERVFAIL);
2717 			}
2718 			/* ignore subq, no special init required */
2719 			iq->num_current_queries ++;
2720 			if(c == 0xffff)
2721 				break;
2722 			else c++;
2723 		}
2724 		/* if no roots are configured at all, return */
2725 		if(iq->num_current_queries == 0) {
2726 			verbose(VERB_ALGO, "No root hints or fwds, giving up "
2727 				"on qclass ANY");
2728 			return error_response(qstate, id, LDNS_RCODE_REFUSED);
2729 		}
2730 		/* return false, wait for queries to return */
2731 	}
2732 	/* if woke up here because of an answer, wait for more answers */
2733 	return 0;
2734 }
2735 
2736 /**
2737  * This handles the final state for first-tier responses (i.e., responses to
2738  * externally generated queries).
2739  *
2740  * @param qstate: query state.
2741  * @param iq: iterator query state.
2742  * @param id: module id.
2743  * @return true if the event needs more processing, false if not. Since this
2744  *         is the final state for an event, it always returns false.
2745  */
2746 static int
2747 processFinished(struct module_qstate* qstate, struct iter_qstate* iq,
2748 	int id)
2749 {
2750 	log_query_info(VERB_QUERY, "finishing processing for",
2751 		&qstate->qinfo);
2752 
2753 	/* store negative cache element for parent side glue. */
2754 	if(iq->query_for_pside_glue && !iq->pside_glue)
2755 		iter_store_parentside_neg(qstate->env, &qstate->qinfo,
2756 			iq->deleg_msg?iq->deleg_msg->rep:
2757 			(iq->response?iq->response->rep:NULL));
2758 	if(!iq->response) {
2759 		verbose(VERB_ALGO, "No response is set, servfail");
2760 		return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2761 	}
2762 
2763 	/* Make sure that the RA flag is set (since the presence of
2764 	 * this module means that recursion is available) */
2765 	iq->response->rep->flags |= BIT_RA;
2766 
2767 	/* Clear the AA flag */
2768 	/* FIXME: does this action go here or in some other module? */
2769 	iq->response->rep->flags &= ~BIT_AA;
2770 
2771 	/* make sure QR flag is on */
2772 	iq->response->rep->flags |= BIT_QR;
2773 
2774 	/* we have finished processing this query */
2775 	qstate->ext_state[id] = module_finished;
2776 
2777 	/* TODO:  we are using a private TTL, trim the response. */
2778 	/* if (mPrivateTTL > 0){IterUtils.setPrivateTTL(resp, mPrivateTTL); } */
2779 
2780 	/* prepend any items we have accumulated */
2781 	if(iq->an_prepend_list || iq->ns_prepend_list) {
2782 		if(!iter_prepend(iq, iq->response, qstate->region)) {
2783 			log_err("prepend rrsets: out of memory");
2784 			return error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2785 		}
2786 		/* reset the query name back */
2787 		iq->response->qinfo = qstate->qinfo;
2788 		/* the security state depends on the combination */
2789 		iq->response->rep->security = sec_status_unchecked;
2790 		/* store message with the finished prepended items,
2791 		 * but only if we did recursion. The nonrecursion referral
2792 		 * from cache does not need to be stored in the msg cache. */
2793 		if(qstate->query_flags&BIT_RD) {
2794 			iter_dns_store(qstate->env, &qstate->qinfo,
2795 				iq->response->rep, 0, qstate->prefetch_leeway,
2796 				iq->dp&&iq->dp->has_parent_side_NS,
2797 				qstate->region, qstate->query_flags);
2798 		}
2799 	}
2800 	qstate->return_rcode = LDNS_RCODE_NOERROR;
2801 	qstate->return_msg = iq->response;
2802 	return 0;
2803 }
2804 
2805 /*
2806  * Return priming query results to interestes super querystates.
2807  *
2808  * Sets the delegation point and delegation message (not nonRD queries).
2809  * This is a callback from walk_supers.
2810  *
2811  * @param qstate: query state that finished.
2812  * @param id: module id.
2813  * @param super: the qstate to inform.
2814  */
2815 void
2816 iter_inform_super(struct module_qstate* qstate, int id,
2817 	struct module_qstate* super)
2818 {
2819 	if(!qstate->is_priming && super->qinfo.qclass == LDNS_RR_CLASS_ANY)
2820 		processClassResponse(qstate, id, super);
2821 	else if(super->qinfo.qtype == LDNS_RR_TYPE_DS && ((struct iter_qstate*)
2822 		super->minfo[id])->state == DSNS_FIND_STATE)
2823 		processDSNSResponse(qstate, id, super);
2824 	else if(qstate->return_rcode != LDNS_RCODE_NOERROR)
2825 		error_supers(qstate, id, super);
2826 	else if(qstate->is_priming)
2827 		prime_supers(qstate, id, super);
2828 	else	processTargetResponse(qstate, id, super);
2829 }
2830 
2831 /**
2832  * Handle iterator state.
2833  * Handle events. This is the real processing loop for events, responsible
2834  * for moving events through the various states. If a processing method
2835  * returns true, then it will be advanced to the next state. If false, then
2836  * processing will stop.
2837  *
2838  * @param qstate: query state.
2839  * @param ie: iterator shared global environment.
2840  * @param iq: iterator query state.
2841  * @param id: module id.
2842  */
2843 static void
2844 iter_handle(struct module_qstate* qstate, struct iter_qstate* iq,
2845 	struct iter_env* ie, int id)
2846 {
2847 	int cont = 1;
2848 	while(cont) {
2849 		verbose(VERB_ALGO, "iter_handle processing q with state %s",
2850 			iter_state_to_string(iq->state));
2851 		switch(iq->state) {
2852 			case INIT_REQUEST_STATE:
2853 				cont = processInitRequest(qstate, iq, ie, id);
2854 				break;
2855 			case INIT_REQUEST_2_STATE:
2856 				cont = processInitRequest2(qstate, iq, id);
2857 				break;
2858 			case INIT_REQUEST_3_STATE:
2859 				cont = processInitRequest3(qstate, iq, id);
2860 				break;
2861 			case QUERYTARGETS_STATE:
2862 				cont = processQueryTargets(qstate, iq, ie, id);
2863 				break;
2864 			case QUERY_RESP_STATE:
2865 				cont = processQueryResponse(qstate, iq, id);
2866 				break;
2867 			case PRIME_RESP_STATE:
2868 				cont = processPrimeResponse(qstate, id);
2869 				break;
2870 			case COLLECT_CLASS_STATE:
2871 				cont = processCollectClass(qstate, id);
2872 				break;
2873 			case DSNS_FIND_STATE:
2874 				cont = processDSNSFind(qstate, iq, id);
2875 				break;
2876 			case FINISHED_STATE:
2877 				cont = processFinished(qstate, iq, id);
2878 				break;
2879 			default:
2880 				log_warn("iterator: invalid state: %d",
2881 					iq->state);
2882 				cont = 0;
2883 				break;
2884 		}
2885 	}
2886 }
2887 
2888 /**
2889  * This is the primary entry point for processing request events. Note that
2890  * this method should only be used by external modules.
2891  * @param qstate: query state.
2892  * @param ie: iterator shared global environment.
2893  * @param iq: iterator query state.
2894  * @param id: module id.
2895  */
2896 static void
2897 process_request(struct module_qstate* qstate, struct iter_qstate* iq,
2898 	struct iter_env* ie, int id)
2899 {
2900 	/* external requests start in the INIT state, and finish using the
2901 	 * FINISHED state. */
2902 	iq->state = INIT_REQUEST_STATE;
2903 	iq->final_state = FINISHED_STATE;
2904 	verbose(VERB_ALGO, "process_request: new external request event");
2905 	iter_handle(qstate, iq, ie, id);
2906 }
2907 
2908 /** process authoritative server reply */
2909 static void
2910 process_response(struct module_qstate* qstate, struct iter_qstate* iq,
2911 	struct iter_env* ie, int id, struct outbound_entry* outbound,
2912 	enum module_ev event)
2913 {
2914 	struct msg_parse* prs;
2915 	struct edns_data edns;
2916 	sldns_buffer* pkt;
2917 
2918 	verbose(VERB_ALGO, "process_response: new external response event");
2919 	iq->response = NULL;
2920 	iq->state = QUERY_RESP_STATE;
2921 	if(event == module_event_noreply || event == module_event_error) {
2922 		if(event == module_event_noreply && iq->sent_count >= 3 &&
2923 			qstate->env->cfg->use_caps_bits_for_id &&
2924 			!iq->caps_fallback) {
2925 			/* start fallback */
2926 			iq->caps_fallback = 1;
2927 			iq->caps_server = 0;
2928 			iq->caps_reply = NULL;
2929 			iq->caps_response = NULL;
2930 			iq->state = QUERYTARGETS_STATE;
2931 			iq->num_current_queries--;
2932 			/* need fresh attempts for the 0x20 fallback, if
2933 			 * that was the cause for the failure */
2934 			iter_dec_attempts(iq->dp, 3);
2935 			verbose(VERB_DETAIL, "Capsforid: timeouts, starting fallback");
2936 			goto handle_it;
2937 		}
2938 		goto handle_it;
2939 	}
2940 	if( (event != module_event_reply && event != module_event_capsfail)
2941 		|| !qstate->reply) {
2942 		log_err("Bad event combined with response");
2943 		outbound_list_remove(&iq->outlist, outbound);
2944 		(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
2945 		return;
2946 	}
2947 
2948 	/* parse message */
2949 	prs = (struct msg_parse*)regional_alloc(qstate->env->scratch,
2950 		sizeof(struct msg_parse));
2951 	if(!prs) {
2952 		log_err("out of memory on incoming message");
2953 		/* like packet got dropped */
2954 		goto handle_it;
2955 	}
2956 	memset(prs, 0, sizeof(*prs));
2957 	memset(&edns, 0, sizeof(edns));
2958 	pkt = qstate->reply->c->buffer;
2959 	sldns_buffer_set_position(pkt, 0);
2960 	if(parse_packet(pkt, prs, qstate->env->scratch) != LDNS_RCODE_NOERROR) {
2961 		verbose(VERB_ALGO, "parse error on reply packet");
2962 		goto handle_it;
2963 	}
2964 	/* edns is not examined, but removed from message to help cache */
2965 	if(parse_extract_edns(prs, &edns) != LDNS_RCODE_NOERROR)
2966 		goto handle_it;
2967 	/* remove CD-bit, we asked for in case we handle validation ourself */
2968 	prs->flags &= ~BIT_CD;
2969 
2970 	/* normalize and sanitize: easy to delete items from linked lists */
2971 	if(!scrub_message(pkt, prs, &iq->qchase, iq->dp->name,
2972 		qstate->env->scratch, qstate->env, ie)) {
2973 		/* if 0x20 enabled, start fallback, but we have no message */
2974 		if(event == module_event_capsfail && !iq->caps_fallback) {
2975 			iq->caps_fallback = 1;
2976 			iq->caps_server = 0;
2977 			iq->caps_reply = NULL;
2978 			iq->caps_response = NULL;
2979 			iq->state = QUERYTARGETS_STATE;
2980 			iq->num_current_queries--;
2981 			verbose(VERB_DETAIL, "Capsforid: scrub failed, starting fallback with no response");
2982 		}
2983 		goto handle_it;
2984 	}
2985 
2986 	/* allocate response dns_msg in region */
2987 	iq->response = dns_alloc_msg(pkt, prs, qstate->region);
2988 	if(!iq->response)
2989 		goto handle_it;
2990 	log_query_info(VERB_DETAIL, "response for", &qstate->qinfo);
2991 	log_name_addr(VERB_DETAIL, "reply from", iq->dp->name,
2992 		&qstate->reply->addr, qstate->reply->addrlen);
2993 	if(verbosity >= VERB_ALGO)
2994 		log_dns_msg("incoming scrubbed packet:", &iq->response->qinfo,
2995 			iq->response->rep);
2996 
2997 	if(event == module_event_capsfail || iq->caps_fallback) {
2998 		/* for fallback we care about main answer, not additionals */
2999 		/* removing that makes comparison more likely to succeed */
3000 		caps_strip_reply(iq->response->rep);
3001 		if(!iq->caps_fallback) {
3002 			/* start fallback */
3003 			iq->caps_fallback = 1;
3004 			iq->caps_server = 0;
3005 			iq->caps_reply = iq->response->rep;
3006 			iq->caps_response = iq->response;
3007 			iq->state = QUERYTARGETS_STATE;
3008 			iq->num_current_queries--;
3009 			verbose(VERB_DETAIL, "Capsforid: starting fallback");
3010 			goto handle_it;
3011 		} else {
3012 			/* check if reply is the same, otherwise, fail */
3013 			if(!iq->caps_reply) {
3014 				iq->caps_reply = iq->response->rep;
3015 				iq->caps_response = iq->response;
3016 				iq->caps_server = -1; /*become zero at ++,
3017 				so that we start the full set of trials */
3018 			} else if(caps_failed_rcode(iq->caps_reply) &&
3019 				!caps_failed_rcode(iq->response->rep)) {
3020 				/* prefer to upgrade to non-SERVFAIL */
3021 				iq->caps_reply = iq->response->rep;
3022 				iq->caps_response = iq->response;
3023 			} else if(!caps_failed_rcode(iq->caps_reply) &&
3024 				caps_failed_rcode(iq->response->rep)) {
3025 				/* if we have non-SERVFAIL as answer then
3026 				 * we can ignore SERVFAILs for the equality
3027 				 * comparison */
3028 				/* no instructions here, skip other else */
3029 			} else if(caps_failed_rcode(iq->caps_reply) &&
3030 				caps_failed_rcode(iq->response->rep)) {
3031 				/* failure is same as other failure in fallbk*/
3032 				/* no instructions here, skip other else */
3033 			} else if(!reply_equal(iq->response->rep, iq->caps_reply,
3034 				qstate->env->scratch)) {
3035 				verbose(VERB_DETAIL, "Capsforid fallback: "
3036 					"getting different replies, failed");
3037 				outbound_list_remove(&iq->outlist, outbound);
3038 				(void)error_response(qstate, id,
3039 					LDNS_RCODE_SERVFAIL);
3040 				return;
3041 			}
3042 			/* continue the fallback procedure at next server */
3043 			iq->caps_server++;
3044 			iq->state = QUERYTARGETS_STATE;
3045 			iq->num_current_queries--;
3046 			verbose(VERB_DETAIL, "Capsforid: reply is equal. "
3047 				"go to next fallback");
3048 			goto handle_it;
3049 		}
3050 	}
3051 	iq->caps_fallback = 0; /* if we were in fallback, 0x20 is OK now */
3052 
3053 handle_it:
3054 	outbound_list_remove(&iq->outlist, outbound);
3055 	iter_handle(qstate, iq, ie, id);
3056 }
3057 
3058 void
3059 iter_operate(struct module_qstate* qstate, enum module_ev event, int id,
3060 	struct outbound_entry* outbound)
3061 {
3062 	struct iter_env* ie = (struct iter_env*)qstate->env->modinfo[id];
3063 	struct iter_qstate* iq = (struct iter_qstate*)qstate->minfo[id];
3064 	verbose(VERB_QUERY, "iterator[module %d] operate: extstate:%s event:%s",
3065 		id, strextstate(qstate->ext_state[id]), strmodulevent(event));
3066 	if(iq) log_query_info(VERB_QUERY, "iterator operate: query",
3067 		&qstate->qinfo);
3068 	if(iq && qstate->qinfo.qname != iq->qchase.qname)
3069 		log_query_info(VERB_QUERY, "iterator operate: chased to",
3070 			&iq->qchase);
3071 
3072 	/* perform iterator state machine */
3073 	if((event == module_event_new || event == module_event_pass) &&
3074 		iq == NULL) {
3075 		if(!iter_new(qstate, id)) {
3076 			(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
3077 			return;
3078 		}
3079 		iq = (struct iter_qstate*)qstate->minfo[id];
3080 		process_request(qstate, iq, ie, id);
3081 		return;
3082 	}
3083 	if(iq && event == module_event_pass) {
3084 		iter_handle(qstate, iq, ie, id);
3085 		return;
3086 	}
3087 	if(iq && outbound) {
3088 		process_response(qstate, iq, ie, id, outbound, event);
3089 		return;
3090 	}
3091 	if(event == module_event_error) {
3092 		verbose(VERB_ALGO, "got called with event error, giving up");
3093 		(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
3094 		return;
3095 	}
3096 
3097 	log_err("bad event for iterator");
3098 	(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
3099 }
3100 
3101 void
3102 iter_clear(struct module_qstate* qstate, int id)
3103 {
3104 	struct iter_qstate* iq;
3105 	if(!qstate)
3106 		return;
3107 	iq = (struct iter_qstate*)qstate->minfo[id];
3108 	if(iq) {
3109 		outbound_list_clear(&iq->outlist);
3110 		if(iq->target_count && --iq->target_count[0] == 0)
3111 			free(iq->target_count);
3112 		iq->num_current_queries = 0;
3113 	}
3114 	qstate->minfo[id] = NULL;
3115 }
3116 
3117 size_t
3118 iter_get_mem(struct module_env* env, int id)
3119 {
3120 	struct iter_env* ie = (struct iter_env*)env->modinfo[id];
3121 	if(!ie)
3122 		return 0;
3123 	return sizeof(*ie) + sizeof(int)*((size_t)ie->max_dependency_depth+1)
3124 		+ donotq_get_mem(ie->donotq) + priv_get_mem(ie->priv);
3125 }
3126 
3127 /**
3128  * The iterator function block
3129  */
3130 static struct module_func_block iter_block = {
3131 	"iterator",
3132 	&iter_init, &iter_deinit, &iter_operate, &iter_inform_super,
3133 	&iter_clear, &iter_get_mem
3134 };
3135 
3136 struct module_func_block*
3137 iter_get_funcblock(void)
3138 {
3139 	return &iter_block;
3140 }
3141 
3142 const char*
3143 iter_state_to_string(enum iter_state state)
3144 {
3145 	switch (state)
3146 	{
3147 	case INIT_REQUEST_STATE :
3148 		return "INIT REQUEST STATE";
3149 	case INIT_REQUEST_2_STATE :
3150 		return "INIT REQUEST STATE (stage 2)";
3151 	case INIT_REQUEST_3_STATE:
3152 		return "INIT REQUEST STATE (stage 3)";
3153 	case QUERYTARGETS_STATE :
3154 		return "QUERY TARGETS STATE";
3155 	case PRIME_RESP_STATE :
3156 		return "PRIME RESPONSE STATE";
3157 	case COLLECT_CLASS_STATE :
3158 		return "COLLECT CLASS STATE";
3159 	case DSNS_FIND_STATE :
3160 		return "DSNS FIND STATE";
3161 	case QUERY_RESP_STATE :
3162 		return "QUERY RESPONSE STATE";
3163 	case FINISHED_STATE :
3164 		return "FINISHED RESPONSE STATE";
3165 	default :
3166 		return "UNKNOWN ITER STATE";
3167 	}
3168 }
3169 
3170 int
3171 iter_state_is_responsestate(enum iter_state s)
3172 {
3173 	switch(s) {
3174 		case INIT_REQUEST_STATE :
3175 		case INIT_REQUEST_2_STATE :
3176 		case INIT_REQUEST_3_STATE :
3177 		case QUERYTARGETS_STATE :
3178 		case COLLECT_CLASS_STATE :
3179 			return 0;
3180 		default:
3181 			break;
3182 	}
3183 	return 1;
3184 }
3185