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 static adns_query query_alloc(adns_state ads, const typeinfo *typei,
37 adns_queryflags flags, struct timeval now) {
38 /* Allocate a virgin query and return it. */
41 qu= malloc(sizeof(*qu)); if (!qu) return 0;
42 qu->answer= malloc(sizeof(*qu->answer)); if (!qu->answer) { free(qu); return 0; }
46 qu->back= qu->next= qu->parent= 0;
47 LIST_INIT(qu->children);
48 LINK_INIT(qu->siblings);
49 LIST_INIT(qu->allocations);
50 qu->interim_allocd= 0;
51 qu->final_allocspace= 0;
56 adns__vbuf_init(&qu->vb);
59 qu->cname_dglen= qu->cname_begin= 0;
61 adns__vbuf_init(&qu->search_vb);
62 qu->search_origlen= qu->search_pos= qu->search_doneabs= 0;
68 qu->udpsent= qu->tcpfailed= 0;
69 timerclear(&qu->timeout);
70 qu->expires= now.tv_sec + MAXTTLBELIEVE;
72 memset(&qu->ctx,0,sizeof(qu->ctx));
74 qu->answer->status= adns_s_ok;
76 qu->answer->type= typei->type;
77 qu->answer->expires= -1;
80 qu->answer->rrsz= typei->rrsz;
85 static void query_submit(adns_state ads, adns_query qu,
86 const typeinfo *typei, vbuf *qumsg_vb, int id,
87 adns_queryflags flags, struct timeval now) {
88 /* Fills in the query message in for a previously-allocated query,
89 * and submits it. Cannot fail.
93 adns__vbuf_init(qumsg_vb);
95 qu->query_dgram= malloc(qu->vb.used);
96 if (!qu->query_dgram) { adns__query_fail(qu,adns_s_nomemory); return; }
99 qu->query_dglen= qu->vb.used;
100 memcpy(qu->query_dgram,qu->vb.buf,qu->vb.used);
102 adns__query_udp(qu,now);
103 adns__autosys(ads,now);
106 adns_status adns__internal_submit(adns_state ads, adns_query *query_r,
107 const typeinfo *typei, vbuf *qumsg_vb, int id,
108 adns_queryflags flags, struct timeval now,
109 const qcontext *ctx) {
112 qu= query_alloc(ads,typei,flags,now);
113 if (!qu) { adns__vbuf_free(qumsg_vb); return adns_s_nomemory; }
116 memcpy(&qu->ctx,ctx,sizeof(qu->ctx));
117 query_submit(ads,qu, typei,qumsg_vb,id,flags,now);
122 static void query_simple(adns_state ads, adns_query qu,
123 const char *owner, int ol,
124 const typeinfo *typei, adns_queryflags flags,
125 struct timeval now) {
130 adns__vbuf_init(&vb);
132 stat= adns__mkquery(ads,&vb,&id, owner,ol, typei,flags);
133 if (stat) { adns__query_fail(qu,stat); return; }
135 query_submit(ads,qu, typei,&vb,id, flags,now);
138 void adns__search_next(adns_state ads, adns_query qu, struct timeval now) {
139 const char *nextentry;
142 if (qu->search_doneabs<0) {
144 qu->search_doneabs= 1;
146 if (qu->search_pos >= ads->nsearchlist) {
147 if (qu->search_doneabs) {
148 stat= adns_s_nxdomain; goto x_fail;
152 qu->search_doneabs= 1;
155 nextentry= ads->searchlist[qu->search_pos++];
160 if (!adns__vbuf_append(&qu->search_vb,".",1) ||
161 !adns__vbuf_appendstr(&qu->search_vb,nextentry)) {
162 stat= adns_s_nomemory; goto x_fail;
164 qu->search_vb.used= qu->search_origlen;
168 free(qu->query_dgram);
169 qu->query_dgram= 0; qu->query_dglen= 0;
171 query_simple(ads,qu, qu->search_vb.buf, qu->search_vb.used, qu->typei, qu->flags, now);
175 adns__query_fail(qu,stat);
178 int adns_submit(adns_state ads,
181 adns_queryflags flags,
183 adns_query *query_r) {
186 const typeinfo *typei;
191 typei= adns__findtype(type);
192 if (!typei) return adns_s_unknownrrtype;
194 r= gettimeofday(&now,0); if (r) goto x_errno;
195 qu= query_alloc(ads,typei,flags,now); if (!qu) goto x_errno;
197 qu->ctx.ext= context;
199 memset(&qu->ctx.info,0,sizeof(qu->ctx.info));
202 if (!ol) { stat= adns_s_querydomaininvalid; goto x_adnsfail; }
203 if (ol>DNS_MAXDOMAIN+1) { stat= adns_s_querydomaintoolong; goto x_adnsfail; }
205 if (ol>=2 && owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_qf_search; ol--; }
207 if (flags & adns_qf_search) {
208 r= adns__vbuf_append(&qu->search_vb,owner,ol);
209 if (!r) { stat= adns_s_nomemory; goto x_adnsfail; }
211 for (ndots=0, p=owner; (p= strchr(p,'.')); p++, ndots++);
212 qu->search_doneabs= (ndots >= ads->searchndots) ? -1 : 0;
214 qu->search_origlen= ol;
216 adns__search_next(ads,qu,now);
220 query_simple(ads,qu, owner,ol, typei,flags, now);
224 adns__query_fail(qu,stat);
233 int adns_synchronous(adns_state ads,
236 adns_queryflags flags,
237 adns_answer **answer_r) {
241 r= adns_submit(ads,owner,type,flags,0,&qu);
244 r= adns_wait(ads,&qu,answer_r,0);
245 if (r) adns_cancel(qu);
250 static void *alloc_common(adns_query qu, size_t sz) {
253 if (!sz) return qu; /* Any old pointer will do */
254 assert(!qu->final_allocspace);
255 an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz));
257 LIST_LINK_TAIL(qu->allocations,an);
258 return (byte*)an + MEM_ROUND(sizeof(*an));
261 void *adns__alloc_interim(adns_query qu, size_t sz) {
263 qu->interim_allocd += sz;
264 return alloc_common(qu,sz);
267 void *adns__alloc_mine(adns_query qu, size_t sz) {
268 return alloc_common(qu,MEM_ROUND(sz));
271 void adns__transfer_interim(adns_query from, adns_query to, void *block, size_t sz) {
275 an= (void*)((byte*)block - MEM_ROUND(sizeof(*an)));
277 assert(!to->final_allocspace);
278 assert(!from->final_allocspace);
280 LIST_UNLINK(from->allocations,an);
281 LIST_LINK_TAIL(to->allocations,an);
283 from->interim_allocd -= sz;
284 to->interim_allocd += sz;
286 if (to->expires > from->expires) to->expires= from->expires;
289 void *adns__alloc_final(adns_query qu, size_t sz) {
290 /* When we're in the _final stage, we _subtract_ from interim_alloc'd
291 * each allocation, and use final_allocspace to point to the next free
297 rp= qu->final_allocspace;
299 qu->interim_allocd -= sz;
300 assert(qu->interim_allocd>=0);
301 qu->final_allocspace= (byte*)rp + sz;
305 static void cancel_children(adns_query qu) {
306 adns_query cqu, ncqu;
308 for (cqu= qu->children.head; cqu; cqu= ncqu) {
309 ncqu= cqu->siblings.next;
312 LIST_INIT(qu->children);
315 void adns__reset_cnameonly(adns_query qu) {
316 assert(!qu->final_allocspace);
320 qu->interim_allocd= qu->answer->cname ? MEM_ROUND(strlen(qu->answer->cname)+1) : 0;
323 static void free_query_allocs(adns_query qu) {
327 for (an= qu->allocations.head; an; an= ann) { ann= an->next; free(an); }
328 adns__vbuf_free(&qu->vb);
331 void adns_cancel(adns_query qu) {
333 case query_udp: case query_tcpwait: case query_tcpsent:
334 LIST_UNLINK(qu->ads->timew,qu);
337 LIST_UNLINK(qu->ads->childw,qu);
340 LIST_UNLINK(qu->ads->output,qu);
345 free_query_allocs(qu);
350 void adns__update_expires(adns_query qu, unsigned long ttl, struct timeval now) {
353 assert(ttl <= MAXTTLBELIEVE);
354 max= now.tv_sec + ttl;
355 if (qu->expires < max) return;
359 static void makefinal_query(adns_query qu) {
365 if (qu->interim_allocd) {
366 ans= realloc(qu->answer, MEM_ROUND(MEM_ROUND(sizeof(*ans)) + qu->interim_allocd));
367 if (!ans) goto x_nomem;
371 qu->final_allocspace= (byte*)ans + MEM_ROUND(sizeof(*ans));
372 adns__makefinal_str(qu,&ans->cname);
375 adns__makefinal_block(qu, &ans->rrs.untyped, ans->nrrs*ans->rrsz);
377 for (rrn=0; rrn<ans->nrrs; rrn++)
378 qu->typei->makefinal(qu, ans->rrs.bytes + rrn*ans->rrsz);
381 free_query_allocs(qu);
385 qu->answer->status= adns_s_nomemory;
386 qu->answer->cname= 0;
387 adns__reset_cnameonly(qu);
388 free_query_allocs(qu);
391 void adns__query_done(adns_query qu) {
398 if (ans->nrrs && qu->typei->diff_needswap) {
399 if (!adns__vbuf_ensure(&qu->vb,qu->typei->rrsz)) {
400 adns__query_fail(qu,adns_s_nomemory);
403 adns__isort(ans->rrs.bytes, ans->nrrs, ans->rrsz,
405 (int(*)(void*, const void*, const void*))qu->typei->diff_needswap,
409 ans->expires= qu->expires;
412 LIST_UNLINK_PART(parent->children,qu,siblings.);
413 qu->ctx.callback(parent,qu);
414 free_query_allocs(qu);
418 LIST_LINK_TAIL(qu->ads->output,qu);
422 void adns__query_fail(adns_query qu, adns_status stat) {
423 adns__reset_cnameonly(qu);
424 qu->answer->status= stat;
425 adns__query_done(qu);
428 void adns__makefinal_str(adns_query qu, char **strp) {
430 char *before, *after;
435 after= adns__alloc_final(qu,l);
436 memcpy(after,before,l);
440 void adns__makefinal_block(adns_query qu, void **blpp, size_t sz) {
441 void *before, *after;
445 after= adns__alloc_final(qu,sz);
446 memcpy(after,before,sz);