--- deadwood-2.9.03/src/DwRecurse.c.orig	2010-08-04 21:21:26.000000000 -0700
+++ deadwood-2.9.03/src/DwRecurse.c	2010-08-04 23:19:14.000000000 -0700
@@ -442,7 +442,7 @@
 
 int dwx_check_answer_section(dw_str *in, dw_str *query, dns_details *view) {
         int32_t qtype = 0; /* The type of record they want in the query */
-        int counter = 0, cname_chain = 0, use_cname = 1;
+        int counter = 0, cname_chain = 0, use_cname = 1, offset = 0;
 
         qtype = dw_fetch_u16(query,-1);
         if(qtype == -1) {
@@ -460,22 +460,27 @@
                                 in,view->look->an[counter]) == 1 &&
                                 dw_fetch_u16(in,view->look->an[counter + 1]) ==
                                 qtype) { /* Direct answer for our query */
+			offset++;
                         use_cname = 0; /* No CNAMEs after direct answer */
                 } else if(cname_chain == 0 && qtype != RR_CNAME &&
-                                (counter >> 1) == cname_chain && use_cname == 1
-                                && dwx_dname_issame_2dw(query,0,in,
+                                offset == cname_chain && 
+				use_cname == 1 && 
+				dwx_dname_issame_2dw(query,0,in,
                                 view->look->an[counter]) == 1 &&
                                 dw_fetch_u16(in,view->look->an[counter + 1]) ==
                                 RR_CNAME) { /* First answer a CNAME */
+			offset++;
                         cname_chain++;
                 } else if(cname_chain > 0 && qtype != RR_CNAME &&
-                                (counter >> 1) == cname_chain && use_cname == 1
-                                && dwx_dname_issame_dw(in,
+                                offset == cname_chain && 
+				use_cname == 1 && 
+				dwx_dname_issame_dw(in,
                                 view->look->an[((cname_chain - 1) * 2) + 1]
                                 + 10 /* 10: TYPE, CLASS, TTL, RDLENGTH */,
                                 view->look->an[counter]) == 1 &&
                                 dw_fetch_u16(in,view->look->an[counter + 1]) ==
                                 RR_CNAME) { /* CNAME chain member */
+			offset++;
                         cname_chain++;
                 } else if(cname_chain > 0 && qtype != RR_CNAME &&
                                 dwx_dname_issame_dw(in,
@@ -484,6 +489,7 @@
                                 view->look->an[counter]) == 1 &&
                                 dw_fetch_u16(in,view->look->an[counter + 1]) ==
                                 qtype) { /* Answer after CNAME chain */
+			offset++;
                         use_cname = 0; /* Answer found; no more CNAMEs */
                 } else { /* Not an answer to our query nor a CNAME chain */
                         view->an_types[(counter >> 1)] = RRX_IGNORE;
@@ -951,7 +957,7 @@
 
         for(counter = 0; counter < view->look->ancount * 2; counter += 2) {
                 if(view->an_types[(counter >> 1)] == RRX_IGNORE) {
-                        break;
+                        continue;
                 }
                 if(view->an_types[(counter >> 1)] != RR_CNAME) {
                         goto catch_dwx_make_cname_refer;
@@ -1403,7 +1409,8 @@
         int32_t query_type = 0;
         int counter = 0;
         int max = 32;
-
+	int index = -1;
+	
         if(query == 0 || view == 0 || view->look == 0) {
                 return TYPE_ERROR;
         }
@@ -1419,24 +1426,32 @@
                 if(query_type == RR_ANY) {
                         return TYPE_ANSWER;
                 }
-                if(view->an_types[0] == dwx_rrtype_number(query_type)) {
-                        return TYPE_ANSWER;
-                }
-                if(view->an_types[0] == RR_CNAME) { /* CNAME chain */
-                        if(view->look->ancount < max) {
-                                max = view->look->ancount;
-                        }
-                        for(counter = 1 ; counter < max ; counter++) {
-                                if(view->an_types[counter] ==
-                                               dwx_rrtype_number(query_type)) {
-                                        return TYPE_ANSWER;
-                                }
-                                if(view->an_types[counter] != RR_CNAME &&
-                                   view->an_types[counter] != RRX_IGNORE) {
-                                        return TYPE_NO_USEFUL_DATA;
+		for(index = 0; index < 7; index++ ) {
+                	if(view->an_types[index] == 
+					dwx_rrtype_number(query_type)) {
+                        	return TYPE_ANSWER;
+                	}
+                	if(view->an_types[index] == RR_CNAME) { /* CNAMEs */
+                        	if(view->look->ancount < max) {
+                                	max = view->look->ancount;
+                        	}
+                        	for(counter = index + 1 ; counter < max ; 
+						counter++) {
+                                	if(view->an_types[counter] ==
+                                           dwx_rrtype_number(query_type)) {
+                                        	return TYPE_ANSWER;
+                                	}
+                                	if(view->an_types[counter] != RR_CNAME 
+					   && view->an_types[counter] != 
+					   RRX_IGNORE) {
+                                        	return TYPE_NO_USEFUL_DATA;
+					}
                                 }
-                        }
                         return TYPE_CNAME_REFER;
+                        }
+			if(view->an_types[index] != RRX_IGNORE) {
+				return TYPE_NO_USEFUL_DATA;
+			}
                 }
         }
         if(view->ns_types == 0) {
