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