Summary of bug: Deadwood was unable to resolve a name where the IP for
	a glueless NS referral was one that never appears in the AN
	section of a DNS reply because of a misconfigured DNS server.  

To wit:

$ askmara Aoncetv-ipn.net. 198.41.0.4
# Querying the server with the IP 198.41.0.4
# Question: Aoncetv-ipn.net.
# NS replies:
#net. +172800 ns a.gtld-servers.net.
#net. +172800 ns b.gtld-servers.net.
#net. +172800 ns c.gtld-servers.net.
#net. +172800 ns d.gtld-servers.net.
#net. +172800 ns e.gtld-servers.net.
#net. +172800 ns f.gtld-servers.net.
#net. +172800 ns g.gtld-servers.net.
#net. +172800 ns h.gtld-servers.net.
#net. +172800 ns i.gtld-servers.net.
#net. +172800 ns j.gtld-servers.net.
#net. +172800 ns k.gtld-servers.net.
#net. +172800 ns l.gtld-servers.net.
#net. +172800 ns m.gtld-servers.net.
# AR replies:
#a.gtld-servers.net. +172800 a 192.5.6.30
#b.gtld-servers.net. +172800 a 192.33.14.30
#c.gtld-servers.net. +172800 a 192.26.92.30
#d.gtld-servers.net. +172800 a 192.31.80.30
#e.gtld-servers.net. +172800 a 192.12.94.30
#f.gtld-servers.net. +172800 a 192.35.51.30
#g.gtld-servers.net. +172800 a 192.42.93.30
#h.gtld-servers.net. +172800 a 192.54.112.30
#i.gtld-servers.net. +172800 a 192.43.172.30
#j.gtld-servers.net. +172800 a 192.48.79.30
#k.gtld-servers.net. +172800 a 192.52.178.30
#l.gtld-servers.net. +172800 a 192.41.162.30
#m.gtld-servers.net. +172800 a 192.55.83.30
#a.gtld-servers.net. +172800 aaaa 2001:503:a83e:0:0:0:2:30
$ askmara Aoncetv-ipn.net. 192.5.6.30
# Querying the server with the IP 192.5.6.30
# Question: Aoncetv-ipn.net.
# NS replies:
oncetv-ipn.net. +172800 ns ns01.canaloncesi.tv.
oncetv-ipn.net. +172800 ns ns02.canaloncesi.tv.
# AR replies:
$ askmara Ans01.canaloncesi.tv. 198.41.0.4
# Querying the server with the IP 198.41.0.4
# Question: Ans01.canaloncesi.tv.
# NS replies:
#tv. +172800 ns l5.nstld.com.
#tv. +172800 ns h5.nstld.com.
#tv. +172800 ns g5.nstld.com.
#tv. +172800 ns f5.nstld.com.
#tv. +172800 ns d5.nstld.com.
#tv. +172800 ns c5.nstld.com.
#tv. +172800 ns a5.nstld.com.
# AR replies:
#l5.nstld.com. +172800 a 192.41.162.34
#h5.nstld.com. +172800 a 192.54.112.34
#h5.nstld.com. +172800 aaaa 2001:502:8cc:0:0:0:2:34
#g5.nstld.com. +172800 a 192.42.93.34
#f5.nstld.com. +172800 a 192.35.51.34
#f5.nstld.com. +172800 aaaa 2001:503:d414:0:0:0:2:34
#d5.nstld.com. +172800 a 192.31.80.34
#c5.nstld.com. +172800 a 192.26.92.34
#a5.nstld.com. +172800 a 192.5.6.34
$ askmara Ans01.canaloncesi.tv. 192.41.162.34
# Querying the server with the IP 192.41.162.34
# Question: Ans01.canaloncesi.tv.
# NS replies:
#canaloncesi.tv. +172800 ns ns01.canaloncesi.tv.
#canaloncesi.tv. +172800 ns ns02.canaloncesi.tv.
# AR replies:
ns01.canaloncesi.tv. +172800 a 192.99.17.117
#ns02.canaloncesi.tv. +172800 a 192.99.17.117
$ askmara Ans01.canaloncesi.tv. 192.99.17.117
# Querying the server with the IP 192.99.17.117
# Remote server said: NAME ERROR
# Question: Ans01.canaloncesi.tv.
# NS replies:
#canaloncesi.tv. +10800 soa ns5002137.ip-192-99-17.net. dserrano@oncetvmexico.ipn.mx. 2013112303 10800 3600 604800 10800
# AR replies:
$ askmara Ans02.canaloncesi.tv. 192.99.17.117
# Querying the server with the IP 192.99.17.117
# Remote server said: NAME ERROR
# Question: Ans02.canaloncesi.tv.
# NS replies:
#canaloncesi.tv. +10800 soa ns5002137.ip-192-99-17.net. dserrano@oncetvmexico.ipn.mx. 2013112303 10800 3600 604800 10800
# AR replies:

Deadwood was unable to resolve oncetv-ipn.net.

My fix: Allow an answer in the AR section that is a direct answer to
our question (we will have to move the answer to the AR section, but
it will save an additional DNS query in a lot of cases, so has value
beyond oncetv-ipn.net)

The fix:

dwx_determine_answer_type() is now able to look for a direct answer
in the AR section of their reply.  If this happens, said answer is to
be converted in to a "TYPE_ANSWER" packet; we move a single answer
from the AR to the AN section.

The resulting code, dwx_answer_in_ar(), looks like a combination of
dwx_remake_complete_reply() and dwx_copy_over_section()

