98db6da3 |
1 | /* |
2 | * query.c |
3 | * - overall query management (allocation, completion) |
4 | * - per-query memory management |
5 | * - query submission and cancellation (user-visible and internal) |
6 | */ |
7 | /* |
8 | * This file is part of adns, which is Copyright (C) 1997, 1998 Ian Jackson |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License as published by |
12 | * the Free Software Foundation; either version 2, or (at your option) |
13 | * any later version. |
14 | * |
15 | * This program is distributed in the hope that it will be useful, |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | * GNU General Public License for more details. |
19 | * |
20 | * You should have received a copy of the GNU General Public License |
21 | * along with this program; if not, write to the Free Software Foundation, |
22 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
23 | */ |
d05cc330 |
24 | |
48cb0b4b |
25 | #include "internal.h" |
6f17710a |
26 | |
98db6da3 |
27 | #include <stdlib.h> |
28 | #include <unistd.h> |
29 | #include <errno.h> |
cd1bde2f |
30 | #include <string.h> |
6f17710a |
31 | |
98db6da3 |
32 | #include <sys/time.h> |
6f17710a |
33 | |
98db6da3 |
34 | #include "internal.h" |
96e79df5 |
35 | |
98db6da3 |
36 | int adns__internal_submit(adns_state ads, adns_query *query_r, |
11c8bf9b |
37 | const typeinfo *typei, vbuf *qumsg_vb, int id, |
98db6da3 |
38 | adns_queryflags flags, struct timeval now, |
39 | adns_status failstat, const qcontext *ctx) { |
40 | adns_query qu; |
98db6da3 |
41 | |
42 | qu= malloc(sizeof(*qu)); if (!qu) goto x_nomemory; |
43 | qu->answer= malloc(sizeof(*qu->answer)); if (!qu->answer) goto x_freequ_nomemory; |
44 | |
11c8bf9b |
45 | qu->ads= ads; |
98db6da3 |
46 | qu->state= query_udp; |
47 | qu->back= qu->next= qu->parent= 0; |
48 | LIST_INIT(qu->children); |
49 | qu->siblings.next= qu->siblings.back= 0; |
bc01473e |
50 | LIST_INIT(qu->allocations); |
98db6da3 |
51 | qu->interim_allocd= 0; |
11c8bf9b |
52 | qu->final_allocspace= 0; |
98db6da3 |
53 | |
11c8bf9b |
54 | qu->typei= typei; |
98db6da3 |
55 | adns__vbuf_init(&qu->vb); |
56 | |
57 | qu->cname_dgram= 0; |
58 | qu->cname_dglen= qu->cname_begin= 0; |
48cb0b4b |
59 | |
98db6da3 |
60 | qu->id= id; |
61 | qu->flags= flags; |
62 | qu->udpretries= 0; |
63 | qu->udpnextserver= 0; |
64 | qu->udpsent= qu->tcpfailed= 0; |
65 | timerclear(&qu->timeout); |
cd1bde2f |
66 | memcpy(&qu->ctx,ctx,sizeof(qu->ctx)); |
98db6da3 |
67 | |
68 | qu->answer->status= adns_s_ok; |
69 | qu->answer->cname= 0; |
11c8bf9b |
70 | qu->answer->type= typei->type; |
98db6da3 |
71 | qu->answer->nrrs= 0; |
72 | qu->answer->rrs= 0; |
11c8bf9b |
73 | qu->answer->rrsz= typei->rrsz; |
98db6da3 |
74 | |
75 | *query_r= qu; |
96e79df5 |
76 | |
f47cdeec |
77 | qu->query_dglen= qumsg_vb->used; |
78 | if (qumsg_vb->used) { |
79 | qu->query_dgram= malloc(qumsg_vb->used); |
80 | if (!qu->query_dgram) { |
9b86645c |
81 | adns__query_fail(qu,adns_s_nomemory); |
f47cdeec |
82 | return adns_s_ok; |
83 | } |
84 | memcpy(qu->query_dgram,qumsg_vb->buf,qumsg_vb->used); |
85 | } else { |
86 | qu->query_dgram= 0; |
96e79df5 |
87 | } |
98db6da3 |
88 | qu->vb= *qumsg_vb; |
89 | adns__vbuf_init(qumsg_vb); |
90 | |
91 | if (failstat) { |
11c8bf9b |
92 | adns__query_fail(qu,failstat); |
93 | return adns_s_ok; |
6f17710a |
94 | } |
11c8bf9b |
95 | adns__query_udp(qu,now); |
98db6da3 |
96 | adns__autosys(ads,now); |
97 | |
11c8bf9b |
98 | return adns_s_ok; |
98db6da3 |
99 | |
100 | x_freequ_nomemory: |
101 | free(qu); |
102 | x_nomemory: |
11c8bf9b |
103 | adns__vbuf_free(qumsg_vb); |
9b86645c |
104 | return adns_s_nomemory; |
48cb0b4b |
105 | } |
106 | |
98db6da3 |
107 | int adns_submit(adns_state ads, |
108 | const char *owner, |
109 | adns_rrtype type, |
110 | adns_queryflags flags, |
111 | void *context, |
112 | adns_query *query_r) { |
113 | qcontext ctx; |
11c8bf9b |
114 | int id, r, ol; |
98db6da3 |
115 | vbuf vb; |
11c8bf9b |
116 | adns_status stat; |
117 | const typeinfo *typei; |
118 | struct timeval now; |
98db6da3 |
119 | |
11c8bf9b |
120 | typei= adns__findtype(type); |
9b86645c |
121 | if (!typei) return adns_s_unknownrrtype; |
11c8bf9b |
122 | |
98db6da3 |
123 | ctx.ext= context; |
cd1bde2f |
124 | ctx.callback= 0; |
125 | memset(&ctx.info,0,sizeof(ctx.info)); |
126 | |
98db6da3 |
127 | r= gettimeofday(&now,0); if (r) return errno; |
128 | id= 0; |
129 | |
130 | adns__vbuf_init(&vb); |
131 | |
132 | ol= strlen(owner); |
9b86645c |
133 | if (ol<=1 || ol>DNS_MAXDOMAIN+1) { stat= adns_s_querydomaintoolong; goto xit; } |
98db6da3 |
134 | |
135 | if (owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_qf_search; ol--; } |
136 | |
11c8bf9b |
137 | stat= adns__mkquery(ads,&vb,&id, owner,ol, typei,flags); |
98db6da3 |
138 | |
139 | xit: |
11c8bf9b |
140 | return adns__internal_submit(ads,query_r, typei,&vb,id, flags,now, stat,&ctx); |
96e79df5 |
141 | } |
142 | |
98db6da3 |
143 | int adns_synchronous(adns_state ads, |
144 | const char *owner, |
145 | adns_rrtype type, |
146 | adns_queryflags flags, |
147 | adns_answer **answer_r) { |
148 | adns_query qu; |
149 | int r; |
150 | |
151 | r= adns_submit(ads,owner,type,flags,0,&qu); |
152 | if (r) return r; |
48cb0b4b |
153 | |
f318f883 |
154 | r= adns_wait(ads,&qu,answer_r,0); |
11c8bf9b |
155 | if (r) adns_cancel(qu); |
96e79df5 |
156 | |
f318f883 |
157 | return r; |
98db6da3 |
158 | } |
159 | |
c9afe7bb |
160 | static void *alloc_common(adns_query qu, size_t sz) { |
98db6da3 |
161 | allocnode *an; |
162 | |
f47cdeec |
163 | if (!sz) return qu; /* Any old pointer will do */ |
98db6da3 |
164 | assert(!qu->final_allocspace); |
98db6da3 |
165 | an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz)); |
f2ad23ee |
166 | if (!an) return 0; |
bc01473e |
167 | LIST_LINK_TAIL(qu->allocations,an); |
98db6da3 |
168 | return (byte*)an + MEM_ROUND(sizeof(*an)); |
169 | } |
48cb0b4b |
170 | |
c9afe7bb |
171 | void *adns__alloc_interim(adns_query qu, size_t sz) { |
172 | sz= MEM_ROUND(sz); |
173 | qu->interim_allocd += sz; |
174 | return alloc_common(qu,sz); |
175 | } |
176 | |
177 | void *adns__alloc_mine(adns_query qu, size_t sz) { |
178 | return alloc_common(qu,MEM_ROUND(sz)); |
179 | } |
180 | |
bc01473e |
181 | void adns__transfer_interim(adns_query from, adns_query to, void *block, size_t sz) { |
182 | allocnode *an; |
183 | |
184 | if (!block) return; |
185 | an= (void*)((byte*)block - MEM_ROUND(sizeof(*an))); |
186 | |
187 | assert(!to->final_allocspace); |
188 | assert(!from->final_allocspace); |
189 | |
190 | LIST_UNLINK(from->allocations,an); |
191 | LIST_LINK_TAIL(to->allocations,an); |
192 | |
193 | from->interim_allocd -= sz; |
194 | to->interim_allocd += sz; |
195 | } |
196 | |
98db6da3 |
197 | void *adns__alloc_final(adns_query qu, size_t sz) { |
198 | /* When we're in the _final stage, we _subtract_ from interim_alloc'd |
199 | * each allocation, and use final_allocspace to point to the next free |
200 | * bit. |
201 | */ |
202 | void *rp; |
203 | |
204 | sz= MEM_ROUND(sz); |
205 | rp= qu->final_allocspace; |
206 | assert(rp); |
207 | qu->interim_allocd -= sz; |
208 | assert(qu->interim_allocd>=0); |
209 | qu->final_allocspace= (byte*)rp + sz; |
210 | return rp; |
211 | } |
212 | |
61093792 |
213 | static void cancel_children(adns_query qu) { |
214 | adns_query cqu, ncqu; |
215 | |
216 | for (cqu= qu->children.head; cqu; cqu= ncqu) { |
217 | ncqu= cqu->siblings.next; |
218 | adns_cancel(cqu); |
219 | } |
220 | LIST_INIT(qu->children); |
221 | } |
222 | |
11c8bf9b |
223 | void adns__reset_cnameonly(adns_query qu) { |
9a2b67d4 |
224 | assert(!qu->final_allocspace); |
61093792 |
225 | cancel_children(qu); |
98db6da3 |
226 | qu->answer->nrrs= 0; |
227 | qu->answer->rrs= 0; |
228 | qu->interim_allocd= qu->answer->cname ? MEM_ROUND(strlen(qu->answer->cname)+1) : 0; |
d05cc330 |
229 | } |
230 | |
f318f883 |
231 | static void free_query_allocs(adns_query qu) { |
232 | allocnode *an, *ann; |
f318f883 |
233 | |
61093792 |
234 | cancel_children(qu); |
bc01473e |
235 | for (an= qu->allocations.head; an; an= ann) { ann= an->next; free(an); } |
f318f883 |
236 | adns__vbuf_free(&qu->vb); |
237 | } |
238 | |
26eb6bdc |
239 | void adns_cancel(adns_query qu) { |
f318f883 |
240 | switch (qu->state) { |
241 | case query_udp: case query_tcpwait: case query_tcpsent: |
26eb6bdc |
242 | LIST_UNLINK(qu->ads->timew,qu); |
f318f883 |
243 | break; |
244 | case query_child: |
26eb6bdc |
245 | LIST_UNLINK(qu->ads->childw,qu); |
f318f883 |
246 | break; |
247 | case query_done: |
26eb6bdc |
248 | LIST_UNLINK(qu->ads->output,qu); |
f318f883 |
249 | break; |
250 | default: |
251 | abort(); |
252 | } |
253 | free_query_allocs(qu); |
254 | free(qu->answer); |
255 | free(qu); |
256 | } |
bc01473e |
257 | |
258 | static void makefinal_query(adns_query qu) { |
5c596e4d |
259 | adns_answer *ans; |
f318f883 |
260 | int rrn; |
965c9782 |
261 | |
f318f883 |
262 | ans= qu->answer; |
bc01473e |
263 | |
f318f883 |
264 | if (qu->interim_allocd) { |
f318f883 |
265 | ans= realloc(qu->answer, MEM_ROUND(MEM_ROUND(sizeof(*ans)) + qu->interim_allocd)); |
bc01473e |
266 | if (!ans) goto x_nomem; |
f2ad23ee |
267 | qu->answer= ans; |
268 | } |
965c9782 |
269 | |
f318f883 |
270 | qu->final_allocspace= (byte*)ans + MEM_ROUND(sizeof(*ans)); |
965c9782 |
271 | adns__makefinal_str(qu,&ans->cname); |
f318f883 |
272 | |
965c9782 |
273 | if (ans->nrrs) { |
f318f883 |
274 | adns__makefinal_block(qu, &ans->rrs.untyped, ans->nrrs*ans->rrsz); |
965c9782 |
275 | |
f318f883 |
276 | for (rrn=0; rrn<ans->nrrs; rrn++) |
277 | qu->typei->makefinal(qu, ans->rrs.bytes + rrn*ans->rrsz); |
f318f883 |
278 | } |
bc01473e |
279 | |
f318f883 |
280 | free_query_allocs(qu); |
bc01473e |
281 | return; |
965c9782 |
282 | |
bc01473e |
283 | x_nomem: |
9b86645c |
284 | qu->answer->status= adns_s_nomemory; |
bc01473e |
285 | qu->answer->cname= 0; |
286 | adns__reset_cnameonly(qu); |
287 | free_query_allocs(qu); |
288 | } |
289 | |
290 | void adns__query_done(adns_query qu) { |
291 | adns_answer *ans; |
292 | adns_query parent; |
293 | |
d05cc330 |
294 | qu->id= -1; |
bc01473e |
295 | ans= qu->answer; |
296 | |
297 | if (ans->nrrs && qu->typei->diff_needswap) { |
298 | if (!adns__vbuf_ensure(&qu->vb,qu->typei->rrsz)) { |
9b86645c |
299 | adns__query_fail(qu,adns_s_nomemory); |
bc01473e |
300 | return; |
301 | } |
302 | adns__isort(ans->rrs.bytes, ans->nrrs, ans->rrsz, |
8c3aa944 |
303 | qu->vb.buf, |
304 | (int(*)(void*, const void*, const void*))qu->typei->diff_needswap, |
305 | qu->ads); |
bc01473e |
306 | } |
a095483e |
307 | |
bc01473e |
308 | parent= qu->parent; |
309 | if (parent) { |
310 | LIST_UNLINK_PART(parent->children,qu,siblings.); |
cd1bde2f |
311 | qu->ctx.callback(parent,qu); |
bc01473e |
312 | free_query_allocs(qu); |
313 | free(qu); |
bc01473e |
314 | } else { |
315 | makefinal_query(qu); |
316 | LIST_LINK_TAIL(qu->ads->output,qu); |
317 | } |
6f17710a |
318 | } |
319 | |
11c8bf9b |
320 | void adns__query_fail(adns_query qu, adns_status stat) { |
321 | adns__reset_cnameonly(qu); |
965c9782 |
322 | qu->answer->status= stat; |
11c8bf9b |
323 | adns__query_done(qu); |
6f17710a |
324 | } |
98db6da3 |
325 | |
326 | void adns__makefinal_str(adns_query qu, char **strp) { |
327 | int l; |
328 | char *before, *after; |
329 | |
330 | before= *strp; |
9a2b67d4 |
331 | if (!before) return; |
98db6da3 |
332 | l= strlen(before)+1; |
333 | after= adns__alloc_final(qu,l); |
334 | memcpy(after,before,l); |
335 | *strp= after; |
336 | } |
337 | |
11c8bf9b |
338 | void adns__makefinal_block(adns_query qu, void **blpp, size_t sz) { |
9a2b67d4 |
339 | void *before, *after; |
98db6da3 |
340 | |
9a2b67d4 |
341 | before= *blpp; |
342 | if (!before) return; |
98db6da3 |
343 | after= adns__alloc_final(qu,sz); |
9a2b67d4 |
344 | memcpy(after,before,sz); |
98db6da3 |
345 | *blpp= after; |
346 | } |