+static adns_status append_addrs(adns_query qu, adns_query from, size_t rrsz,
+ adns_rr_addr **dp, int *dlen,
+ const adns_rr_addr *sp, int slen)
+{
+ size_t drrsz = *dlen*rrsz, srrsz = slen*rrsz;
+ byte *p;
+
+ if (!slen) return adns_s_ok;
+ p = adns__alloc_interim(qu, drrsz + srrsz);
+ if (!p) R_NOMEM;
+ if (*dlen) {
+ memcpy(p, *dp, drrsz);
+ adns__free_interim(qu, *dp);
+ }
+ memcpy(p + drrsz, sp, srrsz);
+ *dlen += slen;
+ *dp = (adns_rr_addr *)p;
+ if (from && qu->expires > from->expires) qu->expires = from->expires;
+ return adns_s_ok;
+}
+
+static void icb_addr(adns_query parent, adns_query child);
+
+static void addr_subqueries(adns_query qu, struct timeval now,
+ adns_queryflags qf_extra,
+ const byte *qd_dgram, int qd_dglen)
+{
+ int i, err, id;
+ adns_query cqu;
+ adns_queryflags qf = (qu->flags & ~adns_qf_search) | qf_extra;
+ adns_rrtype qtf = qu->answer->type & adns__qtf_deref;
+ unsigned which = qu->ctx.tinfo.addr.want & ~qu->ctx.tinfo.addr.have;
+ qcontext ctx;
+
+ memset(&ctx, 0, sizeof(ctx));
+ ctx.callback = icb_addr;
+ for (i = 0; i < addr_nrrtypes; i++) {
+ if (!(which & (1 << i))) continue;
+ err = adns__mkquery_frdgram(qu->ads, &qu->vb, &id, qd_dgram, qd_dglen,
+ DNS_HDRSIZE, addr_all_rrtypes[i], qf);
+ if (err) goto x_error;
+ err = adns__internal_submit(qu->ads, &cqu, &tinfo_addrsub,
+ addr_all_rrtypes[i] | qtf,
+ &qu->vb, id, qf, now, &ctx);
+ if (err) goto x_error;
+ cqu->answer->rrsz = qu->answer->rrsz;
+ cqu->parent = qu;
+ LIST_LINK_TAIL_PART(qu->children, cqu,siblings.);
+ }
+ qu->state = query_childw;
+ LIST_LINK_TAIL(qu->ads->childw, qu);
+ return;
+
+x_error:
+ adns__query_fail(qu, err);
+}
+
+static adns_status addr_submit(adns_query parent, adns_query *query_r,
+ vbuf *qumsg_vb, int id, unsigned want,
+ adns_queryflags flags, struct timeval now,
+ qcontext *ctx)
+{
+ /* This is effectively a substitute for adns__internal_submit, intended for
+ * the case where the caller (possibly) only wants a subset of the
+ * available record types. The memory management and callback rules are
+ * the same as for adns__internal_submit.
+ *
+ * Some differences: the query is linked onto the parent's children list
+ * before exit (though the parent's state is not changed, and it is not
+ * linked into the childw list queue); and we fiddle with the `tinfo'
+ * portion of the context structure (yes, modifying *ctx), since this is,
+ * in fact, the main purpose of this function.
+ */
+
+ adns_state ads = parent->ads;
+ adns_query qu;
+ adns_status err;
+ adns_rrtype type =
+ (adns_r_addr & adns_rrt_reprmask) |
+ (parent->answer->type & ~adns_rrt_reprmask);
+
+ ctx->tinfo.addr.want = want;
+ ctx->tinfo.addr.have = 0;
+ err = adns__internal_submit(ads, &qu, adns__findtype(adns_r_addr),
+ type, qumsg_vb, id, flags, now, ctx);
+ if (err) return err;
+
+ qu->parent = parent;
+ LIST_LINK_TAIL_PART(parent->children, qu, siblings.);
+ *query_r = qu;
+ return adns_s_ok;
+}
+
+static adns_status copy_cname_from_child(adns_query parent, adns_query child)
+{
+ adns_answer *pans = parent->answer, *cans = child->answer;
+ size_t n = strlen(cans->cname) + 1;
+
+ pans->cname = adns__alloc_preserved(parent, n);
+ if (!pans->cname) R_NOMEM;
+ memcpy(pans->cname, cans->cname, n);
+ return adns_s_ok;
+}
+
+static void done_addr_type(adns_query qu, adns_rrtype type)
+ { qu->ctx.tinfo.addr.have |= addr_rrtypeflag(type); }
+