X-Git-Url: https://git.distorted.org.uk/~mdw/adns/blobdiff_plain/7e6a84a102fb70052a7c02ab80f26fd2e2cfb943..74c948317bd4785fbfa8b6047b8da2e1705b1863:/src/query.c diff --git a/src/query.c b/src/query.c index 81c04cb..b9860bf 100644 --- a/src/query.c +++ b/src/query.c @@ -72,11 +72,11 @@ static adns_query query_alloc(adns_state ads, const typeinfo *typei, memset(&qu->ctx,0,sizeof(qu->ctx)); qu->answer->status= adns_s_ok; - qu->answer->cname= 0; + qu->answer->cname= qu->answer->owner= 0; qu->answer->type= typei->type; qu->answer->expires= -1; qu->answer->nrrs= 0; - qu->answer->rrs= 0; + qu->answer->rrs.untyped= 0; qu->answer->rrsz= typei->rrsz; return qu; @@ -156,12 +156,11 @@ void adns__search_next(adns_state ads, adns_query qu, struct timeval now) { } } + qu->search_vb.used= qu->search_origlen; if (nextentry) { if (!adns__vbuf_append(&qu->search_vb,".",1) || !adns__vbuf_appendstr(&qu->search_vb,nextentry)) { stat= adns_s_nomemory; goto x_fail; - } else { - qu->search_vb.used= qu->search_origlen; } } @@ -175,6 +174,20 @@ x_fail: adns__query_fail(qu,stat); } +static int save_owner(adns_query qu, const char *owner, int ol) { + /* Returns 1 if OK, otherwise there was no memory. */ + adns_answer *ans; + + ans= qu->answer; + assert(!ans->owner); + + ans->owner= adns__alloc_interim(qu,ol+1); if (!ans->owner) return 0; + + memcpy(ans->owner,owner,ol); + ans->owner[ol]= 0; + return 1; +} + int adns_submit(adns_state ads, const char *owner, adns_rrtype type, @@ -198,11 +211,16 @@ int adns_submit(adns_state ads, qu->ctx.callback= 0; memset(&qu->ctx.info,0,sizeof(qu->ctx.info)); + *query_r= qu; + ol= strlen(owner); if (!ol) { stat= adns_s_querydomaininvalid; goto x_adnsfail; } if (ol>DNS_MAXDOMAIN+1) { stat= adns_s_querydomaintoolong; goto x_adnsfail; } - if (ol>=2 && owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_qf_search; ol--; } + if (ol>=1 && owner[ol-1]=='.' && (ol<2 || owner[ol-2]!='\\')) { + flags &= ~adns_qf_search; + ol--; + } if (flags & adns_qf_search) { r= adns__vbuf_append(&qu->search_vb,owner,ol); @@ -210,14 +228,14 @@ int adns_submit(adns_state ads, for (ndots=0, p=owner; (p= strchr(p,'.')); p++, ndots++); qu->search_doneabs= (ndots >= ads->searchndots) ? -1 : 0; - qu->search_origlen= ol; - adns__search_next(ads,qu,now); - return 0; + } else { + if (flags & adns_qf_owner) { + if (!save_owner(qu,owner,ol)) { stat= adns_s_nomemory; goto x_adnsfail; } + } + query_simple(ads,qu, owner,ol, typei,flags, now); } - - query_simple(ads,qu, owner,ol, typei,flags, now); return 0; x_adnsfail: @@ -316,7 +334,7 @@ void adns__reset_cnameonly(adns_query qu) { assert(!qu->final_allocspace); cancel_children(qu); qu->answer->nrrs= 0; - qu->answer->rrs= 0; + qu->answer->rrs.untyped= 0; qu->interim_allocd= qu->answer->cname ? MEM_ROUND(strlen(qu->answer->cname)+1) : 0; } @@ -370,6 +388,7 @@ static void makefinal_query(adns_query qu) { qu->final_allocspace= (byte*)ans + MEM_ROUND(sizeof(*ans)); adns__makefinal_str(qu,&ans->cname); + adns__makefinal_str(qu,&ans->owner); if (ans->nrrs) { adns__makefinal_block(qu, &ans->rrs.untyped, ans->nrrs*ans->rrsz); @@ -395,6 +414,14 @@ void adns__query_done(adns_query qu) { qu->id= -1; ans= qu->answer; + if (qu->flags & adns_qf_owner && qu->flags & adns_qf_search && + ans->status != adns_s_nomemory) { + if (!save_owner(qu, qu->search_vb.buf, qu->search_vb.used)) { + adns__query_fail(qu,adns_s_nomemory); + return; + } + } + if (ans->nrrs && qu->typei->diff_needswap) { if (!adns__vbuf_ensure(&qu->vb,qu->typei->rrsz)) { adns__query_fail(qu,adns_s_nomemory); @@ -410,6 +437,7 @@ void adns__query_done(adns_query qu) { parent= qu->parent; if (parent) { LIST_UNLINK_PART(parent->children,qu,siblings.); + if (!parent->children.head) LIST_UNLINK(qu->ads->childw,parent); qu->ctx.callback(parent,qu); free_query_allocs(qu); free(qu);