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