5 static void cname_recurse(adns_state ads
, adns_query qu
, adns_queryflags xflags
) {
9 void adns__procdgram(adns_state ads
, const byte
*dgram
, int dglen
,
10 int serv
, struct timeval now
) {
11 int cbyte
, rrstart
, wantedrrs
, rri
, foundsoa
, foundns
;
12 int id
, f1
, f2
, qdcount
, ancount
, nscount
, arcount
, flg_ra
, flg_tc
;
13 int rrtype
, rrclass
, rdlength
, rdstart
, ownermatched
, ownerstart
;
14 int anstart
, nsstart
, arstart
;
23 if (dglen
<DNS_HDRSIZE
) {
24 adns__diag(ads
,serv
,"received datagram too short for message header (%d)",dglen
);
34 assert(cbyte
== DNS_HDRSIZE
);
40 adns__diag(ads
,serv
,"server sent us a query, not a response");
44 adns__diag(ads
,serv
,"server sent us unknown opcode %d (wanted 0=QUERY)",
49 adns__diag(ads
,serv
,"server sent reply without quoting our question");
51 } else if (qdcount
>1) {
52 adns__diag(ads
,serv
,"server claimed to answer %d questions with one message",
56 for (qu
= ads
->timew
.head
; qu
; qu
= nqu
) {
58 if (qu
->id
!= id
) continue;
59 if (dglen
< qu
->querylen
) continue;
60 if (memcmp(qu
->querymsg
+DNS_HDRSIZE
,dgram
+DNS_HDRSIZE
,qu
->querylen
-DNS_HDRSIZE
))
64 assert(qu
->cnameoff
== -1);
65 anstart
= qu
->querylen
;
67 adns__debug(ads
,serv
,"reply not found (id=%02x)",id
);
71 LIST_UNLINK(ads
->timew
,qu
);
72 /* We're definitely going to do something with this query now */
75 adns__diag(ads
,serv
,"server thinks we didn't ask for recursive lookup");
76 adns__query_fail(ads
,qu
,adns_s_serverfaulty
);
85 case rcode_formaterror
:
86 adns__warn(ads
,serv
,"server cannot understand our query (Format Error)");
87 adns__query_fail(ads
,qu
,adns_s_serverfaulty
);
90 adns__query_fail(ads
,qu
,adns_s_servfail
);
93 adns__warn(ads
,serv
,"server claims not to implement our query");
94 adns__query_fail(ads
,qu
,adns_s_notimplemented
);
97 adns__warn(ads
,serv
,"server refused our query");
98 adns__query_fail(ads
,qu
,adns_s_refused
);
101 adns__warn(ads
,serv
,"server gave unknown response code %d",rcode
);
102 adns__query_fail(ads
,qu
,adns_s_reasonunknown
);
106 /* Now, take a look at the answer section, and see if it is complete.
107 * If it has any CNAMEs we stuff them in the answer.
110 for (rri
= 0; rri
<ancount
; rri
++) {
112 if (qu
->cnameoff
>= 0) {
113 st
= adns__get_rr_temp(ads
,qu
,serv
, dgram
,dglen
,&cbyte
,
114 &rrtype
,&rrclass
,&rdlength
,&rdstart
,
115 dgram
,dglen
,qu
->cnameoff
, &ownermatched
);
117 st
= adns__get_rr_temp(ads
,qu
,serv
, dgram
,dglen
,&cbyte
,
118 &rrtype
,&rrclass
,&rdlength
,&rdstart
,
119 qu
->querymsg
,qu
->querylen
,DNS_HDRSIZE
, &ownermatched
);
121 if (st
) adns__query_fail(ads
,qu
,st
);
122 if (rrtype
== -1) goto x_truncated
;
124 if (rrclass
!= DNS_CLASS_IN
) {
125 adns__diag(ads
,serv
,"ignoring answer RR with wrong class %d (expected IN=%d)",
126 rrclass
,DNS_CLASS_IN
);
130 if (ads
->iflags
& adns_if_debug
) {
131 st
= adns__get_domain_temp(ads
,qu
,serv
, dgram
,dglen
,&rrstart
,dglen
, &ownerstart
);
133 adns__debug(ads
,serv
, "ignoring RR with an irrelevant owner"
134 " whose format is bad, code %d",st
);
135 else if (ownerstart
>=0)
136 adns__debug(ads
,serv
, "ignoring RR with an irrelevant owner"
137 " \"%s\"", qu
->ans
.buf
+ownerstart
);
139 adns__debug(ads
,serv
,"ignoring RR with an irrelevant truncated owner");
143 if (qu
->cnameoff
<0 &&
144 (qu
->typei
->type
& adns__rrt_typemask
) != adns_r_cname
&&
145 rrtype
== adns_r_cname
) { /* Ignore second and subsequent CNAMEs */
146 st
= adns__get_domain_perm(ads
,qu
,serv
, dgram
,dglen
,
147 &rdstart
,rdstart
+rdlength
,&qu
->cnameoff
);
148 if (st
) { adns__query_fail(ads
,qu
,st
); return; }
149 if (qu
->cnameoff
==-1) goto x_truncated
;
150 /* If we find the answer section truncated after this point we restart
151 * the query at the CNAME; if beforehand then we obviously have to use
152 * TCP. If there is no truncation we can use the whole answer if
153 * it contains the relevant info.
155 } else if (rrtype
== (qu
->typei
->type
& adns__rrt_typemask
)) {
158 adns__debug(ads
,serv
,"ignoring answer RR with irrelevant type %d",rrtype
);
162 /* If we got here then the answer section is intact. */
166 /* Oops, NODATA or NXDOMAIN or perhaps a referral (which would be a problem) */
168 if (rcode
== rcode_nxdomain
) {
169 adns__query_finish(ads
,qu
,adns_s_nxdomain
);
173 /* RFC2308: NODATA has _either_ a SOA _or_ _no_ NS records in authority section */
174 foundsoa
= 0; foundns
= 0;
175 for (rri
= 0; rri
<nscount
; rri
++) {
177 st
= adns__get_rr_temp(ads
,qu
,serv
, dgram
,dglen
,&cbyte
,
178 &rrtype
,&rrclass
, &rdlength
,&rdstart
, 0,0,0,0);
179 if (st
) { adns__query_fail(ads
,qu
,st
); return; }
180 if (rrtype
==-1) goto x_truncated
;
181 if (rrclass
!= DNS_CLASS_IN
) {
182 adns__diag(ads
,serv
,"ignoring authority RR with wrong class %d (expected IN=%d)",
183 rrclass
,DNS_CLASS_IN
);
186 if (rrtype
== adns_r_soa_raw
) { foundsoa
= 1; break; }
187 else if (rrtype
== adns_r_ns_raw
) { foundns
= 1; }
190 if (foundsoa
|| !foundns
) {
191 /* Aha ! A NODATA response, good. */
192 adns__query_finish(ads
,qu
,adns_s_nodata
);
196 /* Now what ? No relevant answers, no SOA, and at least some NS's.
197 * Looks like a referral. Just one last chance ... if we came across
198 * a CNAME then perhaps we should do our own CNAME lookup.
200 if (qu
->cnameoff
!= -1) { cname_recurse(ads
,qu
,0); return; }
202 /* Bloody hell, I thought we asked for recursion ? */
204 adns__diag(ads
,serv
,"server is not willing to do recursive lookups for us");
205 adns__query_fail(ads
,qu
,adns_s_norecurse
);
208 adns__diag(ads
,serv
,"server claims to do recursion, but gave us a referral");
209 adns__query_fail(ads
,qu
,adns_s_serverfaulty
);
213 /* Now, we have some RRs which we wanted. */
215 qu
->rrsoff
= adns__vbuf_malloc(&qu
->ans
,qu
->typei
->rrsz
*wantedrrs
);
216 if (qu
->rrsoff
== -1) adns__query_fail(ads
,qu
,adns_s_nolocalmem
);
221 for (rri
=0; rri
<ancount
; rri
++) {
222 st
= adns__get_rr_temp(ads
,qu
,serv
, dgram
,dglen
,&cbyte
,
223 &rrtype
,&rrclass
, &rdlength
,&rdstart
, 0,0,0,0);
224 assert(!st
); assert(rrtype
!= -1);
225 if (rrclass
!= DNS_CLASS_IN
||
226 rrtype
!= (qu
->typei
->type
& adns__rrt_typemask
))
228 assert(currentrrs
<wantedrrs
);
229 st
= qu
->typei
->get_fn(ads
,qu
,serv
, dgram
,dglen
, &rdstart
,rdstart
+rdlength
,
230 nsstart
,arcount
,&arstart
, qu
->rrsoff
,¤trrs
);
231 if (st
) { adns__query_fail(ads
,qu
,st
); return; }
232 if (currentrrs
==-1) goto x_truncated
;
235 /* This may have generated some child queries ... */
236 if (qu
->children
.head
) {
237 qu
->state
= query_child
;
238 LIST_LINK_TAIL(ads
->childw
,qu
);
242 adns__query_finish(ads
,qu
,adns_s_ok
);
247 adns__diag(ads
,serv
,"server sent datagram which points outside itself");
248 adns__query_fail(ads
,qu
,adns_s_serverfaulty
);
251 if (qu
->cnameoff
!= -1) { cname_recurse(ads
,qu
,adns_qf_usevc
); return; }
254 ans
= (adns_answer
*)qu
->ans
.buf
;
256 qu
->ans
.used
= sizeof(adns_answer
);
257 qu
->flags
|= adns_qf_usevc
;
258 adns__query_udp(ads
,qu
,now
);