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