X-Git-Url: https://git.distorted.org.uk/~mdw/adns/blobdiff_plain/8837244370251f16e8f41097d4cbe5ef0058038c..eaa4473118b245a9dc1d806309eb70d4b2c6eaa4:/src/query.c diff --git a/src/query.c b/src/query.c index 9c7fffe..666fdd7 100644 --- a/src/query.c +++ b/src/query.c @@ -5,7 +5,7 @@ * - query submission and cancellation (user-visible and internal) */ /* - * This file is part of adns, which is Copyright (C) 1997, 1998 Ian Jackson + * This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,6 +27,7 @@ #include #include #include +#include #include @@ -46,7 +47,7 @@ int adns__internal_submit(adns_state ads, adns_query *query_r, qu->back= qu->next= qu->parent= 0; LIST_INIT(qu->children); qu->siblings.next= qu->siblings.back= 0; - qu->allocations= 0; + LIST_INIT(qu->allocations); qu->interim_allocd= 0; qu->final_allocspace= 0; @@ -62,7 +63,8 @@ int adns__internal_submit(adns_state ads, adns_query *query_r, qu->udpnextserver= 0; qu->udpsent= qu->tcpfailed= 0; timerclear(&qu->timeout); - memcpy(&qu->context,ctx,sizeof(qu->context)); + memcpy(&qu->ctx,ctx,sizeof(qu->ctx)); + qu->expires= now.tv_sec + MAXTTLBELIEVE; qu->answer->status= adns_s_ok; qu->answer->cname= 0; @@ -77,7 +79,7 @@ int adns__internal_submit(adns_state ads, adns_query *query_r, if (qumsg_vb->used) { qu->query_dgram= malloc(qumsg_vb->used); if (!qu->query_dgram) { - adns__query_fail(qu,adns_s_nolocalmem); + adns__query_fail(qu,adns_s_nomemory); return adns_s_ok; } memcpy(qu->query_dgram,qumsg_vb->buf,qumsg_vb->used); @@ -100,7 +102,7 @@ int adns__internal_submit(adns_state ads, adns_query *query_r, free(qu); x_nomemory: adns__vbuf_free(qumsg_vb); - return adns_s_nolocalmem; + return adns_s_nomemory; } int adns_submit(adns_state ads, @@ -117,16 +119,19 @@ int adns_submit(adns_state ads, struct timeval now; typei= adns__findtype(type); - if (!typei) return adns_s_notimplemented; + if (!typei) return adns_s_unknownrrtype; ctx.ext= context; + ctx.callback= 0; + memset(&ctx.info,0,sizeof(ctx.info)); + r= gettimeofday(&now,0); if (r) return errno; id= 0; adns__vbuf_init(&vb); ol= strlen(owner); - if (ol<=1 || ol>DNS_MAXDOMAIN+1) { stat= adns_s_domaintoolong; goto xit; } + if (ol<=1 || ol>DNS_MAXDOMAIN+1) { stat= adns_s_querydomaintoolong; goto xit; } if (owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_qf_search; ol--; } @@ -160,8 +165,7 @@ static void *alloc_common(adns_query qu, size_t sz) { assert(!qu->final_allocspace); an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz)); if (!an) return 0; - an->next= qu->allocations; - qu->allocations= an; + LIST_LINK_TAIL(qu->allocations,an); return (byte*)an + MEM_ROUND(sizeof(*an)); } @@ -175,6 +179,24 @@ void *adns__alloc_mine(adns_query qu, size_t sz) { return alloc_common(qu,MEM_ROUND(sz)); } +void adns__transfer_interim(adns_query from, adns_query to, void *block, size_t sz) { + allocnode *an; + + if (!block) return; + an= (void*)((byte*)block - MEM_ROUND(sizeof(*an))); + + assert(!to->final_allocspace); + assert(!from->final_allocspace); + + LIST_UNLINK(from->allocations,an); + LIST_LINK_TAIL(to->allocations,an); + + from->interim_allocd -= sz; + to->interim_allocd += sz; + + if (to->expires > from->expires) to->expires= from->expires; +} + void *adns__alloc_final(adns_query qu, size_t sz) { /* When we're in the _final stage, we _subtract_ from interim_alloc'd * each allocation, and use final_allocspace to point to the next free @@ -191,9 +213,19 @@ void *adns__alloc_final(adns_query qu, size_t sz) { return rp; } +static void cancel_children(adns_query qu) { + adns_query cqu, ncqu; + + for (cqu= qu->children.head; cqu; cqu= ncqu) { + ncqu= cqu->siblings.next; + adns_cancel(cqu); + } + LIST_INIT(qu->children); +} + void adns__reset_cnameonly(adns_query qu) { - /* fixme: cancel children */ assert(!qu->final_allocspace); + cancel_children(qu); qu->answer->nrrs= 0; qu->answer->rrs= 0; qu->interim_allocd= qu->answer->cname ? MEM_ROUND(strlen(qu->answer->cname)+1) : 0; @@ -201,26 +233,22 @@ void adns__reset_cnameonly(adns_query qu) { static void free_query_allocs(adns_query qu) { allocnode *an, *ann; - adns_query cqu, ncqu; - for (cqu= qu->children.head; cqu; cqu= ncqu) { - ncqu= cqu->siblings.next; - adns_cancel(cqu); - } - for (an= qu->allocations; an; an= ann) { ann= an->next; free(an); } + cancel_children(qu); + for (an= qu->allocations.head; an; an= ann) { ann= an->next; free(an); } adns__vbuf_free(&qu->vb); } -void adns_cancel(adns_query query) { +void adns_cancel(adns_query qu) { switch (qu->state) { case query_udp: case query_tcpwait: case query_tcpsent: - LIST_UNLINK(ads->timew,qu); + LIST_UNLINK(qu->ads->timew,qu); break; case query_child: - LIST_UNLINK(ads->childw,qu); + LIST_UNLINK(qu->ads->childw,qu); break; case query_done: - LIST_UNLINK(ads->output,qu); + LIST_UNLINK(qu->ads->output,qu); break; default: abort(); @@ -229,26 +257,25 @@ void adns_cancel(adns_query query) { free(qu->answer); free(qu); } - -void adns__query_done(adns_query qu) { + +void adns__update_expires(adns_query qu, unsigned long ttl, struct timeval now) { + time_t max; + + assert(ttl <= MAXTTLBELIEVE); + max= now.tv_sec + ttl; + if (qu->expires < max) return; + qu->expires= max; +} + +static void makefinal_query(adns_query qu) { adns_answer *ans; int rrn; ans= qu->answer; - + if (qu->interim_allocd) { - if (qu->answer->nrrs && qu->typei->diff_needswap) { - if (!adns__vbuf_ensure(&qu->vb,qu->typei->rrsz)) { - adns__query_fail(qu,adns_s_nolocalmem); - return; - } - } ans= realloc(qu->answer, MEM_ROUND(MEM_ROUND(sizeof(*ans)) + qu->interim_allocd)); - if (!ans) { - qu->answer->cname= 0; - adns__query_fail(qu, adns_s_nolocalmem); - return; - } + if (!ans) goto x_nomem; qu->answer= ans; } @@ -260,16 +287,47 @@ void adns__query_done(adns_query qu) { for (rrn=0; rrnnrrs; rrn++) qu->typei->makefinal(qu, ans->rrs.bytes + rrn*ans->rrsz); - - if (qu->typei->diff_needswap) - adns__isort(ans->rrs.bytes, ans->nrrs, ans->rrsz, - qu->vb.buf, qu->typei->diff_needswap); } - + free_query_allocs(qu); + return; + x_nomem: + qu->answer->status= adns_s_nomemory; + qu->answer->cname= 0; + adns__reset_cnameonly(qu); + free_query_allocs(qu); +} + +void adns__query_done(adns_query qu) { + adns_answer *ans; + adns_query parent; + qu->id= -1; - LIST_LINK_TAIL(qu->ads->output,qu); + ans= qu->answer; + + if (ans->nrrs && qu->typei->diff_needswap) { + if (!adns__vbuf_ensure(&qu->vb,qu->typei->rrsz)) { + adns__query_fail(qu,adns_s_nomemory); + return; + } + adns__isort(ans->rrs.bytes, ans->nrrs, ans->rrsz, + qu->vb.buf, + (int(*)(void*, const void*, const void*))qu->typei->diff_needswap, + qu->ads); + } + + ans->expires= qu->expires; + parent= qu->parent; + if (parent) { + LIST_UNLINK_PART(parent->children,qu,siblings.); + qu->ctx.callback(parent,qu); + free_query_allocs(qu); + free(qu); + } else { + makefinal_query(qu); + LIST_LINK_TAIL(qu->ads->output,qu); + } } void adns__query_fail(adns_query qu, adns_status stat) {