3 * - main handling and parsing routine for received datagrams
7 * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
9 * It is part of adns, which is
10 * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
11 * Copyright (C) 1999-2000 Tony Finch <dot@dotat.at>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software Foundation,
25 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #ifdef ADNS_JGAA_WIN32
29 # include "adns_win32.h"
36 void adns__procdgram(adns_state ads
, const byte
*dgram
, int dglen
,
37 int serv
, int viatcp
, struct timeval now
) {
38 int cbyte
, rrstart
, wantedrrs
, rri
, foundsoa
, foundns
, cname_here
;
39 int id
, f1
, f2
, qdcount
, ancount
, nscount
, arcount
;
40 int flg_ra
, flg_rd
, flg_tc
, flg_qr
, opcode
;
41 int rrtype
, rrclass
, rdlength
, rdstart
;
42 int anstart
, nsstart
, arstart
;
43 int ownermatched
, l
, nrrs
;
44 unsigned long ttl
, soattl
;
45 const typeinfo
*typei
;
50 byte
*newquery
, *rrsdata
;
53 if (dglen
<DNS_HDRSIZE
) {
54 adns__diag(ads
,serv
,0,"received datagram too short for message header (%d)",dglen
);
65 assert(cbyte
== DNS_HDRSIZE
);
77 adns__diag(ads
,serv
,0,"server sent us a query, not a response");
81 adns__diag(ads
,serv
,0,"server sent us unknown opcode %d (wanted 0=QUERY)",opcode
);
86 /* See if we can find the relevant query, or leave qu=0 otherwise ... */
89 for (qu
= viatcp
? ads
->tcpw
.head
: ads
->udpw
.head
; qu
; qu
= nqu
) {
91 if (qu
->id
!= id
) continue;
92 if (dglen
< qu
->query_dglen
) continue;
93 if (memcmp(qu
->query_dgram
+DNS_HDRSIZE
,
95 (size_t) qu
->query_dglen
-DNS_HDRSIZE
))
98 assert(qu
->state
== query_tcpw
);
100 assert(qu
->state
== query_tosend
);
101 if (!(qu
->udpsent
& (1<<serv
))) continue;
106 /* We're definitely going to do something with this query now */
107 if (viatcp
) LIST_UNLINK(ads
->tcpw
,qu
);
108 else LIST_UNLINK(ads
->udpw
,qu
);
112 /* If we're going to ignore the packet, we return as soon as we have
113 * failed the query (if any) and printed the warning message (if
120 case rcode_formaterror
:
121 adns__warn(ads
,serv
,qu
,"server cannot understand our query (Format Error)");
122 if (qu
) adns__query_fail(qu
,adns_s_rcodeformaterror
);
125 if (qu
) adns__query_fail(qu
,adns_s_rcodeservfail
);
126 else adns__debug(ads
,serv
,qu
,"server failure on unidentifiable query");
129 adns__warn(ads
,serv
,qu
,"server claims not to implement our query");
130 if (qu
) adns__query_fail(qu
,adns_s_rcodenotimplemented
);
133 adns__debug(ads
,serv
,qu
,"server refused our query");
134 if (qu
) adns__query_fail(qu
,adns_s_rcoderefused
);
137 adns__warn(ads
,serv
,qu
,"server gave unknown response code %d",rcode
);
138 if (qu
) adns__query_fail(qu
,adns_s_rcodeunknown
);
144 adns__diag(ads
,serv
,0,"server sent reply without quoting our question");
145 } else if (qdcount
>1) {
146 adns__diag(ads
,serv
,0,"server claimed to answer %d questions with one message",
148 } else if (ads
->iflags
& adns_if_debug
) {
149 adns__vbuf_init(&tempvb
);
150 adns__debug(ads
,serv
,0,"reply not found, id %02x, query owner %s",
151 id
, adns__diag_domain(ads
,serv
,0,&tempvb
,dgram
,dglen
,DNS_HDRSIZE
));
152 adns__vbuf_free(&tempvb
);
157 /* We're definitely going to do something with this packet and this query now. */
159 anstart
= qu
->query_dglen
;
162 /* Now, take a look at the answer section, and see if it is complete.
163 * If it has any CNAMEs we stuff them in the answer.
167 for (rri
= 0; rri
<ancount
; rri
++) {
169 st
= adns__findrr(qu
,serv
, dgram
,dglen
,&cbyte
,
170 &rrtype
,&rrclass
,&ttl
, &rdlength
,&rdstart
,
172 if (st
) { adns__query_fail(qu
,st
); return; }
173 if (rrtype
== -1) goto x_truncated
;
175 if (rrclass
!= DNS_CLASS_IN
) {
176 adns__diag(ads
,serv
,qu
,"ignoring answer RR with wrong class %d (expected IN=%d)",
177 rrclass
,DNS_CLASS_IN
);
181 if (ads
->iflags
& adns_if_debug
) {
182 adns__debug(ads
,serv
,qu
,"ignoring RR with an unexpected owner %s",
183 adns__diag_domain(ads
,serv
,qu
, &qu
->vb
, dgram
,dglen
,rrstart
));
187 if (rrtype
== adns_r_cname
&&
188 (qu
->typei
->type
& adns__rrt_typemask
) != adns_r_cname
) {
189 if (qu
->flags
& adns_qf_cname_forbid
) {
190 adns__query_fail(qu
,adns_s_prohibitedcname
);
192 } else if (qu
->cname_dgram
) { /* Ignore second and subsequent CNAME(s) */
193 adns__debug(ads
,serv
,qu
,"allegedly canonical name %s is actually alias for %s",
195 adns__diag_domain(ads
,serv
,qu
, &qu
->vb
, dgram
,dglen
,rdstart
));
196 adns__query_fail(qu
,adns_s_prohibitedcname
);
198 } else if (wantedrrs
) { /* Ignore CNAME(s) after RR(s). */
199 adns__debug(ads
,serv
,qu
,"ignoring CNAME (to %s) coexisting with RR",
200 adns__diag_domain(ads
,serv
,qu
, &qu
->vb
, dgram
,dglen
,rdstart
));
202 qu
->cname_begin
= rdstart
;
203 qu
->cname_dglen
= dglen
;
204 st
= adns__parse_domain(ads
,serv
,qu
, &qu
->vb
,
205 qu
->flags
& adns_qf_quotefail_cname
? 0 : pdf_quoteok
,
206 dgram
,dglen
, &rdstart
,rdstart
+rdlength
);
207 if (!qu
->vb
.used
) goto x_truncated
;
208 if (st
) { adns__query_fail(qu
,st
); return; }
209 l
= strlen(qu
->vb
.buf
)+1;
210 qu
->answer
->cname
= adns__alloc_preserved(qu
,(size_t) l
);
211 if (!qu
->answer
->cname
) { adns__query_fail(qu
,adns_s_nomemory
); return; }
213 qu
->cname_dgram
= adns__alloc_mine(qu
, (size_t) dglen
);
214 memcpy(qu
->cname_dgram
,dgram
,(size_t) dglen
);
216 memcpy(qu
->answer
->cname
,qu
->vb
.buf
, (size_t) l
);
218 adns__update_expires(qu
,ttl
,now
);
219 /* If we find the answer section truncated after this point we restart
220 * the query at the CNAME; if beforehand then we obviously have to use
221 * TCP. If there is no truncation we can use the whole answer if
222 * it contains the relevant info.
225 } else if (rrtype
== (qu
->typei
->type
& adns__rrt_typemask
)) {
228 adns__debug(ads
,serv
,qu
,"ignoring answer RR with irrelevant type %d",rrtype
);
232 /* We defer handling truncated responses here, in case there was a CNAME
233 * which we could use.
235 if (flg_tc
) goto x_truncated
;
240 /* Oops, NODATA or NXDOMAIN or perhaps a referral (which would be a problem) */
242 /* RFC2308: NODATA has _either_ a SOA _or_ _no_ NS records in authority section */
243 foundsoa
= 0; soattl
= 0; foundns
= 0;
244 for (rri
= 0; rri
<nscount
; rri
++) {
246 st
= adns__findrr(qu
,serv
, dgram
,dglen
,&cbyte
,
247 &rrtype
,&rrclass
,&ttl
, &rdlength
,&rdstart
, 0);
248 if (st
) { adns__query_fail(qu
,st
); return; }
249 if (rrtype
==-1) goto x_truncated
;
250 if (rrclass
!= DNS_CLASS_IN
) {
251 adns__diag(ads
,serv
,qu
,
252 "ignoring authority RR with wrong class %d (expected IN=%d)",
253 rrclass
,DNS_CLASS_IN
);
256 if (rrtype
== adns_r_soa_raw
) { foundsoa
= 1; soattl
= ttl
; break; }
257 else if (rrtype
== adns_r_ns_raw
) { foundns
= 1; }
260 if (rcode
== rcode_nxdomain
) {
261 /* We still wanted to look for the SOA so we could find the TTL. */
262 adns__update_expires(qu
,soattl
,now
);
264 if (qu
->flags
& adns_qf_search
) {
265 adns__search_next(ads
,qu
,now
);
267 adns__query_fail(qu
,adns_s_nxdomain
);
272 if (foundsoa
|| !foundns
) {
273 /* Aha ! A NODATA response, good. */
274 adns__update_expires(qu
,soattl
,now
);
275 adns__query_fail(qu
,adns_s_nodata
);
279 /* Now what ? No relevant answers, no SOA, and at least some NS's.
280 * Looks like a referral. Just one last chance ... if we came across
281 * a CNAME in this datagram then we should probably do our own CNAME
282 * lookup now in the hope that we won't get a referral again.
284 if (cname_here
) goto x_restartquery
;
286 /* Bloody hell, I thought we asked for recursion ? */
288 adns__diag(ads
,serv
,qu
,"server is not willing to do recursive lookups for us");
289 adns__query_fail(qu
,adns_s_norecurse
);
292 adns__diag(ads
,serv
,qu
,"server thinks we didn't ask for recursive lookup");
294 adns__debug(ads
,serv
,qu
,"server claims to do recursion, but gave us a referral");
295 adns__query_fail(qu
,adns_s_invalidresponse
);
300 /* Now, we have some RRs which we wanted. */
302 qu
->answer
->rrs
.untyped
= adns__alloc_interim(qu
,(size_t) qu
->typei
->rrsz
*wantedrrs
);
303 if (!qu
->answer
->rrs
.untyped
) { adns__query_fail(qu
,adns_s_nomemory
); return; }
307 rrsdata
= qu
->answer
->rrs
.bytes
;
314 pai
.nsstart
= nsstart
;
315 pai
.nscount
= nscount
;
316 pai
.arcount
= arcount
;
319 for (rri
=0, nrrs
=0; rri
<ancount
; rri
++) {
320 st
= adns__findrr(qu
,serv
, dgram
,dglen
,&cbyte
,
321 &rrtype
,&rrclass
,&ttl
, &rdlength
,&rdstart
,
323 assert(!st
); assert(rrtype
!= -1);
324 if (rrclass
!= DNS_CLASS_IN
||
325 rrtype
!= (qu
->typei
->type
& adns__rrt_typemask
) ||
328 adns__update_expires(qu
,ttl
,now
);
329 st
= typei
->parse(&pai
, rdstart
,rdstart
+rdlength
, rrsdata
+nrrs
*typei
->rrsz
);
330 if (st
) { adns__query_fail(qu
,st
); return; }
331 if (rdstart
==-1) goto x_truncated
;
334 assert(nrrs
==wantedrrs
);
335 qu
->answer
->nrrs
= nrrs
;
337 /* This may have generated some child queries ... */
338 if (qu
->children
.head
) {
339 qu
->state
= query_childw
;
340 LIST_LINK_TAIL(ads
->childw
,qu
);
343 adns__query_done(qu
);
349 adns__diag(ads
,serv
,qu
,"server sent datagram which points outside itself");
350 adns__query_fail(qu
,adns_s_invalidresponse
);
353 qu
->flags
|= adns_qf_usevc
;
356 if (qu
->cname_dgram
) {
357 st
= adns__mkquery_frdgram(qu
->ads
,&qu
->vb
,&qu
->id
,
358 qu
->cname_dgram
, qu
->cname_dglen
, qu
->cname_begin
,
359 qu
->typei
->type
, qu
->flags
);
360 if (st
) { adns__query_fail(qu
,st
); return; }
362 newquery
= realloc(qu
->query_dgram
, (size_t) qu
->vb
.used
);
363 if (!newquery
) { adns__query_fail(qu
,adns_s_nomemory
); return; }
365 qu
->query_dgram
= newquery
;
366 qu
->query_dglen
= qu
->vb
.used
;
367 memcpy(newquery
,qu
->vb
.buf
, (size_t) qu
->vb
.used
);
370 if (qu
->state
== query_tcpw
) qu
->state
= query_tosend
;
372 adns__reset_preserved(qu
);
373 adns__query_send(qu
,now
);