3 * - overall query management (allocation, completion)
4 * - per-query memory management
5 * - query submission and cancellation (user-visible and internal)
8 * This file is part of adns, which is Copyright (C) 1997, 1998 Ian Jackson
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)
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.
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.
35 int adns__internal_submit(adns_state ads
, adns_query
*query_r
,
36 const typeinfo
*typei
, vbuf
*qumsg_vb
, int id
,
37 adns_queryflags flags
, struct timeval now
,
38 adns_status failstat
, const qcontext
*ctx
) {
41 qu
= malloc(sizeof(*qu
)); if (!qu
) goto x_nomemory
;
42 qu
->answer
= malloc(sizeof(*qu
->answer
)); if (!qu
->answer
) goto x_freequ_nomemory
;
46 qu
->back
= qu
->next
= qu
->parent
= 0;
47 LIST_INIT(qu
->children
);
48 qu
->siblings
.next
= qu
->siblings
.back
= 0;
49 LIST_INIT(qu
->allocations
);
50 qu
->interim_allocd
= 0;
51 qu
->final_allocspace
= 0;
54 adns__vbuf_init(&qu
->vb
);
57 qu
->cname_dglen
= qu
->cname_begin
= 0;
63 qu
->udpsent
= qu
->tcpfailed
= 0;
64 timerclear(&qu
->timeout
);
65 memcpy(&qu
->context
,ctx
,sizeof(qu
->context
));
67 qu
->answer
->status
= adns_s_ok
;
69 qu
->answer
->type
= typei
->type
;
72 qu
->answer
->rrsz
= typei
->rrsz
;
76 qu
->query_dglen
= qumsg_vb
->used
;
78 qu
->query_dgram
= malloc(qumsg_vb
->used
);
79 if (!qu
->query_dgram
) {
80 adns__query_fail(qu
,adns_s_nolocalmem
);
83 memcpy(qu
->query_dgram
,qumsg_vb
->buf
,qumsg_vb
->used
);
88 adns__vbuf_init(qumsg_vb
);
91 adns__query_fail(qu
,failstat
);
94 adns__query_udp(qu
,now
);
95 adns__autosys(ads
,now
);
102 adns__vbuf_free(qumsg_vb
);
103 return adns_s_nolocalmem
;
106 int adns_submit(adns_state ads
,
109 adns_queryflags flags
,
111 adns_query
*query_r
) {
116 const typeinfo
*typei
;
119 typei
= adns__findtype(type
);
120 if (!typei
) return adns_s_notimplemented
;
123 r
= gettimeofday(&now
,0); if (r
) return errno
;
126 adns__vbuf_init(&vb
);
129 if (ol
<=1 || ol
>DNS_MAXDOMAIN
+1) { stat
= adns_s_domaintoolong
; goto xit
; }
131 if (owner
[ol
-1]=='.' && owner
[ol
-2]!='\\') { flags
&= ~adns_qf_search
; ol
--; }
133 stat
= adns__mkquery(ads
,&vb
,&id
, owner
,ol
, typei
,flags
);
136 return adns__internal_submit(ads
,query_r
, typei
,&vb
,id
, flags
,now
, stat
,&ctx
);
139 int adns_synchronous(adns_state ads
,
142 adns_queryflags flags
,
143 adns_answer
**answer_r
) {
147 r
= adns_submit(ads
,owner
,type
,flags
,0,&qu
);
150 r
= adns_wait(ads
,&qu
,answer_r
,0);
151 if (r
) adns_cancel(qu
);
156 static void *alloc_common(adns_query qu
, size_t sz
) {
159 if (!sz
) return qu
; /* Any old pointer will do */
160 assert(!qu
->final_allocspace
);
161 an
= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an
)) + sz
));
163 LIST_LINK_TAIL(qu
->allocations
,an
);
164 return (byte
*)an
+ MEM_ROUND(sizeof(*an
));
167 void *adns__alloc_interim(adns_query qu
, size_t sz
) {
169 qu
->interim_allocd
+= sz
;
170 return alloc_common(qu
,sz
);
173 void *adns__alloc_mine(adns_query qu
, size_t sz
) {
174 return alloc_common(qu
,MEM_ROUND(sz
));
177 void adns__transfer_interim(adns_query from
, adns_query to
, void *block
, size_t sz
) {
181 an
= (void*)((byte
*)block
- MEM_ROUND(sizeof(*an
)));
183 assert(!to
->final_allocspace
);
184 assert(!from
->final_allocspace
);
186 LIST_UNLINK(from
->allocations
,an
);
187 LIST_LINK_TAIL(to
->allocations
,an
);
189 from
->interim_allocd
-= sz
;
190 to
->interim_allocd
+= sz
;
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
201 rp
= qu
->final_allocspace
;
203 qu
->interim_allocd
-= sz
;
204 assert(qu
->interim_allocd
>=0);
205 qu
->final_allocspace
= (byte
*)rp
+ sz
;
209 void adns__reset_cnameonly(adns_query qu
) {
210 /* fixme: cancel children */
211 assert(!qu
->final_allocspace
);
214 qu
->interim_allocd
= qu
->answer
->cname ?
MEM_ROUND(strlen(qu
->answer
->cname
)+1) : 0;
217 static void free_query_allocs(adns_query qu
) {
219 adns_query cqu
, ncqu
;
221 for (cqu
= qu
->children
.head
; cqu
; cqu
= ncqu
) {
222 ncqu
= cqu
->siblings
.next
;
225 for (an
= qu
->allocations
.head
; an
; an
= ann
) { ann
= an
->next
; free(an
); }
226 adns__vbuf_free(&qu
->vb
);
229 void adns_cancel(adns_query qu
) {
231 case query_udp
: case query_tcpwait
: case query_tcpsent
:
232 LIST_UNLINK(qu
->ads
->timew
,qu
);
235 LIST_UNLINK(qu
->ads
->childw
,qu
);
238 LIST_UNLINK(qu
->ads
->output
,qu
);
243 free_query_allocs(qu
);
248 static void makefinal_query(adns_query qu
) {
254 if (qu
->interim_allocd
) {
255 ans
= realloc(qu
->answer
, MEM_ROUND(MEM_ROUND(sizeof(*ans
)) + qu
->interim_allocd
));
256 if (!ans
) goto x_nomem
;
260 qu
->final_allocspace
= (byte
*)ans
+ MEM_ROUND(sizeof(*ans
));
261 adns__makefinal_str(qu
,&ans
->cname
);
264 adns__makefinal_block(qu
, &ans
->rrs
.untyped
, ans
->nrrs
*ans
->rrsz
);
266 for (rrn
=0; rrn
<ans
->nrrs
; rrn
++)
267 qu
->typei
->makefinal(qu
, ans
->rrs
.bytes
+ rrn
*ans
->rrsz
);
270 free_query_allocs(qu
);
274 qu
->answer
->status
= adns_s_nolocalmem
;
275 qu
->answer
->cname
= 0;
276 adns__reset_cnameonly(qu
);
277 free_query_allocs(qu
);
280 void adns__query_done(adns_query qu
) {
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
);
292 adns__isort(ans
->rrs
.bytes
, ans
->nrrs
, ans
->rrsz
,
293 qu
->vb
.buf
, qu
->typei
->diff_needswap
);
298 LIST_UNLINK_PART(parent
->children
,qu
,siblings
.);
299 qu
->context
.intern
.callback(parent
,qu
);
300 free_query_allocs(qu
);
302 if (!parent
->children
.head
) adns__query_done(parent
);
305 LIST_LINK_TAIL(qu
->ads
->output
,qu
);
309 void adns__query_fail(adns_query qu
, adns_status stat
) {
310 adns__reset_cnameonly(qu
);
311 qu
->answer
->status
= stat
;
312 adns__query_done(qu
);
315 void adns__makefinal_str(adns_query qu
, char **strp
) {
317 char *before
, *after
;
322 after
= adns__alloc_final(qu
,l
);
323 memcpy(after
,before
,l
);
327 void adns__makefinal_block(adns_query qu
, void **blpp
, size_t sz
) {
328 void *before
, *after
;
332 after
= adns__alloc_final(qu
,sz
);
333 memcpy(after
,before
,sz
);