3 * - main handling and parsing routine for received datagrams
6 * This file is part of adns, which is Copyright (C) 1997, 1998 Ian Jackson
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 static void cname_recurse(adns_query qu
, adns_queryflags xflags
) {
29 void adns__procdgram(adns_state ads
, const byte
*dgram
, int dglen
,
30 int serv
, struct timeval now
) {
31 int cbyte
, rrstart
, wantedrrs
, rri
, foundsoa
, foundns
;
32 int id
, f1
, f2
, qdcount
, ancount
, nscount
, arcount
;
33 int flg_ra
, flg_rd
, flg_tc
, flg_qr
, opcode
;
34 int rrtype
, rrclass
, rdlength
, rdstart
, ownermatched
, l
;
35 int anstart
, nsstart
, arstart
;
40 if (dglen
<DNS_HDRSIZE
) {
41 adns__diag(ads
,serv
,0,"received datagram too short for message header (%d)",dglen
);
52 assert(cbyte
== DNS_HDRSIZE
);
62 adns__diag(ads
,serv
,0,"server sent us a query, not a response");
66 adns__diag(ads
,serv
,0,"server sent us unknown opcode %d (wanted 0=QUERY)",opcode
);
70 adns__diag(ads
,serv
,0,"server sent reply without quoting our question");
72 } else if (qdcount
>1) {
73 adns__diag(ads
,serv
,0,"server claimed to answer %d questions with one message",
77 for (qu
= ads
->timew
.head
; qu
; qu
= nqu
) {
79 if (qu
->id
!= id
) continue;
80 if (dglen
< qu
->query_dglen
) continue;
81 if (memcmp(qu
->query_dgram
+DNS_HDRSIZE
,
83 qu
->query_dglen
-DNS_HDRSIZE
))
87 anstart
= qu
->query_dglen
;
89 adns__debug(ads
,serv
,0,"reply not found (id=%02x)",id
);
93 LIST_UNLINK(ads
->timew
,qu
);
94 /* We're definitely going to do something with this query now */
100 case rcode_formaterror
:
101 adns__warn(ads
,serv
,qu
,"server cannot understand our query (Format Error)");
102 adns__query_fail(qu
,adns_s_serverfaulty
);
105 adns__query_fail(qu
,adns_s_servfail
);
108 adns__warn(ads
,serv
,qu
,"server claims not to implement our query");
109 adns__query_fail(qu
,adns_s_notimplemented
);
112 adns__warn(ads
,serv
,qu
,"server refused our query");
113 adns__query_fail(qu
,adns_s_refused
);
116 adns__warn(ads
,serv
,qu
,"server gave unknown response code %d",rcode
);
117 adns__query_fail(qu
,adns_s_reasonunknown
);
121 /* Now, take a look at the answer section, and see if it is complete.
122 * If it has any CNAMEs we stuff them in the answer.
125 for (rri
= 0; rri
<ancount
; rri
++) {
127 st
= adns__findrr(qu
,serv
, dgram
,dglen
,&cbyte
,
128 &rrtype
,&rrclass
,&rdlength
,&rdstart
,
130 if (st
) adns__query_fail(qu
,st
);
131 if (rrtype
== -1) goto x_truncated
;
133 if (rrclass
!= DNS_CLASS_IN
) {
134 adns__diag(ads
,serv
,qu
,"ignoring answer RR with wrong class %d (expected IN=%d)",
135 rrclass
,DNS_CLASS_IN
);
139 if (ads
->iflags
& adns_if_debug
) {
140 adns__debug(ads
,serv
,qu
,"ignoring RR with an unexpected owner %s",
141 adns__diag_domain(ads
,serv
,qu
, &qu
->vb
,qu
->flags
,
142 dgram
,dglen
,rrstart
));
146 if (rrtype
== adns_r_cname
&&
147 (qu
->typei
->type
& adns__rrt_typemask
) != adns_r_cname
) {
148 if (!qu
->cname_dgram
) { /* Ignore second and subsequent CNAMEs */
149 qu
->cname_dgram
= adns__alloc_mine(qu
,dglen
);
150 if (!qu
->cname_dgram
) return;
151 qu
->cname_begin
= rdstart
;
152 qu
->cname_dglen
= dglen
;
153 st
= adns__parse_domain(ads
,serv
,qu
, &qu
->vb
,qu
->flags
,
154 dgram
,dglen
, &rdstart
,rdstart
+rdlength
);
155 if (!qu
->vb
.used
) goto x_truncated
;
156 if (st
) { adns__query_fail(qu
,st
); return; }
157 l
= strlen(qu
->vb
.buf
)+1;
158 qu
->answer
->cname
= adns__alloc_interim(qu
,l
);
159 if (!qu
->answer
->cname
) return;
160 memcpy(qu
->answer
->cname
,qu
->vb
.buf
,l
);
161 /* If we find the answer section truncated after this point we restart
162 * the query at the CNAME; if beforehand then we obviously have to use
163 * TCP. If there is no truncation we can use the whole answer if
164 * it contains the relevant info.
167 adns__debug(ads
,serv
,qu
,"ignoring duplicate CNAME (%s, as well as %s)",
168 adns__diag_domain(ads
,serv
,qu
, &qu
->vb
,qu
->flags
,
169 dgram
,dglen
,rdstart
),
172 } else if (rrtype
== (qu
->typei
->type
& adns__rrt_typemask
)) {
175 adns__debug(ads
,serv
,qu
,"ignoring answer RR with irrelevant type %d",rrtype
);
179 /* If we got here then the answer section is intact. */
183 /* Oops, NODATA or NXDOMAIN or perhaps a referral (which would be a problem) */
185 if (rcode
== rcode_nxdomain
) {
186 adns__query_fail(qu
,adns_s_nxdomain
);
190 /* RFC2308: NODATA has _either_ a SOA _or_ _no_ NS records in authority section */
191 foundsoa
= 0; foundns
= 0;
192 for (rri
= 0; rri
<nscount
; rri
++) {
194 st
= adns__findrr(qu
,serv
, dgram
,dglen
,&cbyte
,
195 &rrtype
,&rrclass
,&rdlength
,&rdstart
, 0);
196 if (st
) { adns__query_fail(qu
,st
); return; }
197 if (rrtype
==-1) goto x_truncated
;
198 if (rrclass
!= DNS_CLASS_IN
) {
199 adns__diag(ads
,serv
,qu
,
200 "ignoring authority RR with wrong class %d (expected IN=%d)",
201 rrclass
,DNS_CLASS_IN
);
204 if (rrtype
== adns_r_soa_raw
) { foundsoa
= 1; break; }
205 else if (rrtype
== adns_r_ns_raw
) { foundns
= 1; }
208 if (foundsoa
|| !foundns
) {
209 /* Aha ! A NODATA response, good. */
210 adns__query_fail(qu
,adns_s_nodata
);
214 /* Now what ? No relevant answers, no SOA, and at least some NS's.
215 * Looks like a referral. Just one last chance ... if we came across
216 * a CNAME in this datagram then we should probably do our own CNAME
217 * lookup now in the hope that we won't get a referral again.
219 if (qu
->cname_dgram
== dgram
) { cname_recurse(qu
,0); return; }
221 /* Bloody hell, I thought we asked for recursion ? */
223 adns__diag(ads
,serv
,qu
,"server thinks we didn't ask for recursive lookup");
226 adns__diag(ads
,serv
,qu
,"server is not willing to do recursive lookups for us");
227 adns__query_fail(qu
,adns_s_norecurse
);
229 adns__diag(ads
,serv
,qu
,"server claims to do recursion, but gave us a referral");
230 adns__query_fail(qu
,adns_s_serverfaulty
);
235 /* Now, we have some RRs which we wanted. */
237 qu
->answer
->rrs
.untyped
= adns__alloc_interim(qu
,qu
->typei
->rrsz
*wantedrrs
);
238 if (!qu
->answer
->rrs
.untyped
) return;
242 for (rri
=0; rri
<ancount
; rri
++) {
243 st
= adns__findrr(qu
,serv
, dgram
,dglen
,&cbyte
,
244 &rrtype
,&rrclass
,&rdlength
,&rdstart
,
246 assert(!st
); assert(rrtype
!= -1);
247 if (rrclass
!= DNS_CLASS_IN
||
248 rrtype
!= (qu
->typei
->type
& adns__rrt_typemask
) ||
251 assert(qu
->answer
->nrrs
<wantedrrs
);
252 st
= qu
->typei
->parse(qu
,serv
,
253 dgram
,dglen
, rdstart
,rdstart
+rdlength
,
254 qu
->answer
->rrs
.bytes
+qu
->answer
->nrrs
*qu
->typei
->rrsz
);
255 if (st
) { adns__query_fail(qu
,st
); return; }
256 if (rdstart
==-1) goto x_truncated
;
259 /* This may have generated some child queries ... */
260 if (qu
->children
.head
) {
261 qu
->state
= query_child
;
262 LIST_LINK_TAIL(ads
->childw
,qu
);
266 adns__query_done(qu
);
271 adns__diag(ads
,serv
,qu
,"server sent datagram which points outside itself");
272 adns__query_fail(qu
,adns_s_serverfaulty
);
275 if (qu
->cname_dgram
) { cname_recurse(qu
,adns_qf_usevc
); return; }
276 adns__reset_cnameonly(qu
);
277 qu
->flags
|= adns_qf_usevc
;
278 adns__query_udp(qu
,now
);