+#define ADDR_MAXRRTYPES 2
+
+static void addr_rrtypes(adns_state ads, adns_rrtype type,
+ adns_queryflags qf,
+ adns_rrtype *rrty, size_t *nrrty)
+{
+ size_t n = 0;
+ adns_rrtype qtf = type & adns__qtf_deref;
+
+ if (!(type & adns__qtf_bigaddr) || !(type & adns__qtf_manyaf))
+ qf = (qf & adns__qf_afmask) | adns_qf_ipv4_only;
+
+ if (qf & adns_qf_ipv4_only) rrty[n++] = adns_r_a | qtf;
+ if (qf & adns_qf_ipv6_only) rrty[n++] = adns_r_aaaa | qtf;
+
+ *nrrty = n;
+}
+
+static size_t addr_rrsz(adns_query qu)
+{
+ return qu->answer->type & adns__qtf_bigaddr ?
+ sizeof(adns_rr_addr) : sizeof(adns_rr_addr_v4only);
+}
+
+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 = 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)
+{
+ adns_state ads = parent->ads;
+ adns_answer *pans = parent->answer, *cans = child->answer;
+ struct timeval tvbuf;
+ adns_status err;
+ const struct timeval *now = 0;
+
+ /* Must handle CNAMEs correctly. This gets very hairy if the answers we
+ * get are inconsistent.
+ */
+
+ if ((parent->flags & adns_qf_search) &&
+ cans->status == adns_s_nxdomain) {
+ if (parent->expires > child->expires) parent->expires = child->expires;
+ adns__cancel_children(parent);
+ adns__free_interim(parent, pans->rrs.bytes);
+ pans->rrs.bytes = 0; pans->nrrs = 0;
+ adns__must_gettimeofday(ads, &now, &tvbuf);
+ if (now) adns__search_next(ads, parent, *now);
+ return;
+ }
+
+ if (cans->status && cans->status != adns_s_nodata) {
+ adns__query_fail(parent, cans->status);
+ return;
+ }
+
+ assert(pans->rrsz == cans->rrsz);
+ err = append_addrs(parent, child, pans->rrsz,
+ &pans->rrs.addr, &pans->nrrs,
+ cans->rrs.addr, cans->nrrs);
+ if (err) { adns__query_fail(parent, err); return; }
+
+ if (parent->children.head) LIST_LINK_TAIL(ads->childw, parent);
+ else if (!pans->nrrs) adns__query_fail(parent, adns_s_nodata);
+ else adns__query_done(parent);
+}
+
+static void addr_subqueries(adns_query qu, struct timeval now,
+ const adns_rrtype *rrty, size_t nrrty)
+{
+ int i, err, id;
+ adns_query cqu;
+ adns_queryflags qf =
+ (qu->flags | adns__qf_senddirect) &
+ ~(adns_qf_search);
+ qcontext ctx;
+
+ if (!(qu->answer->type & adns__qtf_bigaddr))
+ qu->answer->rrsz = sizeof(adns_rr_addr_v4only);
+
+ /* This always makes child queries, even if there's only the one. This
+ * seems wasteful, but there's only one case where it'd be safe -- namely
+ * IPv4-only -- and that's not the case I want to optimize.
+ */
+ memset(&ctx, 0, sizeof(ctx));
+ ctx.callback = icb_addr;
+ for (i = 0; i < nrrty; i++) {
+ err = adns__mkquery_frdgram(qu->ads, &qu->vb, &id, qu->query_dgram,
+ qu->query_dglen, DNS_HDRSIZE, rrty[i], qf);
+ if (err) goto x_error;
+ err = adns__internal_submit(qu->ads, &cqu, qu->typei, rrty[i],
+ &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,
+ const adns_rrtype *rrty, size_t nrrty,
+ adns_queryflags flags, struct timeval now,
+ const 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).
+ */
+
+ adns_state ads = parent->ads;
+ adns_status err;
+ adns_rrtype type =
+ (adns_r_addr & adns_rrt_reprmask) |
+ (parent->answer->type & ~adns_rrt_reprmask);
+
+ err = adns__internal_submit(ads, query_r, adns__findtype(adns_r_addr),
+ type, qumsg_vb, id, flags | adns__qf_nosend,
+ now, ctx);
+ if (err) return err;
+
+ (*query_r)->parent = parent;
+ LIST_LINK_TAIL_PART(parent->children, *query_r, siblings.);
+ addr_subqueries(*query_r, now, rrty, nrrty);
+ return adns_s_ok;
+}
+
+static void qs_addr(adns_query qu, struct timeval now)
+{
+ adns_rrtype rrty[ADDR_MAXRRTYPES];
+ size_t nrrty;
+
+ addr_rrtypes(qu->ads, qu->answer->type, qu->flags, rrty, &nrrty);
+ addr_subqueries(qu, now, rrty, nrrty);
+}
+