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