+
+ /* We're definitely going to do something with this packet and this
+ * query now. */
+
+ anstart= qu->query_dglen;
+ arstart= -1;
+
+ /* Now, take a look at the answer section, and see if it is complete.
+ * If it has any CNAMEs we stuff them in the answer.
+ */
+ wantedrrs= 0;
+ cbyte= anstart;
+ for (rri= 0; rri<ancount; rri++) {
+ rrstart= cbyte;
+ st= adns__findrr(qu,serv, dgram,dglen,&cbyte,
+ &rrtype,&rrclass,&ttl, &rdlength,&rdstart,
+ &ownermatched);
+ if (st) { adns__query_fail(qu,st); return; }
+ if (rrtype == -1) goto x_truncated;
+
+ if (rrclass != DNS_CLASS_IN) {
+ adns__diag(ads,serv,qu,"ignoring answer RR with wrong class %d"
+ " (expected IN=%d)", rrclass,DNS_CLASS_IN);
+ continue;
+ }
+ if (!ownermatched) {
+ if (ads->iflags & adns_if_debug) {
+ adns__debug(ads,serv,qu,"ignoring RR with an unexpected owner %s",
+ adns__diag_domain(ads,serv,qu, &qu->vb,
+ dgram,dglen,rrstart));
+ }
+ continue;
+ }
+ if (rrtype == adns_r_cname &&
+ (qu->answer->type & adns_rrt_typemask) != adns_r_cname) {
+ if (qu->flags & adns_qf_cname_forbid) {
+ adns__query_fail(qu,adns_s_prohibitedcname);
+ return;
+ } else if (qu->cname_dgram) { /* Ignore second and subsequent CNAME(s) */
+ adns__debug(ads,serv,qu,"allegedly canonical name %s"
+ " is actually alias for %s", qu->answer->cname,
+ adns__diag_domain(ads,serv,qu, &qu->vb,
+ dgram,dglen,rdstart));
+ adns__query_fail(qu,adns_s_prohibitedcname);
+ return;
+ } else if (wantedrrs) { /* Ignore CNAME(s) after RR(s). */
+ adns__debug(ads,serv,qu,"ignoring CNAME (to %s) coexisting with RR",
+ adns__diag_domain(ads,serv,qu, &qu->vb,
+ dgram,dglen,rdstart));
+ } else {
+ qu->cname_begin= rdstart;
+ qu->cname_dglen= dglen;
+ st= adns__parse_domain(ads,serv,qu, &qu->vb,
+ qu->flags & adns_qf_quotefail_cname
+ ? 0 : pdf_quoteok,
+ dgram,dglen, &rdstart,rdstart+rdlength);
+ if (!qu->vb.used) goto x_truncated;
+ if (st) { adns__query_fail(qu,st); return; }
+ l= strlen(qu->vb.buf)+1;
+ qu->answer->cname= adns__alloc_preserved(qu,l);
+ if (!qu->answer->cname) {
+ adns__query_fail(qu,adns_s_nomemory);
+ return;
+ }
+
+ qu->cname_dgram= adns__alloc_mine(qu,dglen);
+ memcpy(qu->cname_dgram,dgram,dglen);
+
+ memcpy(qu->answer->cname,qu->vb.buf,l);
+ cname_here= 1;
+ adns__update_expires(qu,ttl,now);
+ /* If we find the answer section truncated after this point we restart
+ * the query at the CNAME; if beforehand then we obviously have to use
+ * TCP. If there is no truncation we can use the whole answer if
+ * it contains the relevant info.
+ */
+ }
+ } else if (rrtype == (qu->answer->type & adns_rrt_typemask)) {
+ wantedrrs++;
+ } else {
+ adns__debug(ads,serv,qu,"ignoring answer RR"
+ " with irrelevant type %d",rrtype);
+ }
+ }
+
+ /* We defer handling truncated responses here, in case there was a CNAME
+ * which we could use.
+ */
+ if (flg_tc) goto x_truncated;
+
+ nsstart= cbyte;
+
+ if (!wantedrrs) {
+ /* Oops, NODATA or NXDOMAIN or perhaps a referral
+ * (which would be a problem) */
+
+ /* RFC2308: NODATA has _either_ a SOA _or_ _no_ NS records
+ * in authority section */
+ foundsoa= 0; soattl= 0; foundns= 0;
+ for (rri= 0; rri<nscount; rri++) {
+ rrstart= cbyte;
+ st= adns__findrr(qu,serv, dgram,dglen,&cbyte,
+ &rrtype,&rrclass,&ttl, &rdlength,&rdstart, 0);
+ if (st) { adns__query_fail(qu,st); return; }
+ if (rrtype==-1) goto x_truncated;
+ if (rrclass != DNS_CLASS_IN) {
+ adns__diag(ads,serv,qu,
+ "ignoring authority RR with wrong class %d"
+ " (expected IN=%d)", rrclass,DNS_CLASS_IN);
+ continue;
+ }
+ if (rrtype == adns_r_soa_raw) { foundsoa= 1; soattl= ttl; break; }
+ else if (rrtype == adns_r_ns_raw) { foundns= 1; }
+ }
+
+ if (rcode == rcode_nxdomain) {
+ /* We still wanted to look for the SOA so we could find the TTL. */
+ adns__update_expires(qu,soattl,now);
+
+ if (qu->flags & adns_qf_search && !qu->cname_dgram) {
+ adns__search_next(ads,qu,now);
+ } else {
+ adns__query_fail(qu,adns_s_nxdomain);
+ }
+ return;
+ }
+
+ if (foundsoa || !foundns) {
+ /* Aha ! A NODATA response, good. */
+ adns__update_expires(qu,soattl,now);
+ adns__query_fail(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 in this datagram then we should probably do our own CNAME
+ * lookup now in the hope that we won't get a referral again.
+ */
+ if (cname_here) goto x_restartquery;
+
+ /* Bloody hell, I thought we asked for recursion ? */
+ if (!flg_ra) {
+ adns__diag(ads,serv,qu,"server is not willing"
+ " to do recursive lookups for us");
+ adns__query_fail(qu,adns_s_norecurse);
+ } else {
+ if (!flg_rd)
+ adns__diag(ads,serv,qu,"server thinks"
+ " we didn't ask for recursive lookup");
+ else
+ adns__debug(ads,serv,qu,"server claims to do recursion,"
+ " but gave us a referral");
+ adns__query_fail(qu,adns_s_invalidresponse);
+ }