f8feef7dae6c82171edd0d332ed6b93747459837
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;
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_dgram
= malloc(qumsg_vb
->used
);
77 if (!qu
->query_dgram
) {
78 adns__query_fail(qu
,adns_s_nolocalmem
);
81 memcpy(qu
->query_dgram
,qumsg_vb
->buf
,qumsg_vb
->used
);
83 adns__vbuf_init(qumsg_vb
);
86 adns__query_fail(qu
,failstat
);
89 adns__query_udp(qu
,now
);
90 adns__autosys(ads
,now
);
97 adns__vbuf_free(qumsg_vb
);
98 return adns_s_nolocalmem
;
101 int adns_submit(adns_state ads
,
104 adns_queryflags flags
,
106 adns_query
*query_r
) {
111 const typeinfo
*typei
;
114 typei
= adns__findtype(type
);
115 if (!typei
) return adns_s_notimplemented
;
118 r
= gettimeofday(&now
,0); if (r
) return errno
;
121 adns__vbuf_init(&vb
);
124 if (ol
<=1 || ol
>DNS_MAXDOMAIN
+1) { stat
= adns_s_invaliddomain
; goto xit
; }
126 if (owner
[ol
-1]=='.' && owner
[ol
-2]!='\\') { flags
&= ~adns_qf_search
; ol
--; }
128 stat
= adns__mkquery(ads
,&vb
,&id
, owner
,ol
, typei
,flags
);
131 return adns__internal_submit(ads
,query_r
, typei
,&vb
,id
, flags
,now
, stat
,&ctx
);
134 int adns_synchronous(adns_state ads
,
137 adns_queryflags flags
,
138 adns_answer
**answer_r
) {
142 r
= adns_submit(ads
,owner
,type
,flags
,0,&qu
);
146 r
= adns_wait(ads
,&qu
,answer_r
,0);
148 if (r
) adns_cancel(qu
);
152 void adns_cancel(adns_query query
) {
156 void *adns__alloc_interim(adns_query qu
, size_t sz
) {
159 assert(!qu
->final_allocspace
);
161 an
= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an
)) + sz
));
163 adns__query_fail(qu
,adns_s_nolocalmem
);
166 qu
->interim_allocd
+= sz
;
167 an
->next
= qu
->allocations
;
169 return (byte
*)an
+ MEM_ROUND(sizeof(*an
));
172 void *adns__alloc_final(adns_query qu
, size_t sz
) {
173 /* When we're in the _final stage, we _subtract_ from interim_alloc'd
174 * each allocation, and use final_allocspace to point to the next free
180 rp
= qu
->final_allocspace
;
182 qu
->interim_allocd
-= sz
;
183 assert(qu
->interim_allocd
>=0);
184 qu
->final_allocspace
= (byte
*)rp
+ sz
;
188 void adns__reset_cnameonly(adns_query qu
) {
189 assert(qu
->final_allocspace
);
192 qu
->interim_allocd
= qu
->answer
->cname ?
MEM_ROUND(strlen(qu
->answer
->cname
)+1) : 0;
195 void adns__query_done(adns_query qu
) {
200 qu
->answer
= ans
= realloc(qu
->answer
,
201 MEM_ROUND(MEM_ROUND(sizeof(*ans
)) +
202 qu
->interim_allocd
));
203 qu
->final_allocspace
= (byte
*)qu
->answer
+ MEM_ROUND(sizeof(*ans
));
205 adns__makefinal_str(qu
,&ans
->cname
);
207 adns__makefinal_block(qu
,&ans
->rrs
.untyped
,ans
->rrsz
*ans
->nrrs
);
208 for (i
=0; i
<ans
->nrrs
; i
++)
209 qu
->typei
->makefinal(qu
,ans
->rrs
.bytes
+ans
->rrsz
*i
);
212 for (an
= qu
->allocations
; an
; an
= ann
) { ann
= an
->next
; free(an
); }
214 adns__vbuf_free(&qu
->vb
);
217 LIST_LINK_TAIL(qu
->ads
->output
,qu
);
220 void adns__query_fail(adns_query qu
, adns_status stat
) {
221 adns__reset_cnameonly(qu
);
222 qu
->answer
->status
= stat
;
223 adns__query_done(qu
);
226 void adns__makefinal_str(adns_query qu
, char **strp
) {
228 char *before
, *after
;
232 after
= adns__alloc_final(qu
,l
);
233 memcpy(after
,before
,l
);
237 void adns__makefinal_block(adns_query qu
, void **blpp
, size_t sz
) {
240 after
= adns__alloc_final(qu
,sz
);
241 memcpy(after
,*blpp
,sz
);