Proper spec. of NULL handling.
[adns] / src / reply.c
CommitLineData
e576be50 1/*
2 * reply.c
3 * - main handling and parsing routine for received datagrams
4 */
5/*
6 * This file is part of adns, which is Copyright (C) 1997, 1998 Ian Jackson
7 *
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)
11 * any later version.
12 *
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.
17 *
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.
21 */
4353a5c4 22
23#include "internal.h"
24
3955725c 25static void cname_recurse(adns_query qu, adns_queryflags xflags) {
ffbda80c 26 adns__diag(qu->ads,-1,qu,"cname following not implemented fixme");
27 adns__query_fail(qu,adns_s_notimplemented);
b9de380c 28}
29
98a3f706 30void adns__procdgram(adns_state ads, const byte *dgram, int dglen,
31 int serv, struct timeval now) {
86e7b8d9 32 int cbyte, rrstart, wantedrrs, rri, foundsoa, foundns, cname_here;
3955725c 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, ownermatched, l;
98a3f706 36 int anstart, nsstart, arstart;
98a3f706 37 adns_query qu, nqu;
38 dns_rcode rcode;
b9de380c 39 adns_status st;
86e7b8d9 40 vbuf tempvb;
ec477b9e 41
98a3f706 42 if (dglen<DNS_HDRSIZE) {
3955725c 43 adns__diag(ads,serv,0,"received datagram too short for message header (%d)",dglen);
ec477b9e 44 return;
45 }
9557e604 46 cbyte= 0;
b9de380c 47 GET_W(cbyte,id);
48 GET_B(cbyte,f1);
49 GET_B(cbyte,f2);
50 GET_W(cbyte,qdcount);
51 GET_W(cbyte,ancount);
52 GET_W(cbyte,nscount);
53 GET_W(cbyte,arcount);
98a3f706 54 assert(cbyte == DNS_HDRSIZE);
ec477b9e 55
31144a72 56 flg_qr= f1&0x80;
57 opcode= (f1&0x78)>>3;
98a3f706 58 flg_tc= f1&0x20;
31144a72 59 flg_rd= f1&0x01;
0ba0614a 60 flg_ra= f2&0x80;
86e7b8d9 61 rcode= (f2&0x0f);
0ba0614a 62
86e7b8d9 63 cname_here= 0;
64
9557e604 65 if (!flg_qr) {
3955725c 66 adns__diag(ads,serv,0,"server sent us a query, not a response");
ec477b9e 67 return;
68 }
31144a72 69 if (opcode) {
3955725c 70 adns__diag(ads,serv,0,"server sent us unknown opcode %d (wanted 0=QUERY)",opcode);
ec477b9e 71 return;
72 }
73 if (!qdcount) {
3955725c 74 adns__diag(ads,serv,0,"server sent reply without quoting our question");
ec477b9e 75 return;
76 } else if (qdcount>1) {
3955725c 77 adns__diag(ads,serv,0,"server claimed to answer %d questions with one message",
ec477b9e 78 qdcount);
79 return;
80 }
98a3f706 81 for (qu= ads->timew.head; qu; qu= nqu) {
ec477b9e 82 nqu= qu->next;
83 if (qu->id != id) continue;
3955725c 84 if (dglen < qu->query_dglen) continue;
85 if (memcmp(qu->query_dgram+DNS_HDRSIZE,
86 dgram+DNS_HDRSIZE,
87 qu->query_dglen-DNS_HDRSIZE))
b9de380c 88 continue;
ec477b9e 89 break;
90 }
91 if (!qu) {
86e7b8d9 92 if (ads->iflags & adns_if_debug) {
93 adns__vbuf_init(&tempvb);
94 adns__debug(ads,serv,0,"reply not found, id %02x, query owner %s",
95 id, adns__diag_domain(ads,serv,0,&tempvb,adns_qf_anyquote,
96 dgram,dglen,DNS_HDRSIZE));
97 adns__vbuf_free(&tempvb);
98 }
ec477b9e 99 return;
100 }
86e7b8d9 101 anstart= qu->query_dglen;
98a3f706 102
103 LIST_UNLINK(ads->timew,qu);
104 /* We're definitely going to do something with this query now */
105
b9de380c 106 switch (rcode) {
107 case rcode_noerror:
108 case rcode_nxdomain:
ec477b9e 109 break;
b9de380c 110 case rcode_formaterror:
31144a72 111 adns__warn(ads,serv,qu,"server cannot understand our query (Format Error)");
3955725c 112 adns__query_fail(qu,adns_s_serverfaulty);
ec477b9e 113 return;
98a3f706 114 case rcode_servfail:
3955725c 115 adns__query_fail(qu,adns_s_servfail);
ec477b9e 116 return;
b9de380c 117 case rcode_notimp:
31144a72 118 adns__warn(ads,serv,qu,"server claims not to implement our query");
3955725c 119 adns__query_fail(qu,adns_s_notimplemented);
b9de380c 120 return;
121 case rcode_refused:
31144a72 122 adns__warn(ads,serv,qu,"server refused our query");
3955725c 123 adns__query_fail(qu,adns_s_refused);
b9de380c 124 return;
125 default:
31144a72 126 adns__warn(ads,serv,qu,"server gave unknown response code %d",rcode);
3955725c 127 adns__query_fail(qu,adns_s_reasonunknown);
b9de380c 128 return;
129 }
130
131 /* Now, take a look at the answer section, and see if it is complete.
132 * If it has any CNAMEs we stuff them in the answer.
133 */
134 wantedrrs= 0;
86e7b8d9 135 cbyte= anstart;
b9de380c 136 for (rri= 0; rri<ancount; rri++) {
137 rrstart= cbyte;
3955725c 138 st= adns__findrr(qu,serv, dgram,dglen,&cbyte,
139 &rrtype,&rrclass,&rdlength,&rdstart,
140 &ownermatched);
86e7b8d9 141 if (st) { adns__query_fail(qu,st); return; }
0ba0614a 142 if (rrtype == -1) goto x_truncated;
143
b9de380c 144 if (rrclass != DNS_CLASS_IN) {
31144a72 145 adns__diag(ads,serv,qu,"ignoring answer RR with wrong class %d (expected IN=%d)",
b9de380c 146 rrclass,DNS_CLASS_IN);
147 continue;
148 }
0ba0614a 149 if (!ownermatched) {
98a3f706 150 if (ads->iflags & adns_if_debug) {
31144a72 151 adns__debug(ads,serv,qu,"ignoring RR with an unexpected owner %s",
3955725c 152 adns__diag_domain(ads,serv,qu, &qu->vb,qu->flags,
153 dgram,dglen,rrstart));
0ba0614a 154 }
b9de380c 155 continue;
156 }
31144a72 157 if (rrtype == adns_r_cname &&
158 (qu->typei->type & adns__rrt_typemask) != adns_r_cname) {
3955725c 159 if (!qu->cname_dgram) { /* Ignore second and subsequent CNAMEs */
160 qu->cname_dgram= adns__alloc_mine(qu,dglen);
161 if (!qu->cname_dgram) return;
31144a72 162 qu->cname_begin= rdstart;
31144a72 163 qu->cname_dglen= dglen;
3955725c 164 st= adns__parse_domain(ads,serv,qu, &qu->vb,qu->flags,
31144a72 165 dgram,dglen, &rdstart,rdstart+rdlength);
3955725c 166 if (!qu->vb.used) goto x_truncated;
167 if (st) { adns__query_fail(qu,st); return; }
168 l= strlen(qu->vb.buf)+1;
169 qu->answer->cname= adns__alloc_interim(qu,l);
8e5b0abb 170 if (!qu->answer->cname) return;
3955725c 171 memcpy(qu->answer->cname,qu->vb.buf,l);
86e7b8d9 172 cname_here= 1;
31144a72 173 /* If we find the answer section truncated after this point we restart
174 * the query at the CNAME; if beforehand then we obviously have to use
175 * TCP. If there is no truncation we can use the whole answer if
176 * it contains the relevant info.
177 */
178 } else {
179 adns__debug(ads,serv,qu,"ignoring duplicate CNAME (%s, as well as %s)",
3955725c 180 adns__diag_domain(ads,serv,qu, &qu->vb,qu->flags,
181 dgram,dglen,rdstart),
182 qu->answer->cname);
31144a72 183 }
98a3f706 184 } else if (rrtype == (qu->typei->type & adns__rrt_typemask)) {
b9de380c 185 wantedrrs++;
186 } else {
31144a72 187 adns__debug(ads,serv,qu,"ignoring answer RR with irrelevant type %d",rrtype);
b9de380c 188 }
189 }
190
191 /* If we got here then the answer section is intact. */
192 nsstart= cbyte;
193
194 if (!wantedrrs) {
195 /* Oops, NODATA or NXDOMAIN or perhaps a referral (which would be a problem) */
196
197 if (rcode == rcode_nxdomain) {
3955725c 198 adns__query_fail(qu,adns_s_nxdomain);
b9de380c 199 return;
200 }
201
202 /* RFC2308: NODATA has _either_ a SOA _or_ _no_ NS records in authority section */
0ba0614a 203 foundsoa= 0; foundns= 0;
b9de380c 204 for (rri= 0; rri<nscount; rri++) {
0ba0614a 205 rrstart= cbyte;
3955725c 206 st= adns__findrr(qu,serv, dgram,dglen,&cbyte,
207 &rrtype,&rrclass,&rdlength,&rdstart, 0);
208 if (st) { adns__query_fail(qu,st); return; }
0ba0614a 209 if (rrtype==-1) goto x_truncated;
210 if (rrclass != DNS_CLASS_IN) {
31144a72 211 adns__diag(ads,serv,qu,
212 "ignoring authority RR with wrong class %d (expected IN=%d)",
0ba0614a 213 rrclass,DNS_CLASS_IN);
214 continue;
215 }
216 if (rrtype == adns_r_soa_raw) { foundsoa= 1; break; }
217 else if (rrtype == adns_r_ns_raw) { foundns= 1; }
b9de380c 218 }
0ba0614a 219
220 if (foundsoa || !foundns) {
221 /* Aha ! A NODATA response, good. */
3955725c 222 adns__query_fail(qu,adns_s_nodata);
0ba0614a 223 return;
224 }
225
226 /* Now what ? No relevant answers, no SOA, and at least some NS's.
227 * Looks like a referral. Just one last chance ... if we came across
31144a72 228 * a CNAME in this datagram then we should probably do our own CNAME
229 * lookup now in the hope that we won't get a referral again.
0ba0614a 230 */
86e7b8d9 231 if (cname_here) { cname_recurse(qu,0); return; }
0ba0614a 232
233 /* Bloody hell, I thought we asked for recursion ? */
31144a72 234 if (flg_rd) {
235 adns__diag(ads,serv,qu,"server thinks we didn't ask for recursive lookup");
236 }
0ba0614a 237 if (!flg_ra) {
31144a72 238 adns__diag(ads,serv,qu,"server is not willing to do recursive lookups for us");
3955725c 239 adns__query_fail(qu,adns_s_norecurse);
31144a72 240 } else {
241 adns__diag(ads,serv,qu,"server claims to do recursion, but gave us a referral");
3955725c 242 adns__query_fail(qu,adns_s_serverfaulty);
0ba0614a 243 }
b9de380c 244 return;
245 }
246
0ba0614a 247 /* Now, we have some RRs which we wanted. */
b9de380c 248
3955725c 249 qu->answer->rrs.untyped= adns__alloc_interim(qu,qu->typei->rrsz*wantedrrs);
250 if (!qu->answer->rrs.untyped) return;
98a3f706 251
252 cbyte= anstart;
98a3f706 253 arstart= -1;
254 for (rri=0; rri<ancount; rri++) {
3955725c 255 st= adns__findrr(qu,serv, dgram,dglen,&cbyte,
256 &rrtype,&rrclass,&rdlength,&rdstart,
257 &ownermatched);
98a3f706 258 assert(!st); assert(rrtype != -1);
259 if (rrclass != DNS_CLASS_IN ||
31144a72 260 rrtype != (qu->typei->type & adns__rrt_typemask) ||
261 !ownermatched)
98a3f706 262 continue;
3955725c 263 assert(qu->answer->nrrs<wantedrrs);
264 st= qu->typei->parse(qu,serv,
265 dgram,dglen, rdstart,rdstart+rdlength,
266 qu->answer->rrs.bytes+qu->answer->nrrs*qu->typei->rrsz);
267 if (st) { adns__query_fail(qu,st); return; }
31144a72 268 if (rdstart==-1) goto x_truncated;
86e7b8d9 269 qu->answer->nrrs++;
98a3f706 270 }
b9de380c 271
98a3f706 272 /* This may have generated some child queries ... */
273 if (qu->children.head) {
274 qu->state= query_child;
275 LIST_LINK_TAIL(ads->childw,qu);
276 return;
b9de380c 277 }
ec477b9e 278
3955725c 279 adns__query_done(qu);
98a3f706 280 return;
0ba0614a 281
e576be50 282 x_truncated:
98a3f706 283 if (!flg_tc) {
31144a72 284 adns__diag(ads,serv,qu,"server sent datagram which points outside itself");
3955725c 285 adns__query_fail(qu,adns_s_serverfaulty);
98a3f706 286 return;
287 }
3955725c 288 if (qu->cname_dgram) { cname_recurse(qu,adns_qf_usevc); return; }
289 adns__reset_cnameonly(qu);
98a3f706 290 qu->flags |= adns_qf_usevc;
3955725c 291 adns__query_udp(qu,now);
98a3f706 292}