3 * - main handling and parsing routine for received datagrams
6 * This file is part of adns, which is Copyright Ian Jackson
7 * and contributors (see the file INSTALL for full details).
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation.
27 void adns__procdgram(adns_state ads, const byte *dgram, int dglen,
28 int serv, int viatcp, struct timeval now) {
29 int cbyte, rrstart, wantedrrs, rri, foundsoa, foundns, cname_here;
30 int id, f1, f2, qdcount, ancount, nscount, arcount;
31 int flg_ra, flg_rd, flg_tc, flg_qr, opcode;
32 int rrtype, rrclass, rdlength, rdstart;
34 int ownermatched, l, nrrs, restartfrom;
35 unsigned long ttl, soattl;
36 const typeinfo *typei;
41 byte *newquery, *rrsdata;
44 if (dglen<DNS_HDRSIZE) {
45 adns__diag(ads,serv,0,"received datagram"
46 " too short for message header (%d)",dglen);
57 assert(cbyte == DNS_HDRSIZE);
69 adns__diag(ads,serv,0,"server sent us a query, not a response");
73 adns__diag(ads,serv,0,"server sent us unknown opcode"
74 " %d (wanted 0=QUERY)",opcode);
79 /* See if we can find the relevant query, or leave qu=0 otherwise ... */
82 for (qu= viatcp ? ads->tcpw.head : ads->udpw.head; qu; qu= nqu) {
84 if (qu->id != id) continue;
85 if (dglen < qu->query_dglen) continue;
86 if (memcmp(qu->query_dgram+DNS_HDRSIZE,
88 qu->query_dglen-DNS_HDRSIZE))
91 assert(qu->state == query_tcpw);
93 assert(qu->state == query_tosend);
94 if (!(qu->udpsent & (1<<serv))) continue;
99 /* We're definitely going to do something with this query now */
100 if (viatcp) LIST_UNLINK(ads->tcpw,qu);
101 else LIST_UNLINK(ads->udpw,qu);
105 /* If we're going to ignore the packet, we return as soon as we have
106 * failed the query (if any) and printed the warning message (if
113 case rcode_formaterror:
114 adns__warn(ads,serv,qu,"server cannot understand our query"
116 if (qu) adns__query_fail(qu,adns_s_rcodeformaterror);
119 if (qu) adns__query_fail(qu,adns_s_rcodeservfail);
120 else adns__debug(ads,serv,qu,"server failure on unidentifiable query");
123 adns__warn(ads,serv,qu,"server claims not to implement our query");
124 if (qu) adns__query_fail(qu,adns_s_rcodenotimplemented);
127 adns__debug(ads,serv,qu,"server refused our query");
128 if (qu) adns__query_fail(qu,adns_s_rcoderefused);
131 adns__warn(ads,serv,qu,"server gave unknown response code %d",rcode);
132 if (qu) adns__query_fail(qu,adns_s_rcodeunknown);
138 adns__diag(ads,serv,0,"server sent reply without quoting our question");
139 } else if (qdcount>1) {
140 adns__diag(ads,serv,0,"server claimed to answer %d"
141 " questions with one message", qdcount);
142 } else if (ads->iflags & adns_if_debug) {
143 adns__vbuf_init(&tempvb);
144 adns__debug(ads,serv,0,"reply not found, id %02x, query owner %s",
145 id, adns__diag_domain(ads,serv,0,&tempvb,
146 dgram,dglen,DNS_HDRSIZE));
147 adns__vbuf_free(&tempvb);
152 /* We're definitely going to do something with this packet and this
155 anstart= qu->query_dglen;
157 /* Now, take a look at the answer section, and see if it is complete.
158 * If it has any CNAMEs we stuff them in the answer.
163 for (rri= 0; rri<ancount; rri++) {
165 st= adns__findrr(qu,serv, dgram,dglen,&cbyte,
166 &rrtype,&rrclass,&ttl, &rdlength,&rdstart,
168 if (st) { adns__query_fail(qu,st); return; }
169 if (rrtype == -1) goto x_truncated;
171 if (rrclass != DNS_CLASS_IN) {
172 adns__diag(ads,serv,qu,"ignoring answer RR with wrong class %d"
173 " (expected IN=%d)", rrclass,DNS_CLASS_IN);
177 if (ads->iflags & adns_if_debug) {
178 adns__debug(ads,serv,qu,"ignoring RR with an unexpected owner %s",
179 adns__diag_domain(ads,serv,qu, &qu->vb,
180 dgram,dglen,rrstart));
184 if (rrtype == adns_r_cname &&
185 (qu->answer->type & adns_rrt_typemask) != adns_r_cname) {
186 if (qu->flags & adns_qf_cname_forbid) {
187 adns__query_fail(qu,adns_s_prohibitedcname);
189 } else if (qu->cname_dgram) { /* Ignore second and subsequent CNAME(s) */
190 adns__debug(ads,serv,qu,"allegedly canonical name %s"
191 " is actually alias for %s", qu->answer->cname,
192 adns__diag_domain(ads,serv,qu, &qu->vb,
193 dgram,dglen,rdstart));
194 adns__query_fail(qu,adns_s_prohibitedcname);
196 } else if (wantedrrs) { /* Ignore CNAME(s) after RR(s). */
197 adns__debug(ads,serv,qu,"ignoring CNAME (to %s) coexisting with RR",
198 adns__diag_domain(ads,serv,qu, &qu->vb,
199 dgram,dglen,rdstart));
201 qu->cname_begin= rdstart;
202 qu->cname_dglen= dglen;
203 st= adns__parse_domain(ads,serv,qu, &qu->vb,
204 qu->flags & adns_qf_quotefail_cname
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,l);
211 if (!qu->answer->cname) {
212 adns__query_fail(qu,adns_s_nomemory);
216 qu->cname_dgram= adns__alloc_mine(qu,dglen);
217 memcpy(qu->cname_dgram,dgram,dglen);
219 memcpy(qu->answer->cname,qu->vb.buf,l);
221 adns__update_expires(qu,ttl,now);
222 /* If we find the answer section truncated after this point we restart
223 * the query at the CNAME; if beforehand then we obviously have to use
224 * TCP. If there is no truncation we can use the whole answer if
225 * it contains the relevant info.
228 } else if (rrtype == (qu->answer->type & adns_rrt_typemask)) {
229 if (restartfrom==-1) restartfrom= rri;
232 adns__debug(ads,serv,qu,"ignoring answer RR"
233 " with irrelevant type %d",rrtype);
237 /* We defer handling truncated responses here, in case there was a CNAME
238 * which we could use.
240 if (flg_tc) goto x_truncated;
245 /* Oops, NODATA or NXDOMAIN or perhaps a referral
246 * (which would be a problem) */
248 /* RFC2308: NODATA has _either_ a SOA _or_ _no_ NS records
249 * in authority section */
250 foundsoa= 0; soattl= 0; foundns= 0;
251 for (rri= 0; rri<nscount; rri++) {
253 st= adns__findrr(qu,serv, dgram,dglen,&cbyte,
254 &rrtype,&rrclass,&ttl, &rdlength,&rdstart, 0);
255 if (st) { adns__query_fail(qu,st); return; }
256 if (rrtype==-1) goto x_truncated;
257 if (rrclass != DNS_CLASS_IN) {
258 adns__diag(ads,serv,qu,
259 "ignoring authority RR with wrong class %d"
260 " (expected IN=%d)", rrclass,DNS_CLASS_IN);
263 if (rrtype == adns_r_soa_raw) { foundsoa= 1; soattl= ttl; break; }
264 else if (rrtype == adns_r_ns_raw) { foundns= 1; }
267 if (rcode == rcode_nxdomain) {
268 /* We still wanted to look for the SOA so we could find the TTL. */
269 adns__update_expires(qu,soattl,now);
271 if (qu->flags & adns_qf_search && !qu->cname_dgram) {
272 adns__search_next(ads,qu,now);
274 adns__query_fail(qu,adns_s_nxdomain);
279 if (foundsoa || !foundns) {
280 /* Aha ! A NODATA response, good. */
281 adns__update_expires(qu,soattl,now);
282 adns__query_fail(qu,adns_s_nodata);
286 /* Now what ? No relevant answers, no SOA, and at least some NS's.
287 * Looks like a referral. Just one last chance ... if we came across
288 * a CNAME in this datagram then we should probably do our own CNAME
289 * lookup now in the hope that we won't get a referral again.
291 if (cname_here) goto x_restartquery;
293 /* Bloody hell, I thought we asked for recursion ? */
295 adns__diag(ads,serv,qu,"server is not willing"
296 " to do recursive lookups for us");
297 adns__query_fail(qu,adns_s_norecurse);
300 adns__diag(ads,serv,qu,"server thinks"
301 " we didn't ask for recursive lookup");
303 adns__debug(ads,serv,qu,"server claims to do recursion,"
304 " but gave us a referral");
305 adns__query_fail(qu,adns_s_invalidresponse);
310 /* Now, we have some RRs which we wanted. */
312 qu->answer->rrs.untyped= adns__alloc_interim(qu,qu->answer->rrsz*wantedrrs);
313 if (!qu->answer->rrs.untyped) {
314 adns__query_fail(qu,adns_s_nomemory);
320 rrsdata= qu->answer->rrs.bytes;
327 pai.nsstart= nsstart;
328 pai.nscount= nscount;
329 pai.arcount= arcount;
332 assert(restartfrom>=0);
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 (rri < restartfrom ||
339 rrtype != (qu->answer->type & adns_rrt_typemask) ||
340 rrclass != DNS_CLASS_IN ||
343 adns__update_expires(qu,ttl,now);
344 st= typei->parse(&pai, rdstart,rdstart+rdlength,
345 rrsdata+nrrs*qu->answer->rrsz);
346 if (st) { adns__query_fail(qu,st); return; }
347 if (rdstart==-1) goto x_truncated;
350 assert(nrrs==wantedrrs);
351 qu->answer->nrrs= nrrs;
353 /* This may have generated some child queries ... */
354 if (qu->children.head) {
355 qu->state= query_childw;
356 LIST_LINK_TAIL(ads->childw,qu);
359 adns__query_done(qu);
365 adns__diag(ads,serv,qu,"server sent datagram which points outside itself");
366 adns__query_fail(qu,adns_s_invalidresponse);
369 if (qu->flags & adns_qf_usevc) {
370 adns__diag(ads,serv,qu,"server sent datagram with TC over TCP");
371 adns__query_fail(qu,adns_s_invalidresponse);
374 qu->flags |= adns_qf_usevc;
377 if (qu->cname_dgram) {
378 st= adns__mkquery_frdgram(qu->ads,&qu->vb,&qu->id,
379 qu->cname_dgram,qu->cname_dglen,qu->cname_begin,
380 qu->answer->type, qu->flags);
381 if (st) { adns__query_fail(qu,st); return; }
383 newquery= realloc(qu->query_dgram,qu->vb.used);
384 if (!newquery) { adns__query_fail(qu,adns_s_nomemory); return; }
386 qu->query_dgram= newquery;
387 qu->query_dglen= qu->vb.used;
388 memcpy(newquery,qu->vb.buf,qu->vb.used);
391 if (qu->state == query_tcpw) qu->state= query_tosend;
393 adns__reset_preserved(qu);
394 adns__query_send(qu,now);