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