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