--- deadwood-3.2.04/src/DwRecurse.h.orig	2013-12-03 02:15:15.000000000 -0500
+++ deadwood-3.2.04/src/DwRecurse.h	2013-12-03 02:41:20.000000000 -0500
@@ -56,6 +56,7 @@
 
 #define RRX_IGNORE 36 /* Do not use this record */
 #define RRX_OOB_NS 37 /* NS record with bad bailiwick (Out-Of-Bailiwick) */
+#define RRX_ANSWER_IN_AR 38 /* Answer to question in AR section */
 /* These are used for linking A and AAAA glue records in the AR section of
  * a DNS reply with NS records in the NS section; the ranges for both
  * must be the same */
@@ -77,6 +78,7 @@
 #define TYPE_NO_USEFUL_DATA 19
 #define TYPE_ERROR 20
 #define TYPE_UPSTREAM_REFER 22 /* Upstream server; set RD and stop here */
+#define TYPE_ANSWER_IN_AR 23 /* Answer only in AR section of reply */
 /* These won't be used by Deadwood, but may be useful if I ever
  * expand DwHash to support for datatypes for elements besides
  * strings.
--- deadwood-3.2.04/src/DwRecurse.c.orig	2013-12-03 02:15:20.000000000 -0500
+++ deadwood-3.2.04/src/DwRecurse.c	2013-12-03 04:46:07.000000000 -0500
@@ -1418,7 +1418,7 @@
  * sent us, determine what type of reply it is (direct answer, full CNAME
  * chain referral, incomplete CNAME chain referral, or NS referral) */
 
-int dwx_determine_answer_type(dns_details *view, dw_str *query) {
+int dwx_determine_answer_type(dns_details *view, dw_str *query, dw_str *in) {
         int32_t query_type = 0;
         int counter = 0;
         int max = 32;
@@ -1476,6 +1476,16 @@
                 return TYPE_NOT_THERE; /* May be NXDOMAIN; look at header */
         }
         if(view->ns_types[0] == RR_NS) {
+		/* oncetv-ipn.net fix; also speeds up Deadwood */
+                for(index = 0; index < view->look->arcount * 2; index +=2) {
+			if(dwx_dname_issame_2dw(query,0,in,
+					view->look->ar[index]) == 1 &&
+			   dw_fetch_u16(in,view->look->ar[index + 1]) == 
+					query_type) {
+				view->ar_types[(index >> 1)]=RRX_ANSWER_IN_AR;
+				return TYPE_ANSWER_IN_AR;
+			}
+		}
                 return TYPE_NS_REFER;
         }
         return TYPE_NO_USEFUL_DATA;
@@ -1557,6 +1567,51 @@
         return out;
 }
 
+/* oncetv-ipn.net fix: If the answer is in the AR section, we return
+ * a packet where that answer (singular for the simple reason coding for 
+ * multiple is more difficult and no one is paying me to write this code) 
+ * is moved to the AN section */
+dw_str *dwx_answer_in_ar(dns_details *view, dw_str *in) {
+	dw_str *tmp = 0, *out = 0;
+	int32_t dname_len = 0;
+	int index = 0;
+
+	if(view == 0 || in == 0) {
+		return 0; /* Sanity test */
+	}
+
+	for(index = 0; index < view->look->arcount; index++) {
+		if(view->ar_types[index] == RRX_ANSWER_IN_AR && tmp == 0) {
+			tmp = dwx_get_1_dns_rr(in,view->look->ar[index * 2]);
+			dname_len = view->look->ar[(index * 2) + 1] -
+					view->look->ar[index * 2];
+		}
+	}
+
+	if(tmp == 0) {
+		return 0;
+	}
+	out = dw_create(tmp->len + 14);
+	if(out == 0) {
+		dw_destroy(tmp);
+		return 0;
+	}
+	if(dw_append(tmp,out) == -1 ||
+	   dw_push_u16(0,out) == -1 || /* Offset 1: Top of string */
+	   dw_push_u16(dname_len,out) == -1 || /* Offset 2: End of dname */
+	   dw_push_u16(1,out) == -1 || /* One answer */
+	   dw_push_u16(0,out) == -1 || /* No NS records */
+	   dw_push_u16(0,out) == -1 || /* No AR records (moved to AN) */
+           dw_addchar(TYPE_ANSWER,out) == -1) {
+		dw_destroy(tmp);
+		dw_destroy(out);
+		return 0;
+	}
+
+	dw_destroy(tmp);
+	return out;
+}
+
 /* Look at a DNS reply and determine how to proceed
  *
  * Input: The reply we got ("in"), and the query we sent ("query")
@@ -1592,7 +1647,14 @@
            dwx_cleanup_ns_ar(view) == -1) {
                 goto catch_dwx_dissect_packet;
         }
-        type = dwx_determine_answer_type(view,query);
+        type = dwx_determine_answer_type(view,query,in);
+	if(type == TYPE_ANSWER_IN_AR) {
+		out = dwx_answer_in_ar(view, in);
+		if(view != 0) {
+			dwx_zap_dns_details(view);
+		}
+		return out; 
+	}
         if(type == TYPE_NS_REFER && (
            dwx_check_bailiwick_ns_section(view,query,bailiwick) == -1 ||
            dwx_link_ns_records(view) == -1 )) {
@@ -2532,6 +2594,9 @@
                 }
         }
 
+	if(place == 0) {
+		goto catch_dwx_get_rr_from_answer;
+	}
         place = dwr_rng(rng_seed) % place;
         type = dw_fetch_u16(answer,stack[place]);
         if(type == RR_A) {
