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 adns_rrtype type
, vbuf
*qumsg_vb
, int id
,
37 adns_queryflags flags
, struct timeval now
,
38 adns_status failstat
, const qcontext
*ctx
) {
43 const typeinfo
*typei
;
46 qu
= malloc(sizeof(*qu
)); if (!qu
) goto x_nomemory
;
47 qu
->answer
= malloc(sizeof(*qu
->answer
)); if (!qu
->answer
) goto x_freequ_nomemory
;
50 qu
->back
= qu
->next
= qu
->parent
= 0;
51 LIST_INIT(qu
->children
);
52 qu
->siblings
.next
= qu
->siblings
.back
= 0;
54 qu
->interim_allocd
= 0;
57 qu
->typei
= adns__findtype(type
);
58 adns__vbuf_init(&qu
->vb
);
61 qu
->cname_dglen
= qu
->cname_begin
= 0;
67 qu
->udpsent
= qu
->tcpfailed
= 0;
68 timerclear(&qu
->timeout
);
69 memcpy(&qu
->context
,ctx
,sizeof(qu
->context
));
70 memcpy(qu
->owner
,owner
,ol
); qu
->owner
[ol
]= 0;
72 qu
->answer
->status
= adns_s_ok
;
74 qu
->answer
->type
= type
;
79 qu
->answer
->rrsz
= qu
->rrsz
;
82 failstat
= adns_s_notimplemented
;
87 qu
->query_dgram
= malloc(qumsg_vb
->used
);
88 if (!qu
->query_dgram
) {
89 adns__query_fail(ads
,qu
,adns_s_nomemory
);
92 memcpy(qu
->query_dgram
,qumsg_vb
->buf
,qumsg_vb
->used
);
94 adns__vbuf_init(qumsg_vb
);
97 adns__query_fail(ads
,qu
,failstat
);
100 adns__query_udp(ads
,qu
,now
);
101 adns__autosys(ads
,now
);
109 return adns_s_nomemory
;
112 int adns_submit(adns_state ads
,
115 adns_queryflags flags
,
117 adns_query
*query_r
) {
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_invaliddomain
; 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
, type
,&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
);
151 r
= adns_wait(ads
,&qu
,answer_r
,0);
153 if (r
) adns_cancel(ads
,qu
);
157 void adns_cancel(adns_state ads
, adns_query query
) {
161 void *adns__alloc_interim(adns_state ads
, adns_query qu
, size_t sz
) {
164 assert(!qu
->final_allocspace
);
166 an
= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an
)) + sz
));
168 adns__query_fail(ads
,qu
,adns_s_nolocalmem
);
171 qu
->permalloclen
+= sz
;
172 an
->next
= qu
->allocations
;
174 return (byte
*)an
+ MEM_ROUND(sizeof(*an
));
177 void *adns__alloc_final(adns_query qu
, size_t sz
) {
178 /* When we're in the _final stage, we _subtract_ from interim_alloc'd
179 * each allocation, and use final_allocspace to point to the next free
185 rp
= qu
->final_allocspace
;
187 qu
->interim_allocd
-= sz
;
188 assert(qu
->interim_allocd
>=0);
189 qu
->final_allocspace
= (byte
*)rp
+ sz
;
193 void adns__reset_cnameonly(adns_state ads
, adns_query qu
) {
194 assert(qu
->final_allocspace
);
197 qu
->interim_allocd
= qu
->answer
->cname ?
MEM_ROUND(strlen(qu
->answer
->cname
)+1) : 0;
200 static void adns__query_done(adns_state ads
, adns_query qu
) {
205 qu
->answer
= ans
= realloc(qu
->answer
,
206 MEM_ROUND(MEM_ROUND(sizeof(*ans
)) +
207 qu
->interim_allocd
));
208 qu
->final_allocspace
= (byte
*)qu
->answer
+ MEM_ROUND(sizeof(*ans
));
210 adns__makefinal_str(qu
,&ans
->cname
);
212 adns__makefinal_block(qu
,&ans
->rrs
.untyped
,ans
->rrsz
*ans
->nrrs
);
213 for (i
=0; i
<ans
->nrrs
; i
++)
214 qu
->typei
->makefinal(ads
,qu
,ans
->rrs
.bytes
+ans
->rrsz
*i
);
217 for (an
= qu
->allocations
; an
; an
= ann
) { ann
= an
->next
; free(an
); }
219 adns__vbuf_free(&qu
->vb
);
222 LIST_LINK_TAIL(ads
->output
,qu
);
225 void adns__query_fail(adns_state ads
, adns_query qu
, adns_status stat
) {
226 adns__reset_cnameonly(ads
,qu
);
227 qu
->answer
->status
= stat
;
228 adns__query_done(ads
,qu
);
231 void adns__makefinal_str(adns_query qu
, char **strp
) {
233 char *before
, *after
;
237 after
= adns__alloc_final(qu
,l
);
238 memcpy(after
,before
,l
);
242 void adns__makefinal_block(adns__query qu
, void **blpp
, size_t sz
) {
245 after
= adns__alloc_final(qu
,sz
);
246 memcpy(after
,*blpp
,sz
);