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> |
6f17710a |
30 | |
98db6da3 |
31 | #include <sys/time.h> |
6f17710a |
32 | |
98db6da3 |
33 | #include "internal.h" |
96e79df5 |
34 | |
98db6da3 |
35 | int adns__internal_submit(adns_state ads, adns_query *query_r, |
11c8bf9b |
36 | const typeinfo *typei, vbuf *qumsg_vb, int id, |
98db6da3 |
37 | adns_queryflags flags, struct timeval now, |
38 | adns_status failstat, const qcontext *ctx) { |
39 | adns_query qu; |
98db6da3 |
40 | |
41 | qu= malloc(sizeof(*qu)); if (!qu) goto x_nomemory; |
42 | qu->answer= malloc(sizeof(*qu->answer)); if (!qu->answer) goto x_freequ_nomemory; |
43 | |
11c8bf9b |
44 | qu->ads= ads; |
98db6da3 |
45 | qu->state= query_udp; |
46 | qu->back= qu->next= qu->parent= 0; |
47 | LIST_INIT(qu->children); |
48 | qu->siblings.next= qu->siblings.back= 0; |
bc01473e |
49 | LIST_INIT(qu->allocations); |
98db6da3 |
50 | qu->interim_allocd= 0; |
11c8bf9b |
51 | qu->final_allocspace= 0; |
98db6da3 |
52 | |
11c8bf9b |
53 | qu->typei= typei; |
98db6da3 |
54 | adns__vbuf_init(&qu->vb); |
55 | |
56 | qu->cname_dgram= 0; |
57 | qu->cname_dglen= qu->cname_begin= 0; |
48cb0b4b |
58 | |
98db6da3 |
59 | qu->id= id; |
60 | qu->flags= flags; |
61 | qu->udpretries= 0; |
62 | qu->udpnextserver= 0; |
63 | qu->udpsent= qu->tcpfailed= 0; |
64 | timerclear(&qu->timeout); |
65 | memcpy(&qu->context,ctx,sizeof(qu->context)); |
98db6da3 |
66 | |
67 | qu->answer->status= adns_s_ok; |
68 | qu->answer->cname= 0; |
11c8bf9b |
69 | qu->answer->type= typei->type; |
98db6da3 |
70 | qu->answer->nrrs= 0; |
71 | qu->answer->rrs= 0; |
11c8bf9b |
72 | qu->answer->rrsz= typei->rrsz; |
98db6da3 |
73 | |
74 | *query_r= qu; |
96e79df5 |
75 | |
f47cdeec |
76 | qu->query_dglen= qumsg_vb->used; |
77 | if (qumsg_vb->used) { |
78 | qu->query_dgram= malloc(qumsg_vb->used); |
79 | if (!qu->query_dgram) { |
80 | adns__query_fail(qu,adns_s_nolocalmem); |
81 | return adns_s_ok; |
82 | } |
83 | memcpy(qu->query_dgram,qumsg_vb->buf,qumsg_vb->used); |
84 | } else { |
85 | qu->query_dgram= 0; |
96e79df5 |
86 | } |
98db6da3 |
87 | qu->vb= *qumsg_vb; |
88 | adns__vbuf_init(qumsg_vb); |
89 | |
90 | if (failstat) { |
11c8bf9b |
91 | adns__query_fail(qu,failstat); |
92 | return adns_s_ok; |
6f17710a |
93 | } |
11c8bf9b |
94 | adns__query_udp(qu,now); |
98db6da3 |
95 | adns__autosys(ads,now); |
96 | |
11c8bf9b |
97 | return adns_s_ok; |
98db6da3 |
98 | |
99 | x_freequ_nomemory: |
100 | free(qu); |
101 | x_nomemory: |
11c8bf9b |
102 | adns__vbuf_free(qumsg_vb); |
103 | return adns_s_nolocalmem; |
48cb0b4b |
104 | } |
105 | |
98db6da3 |
106 | int adns_submit(adns_state ads, |
107 | const char *owner, |
108 | adns_rrtype type, |
109 | adns_queryflags flags, |
110 | void *context, |
111 | adns_query *query_r) { |
112 | qcontext ctx; |
11c8bf9b |
113 | int id, r, ol; |
98db6da3 |
114 | vbuf vb; |
11c8bf9b |
115 | adns_status stat; |
116 | const typeinfo *typei; |
117 | struct timeval now; |
98db6da3 |
118 | |
11c8bf9b |
119 | typei= adns__findtype(type); |
120 | if (!typei) return adns_s_notimplemented; |
121 | |
98db6da3 |
122 | ctx.ext= context; |
123 | r= gettimeofday(&now,0); if (r) return errno; |
124 | id= 0; |
125 | |
126 | adns__vbuf_init(&vb); |
127 | |
128 | ol= strlen(owner); |
cfdca685 |
129 | if (ol<=1 || ol>DNS_MAXDOMAIN+1) { stat= adns_s_domaintoolong; goto xit; } |
98db6da3 |
130 | |
131 | if (owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_qf_search; ol--; } |
132 | |
11c8bf9b |
133 | stat= adns__mkquery(ads,&vb,&id, owner,ol, typei,flags); |
98db6da3 |
134 | |
135 | xit: |
11c8bf9b |
136 | return adns__internal_submit(ads,query_r, typei,&vb,id, flags,now, stat,&ctx); |
96e79df5 |
137 | } |
138 | |
98db6da3 |
139 | int adns_synchronous(adns_state ads, |
140 | const char *owner, |
141 | adns_rrtype type, |
142 | adns_queryflags flags, |
143 | adns_answer **answer_r) { |
144 | adns_query qu; |
145 | int r; |
146 | |
147 | r= adns_submit(ads,owner,type,flags,0,&qu); |
148 | if (r) return r; |
48cb0b4b |
149 | |
f318f883 |
150 | r= adns_wait(ads,&qu,answer_r,0); |
11c8bf9b |
151 | if (r) adns_cancel(qu); |
96e79df5 |
152 | |
f318f883 |
153 | return r; |
98db6da3 |
154 | } |
155 | |
c9afe7bb |
156 | static void *alloc_common(adns_query qu, size_t sz) { |
98db6da3 |
157 | allocnode *an; |
158 | |
f47cdeec |
159 | if (!sz) return qu; /* Any old pointer will do */ |
98db6da3 |
160 | assert(!qu->final_allocspace); |
98db6da3 |
161 | an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz)); |
f2ad23ee |
162 | if (!an) return 0; |
bc01473e |
163 | LIST_LINK_TAIL(qu->allocations,an); |
98db6da3 |
164 | return (byte*)an + MEM_ROUND(sizeof(*an)); |
165 | } |
48cb0b4b |
166 | |
c9afe7bb |
167 | void *adns__alloc_interim(adns_query qu, size_t sz) { |
168 | sz= MEM_ROUND(sz); |
169 | qu->interim_allocd += sz; |
170 | return alloc_common(qu,sz); |
171 | } |
172 | |
173 | void *adns__alloc_mine(adns_query qu, size_t sz) { |
174 | return alloc_common(qu,MEM_ROUND(sz)); |
175 | } |
176 | |
bc01473e |
177 | void adns__transfer_interim(adns_query from, adns_query to, void *block, size_t sz) { |
178 | allocnode *an; |
179 | |
180 | if (!block) return; |
181 | an= (void*)((byte*)block - MEM_ROUND(sizeof(*an))); |
182 | |
183 | assert(!to->final_allocspace); |
184 | assert(!from->final_allocspace); |
185 | |
186 | LIST_UNLINK(from->allocations,an); |
187 | LIST_LINK_TAIL(to->allocations,an); |
188 | |
189 | from->interim_allocd -= sz; |
190 | to->interim_allocd += sz; |
191 | } |
192 | |
98db6da3 |
193 | void *adns__alloc_final(adns_query qu, size_t sz) { |
194 | /* When we're in the _final stage, we _subtract_ from interim_alloc'd |
195 | * each allocation, and use final_allocspace to point to the next free |
196 | * bit. |
197 | */ |
198 | void *rp; |
199 | |
200 | sz= MEM_ROUND(sz); |
201 | rp= qu->final_allocspace; |
202 | assert(rp); |
203 | qu->interim_allocd -= sz; |
204 | assert(qu->interim_allocd>=0); |
205 | qu->final_allocspace= (byte*)rp + sz; |
206 | return rp; |
207 | } |
208 | |
11c8bf9b |
209 | void adns__reset_cnameonly(adns_query qu) { |
f318f883 |
210 | /* fixme: cancel children */ |
9a2b67d4 |
211 | assert(!qu->final_allocspace); |
98db6da3 |
212 | qu->answer->nrrs= 0; |
213 | qu->answer->rrs= 0; |
214 | qu->interim_allocd= qu->answer->cname ? MEM_ROUND(strlen(qu->answer->cname)+1) : 0; |
d05cc330 |
215 | } |
216 | |
f318f883 |
217 | static void free_query_allocs(adns_query qu) { |
218 | allocnode *an, *ann; |
219 | adns_query cqu, ncqu; |
220 | |
221 | for (cqu= qu->children.head; cqu; cqu= ncqu) { |
222 | ncqu= cqu->siblings.next; |
223 | adns_cancel(cqu); |
224 | } |
bc01473e |
225 | for (an= qu->allocations.head; an; an= ann) { ann= an->next; free(an); } |
f318f883 |
226 | adns__vbuf_free(&qu->vb); |
227 | } |
228 | |
26eb6bdc |
229 | void adns_cancel(adns_query qu) { |
f318f883 |
230 | switch (qu->state) { |
231 | case query_udp: case query_tcpwait: case query_tcpsent: |
26eb6bdc |
232 | LIST_UNLINK(qu->ads->timew,qu); |
f318f883 |
233 | break; |
234 | case query_child: |
26eb6bdc |
235 | LIST_UNLINK(qu->ads->childw,qu); |
f318f883 |
236 | break; |
237 | case query_done: |
26eb6bdc |
238 | LIST_UNLINK(qu->ads->output,qu); |
f318f883 |
239 | break; |
240 | default: |
241 | abort(); |
242 | } |
243 | free_query_allocs(qu); |
244 | free(qu->answer); |
245 | free(qu); |
246 | } |
bc01473e |
247 | |
248 | static void makefinal_query(adns_query qu) { |
5c596e4d |
249 | adns_answer *ans; |
f318f883 |
250 | int rrn; |
965c9782 |
251 | |
f318f883 |
252 | ans= qu->answer; |
bc01473e |
253 | |
f318f883 |
254 | if (qu->interim_allocd) { |
f318f883 |
255 | ans= realloc(qu->answer, MEM_ROUND(MEM_ROUND(sizeof(*ans)) + qu->interim_allocd)); |
bc01473e |
256 | if (!ans) goto x_nomem; |
f2ad23ee |
257 | qu->answer= ans; |
258 | } |
965c9782 |
259 | |
f318f883 |
260 | qu->final_allocspace= (byte*)ans + MEM_ROUND(sizeof(*ans)); |
965c9782 |
261 | adns__makefinal_str(qu,&ans->cname); |
f318f883 |
262 | |
965c9782 |
263 | if (ans->nrrs) { |
f318f883 |
264 | adns__makefinal_block(qu, &ans->rrs.untyped, ans->nrrs*ans->rrsz); |
965c9782 |
265 | |
f318f883 |
266 | for (rrn=0; rrn<ans->nrrs; rrn++) |
267 | qu->typei->makefinal(qu, ans->rrs.bytes + rrn*ans->rrsz); |
f318f883 |
268 | } |
bc01473e |
269 | |
f318f883 |
270 | free_query_allocs(qu); |
bc01473e |
271 | return; |
965c9782 |
272 | |
bc01473e |
273 | x_nomem: |
274 | qu->answer->status= adns_s_nolocalmem; |
275 | qu->answer->cname= 0; |
276 | adns__reset_cnameonly(qu); |
277 | free_query_allocs(qu); |
278 | } |
279 | |
280 | void adns__query_done(adns_query qu) { |
281 | adns_answer *ans; |
282 | adns_query parent; |
283 | |
d05cc330 |
284 | qu->id= -1; |
bc01473e |
285 | ans= qu->answer; |
286 | |
287 | if (ans->nrrs && qu->typei->diff_needswap) { |
288 | if (!adns__vbuf_ensure(&qu->vb,qu->typei->rrsz)) { |
289 | adns__query_fail(qu,adns_s_nolocalmem); |
290 | return; |
291 | } |
292 | adns__isort(ans->rrs.bytes, ans->nrrs, ans->rrsz, |
293 | qu->vb.buf, qu->typei->diff_needswap); |
294 | } |
a095483e |
295 | |
bc01473e |
296 | parent= qu->parent; |
297 | if (parent) { |
298 | LIST_UNLINK_PART(parent->children,qu,siblings.); |
299 | qu->context.intern.callback(parent,qu); |
300 | free_query_allocs(qu); |
301 | free(qu); |
302 | if (!parent->children.head) adns__query_done(parent); |
303 | } else { |
304 | makefinal_query(qu); |
305 | LIST_LINK_TAIL(qu->ads->output,qu); |
306 | } |
6f17710a |
307 | } |
308 | |
11c8bf9b |
309 | void adns__query_fail(adns_query qu, adns_status stat) { |
310 | adns__reset_cnameonly(qu); |
965c9782 |
311 | qu->answer->status= stat; |
11c8bf9b |
312 | adns__query_done(qu); |
6f17710a |
313 | } |
98db6da3 |
314 | |
315 | void adns__makefinal_str(adns_query qu, char **strp) { |
316 | int l; |
317 | char *before, *after; |
318 | |
319 | before= *strp; |
9a2b67d4 |
320 | if (!before) return; |
98db6da3 |
321 | l= strlen(before)+1; |
322 | after= adns__alloc_final(qu,l); |
323 | memcpy(after,before,l); |
324 | *strp= after; |
325 | } |
326 | |
11c8bf9b |
327 | void adns__makefinal_block(adns_query qu, void **blpp, size_t sz) { |
9a2b67d4 |
328 | void *before, *after; |
98db6da3 |
329 | |
9a2b67d4 |
330 | before= *blpp; |
331 | if (!before) return; |
98db6da3 |
332 | after= adns__alloc_final(qu,sz); |
9a2b67d4 |
333 | memcpy(after,before,sz); |
98db6da3 |
334 | *blpp= after; |
335 | } |