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
9 * Copyright (C) 1997-2000,2003,2006 Ian Jackson
10 * Copyright (C) 1999-2000,2003,2006 Tony Finch
11 * Copyright (C) 1991 Massachusetts Institute of Technology
12 * (See the file INSTALL for full details.)
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
43 static adns_query query_alloc(adns_state ads,
44 const typeinfo *typei, adns_rrtype type,
45 adns_queryflags flags, struct timeval now) {
46 /* Allocate a virgin query and return it. */
49 qu= malloc(sizeof(*qu)); if (!qu) return 0;
50 qu->answer= malloc(sizeof(*qu->answer));
51 if (!qu->answer) { free(qu); return 0; }
54 qu->state= query_tosend;
55 qu->back= qu->next= qu->parent= 0;
56 LIST_INIT(qu->children);
57 LINK_INIT(qu->siblings);
58 LIST_INIT(qu->allocations);
59 qu->interim_allocd= 0;
60 qu->preserved_allocd= 0;
61 qu->final_allocspace= 0;
66 adns__vbuf_init(&qu->vb);
69 qu->cname_dglen= qu->cname_begin= 0;
71 adns__vbuf_init(&qu->search_vb);
72 qu->search_origlen= qu->search_pos= qu->search_doneabs= 0;
74 qu->id= -2; /* will be overwritten with real id before we leave adns */
79 timerclear(&qu->timeout);
80 qu->expires= now.tv_sec + MAXTTLBELIEVE;
82 memset(&qu->ctx,0,sizeof(qu->ctx));
83 memset(&qu->extra,0,sizeof(qu->extra));
85 qu->answer->status= adns_s_ok;
86 qu->answer->cname= qu->answer->owner= 0;
87 qu->answer->type= type;
88 qu->answer->expires= -1;
90 qu->answer->rrs.untyped= 0;
91 qu->answer->rrsz= typei->rrsz;
96 static adns_queryflags default_ip6_flags(adns_state ads)
98 adns_queryflags flags = 0;
100 if (!(ads->iflags & adns_if_ip4only))
101 flags |= adns_qf_ip4;
102 if (!(ads->iflags & adns_if_ip6only))
103 flags |= adns_qf_ip6;
104 if (ads->iflags & adns_if_ip6mapped)
105 flags |= adns_qf_ip6mapped;
110 static void query_submit(adns_state ads, adns_query qu,
111 const typeinfo *typei, vbuf *qumsg_vb, int id,
112 adns_queryflags flags, struct timeval now) {
113 /* Fills in the query message in for a previously-allocated query,
114 * and submits it. Cannot fail. Takes over the memory for qumsg_vb.
118 adns__vbuf_init(qumsg_vb);
120 qu->query_dgram= malloc(qu->vb.used);
121 if (!qu->query_dgram) { adns__query_fail(qu,adns_s_nomemory); return; }
124 qu->query_dglen= qu->vb.used;
125 memcpy(qu->query_dgram,qu->vb.buf,qu->vb.used);
127 adns__query_send(qu,now);
130 /* FIXME: Take a adns_rrtype type artument? */
131 adns_status adns__internal_submit(adns_state ads, adns_query *query_r,
132 const typeinfo *typei, vbuf *qumsg_vb,
134 adns_queryflags flags, struct timeval now,
135 const qcontext *ctx) {
138 if (!(flags & adns__qf_ip_mask))
139 flags |= default_ip6_flags(ads);
141 qu= query_alloc(ads,typei,typei->typekey,flags,now);
142 if (!qu) { adns__vbuf_free(qumsg_vb); return adns_s_nomemory; }
145 memcpy(&qu->ctx,ctx,sizeof(qu->ctx));
147 if (typei->submithook) {
149 adns__vbuf_init(qumsg_vb);
151 typei->submithook(qu, flags, now);
152 if (qu->children.head) {
153 qu->state= query_childw;
154 LIST_LINK_TAIL(ads->childw,qu);
157 else query_submit(ads,qu, typei,qumsg_vb,id,flags,now);
162 static void query_simple(adns_state ads, adns_query qu,
163 const char *owner, int ol,
164 const typeinfo *typei, adns_queryflags flags,
165 struct timeval now) {
170 if (typei->submithook) {
171 stat= adns__mkquery_labels(ads, &qu->vb, owner, ol, typei, flags);
174 typei->submithook(qu, flags, now);
175 if (qu->children.head) {
176 qu->state= query_childw;
177 LIST_LINK_TAIL(ads->childw,qu);
182 stat= adns__mkquery(ads,&qu->vb,&id, owner,ol,
183 typei,qu->answer->type,flags);
187 adns__vbuf_init(&qu->vb);
188 query_submit(ads,qu, typei,&vb_new,id, flags,now);
192 if (stat == adns_s_querydomaintoolong && (flags & adns_qf_search))
193 adns__search_next(ads,qu,now);
195 adns__query_fail(qu,stat);
198 void adns__search_next(adns_state ads, adns_query qu, struct timeval now) {
199 const char *nextentry;
202 if (qu->search_doneabs<0) {
204 qu->search_doneabs= 1;
206 if (qu->search_pos >= ads->nsearchlist) {
207 if (qu->search_doneabs) {
208 qu->search_vb.used= qu->search_origlen;
209 stat= adns_s_nxdomain; goto x_fail;
212 qu->search_doneabs= 1;
215 nextentry= ads->searchlist[qu->search_pos++];
219 qu->search_vb.used= qu->search_origlen;
221 if (!adns__vbuf_append(&qu->search_vb,".",1) ||
222 !adns__vbuf_appendstr(&qu->search_vb,nextentry))
226 free(qu->query_dgram);
227 qu->query_dgram= 0; qu->query_dglen= 0;
229 query_simple(ads,qu, qu->search_vb.buf, qu->search_vb.used,
230 qu->typei, qu->flags, now);
234 stat= adns_s_nomemory;
236 adns__query_fail(qu,stat);
239 static int save_owner(adns_query qu, const char *owner, int ol) {
240 /* Returns 1 if OK, otherwise there was no memory. */
243 if (!(qu->flags & adns_qf_owner)) return 1;
248 ans->owner= adns__alloc_preserved(qu,ol+1); if (!ans->owner) return 0;
250 memcpy(ans->owner,owner,ol);
255 int adns_submit(adns_state ads,
258 adns_queryflags flags,
260 adns_query *query_r) {
263 const typeinfo *typei;
268 adns__consistency(ads,0,cc_entex);
270 if (!(flags & adns__qf_ip_mask))
271 flags |= default_ip6_flags(ads);
273 typei= adns__findtype(type);
274 if (!typei) return ENOSYS;
276 r= gettimeofday(&now,0); if (r) goto x_errno;
277 qu= query_alloc(ads,typei,type,flags,now); if (!qu) goto x_errno;
279 qu->ctx.ext= context;
281 memset(&qu->ctx.info,0,sizeof(qu->ctx.info));
286 if (!ol) { stat= adns_s_querydomaininvalid; goto x_adnsfail; }
287 if (ol>DNS_MAXDOMAIN+1) { stat= adns_s_querydomaintoolong; goto x_adnsfail; }
289 if (ol>=1 && owner[ol-1]=='.' && (ol<2 || owner[ol-2]!='\\')) {
290 flags &= ~adns_qf_search;
295 if (flags & adns_qf_search) {
296 r= adns__vbuf_append(&qu->search_vb,owner,ol);
297 if (!r) { stat= adns_s_nomemory; goto x_adnsfail; }
299 for (ndots=0, p=owner; (p= strchr(p,'.')); p++, ndots++);
300 qu->search_doneabs= (ndots >= ads->searchndots) ? -1 : 0;
301 qu->search_origlen= ol;
302 adns__search_next(ads,qu,now);
304 if (flags & adns_qf_owner) {
305 if (!save_owner(qu,owner,ol)) { stat= adns_s_nomemory; goto x_adnsfail; }
307 query_simple(ads,qu, owner,ol, typei,flags, now);
309 adns__autosys(ads,now);
310 adns__consistency(ads,qu,cc_entex);
314 adns__query_fail(qu,stat);
315 adns__consistency(ads,qu,cc_entex);
321 adns__consistency(ads,0,cc_entex);
325 int adns_submit_reverse_any(adns_state ads,
326 const struct sockaddr *addr,
329 adns_queryflags flags,
331 adns_query *query_r) {
332 const unsigned char *iaddr;
333 char *buf, *buf_free;
337 flags &= ~adns_qf_search;
339 switch (addr->sa_family) {
340 default: return ENOSYS;
342 iaddr= (const unsigned char*) &((const struct sockaddr_in*)addr)->sin_addr;
343 lreq= strlen(zone) + 4*4 + 1;
344 if (lreq > sizeof(shortbuf)) {
346 if (!buf) return errno;
352 sprintf(buf, "%d.%d.%d.%d.%s", iaddr[3], iaddr[2], iaddr[1], iaddr[0], zone);
355 iaddr= (const unsigned char*) &((const struct sockaddr_in6*)addr)->sin6_addr;
356 lreq = strlen(zone) + 2*32 + 1;
357 if (lreq > sizeof(shortbuf)) {
359 if (!buf) return errno;
366 strcpy(buf + 2*32, zone);
369 const unsigned char *p;
370 static const unsigned char hex[16] = "0123456789abcdef";
371 for (i = 0, p = iaddr + 15; i < 2*32; p--) {
372 buf[i++] = hex[*p & 0xf];
374 buf[i++] = hex[*p / 0x10];
379 r= adns_submit(ads,buf,type,flags,context,query_r);
384 int adns_submit_reverse(adns_state ads,
385 const struct sockaddr *addr,
387 adns_queryflags flags,
389 adns_query *query_r) {
391 /* Address record used for forward lookup and consistency check */
395 if (type != adns_r_ptr && type != adns_r_ptr_raw) return EINVAL;
396 memset(&rr, 0, sizeof(rr));
397 rr.addr.sa.sa_family = addr->sa_family;
399 switch (addr->sa_family) {
400 default: return ENOSYS;
402 zone = "in-addr.arpa";
403 rr.len = sizeof(rr.addr.inet);
404 rr.addr.inet.sin_addr = ((const struct sockaddr_in *)addr)->sin_addr;
408 rr.len = sizeof(rr.addr.inet6);
409 rr.addr.inet6.sin6_addr = ((const struct sockaddr_in6 *)addr)->sin6_addr;
413 r= adns_submit_reverse_any(ads,addr,zone,
414 type,flags,context,query_r);
416 (*query_r)->extra.info.ptr_addr = rr;
420 int adns_synchronous(adns_state ads,
423 adns_queryflags flags,
424 adns_answer **answer_r) {
428 r= adns_submit(ads,owner,type,flags,0,&qu);
431 r= adns_wait(ads,&qu,answer_r,0);
432 if (r) adns_cancel(qu);
437 static void *alloc_common(adns_query qu, size_t sz) {
440 if (!sz) return qu; /* Any old pointer will do */
441 assert(!qu->final_allocspace);
442 an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz));
444 LIST_LINK_TAIL(qu->allocations,an);
446 return (byte*)an + MEM_ROUND(sizeof(*an));
449 void *adns__realloc_interim(adns_query qu, void *p, size_t sz) {
454 assert(sz); /* Freeing via realloc not supported */
455 assert(!qu->final_allocspace);
457 an = (allocnode *) ((byte *) p - MEM_ROUND(sizeof(*an)));
458 assert(an->size <= qu->interim_allocd);
460 nan = realloc(an, MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz));
463 qu->interim_allocd -= nan->size;
464 qu->interim_allocd += sz;
467 if (nan->next) nan->next->back = nan;
468 else qu->allocations.tail = nan;
469 if (nan->back) nan->back->next = nan;
470 else qu->allocations.head = nan;
472 return (byte*)nan + MEM_ROUND(sizeof(*nan));
475 void *adns__alloc_interim(adns_query qu, size_t sz) {
479 rv= alloc_common(qu,sz);
481 qu->interim_allocd += sz;
485 void *adns__alloc_preserved(adns_query qu, size_t sz) {
489 rv= adns__alloc_interim(qu,sz);
491 qu->preserved_allocd += sz;
495 void *adns__alloc_mine(adns_query qu, size_t sz) {
496 return alloc_common(qu,MEM_ROUND(sz));
499 void adns__transfer_interim(adns_query from, adns_query to,
500 void *block, size_t sz) {
504 an= (void*)((byte*)block - MEM_ROUND(sizeof(*an)));
506 assert(!to->final_allocspace);
507 assert(!from->final_allocspace);
509 LIST_UNLINK(from->allocations,an);
510 LIST_LINK_TAIL(to->allocations,an);
513 from->interim_allocd -= sz;
514 to->interim_allocd += sz;
516 if (to->expires > from->expires) to->expires= from->expires;
519 void *adns__alloc_final(adns_query qu, size_t sz) {
520 /* When we're in the _final stage, we _subtract_ from interim_alloc'd
521 * each allocation, and use final_allocspace to point to the next free
527 rp= qu->final_allocspace;
529 qu->interim_allocd -= sz;
530 assert(qu->interim_allocd>=0);
531 qu->final_allocspace= (byte*)rp + sz;
535 static void cancel_children(adns_query qu) {
536 adns_query cqu, ncqu;
538 for (cqu= qu->children.head; cqu; cqu= ncqu) {
539 ncqu= cqu->siblings.next;
544 void adns__reset_preserved(adns_query qu) {
545 assert(!qu->final_allocspace);
548 qu->answer->rrs.untyped= 0;
549 qu->interim_allocd= qu->preserved_allocd;
552 static void free_query_allocs(adns_query qu) {
556 for (an= qu->allocations.head; an; an= ann) { ann= an->next; free(an); }
557 LIST_INIT(qu->allocations);
558 adns__vbuf_free(&qu->vb);
559 adns__vbuf_free(&qu->search_vb);
560 free(qu->query_dgram);
564 void adns_cancel(adns_query qu) {
568 adns__consistency(ads,qu,cc_entex);
569 if (qu->parent) LIST_UNLINK_PART(qu->parent->children,qu,siblings.);
572 LIST_UNLINK(ads->udpw,qu);
575 LIST_UNLINK(ads->tcpw,qu);
578 LIST_UNLINK(ads->childw,qu);
581 LIST_UNLINK(ads->output,qu);
586 free_query_allocs(qu);
589 adns__consistency(ads,0,cc_entex);
592 void adns__update_expires(adns_query qu, unsigned long ttl,
593 struct timeval now) {
596 assert(ttl <= MAXTTLBELIEVE);
597 max= now.tv_sec + ttl;
598 if (qu->expires < max) return;
602 static void makefinal_query(adns_query qu) {
608 if (qu->interim_allocd) {
609 ans= realloc(qu->answer,
610 MEM_ROUND(MEM_ROUND(sizeof(*ans)) + qu->interim_allocd));
611 if (!ans) goto x_nomem;
615 qu->final_allocspace= (byte*)ans + MEM_ROUND(sizeof(*ans));
616 adns__makefinal_str(qu,&ans->cname);
617 adns__makefinal_str(qu,&ans->owner);
620 adns__makefinal_block(qu, &ans->rrs.untyped, ans->nrrs*ans->rrsz);
622 for (rrn=0; rrn<ans->nrrs; rrn++)
623 qu->typei->makefinal(qu, ans->rrs.bytes + rrn*ans->rrsz);
626 free_query_allocs(qu);
630 qu->preserved_allocd= 0;
631 qu->answer->cname= 0;
632 qu->answer->owner= 0;
633 adns__reset_preserved(qu); /* (but we just threw away the preserved stuff) */
635 qu->answer->status= adns_s_nomemory;
636 free_query_allocs(qu);
639 void adns__query_done(adns_query qu) {
648 if (qu->flags & adns_qf_search && ans->status != adns_s_nomemory) {
649 if (!save_owner(qu, qu->search_vb.buf, qu->search_vb.used)) {
650 adns__query_fail(qu,adns_s_nomemory);
655 if (ans->nrrs && qu->typei->diff_needswap) {
656 if (!adns__vbuf_ensure(&qu->vb,qu->typei->rrsz)) {
657 adns__query_fail(qu,adns_s_nomemory);
660 adns__isort(ans->rrs.bytes, ans->nrrs, ans->rrsz,
662 (int(*)(void*, const void*, const void*))
663 qu->typei->diff_needswap,
666 if (ans->nrrs && qu->typei->postsort) {
667 qu->typei->postsort(qu->ads, ans->rrs.bytes, ans->nrrs, qu->typei);
670 ans->expires= qu->expires;
673 LIST_UNLINK_PART(parent->children,qu,siblings.);
674 LIST_UNLINK(qu->ads->childw,parent);
675 qu->ctx.callback(parent,qu);
676 free_query_allocs(qu);
681 LIST_LINK_TAIL(qu->ads->output,qu);
682 qu->state= query_done;
686 void adns__query_fail(adns_query qu, adns_status stat) {
687 adns__reset_preserved(qu);
688 qu->answer->status= stat;
689 adns__query_done(qu);
692 void adns__makefinal_str(adns_query qu, char **strp) {
694 char *before, *after;
699 after= adns__alloc_final(qu,l);
700 memcpy(after,before,l);
704 void adns__makefinal_block(adns_query qu, void **blpp, size_t sz) {
705 void *before, *after;
709 after= adns__alloc_final(qu,sz);
710 memcpy(after,before,sz);