e576be50 |
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 | /* |
a719a4be |
8 | * This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson |
e576be50 |
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 | */ |
4353a5c4 |
24 | |
4bec51a4 |
25 | #include "internal.h" |
656b2da9 |
26 | |
e576be50 |
27 | #include <stdlib.h> |
28 | #include <unistd.h> |
29 | #include <errno.h> |
a6536d8b |
30 | #include <string.h> |
656b2da9 |
31 | |
e576be50 |
32 | #include <sys/time.h> |
656b2da9 |
33 | |
e576be50 |
34 | #include "internal.h" |
ddfda861 |
35 | |
e576be50 |
36 | int adns__internal_submit(adns_state ads, adns_query *query_r, |
3955725c |
37 | const typeinfo *typei, vbuf *qumsg_vb, int id, |
e576be50 |
38 | adns_queryflags flags, struct timeval now, |
39 | adns_status failstat, const qcontext *ctx) { |
40 | adns_query qu; |
e576be50 |
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 | |
3955725c |
45 | qu->ads= ads; |
e576be50 |
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; |
551ff40f |
50 | LIST_INIT(qu->allocations); |
e576be50 |
51 | qu->interim_allocd= 0; |
3955725c |
52 | qu->final_allocspace= 0; |
e576be50 |
53 | |
3955725c |
54 | qu->typei= typei; |
e576be50 |
55 | adns__vbuf_init(&qu->vb); |
56 | |
57 | qu->cname_dgram= 0; |
58 | qu->cname_dglen= qu->cname_begin= 0; |
4bec51a4 |
59 | |
e576be50 |
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); |
a6536d8b |
66 | memcpy(&qu->ctx,ctx,sizeof(qu->ctx)); |
e576be50 |
67 | |
68 | qu->answer->status= adns_s_ok; |
69 | qu->answer->cname= 0; |
3955725c |
70 | qu->answer->type= typei->type; |
e576be50 |
71 | qu->answer->nrrs= 0; |
72 | qu->answer->rrs= 0; |
3955725c |
73 | qu->answer->rrsz= typei->rrsz; |
e576be50 |
74 | |
75 | *query_r= qu; |
ddfda861 |
76 | |
a49a6d7b |
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) { |
ea1e31e3 |
81 | adns__query_fail(qu,adns_s_nomemory); |
a49a6d7b |
82 | return adns_s_ok; |
83 | } |
84 | memcpy(qu->query_dgram,qumsg_vb->buf,qumsg_vb->used); |
85 | } else { |
86 | qu->query_dgram= 0; |
ddfda861 |
87 | } |
e576be50 |
88 | qu->vb= *qumsg_vb; |
89 | adns__vbuf_init(qumsg_vb); |
90 | |
91 | if (failstat) { |
3955725c |
92 | adns__query_fail(qu,failstat); |
93 | return adns_s_ok; |
656b2da9 |
94 | } |
3955725c |
95 | adns__query_udp(qu,now); |
e576be50 |
96 | adns__autosys(ads,now); |
97 | |
3955725c |
98 | return adns_s_ok; |
e576be50 |
99 | |
100 | x_freequ_nomemory: |
101 | free(qu); |
102 | x_nomemory: |
3955725c |
103 | adns__vbuf_free(qumsg_vb); |
ea1e31e3 |
104 | return adns_s_nomemory; |
4bec51a4 |
105 | } |
106 | |
e576be50 |
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; |
3955725c |
114 | int id, r, ol; |
e576be50 |
115 | vbuf vb; |
3955725c |
116 | adns_status stat; |
117 | const typeinfo *typei; |
118 | struct timeval now; |
e576be50 |
119 | |
3955725c |
120 | typei= adns__findtype(type); |
ea1e31e3 |
121 | if (!typei) return adns_s_unknownrrtype; |
3955725c |
122 | |
e576be50 |
123 | ctx.ext= context; |
a6536d8b |
124 | ctx.callback= 0; |
125 | memset(&ctx.info,0,sizeof(ctx.info)); |
126 | |
e576be50 |
127 | r= gettimeofday(&now,0); if (r) return errno; |
128 | id= 0; |
129 | |
130 | adns__vbuf_init(&vb); |
131 | |
132 | ol= strlen(owner); |
ea1e31e3 |
133 | if (ol<=1 || ol>DNS_MAXDOMAIN+1) { stat= adns_s_querydomaintoolong; goto xit; } |
e576be50 |
134 | |
135 | if (owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_qf_search; ol--; } |
136 | |
3955725c |
137 | stat= adns__mkquery(ads,&vb,&id, owner,ol, typei,flags); |
e576be50 |
138 | |
139 | xit: |
3955725c |
140 | return adns__internal_submit(ads,query_r, typei,&vb,id, flags,now, stat,&ctx); |
ddfda861 |
141 | } |
142 | |
e576be50 |
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; |
4bec51a4 |
153 | |
88372443 |
154 | r= adns_wait(ads,&qu,answer_r,0); |
3955725c |
155 | if (r) adns_cancel(qu); |
ddfda861 |
156 | |
88372443 |
157 | return r; |
e576be50 |
158 | } |
159 | |
f759e52e |
160 | static void *alloc_common(adns_query qu, size_t sz) { |
e576be50 |
161 | allocnode *an; |
162 | |
a49a6d7b |
163 | if (!sz) return qu; /* Any old pointer will do */ |
e576be50 |
164 | assert(!qu->final_allocspace); |
e576be50 |
165 | an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz)); |
e062dcae |
166 | if (!an) return 0; |
551ff40f |
167 | LIST_LINK_TAIL(qu->allocations,an); |
e576be50 |
168 | return (byte*)an + MEM_ROUND(sizeof(*an)); |
169 | } |
4bec51a4 |
170 | |
f759e52e |
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 | |
551ff40f |
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 | |
e576be50 |
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 | |
9ec44266 |
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 | |
3955725c |
223 | void adns__reset_cnameonly(adns_query qu) { |
9557e604 |
224 | assert(!qu->final_allocspace); |
9ec44266 |
225 | cancel_children(qu); |
e576be50 |
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; |
4353a5c4 |
229 | } |
230 | |
88372443 |
231 | static void free_query_allocs(adns_query qu) { |
232 | allocnode *an, *ann; |
88372443 |
233 | |
9ec44266 |
234 | cancel_children(qu); |
551ff40f |
235 | for (an= qu->allocations.head; an; an= ann) { ann= an->next; free(an); } |
88372443 |
236 | adns__vbuf_free(&qu->vb); |
237 | } |
238 | |
1dfe95d8 |
239 | void adns_cancel(adns_query qu) { |
88372443 |
240 | switch (qu->state) { |
241 | case query_udp: case query_tcpwait: case query_tcpsent: |
1dfe95d8 |
242 | LIST_UNLINK(qu->ads->timew,qu); |
88372443 |
243 | break; |
244 | case query_child: |
1dfe95d8 |
245 | LIST_UNLINK(qu->ads->childw,qu); |
88372443 |
246 | break; |
247 | case query_done: |
1dfe95d8 |
248 | LIST_UNLINK(qu->ads->output,qu); |
88372443 |
249 | break; |
250 | default: |
251 | abort(); |
252 | } |
253 | free_query_allocs(qu); |
254 | free(qu->answer); |
255 | free(qu); |
256 | } |
551ff40f |
257 | |
258 | static void makefinal_query(adns_query qu) { |
98a3f706 |
259 | adns_answer *ans; |
88372443 |
260 | int rrn; |
8e5b0abb |
261 | |
88372443 |
262 | ans= qu->answer; |
551ff40f |
263 | |
88372443 |
264 | if (qu->interim_allocd) { |
88372443 |
265 | ans= realloc(qu->answer, MEM_ROUND(MEM_ROUND(sizeof(*ans)) + qu->interim_allocd)); |
551ff40f |
266 | if (!ans) goto x_nomem; |
e062dcae |
267 | qu->answer= ans; |
268 | } |
8e5b0abb |
269 | |
88372443 |
270 | qu->final_allocspace= (byte*)ans + MEM_ROUND(sizeof(*ans)); |
8e5b0abb |
271 | adns__makefinal_str(qu,&ans->cname); |
88372443 |
272 | |
8e5b0abb |
273 | if (ans->nrrs) { |
88372443 |
274 | adns__makefinal_block(qu, &ans->rrs.untyped, ans->nrrs*ans->rrsz); |
8e5b0abb |
275 | |
88372443 |
276 | for (rrn=0; rrn<ans->nrrs; rrn++) |
277 | qu->typei->makefinal(qu, ans->rrs.bytes + rrn*ans->rrsz); |
88372443 |
278 | } |
551ff40f |
279 | |
88372443 |
280 | free_query_allocs(qu); |
551ff40f |
281 | return; |
8e5b0abb |
282 | |
551ff40f |
283 | x_nomem: |
ea1e31e3 |
284 | qu->answer->status= adns_s_nomemory; |
551ff40f |
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 | |
4353a5c4 |
294 | qu->id= -1; |
551ff40f |
295 | ans= qu->answer; |
296 | |
297 | if (ans->nrrs && qu->typei->diff_needswap) { |
298 | if (!adns__vbuf_ensure(&qu->vb,qu->typei->rrsz)) { |
ea1e31e3 |
299 | adns__query_fail(qu,adns_s_nomemory); |
551ff40f |
300 | return; |
301 | } |
302 | adns__isort(ans->rrs.bytes, ans->nrrs, ans->rrsz, |
09957b1c |
303 | qu->vb.buf, |
304 | (int(*)(void*, const void*, const void*))qu->typei->diff_needswap, |
305 | qu->ads); |
551ff40f |
306 | } |
e553d072 |
307 | |
551ff40f |
308 | parent= qu->parent; |
309 | if (parent) { |
310 | LIST_UNLINK_PART(parent->children,qu,siblings.); |
a6536d8b |
311 | qu->ctx.callback(parent,qu); |
551ff40f |
312 | free_query_allocs(qu); |
313 | free(qu); |
551ff40f |
314 | } else { |
315 | makefinal_query(qu); |
316 | LIST_LINK_TAIL(qu->ads->output,qu); |
317 | } |
656b2da9 |
318 | } |
319 | |
3955725c |
320 | void adns__query_fail(adns_query qu, adns_status stat) { |
321 | adns__reset_cnameonly(qu); |
8e5b0abb |
322 | qu->answer->status= stat; |
3955725c |
323 | adns__query_done(qu); |
656b2da9 |
324 | } |
e576be50 |
325 | |
326 | void adns__makefinal_str(adns_query qu, char **strp) { |
327 | int l; |
328 | char *before, *after; |
329 | |
330 | before= *strp; |
9557e604 |
331 | if (!before) return; |
e576be50 |
332 | l= strlen(before)+1; |
333 | after= adns__alloc_final(qu,l); |
334 | memcpy(after,before,l); |
335 | *strp= after; |
336 | } |
337 | |
3955725c |
338 | void adns__makefinal_block(adns_query qu, void **blpp, size_t sz) { |
9557e604 |
339 | void *before, *after; |
e576be50 |
340 | |
9557e604 |
341 | before= *blpp; |
342 | if (!before) return; |
e576be50 |
343 | after= adns__alloc_final(qu,sz); |
9557e604 |
344 | memcpy(after,before,sz); |
e576be50 |
345 | *blpp= after; |
346 | } |