+ /* RFC2308: NODATA has _either_ a SOA _or_ _no_ NS records in authority section */
+ foundsoa= 0; foundns= 0;
+ for (rri= 0; rri<nscount; rri++) {
+ rrstart= cbyte;
+ st= adns__get_rr_temp(ads,qu,serv, dgram,dglen,&cbyte,
+ &rrtype,&rrclass, &rdlength,&rdstart, 0,0,0,0);
+ if (st) { adns__query_fail(ads,qu,st); return; }
+ if (rrtype==-1) goto x_truncated;
+ if (rrclass != DNS_CLASS_IN) {
+ adns__diag(ads,serv,"ignoring authority RR with wrong class %d (expected IN=%d)",
+ rrclass,DNS_CLASS_IN);
+ continue;
+ }
+ if (rrtype == adns_r_soa_raw) { foundsoa= 1; break; }
+ else if (rrtype == adns_r_ns_raw) { foundns= 1; }
+ }
+
+ if (foundsoa || !foundns) {
+ /* Aha ! A NODATA response, good. */
+ adns__query_finish(ads,qu,adns_s_nodata);
+ return;
+ }
+
+ /* Now what ? No relevant answers, no SOA, and at least some NS's.
+ * Looks like a referral. Just one last chance ... if we came across
+ * a CNAME then perhaps we should do our own CNAME lookup.
+ */
+ if (qu->cnameoff != -1) { cname_recurse(ads,qu,0); return; }
+
+ /* Bloody hell, I thought we asked for recursion ? */
+ if (!flg_ra) {
+ adns__diag(ads,serv,"server is not willing to do recursive lookups for us");
+ adns__query_fail(ads,qu,adns_s_norecurse);
+ return;
+ }
+ adns__diag(ads,serv,"server claims to do recursion, but gave us a referral");
+ adns__query_fail(ads,qu,adns_s_serverfaulty);
+ return;
+ }
+
+ /* Now, we have some RRs which we wanted. */
+
+ qu->rrsoff= adns__vbuf_malloc(&qu->ans,qu->typei->rrsz*wantedrrs);
+ if (qu->rrsoff == -1) adns__query_fail(ads,qu,adns_s_nolocalmem);
+
+ cbyte= anstart;
+ currentrrs= 0;
+ arstart= -1;
+ for (rri=0; rri<ancount; rri++) {
+ st= adns__get_rr_temp(ads,qu,serv, dgram,dglen,&cbyte,
+ &rrtype,&rrclass, &rdlength,&rdstart, 0,0,0,0);
+ assert(!st); assert(rrtype != -1);
+ if (rrclass != DNS_CLASS_IN ||
+ rrtype != (qu->typei->type & adns__rrt_typemask))
+ continue;
+ assert(currentrrs<wantedrrs);
+ st= qu->typei->get_fn(ads,qu,serv, dgram,dglen, &rdstart,rdstart+rdlength,
+ nsstart,arcount,&arstart, qu->rrsoff,¤trrs);
+ if (st) { adns__query_fail(ads,qu,st); return; }
+ if (currentrrs==-1) goto x_truncated;
+ }
+
+ /* This may have generated some child queries ... */
+ if (qu->children.head) {
+ qu->state= query_child;
+ LIST_LINK_TAIL(ads->childw,qu);
+ return;
+ }
+
+ adns__query_finish(ads,qu,adns_s_ok);
+ return;
+
+x_truncated:
+ if (!flg_tc) {
+ adns__diag(ads,serv,"server sent datagram which points outside itself");
+ adns__query_fail(ads,qu,adns_s_serverfaulty);
+ return;
+ }
+ if (qu->cnameoff != -1) { cname_recurse(ads,qu,adns_qf_usevc); return; }
+ qu->cnameoff= -1;
+ qu->rrsoff= -1;
+ ans= (adns_answer*)qu->ans.buf;
+ ans->nrrs= 0;
+ qu->ans.used= sizeof(adns_answer);
+ qu->flags |= adns_qf_usevc;
+ adns__query_udp(ads,qu,now);