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