+ return st;
+}
+
+static void query_simple(adns_state ads, adns_query qu,
+ const char *owner, int ol,
+ const typeinfo *typei, adns_queryflags flags,
+ struct timeval now) {
+ vbuf vb_new;
+ int id;
+ adns_status st;
+
+ st= adns__mkquery(ads,&qu->vb,&id, owner,ol,
+ typei,qu->answer->type, flags);
+ if (st) {
+ if (st == adns_s_querydomaintoolong && (flags & adns_qf_search)) {
+ adns__search_next(ads,qu,now);
+ return;
+ } else {
+ adns__query_fail(qu,st);
+ return;
+ }
+ }
+
+ st= check_domain_name(ads, flags,&qu->ctx,typei, qu->vb.buf,qu->vb.used);
+ if (st) { adns__query_fail(qu,st); return; }
+
+ vb_new= qu->vb;
+ adns__vbuf_init(&qu->vb);
+ query_submit(ads,qu, typei,&vb_new,id, flags,now);
+}
+
+void adns__search_next(adns_state ads, adns_query qu, struct timeval now) {
+ const char *nextentry;
+ adns_status st;
+
+ if (qu->search_doneabs<0) {
+ nextentry= 0;
+ qu->search_doneabs= 1;
+ } else {
+ if (qu->search_pos >= ads->nsearchlist) {
+ if (qu->search_doneabs) {
+ qu->search_vb.used= qu->search_origlen;
+ st= adns_s_nxdomain; goto x_fail;
+ } else {
+ nextentry= 0;
+ qu->search_doneabs= 1;
+ }
+ } else {
+ nextentry= ads->searchlist[qu->search_pos++];
+ }
+ }
+
+ qu->search_vb.used= qu->search_origlen;
+ if (nextentry) {
+ if (!adns__vbuf_append(&qu->search_vb,".",1) ||
+ !adns__vbuf_appendstr(&qu->search_vb,nextentry))
+ goto x_nomemory;
+ }
+
+ free(qu->query_dgram);
+ qu->query_dgram= 0; qu->query_dglen= 0;
+
+ query_simple(ads,qu, qu->search_vb.buf, qu->search_vb.used,
+ qu->typei, qu->flags, now);
+ return;
+
+x_nomemory:
+ st= adns_s_nomemory;
+x_fail:
+ adns__query_fail(qu,st);
+}
+
+static int save_owner(adns_query qu, const char *owner, int ol) {
+ /* Returns 1 if OK, otherwise there was no memory. */
+ adns_answer *ans;
+
+ if (!(qu->flags & adns_qf_owner)) return 1;
+
+ ans= qu->answer;
+ assert(!ans->owner);
+
+ ans->owner= adns__alloc_preserved(qu,ol+1); if (!ans->owner) return 0;
+
+ memcpy(ans->owner,owner,ol);
+ ans->owner[ol]= 0;
+ return 1;