+int adns_submit_reverse_any(adns_state ads,
+ const struct sockaddr *addr,
+ const char *zone,
+ adns_rrtype type,
+ adns_queryflags flags,
+ void *context,
+ adns_query *query_r) {
+ char *buf, *buf_free = 0;
+ char shortbuf[100];
+ int r;
+
+ flags &= ~adns_qf_search;
+
+ buf = shortbuf;
+ r= adns__make_reverse_domain(addr,zone, &buf,sizeof(shortbuf),&buf_free);
+ if (r) return r;
+ r= adns_submit(ads,buf,type,flags,context,query_r);
+ free(buf_free);
+ return r;
+}
+
+int adns_submit_reverse(adns_state ads,
+ const struct sockaddr *addr,
+ adns_rrtype type,
+ adns_queryflags flags,
+ void *context,
+ adns_query *query_r) {
+ if (((type^adns_r_ptr) & adns_rrt_reprmask) &&
+ ((type^adns_r_ptr_raw) & adns_rrt_reprmask))
+ return EINVAL;
+ return adns_submit_reverse_any(ads,addr,0,type,flags,context,query_r);
+}
+
+int adns_synchronous(adns_state ads,
+ const char *owner,
+ adns_rrtype type,
+ adns_queryflags flags,
+ adns_answer **answer_r) {
+ adns_query qu;
+ int r;
+
+ r= adns_submit(ads,owner,type,flags,0,&qu);
+ if (r) return r;
+
+ r= adns_wait(ads,&qu,answer_r,0);
+ if (r) adns_cancel(qu);
+
+ return r;
+}
+
+static void *alloc_common(adns_query qu, size_t sz) {
+ allocnode *an;
+
+ if (!sz) return qu; /* Any old pointer will do */
+ assert(!qu->final_allocspace);
+ an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz));
+ if (!an) return 0;
+ LIST_LINK_TAIL(qu->allocations,an);
+ an->sz= sz;
+ return (byte*)an + MEM_ROUND(sizeof(*an));
+}
+
+void *adns__alloc_interim(adns_query qu, size_t sz) {
+ void *rv;
+
+ sz= MEM_ROUND(sz);
+ rv= alloc_common(qu,sz);
+ if (!rv) return 0;
+ qu->interim_allocd += sz;
+ return rv;
+}
+
+void *adns__alloc_preserved(adns_query qu, size_t sz) {
+ void *rv;
+
+ sz= MEM_ROUND(sz);
+ rv= adns__alloc_interim(qu,sz);
+ if (!rv) return 0;
+ qu->preserved_allocd += sz;
+ return rv;
+}
+
+static allocnode *alloc_info(adns_query qu, void *p, size_t *sz_r) {
+ allocnode *an;
+
+ if (!p || p == qu) { *sz_r= 0; return 0; }
+ an= (allocnode *)((byte *)p - MEM_ROUND(sizeof(allocnode)));
+ *sz_r= MEM_ROUND(an->sz);
+ return an;
+}
+
+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= alloc_info(from, block, &sz);
+
+ if (!an) return;
+
+ 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
+ * bit.