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-1999 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.
36 int adns__internal_submit(adns_state ads
, adns_query
*query_r
,
37 const typeinfo
*typei
, vbuf
*qumsg_vb
, int id
,
38 adns_queryflags flags
, struct timeval now
,
39 adns_status failstat
, const qcontext
*ctx
) {
42 qu
= malloc(sizeof(*qu
)); if (!qu
) goto x_nomemory
;
43 qu
->answer
= malloc(sizeof(*qu
->answer
)); if (!qu
->answer
) goto x_freequ_nomemory
;
47 qu
->back
= qu
->next
= qu
->parent
= 0;
48 LIST_INIT(qu
->children
);
49 qu
->siblings
.next
= qu
->siblings
.back
= 0;
50 LIST_INIT(qu
->allocations
);
51 qu
->interim_allocd
= 0;
52 qu
->final_allocspace
= 0;
55 adns__vbuf_init(&qu
->vb
);
58 qu
->cname_dglen
= qu
->cname_begin
= 0;
64 qu
->udpsent
= qu
->tcpfailed
= 0;
65 timerclear(&qu
->timeout
);
66 memcpy(&qu
->ctx
,ctx
,sizeof(qu
->ctx
));
67 qu
->expires
= now
.tv_sec
+ MAXTTLBELIEVE
;
69 qu
->answer
->status
= adns_s_ok
;
71 qu
->answer
->type
= typei
->type
;
74 qu
->answer
->rrsz
= typei
->rrsz
;
78 qu
->query_dglen
= qumsg_vb
->used
;
80 qu
->query_dgram
= malloc(qumsg_vb
->used
);
81 if (!qu
->query_dgram
) {
82 adns__query_fail(qu
,adns_s_nomemory
);
85 memcpy(qu
->query_dgram
,qumsg_vb
->buf
,qumsg_vb
->used
);
90 adns__vbuf_init(qumsg_vb
);
93 adns__query_fail(qu
,failstat
);
96 adns__query_udp(qu
,now
);
97 adns__autosys(ads
,now
);
104 adns__vbuf_free(qumsg_vb
);
105 return adns_s_nomemory
;
108 int adns_submit(adns_state ads
,
111 adns_queryflags flags
,
113 adns_query
*query_r
) {
118 const typeinfo
*typei
;
121 typei
= adns__findtype(type
);
122 if (!typei
) return adns_s_unknownrrtype
;
126 memset(&ctx
.info
,0,sizeof(ctx
.info
));
128 r
= gettimeofday(&now
,0); if (r
) return errno
;
131 adns__vbuf_init(&vb
);
134 if (ol
<=1 || ol
>DNS_MAXDOMAIN
+1) { stat
= adns_s_querydomaintoolong
; goto xit
; }
136 if (owner
[ol
-1]=='.' && owner
[ol
-2]!='\\') { flags
&= ~adns_qf_search
; ol
--; }
138 stat
= adns__mkquery(ads
,&vb
,&id
, owner
,ol
, typei
,flags
);
141 return adns__internal_submit(ads
,query_r
, typei
,&vb
,id
, flags
,now
, stat
,&ctx
);
144 int adns_synchronous(adns_state ads
,
147 adns_queryflags flags
,
148 adns_answer
**answer_r
) {
152 r
= adns_submit(ads
,owner
,type
,flags
,0,&qu
);
155 r
= adns_wait(ads
,&qu
,answer_r
,0);
156 if (r
) adns_cancel(qu
);
161 static void *alloc_common(adns_query qu
, size_t sz
) {
164 if (!sz
) return qu
; /* Any old pointer will do */
165 assert(!qu
->final_allocspace
);
166 an
= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an
)) + sz
));
168 LIST_LINK_TAIL(qu
->allocations
,an
);
169 return (byte
*)an
+ MEM_ROUND(sizeof(*an
));
172 void *adns__alloc_interim(adns_query qu
, size_t sz
) {
174 qu
->interim_allocd
+= sz
;
175 return alloc_common(qu
,sz
);
178 void *adns__alloc_mine(adns_query qu
, size_t sz
) {
179 return alloc_common(qu
,MEM_ROUND(sz
));
182 void adns__transfer_interim(adns_query from
, adns_query to
, void *block
, size_t sz
) {
186 an
= (void*)((byte
*)block
- MEM_ROUND(sizeof(*an
)));
188 assert(!to
->final_allocspace
);
189 assert(!from
->final_allocspace
);
191 LIST_UNLINK(from
->allocations
,an
);
192 LIST_LINK_TAIL(to
->allocations
,an
);
194 from
->interim_allocd
-= sz
;
195 to
->interim_allocd
+= sz
;
197 if (to
->expires
> from
->expires
) to
->expires
= from
->expires
;
200 void *adns__alloc_final(adns_query qu
, size_t sz
) {
201 /* When we're in the _final stage, we _subtract_ from interim_alloc'd
202 * each allocation, and use final_allocspace to point to the next free
208 rp
= qu
->final_allocspace
;
210 qu
->interim_allocd
-= sz
;
211 assert(qu
->interim_allocd
>=0);
212 qu
->final_allocspace
= (byte
*)rp
+ sz
;
216 static void cancel_children(adns_query qu
) {
217 adns_query cqu
, ncqu
;
219 for (cqu
= qu
->children
.head
; cqu
; cqu
= ncqu
) {
220 ncqu
= cqu
->siblings
.next
;
223 LIST_INIT(qu
->children
);
226 void adns__reset_cnameonly(adns_query qu
) {
227 assert(!qu
->final_allocspace
);
231 qu
->interim_allocd
= qu
->answer
->cname ?
MEM_ROUND(strlen(qu
->answer
->cname
)+1) : 0;
234 static void free_query_allocs(adns_query qu
) {
238 for (an
= qu
->allocations
.head
; an
; an
= ann
) { ann
= an
->next
; free(an
); }
239 adns__vbuf_free(&qu
->vb
);
242 void adns_cancel(adns_query qu
) {
244 case query_udp
: case query_tcpwait
: case query_tcpsent
:
245 LIST_UNLINK(qu
->ads
->timew
,qu
);
248 LIST_UNLINK(qu
->ads
->childw
,qu
);
251 LIST_UNLINK(qu
->ads
->output
,qu
);
256 free_query_allocs(qu
);
261 void adns__update_expires(adns_query qu
, unsigned long ttl
, struct timeval now
) {
264 assert(ttl
<= MAXTTLBELIEVE
);
265 max
= now
.tv_sec
+ ttl
;
266 if (qu
->expires
< max
) return;
270 static void makefinal_query(adns_query qu
) {
276 if (qu
->interim_allocd
) {
277 ans
= realloc(qu
->answer
, MEM_ROUND(MEM_ROUND(sizeof(*ans
)) + qu
->interim_allocd
));
278 if (!ans
) goto x_nomem
;
282 qu
->final_allocspace
= (byte
*)ans
+ MEM_ROUND(sizeof(*ans
));
283 adns__makefinal_str(qu
,&ans
->cname
);
286 adns__makefinal_block(qu
, &ans
->rrs
.untyped
, ans
->nrrs
*ans
->rrsz
);
288 for (rrn
=0; rrn
<ans
->nrrs
; rrn
++)
289 qu
->typei
->makefinal(qu
, ans
->rrs
.bytes
+ rrn
*ans
->rrsz
);
292 free_query_allocs(qu
);
296 qu
->answer
->status
= adns_s_nomemory
;
297 qu
->answer
->cname
= 0;
298 adns__reset_cnameonly(qu
);
299 free_query_allocs(qu
);
302 void adns__query_done(adns_query qu
) {
309 if (ans
->nrrs
&& qu
->typei
->diff_needswap
) {
310 if (!adns__vbuf_ensure(&qu
->vb
,qu
->typei
->rrsz
)) {
311 adns__query_fail(qu
,adns_s_nomemory
);
314 adns__isort(ans
->rrs
.bytes
, ans
->nrrs
, ans
->rrsz
,
316 (int(*)(void*, const void*, const void*))qu
->typei
->diff_needswap
,
320 ans
->expires
= qu
->expires
;
323 LIST_UNLINK_PART(parent
->children
,qu
,siblings
.);
324 qu
->ctx
.callback(parent
,qu
);
325 free_query_allocs(qu
);
329 LIST_LINK_TAIL(qu
->ads
->output
,qu
);
333 void adns__query_fail(adns_query qu
, adns_status stat
) {
334 adns__reset_cnameonly(qu
);
335 qu
->answer
->status
= stat
;
336 adns__query_done(qu
);
339 void adns__makefinal_str(adns_query qu
, char **strp
) {
341 char *before
, *after
;
346 after
= adns__alloc_final(qu
,l
);
347 memcpy(after
,before
,l
);
351 void adns__makefinal_block(adns_query qu
, void **blpp
, size_t sz
) {
352 void *before
, *after
;
356 after
= adns__alloc_final(qu
,sz
);
357 memcpy(after
,before
,sz
);