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