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->preserved_allocd= 0;
52 qu->final_allocspace= 0;
57 adns__vbuf_init(&qu->vb);
60 qu->cname_dglen= qu->cname_begin= 0;
62 adns__vbuf_init(&qu->search_vb);
63 qu->search_origlen= qu->search_pos= qu->search_doneabs= 0;
69 qu->udpsent= qu->tcpfailed= 0;
70 timerclear(&qu->timeout);
71 qu->expires= now.tv_sec + MAXTTLBELIEVE;
73 memset(&qu->ctx,0,sizeof(qu->ctx));
75 qu->answer->status= adns_s_ok;
76 qu->answer->cname= qu->answer->owner= 0;
77 qu->answer->type= typei->type;
78 qu->answer->expires= -1;
80 qu->answer->rrs.untyped= 0;
81 qu->answer->rrsz= typei->rrsz;
86 static void query_submit(adns_state ads, adns_query qu,
87 const typeinfo *typei, vbuf *qumsg_vb, int id,
88 adns_queryflags flags, struct timeval now) {
89 /* Fills in the query message in for a previously-allocated query,
90 * and submits it. Cannot fail.
94 adns__vbuf_init(qumsg_vb);
96 qu->query_dgram= malloc(qu->vb.used);
97 if (!qu->query_dgram) { adns__query_fail(qu,adns_s_nomemory); return; }
100 qu->query_dglen= qu->vb.used;
101 memcpy(qu->query_dgram,qu->vb.buf,qu->vb.used);
103 adns__query_udp(qu,now);
104 adns__autosys(ads,now);
107 adns_status adns__internal_submit(adns_state ads, adns_query *query_r,
108 const typeinfo *typei, vbuf *qumsg_vb, int id,
109 adns_queryflags flags, struct timeval now,
110 const qcontext *ctx) {
113 qu= query_alloc(ads,typei,flags,now);
114 if (!qu) { adns__vbuf_free(qumsg_vb); return adns_s_nomemory; }
117 memcpy(&qu->ctx,ctx,sizeof(qu->ctx));
118 query_submit(ads,qu, typei,qumsg_vb,id,flags,now);
123 static void query_simple(adns_state ads, adns_query qu,
124 const char *owner, int ol,
125 const typeinfo *typei, adns_queryflags flags,
126 struct timeval now) {
131 adns__vbuf_init(&vb);
133 stat= adns__mkquery(ads,&vb,&id, owner,ol, typei,flags);
134 if (stat) { adns__query_fail(qu,stat); return; }
136 query_submit(ads,qu, typei,&vb,id, flags,now);
139 void adns__search_next(adns_state ads, adns_query qu, struct timeval now) {
140 const char *nextentry;
143 if (qu->search_doneabs<0) {
145 qu->search_doneabs= 1;
147 if (qu->search_pos >= ads->nsearchlist) {
148 if (qu->search_doneabs) {
149 stat= adns_s_nxdomain; goto x_fail;
153 qu->search_doneabs= 1;
156 nextentry= ads->searchlist[qu->search_pos++];
160 qu->search_vb.used= qu->search_origlen;
162 if (!adns__vbuf_append(&qu->search_vb,".",1) ||
163 !adns__vbuf_appendstr(&qu->search_vb,nextentry)) {
164 stat= adns_s_nomemory; goto x_fail;
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 static int save_owner(adns_query qu, const char *owner, int ol) {
179 /* Returns 1 if OK, otherwise there was no memory. */
185 ans->owner= adns__alloc_preserved(qu,ol+1); if (!ans->owner) return 0;
187 memcpy(ans->owner,owner,ol);
192 int adns_submit(adns_state ads,
195 adns_queryflags flags,
197 adns_query *query_r) {
200 const typeinfo *typei;
205 typei= adns__findtype(type);
206 if (!typei) return adns_s_unknownrrtype;
208 r= gettimeofday(&now,0); if (r) goto x_errno;
209 qu= query_alloc(ads,typei,flags,now); if (!qu) goto x_errno;
211 qu->ctx.ext= context;
213 memset(&qu->ctx.info,0,sizeof(qu->ctx.info));
218 if (!ol) { stat= adns_s_querydomaininvalid; goto x_adnsfail; }
219 if (ol>DNS_MAXDOMAIN+1) { stat= adns_s_querydomaintoolong; goto x_adnsfail; }
221 if (ol>=1 && owner[ol-1]=='.' && (ol<2 || owner[ol-2]!='\\')) {
222 flags &= ~adns_qf_search;
226 if (flags & adns_qf_search) {
227 r= adns__vbuf_append(&qu->search_vb,owner,ol);
228 if (!r) { stat= adns_s_nomemory; goto x_adnsfail; }
230 for (ndots=0, p=owner; (p= strchr(p,'.')); p++, ndots++);
231 qu->search_doneabs= (ndots >= ads->searchndots) ? -1 : 0;
232 qu->search_origlen= ol;
233 adns__search_next(ads,qu,now);
235 if (flags & adns_qf_owner) {
236 if (!save_owner(qu,owner,ol)) { stat= adns_s_nomemory; goto x_adnsfail; }
238 query_simple(ads,qu, owner,ol, typei,flags, now);
243 adns__query_fail(qu,stat);
252 int adns_synchronous(adns_state ads,
255 adns_queryflags flags,
256 adns_answer **answer_r) {
260 r= adns_submit(ads,owner,type,flags,0,&qu);
263 r= adns_wait(ads,&qu,answer_r,0);
264 if (r) adns_cancel(qu);
269 static void *alloc_common(adns_query qu, size_t sz) {
272 if (!sz) return qu; /* Any old pointer will do */
273 assert(!qu->final_allocspace);
274 an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz));
276 LIST_LINK_TAIL(qu->allocations,an);
277 return (byte*)an + MEM_ROUND(sizeof(*an));
280 void *adns__alloc_interim(adns_query qu, size_t sz) {
284 rv= alloc_common(qu,sz);
286 qu->interim_allocd += sz;
290 void *adns__alloc_preserved(adns_query qu, size_t sz) {
294 rv= adns__alloc_interim(qu,sz);
296 qu->preserved_allocd += sz;
300 void *adns__alloc_mine(adns_query qu, size_t sz) {
301 return alloc_common(qu,MEM_ROUND(sz));
304 void adns__transfer_interim(adns_query from, adns_query to, void *block, size_t sz) {
308 an= (void*)((byte*)block - MEM_ROUND(sizeof(*an)));
310 assert(!to->final_allocspace);
311 assert(!from->final_allocspace);
313 LIST_UNLINK(from->allocations,an);
314 LIST_LINK_TAIL(to->allocations,an);
316 from->interim_allocd -= sz;
317 to->interim_allocd += sz;
319 if (to->expires > from->expires) to->expires= from->expires;
322 void *adns__alloc_final(adns_query qu, size_t sz) {
323 /* When we're in the _final stage, we _subtract_ from interim_alloc'd
324 * each allocation, and use final_allocspace to point to the next free
330 rp= qu->final_allocspace;
332 qu->interim_allocd -= sz;
333 assert(qu->interim_allocd>=0);
334 qu->final_allocspace= (byte*)rp + sz;
338 static void cancel_children(adns_query qu) {
339 adns_query cqu, ncqu;
341 for (cqu= qu->children.head; cqu; cqu= ncqu) {
342 ncqu= cqu->siblings.next;
345 LIST_INIT(qu->children);
348 void adns__reset_preserved(adns_query qu) {
349 assert(!qu->final_allocspace);
352 qu->answer->rrs.untyped= 0;
353 qu->interim_allocd= qu->preserved_allocd;
356 static void free_query_allocs(adns_query qu) {
360 for (an= qu->allocations.head; an; an= ann) { ann= an->next; free(an); }
361 adns__vbuf_free(&qu->vb);
364 void adns_cancel(adns_query qu) {
366 case query_udp: case query_tcpwait: case query_tcpsent:
367 LIST_UNLINK(qu->ads->timew,qu);
370 LIST_UNLINK(qu->ads->childw,qu);
373 LIST_UNLINK(qu->ads->output,qu);
378 free_query_allocs(qu);
383 void adns__update_expires(adns_query qu, unsigned long ttl, struct timeval now) {
386 assert(ttl <= MAXTTLBELIEVE);
387 max= now.tv_sec + ttl;
388 if (qu->expires < max) return;
392 static void makefinal_query(adns_query qu) {
398 if (qu->interim_allocd) {
399 ans= realloc(qu->answer, MEM_ROUND(MEM_ROUND(sizeof(*ans)) + qu->interim_allocd));
400 if (!ans) goto x_nomem;
404 qu->final_allocspace= (byte*)ans + MEM_ROUND(sizeof(*ans));
405 adns__makefinal_str(qu,&ans->cname);
406 adns__makefinal_str(qu,&ans->owner);
409 adns__makefinal_block(qu, &ans->rrs.untyped, ans->nrrs*ans->rrsz);
411 for (rrn=0; rrn<ans->nrrs; rrn++)
412 qu->typei->makefinal(qu, ans->rrs.bytes + rrn*ans->rrsz);
415 free_query_allocs(qu);
419 qu->preserved_allocd= 0;
420 qu->answer->cname= 0;
421 qu->answer->owner= 0;
422 adns__reset_preserved(qu); /* (but we just threw away the preserved stuff) */
424 qu->answer->status= adns_s_nomemory;
425 free_query_allocs(qu);
428 void adns__query_done(adns_query qu) {
437 if (qu->flags & adns_qf_owner && qu->flags & adns_qf_search &&
438 ans->status != adns_s_nomemory) {
439 if (!save_owner(qu, qu->search_vb.buf, qu->search_vb.used)) {
440 adns__query_fail(qu,adns_s_nomemory);
445 if (ans->nrrs && qu->typei->diff_needswap) {
446 if (!adns__vbuf_ensure(&qu->vb,qu->typei->rrsz)) {
447 adns__query_fail(qu,adns_s_nomemory);
450 adns__isort(ans->rrs.bytes, ans->nrrs, ans->rrsz,
452 (int(*)(void*, const void*, const void*))qu->typei->diff_needswap,
456 ans->expires= qu->expires;
459 LIST_UNLINK_PART(parent->children,qu,siblings.);
460 LIST_UNLINK(qu->ads->childw,parent);
461 qu->ctx.callback(parent,qu);
462 free_query_allocs(qu);
466 LIST_LINK_TAIL(qu->ads->output,qu);
470 void adns__query_fail(adns_query qu, adns_status stat) {
471 adns__reset_preserved(qu);
472 qu->answer->status= stat;
473 adns__query_done(qu);
476 void adns__makefinal_str(adns_query qu, char **strp) {
478 char *before, *after;
483 after= adns__alloc_final(qu,l);
484 memcpy(after,before,l);
488 void adns__makefinal_block(adns_query qu, void **blpp, size_t sz) {
489 void *before, *after;
493 after= adns__alloc_final(qu,sz);
494 memcpy(after,before,sz);