3 * - main handling and parsing routine for received datagrams
6 * This file is part of adns, which is
7 * Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson
8 * Copyright (C) 1999-2000,2003,2006 Tony Finch
9 * Copyright (C) 1991 Massachusetts Institute of Technology
10 * (See the file INSTALL for full details.)
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3, or (at your option)
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software Foundation.
30 void adns__procdgram(adns_state ads
, const byte
*dgram
, int dglen
,
31 int serv
, int viatcp
, struct timeval now
) {
32 int cbyte
, rrstart
, wantedrrs
, rri
, foundsoa
, foundns
, cname_here
;
33 int id
, f1
, f2
, qdcount
, ancount
, nscount
, arcount
;
34 int flg_ra
, flg_rd
, flg_tc
, flg_qr
, opcode
;
35 int rrtype
, rrclass
, rdlength
, rdstart
;
37 int ownermatched
, l
, nrrs
;
38 unsigned long ttl
, soattl
;
39 const typeinfo
*typei
;
44 byte
*newquery
, *rrsdata
;
47 if (dglen
<DNS_HDRSIZE
) {
48 adns__diag(ads
,serv
,0,"received datagram"
49 " too short for message header (%d)",dglen
);
60 assert(cbyte
== DNS_HDRSIZE
);
72 adns__diag(ads
,serv
,0,"server sent us a query, not a response");
76 adns__diag(ads
,serv
,0,"server sent us unknown opcode"
77 " %d (wanted 0=QUERY)",opcode
);
82 /* See if we can find the relevant query, or leave qu=0 otherwise ... */
85 for (qu
= viatcp ? ads
->tcpw
.head
: ads
->udpw
.head
; qu
; qu
= nqu
) {
87 if (qu
->id
!= id
) continue;
88 if (dglen
< qu
->query_dglen
) continue;
89 if (memcmp(qu
->query_dgram
+DNS_HDRSIZE
,
91 qu
->query_dglen
-DNS_HDRSIZE
))
94 assert(qu
->state
== query_tcpw
);
96 assert(qu
->state
== query_tosend
);
97 if (!(qu
->udpsent
& (1<<serv
))) continue;
102 /* We're definitely going to do something with this query now */
103 if (viatcp
) LIST_UNLINK(ads
->tcpw
,qu
);
104 else LIST_UNLINK(ads
->udpw
,qu
);
108 /* If we're going to ignore the packet, we return as soon as we have
109 * failed the query (if any) and printed the warning message (if
116 case rcode_formaterror
:
117 adns__warn(ads
,serv
,qu
,"server cannot understand our query"
119 if (qu
) adns__query_fail(qu
,adns_s_rcodeformaterror
);
122 if (qu
) adns__query_fail(qu
,adns_s_rcodeservfail
);
123 else adns__debug(ads
,serv
,qu
,"server failure on unidentifiable query");
126 adns__warn(ads
,serv
,qu
,"server claims not to implement our query");
127 if (qu
) adns__query_fail(qu
,adns_s_rcodenotimplemented
);
130 adns__debug(ads
,serv
,qu
,"server refused our query");
131 if (qu
) adns__query_fail(qu
,adns_s_rcoderefused
);
134 adns__warn(ads
,serv
,qu
,"server gave unknown response code %d",rcode
);
135 if (qu
) adns__query_fail(qu
,adns_s_rcodeunknown
);
141 adns__diag(ads
,serv
,0,"server sent reply without quoting our question");
142 } else if (qdcount
>1) {
143 adns__diag(ads
,serv
,0,"server claimed to answer %d"
144 " questions with one message", qdcount
);
145 } else if (ads
->iflags
& adns_if_debug
) {
146 adns__vbuf_init(&tempvb
);
147 adns__debug(ads
,serv
,0,"reply not found, id %02x, query owner %s",
148 id
, adns__diag_domain(ads
,serv
,0,&tempvb
,
149 dgram
,dglen
,DNS_HDRSIZE
));
150 adns__vbuf_free(&tempvb
);
155 /* We're definitely going to do something with this packet and this
158 anstart
= qu
->query_dglen
;
160 /* Now, take a look at the answer section, and see if it is complete.
161 * If it has any CNAMEs we stuff them in the answer.
165 for (rri
= 0; rri
<ancount
; rri
++) {
167 st
= adns__findrr(qu
,serv
, dgram
,dglen
,&cbyte
,
168 &rrtype
,&rrclass
,&ttl
, &rdlength
,&rdstart
,
170 if (st
) { adns__query_fail(qu
,st
); return; }
171 if (rrtype
== -1) goto x_truncated
;
173 if (rrclass
!= DNS_CLASS_IN
) {
174 adns__diag(ads
,serv
,qu
,"ignoring answer RR with wrong class %d"
175 " (expected IN=%d)", rrclass
,DNS_CLASS_IN
);
179 if (ads
->iflags
& adns_if_debug
) {
180 adns__debug(ads
,serv
,qu
,"ignoring RR with an unexpected owner %s",
181 adns__diag_domain(ads
,serv
,qu
, &qu
->vb
,
182 dgram
,dglen
,rrstart
));
186 if (rrtype
== adns_r_cname
&&
187 (qu
->answer
->type
& adns_rrt_typemask
) != adns_r_cname
) {
188 if (qu
->flags
& adns_qf_cname_forbid
) {
189 adns__query_fail(qu
,adns_s_prohibitedcname
);
191 } else if (qu
->cname_dgram
) { /* Ignore second and subsequent CNAME(s) */
192 adns__debug(ads
,serv
,qu
,"allegedly canonical name %s"
193 " is actually alias for %s", qu
->answer
->cname
,
194 adns__diag_domain(ads
,serv
,qu
, &qu
->vb
,
195 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
,
201 dgram
,dglen
,rdstart
));
203 qu
->cname_begin
= rdstart
;
204 qu
->cname_dglen
= dglen
;
205 st
= adns__parse_domain(ads
,serv
,qu
, &qu
->vb
,
206 qu
->flags
& adns_qf_quotefail_cname
208 dgram
,dglen
, &rdstart
,rdstart
+rdlength
);
209 if (!qu
->vb
.used
) goto x_truncated
;
210 if (st
) { adns__query_fail(qu
,st
); return; }
211 l
= strlen(qu
->vb
.buf
)+1;
212 qu
->answer
->cname
= adns__alloc_preserved(qu
,l
);
213 if (!qu
->answer
->cname
) {
214 adns__query_fail(qu
,adns_s_nomemory
);
218 qu
->cname_dgram
= adns__alloc_mine(qu
,dglen
);
219 memcpy(qu
->cname_dgram
,dgram
,dglen
);
221 memcpy(qu
->answer
->cname
,qu
->vb
.buf
,l
);
223 adns__update_expires(qu
,ttl
,now
);
224 /* If we find the answer section truncated after this point we restart
225 * the query at the CNAME; if beforehand then we obviously have to use
226 * TCP. If there is no truncation we can use the whole answer if
227 * it contains the relevant info.
230 } else if (rrtype
== (qu
->answer
->type
& adns_rrt_typemask
)) {
233 adns__debug(ads
,serv
,qu
,"ignoring answer RR"
234 " with irrelevant type %d",rrtype
);
238 /* We defer handling truncated responses here, in case there was a CNAME
239 * which we could use.
241 if (flg_tc
) goto x_truncated
;
246 /* Oops, NODATA or NXDOMAIN or perhaps a referral
247 * (which would be a problem) */
249 /* RFC2308: NODATA has _either_ a SOA _or_ _no_ NS records
250 * in authority section */
251 foundsoa
= 0; soattl
= 0; foundns
= 0;
252 for (rri
= 0; rri
<nscount
; rri
++) {
254 st
= adns__findrr(qu
,serv
, dgram
,dglen
,&cbyte
,
255 &rrtype
,&rrclass
,&ttl
, &rdlength
,&rdstart
, 0);
256 if (st
) { adns__query_fail(qu
,st
); return; }
257 if (rrtype
==-1) goto x_truncated
;
258 if (rrclass
!= DNS_CLASS_IN
) {
259 adns__diag(ads
,serv
,qu
,
260 "ignoring authority RR with wrong class %d"
261 " (expected IN=%d)", rrclass
,DNS_CLASS_IN
);
264 if (rrtype
== adns_r_soa_raw
) { foundsoa
= 1; soattl
= ttl
; break; }
265 else if (rrtype
== adns_r_ns_raw
) { foundns
= 1; }
268 if (rcode
== rcode_nxdomain
) {
269 /* We still wanted to look for the SOA so we could find the TTL. */
270 adns__update_expires(qu
,soattl
,now
);
272 if (qu
->flags
& adns_qf_search
&& !qu
->cname_dgram
) {
273 adns__search_next(ads
,qu
,now
);
275 adns__query_fail(qu
,adns_s_nxdomain
);
280 if (foundsoa
|| !foundns
) {
281 /* Aha ! A NODATA response, good. */
282 adns__update_expires(qu
,soattl
,now
);
283 adns__query_fail(qu
,adns_s_nodata
);
287 /* Now what ? No relevant answers, no SOA, and at least some NS's.
288 * Looks like a referral. Just one last chance ... if we came across
289 * a CNAME in this datagram then we should probably do our own CNAME
290 * lookup now in the hope that we won't get a referral again.
292 if (cname_here
) goto x_restartquery
;
294 /* Bloody hell, I thought we asked for recursion ? */
296 adns__diag(ads
,serv
,qu
,"server is not willing"
297 " to do recursive lookups for us");
298 adns__query_fail(qu
,adns_s_norecurse
);
301 adns__diag(ads
,serv
,qu
,"server thinks"
302 " we didn't ask for recursive lookup");
304 adns__debug(ads
,serv
,qu
,"server claims to do recursion,"
305 " but gave us a referral");
306 adns__query_fail(qu
,adns_s_invalidresponse
);
311 /* Now, we have some RRs which we wanted. */
313 qu
->answer
->rrs
.untyped
= adns__alloc_interim(qu
,qu
->answer
->rrsz
*wantedrrs
);
314 if (!qu
->answer
->rrs
.untyped
) {
315 adns__query_fail(qu
,adns_s_nomemory
);
321 rrsdata
= qu
->answer
->rrs
.bytes
;
328 pai
.nsstart
= nsstart
;
329 pai
.nscount
= nscount
;
330 pai
.arcount
= arcount
;
333 for (rri
=0, nrrs
=0; rri
<ancount
; rri
++) {
334 st
= adns__findrr(qu
,serv
, dgram
,dglen
,&cbyte
,
335 &rrtype
,&rrclass
,&ttl
, &rdlength
,&rdstart
,
337 assert(!st
); assert(rrtype
!= -1);
338 if (rrclass
!= DNS_CLASS_IN
||
339 rrtype
!= (qu
->answer
->type
& adns_rrt_typemask
) ||
342 adns__update_expires(qu
,ttl
,now
);
343 st
= typei
->parse(&pai
, rdstart
,rdstart
+rdlength
,
344 rrsdata
+nrrs
*qu
->answer
->rrsz
);
345 if (st
) { adns__query_fail(qu
,st
); return; }
346 if (rdstart
==-1) goto x_truncated
;
349 assert(nrrs
==wantedrrs
);
350 qu
->answer
->nrrs
= nrrs
;
352 /* This may have generated some child queries ... */
353 if (qu
->children
.head
) {
354 qu
->state
= query_childw
;
355 LIST_LINK_TAIL(ads
->childw
,qu
);
358 adns__query_done(qu
);
364 adns__diag(ads
,serv
,qu
,"server sent datagram which points outside itself");
365 adns__query_fail(qu
,adns_s_invalidresponse
);
368 qu
->flags
|= adns_qf_usevc
;
371 if (qu
->cname_dgram
) {
372 st
= adns__mkquery_frdgram(qu
->ads
,&qu
->vb
,&qu
->id
,
373 qu
->cname_dgram
,qu
->cname_dglen
,qu
->cname_begin
,
374 qu
->answer
->type
, qu
->flags
);
375 if (st
) { adns__query_fail(qu
,st
); return; }
377 newquery
= realloc(qu
->query_dgram
,qu
->vb
.used
);
378 if (!newquery
) { adns__query_fail(qu
,adns_s_nomemory
); return; }
380 qu
->query_dgram
= newquery
;
381 qu
->query_dglen
= qu
->vb
.used
;
382 memcpy(newquery
,qu
->vb
.buf
,qu
->vb
.used
);
385 if (qu
->state
== query_tcpw
) qu
->state
= query_tosend
;
387 adns__reset_preserved(qu
);
388 adns__query_send(qu
,now
);