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