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