+static unsigned addr_rrtypes(adns_state ads, adns_rrtype type,
+ adns_queryflags qf)
+{
+ adns_queryflags permitaf = 0, hackaf = 0;
+ unsigned want = 0;
+
+ if (!(qf & (adns_qf_ipv4_only | adns_qf_ipv6_only)))
+ qf |= adns_qf_ipv4_only | adns_qf_ipv6_only;
+ if (!(type & adns__qtf_bigaddr) || !(type & adns__qtf_manyaf))
+ qf = (qf & adns__qf_afmask) | adns_qf_ipv4_only;
+ else if (ads->iflags & adns_if_afmask) {
+ if (ads->iflags & adns_if_af_v4only) {
+ permitaf |= adns_qf_ipv4_only;
+ hackaf |= adns_qf_domapv4;
+ }
+ if (ads->iflags & adns_if_af_v6only)
+ permitaf |= adns_qf_ipv6_only;
+ if (qf & permitaf)
+ qf &= hackaf | permitaf | ~adns__qf_afmask;
+ }
+
+ if (qf & adns_qf_ipv4_only) want |= addr_rf_a;
+ if (qf & adns_qf_ipv6_only) want |= addr_rf_aaaa;
+
+ return want;
+}
+
+static int gsz_addr(adns_rrtype type)
+{
+ return 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;
+
+ 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); }
+
+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 now;
+ adns_status err;
+ int id;
+
+ if (!(child->flags & adns__qf_addr_cname) &&
+ (parent->flags & adns__qf_addr_answer) &&
+ (!!pans->cname != !!cans->cname ||
+ (pans->cname && strcmp(pans->cname, cans->cname) != 0))) {
+ /* We've detected an inconsistency in CNAME records, and must deploy
+ * countermeasures.
+ */
+
+ if (!pans->cname) {
+ /* The child has a CNAME record, but the parent doesn't. We must
+ * discard all of the parent's addresses, and substitute the child's.
+ */
+
+ assert(pans->rrsz == cans->rrsz);
+ adns__free_interim(parent, pans->rrs.bytes);
+ adns__transfer_interim(child, parent, cans->rrs.bytes);
+ pans->rrs.bytes = cans->rrs.bytes;
+ pans->nrrs = cans->nrrs;
+ parent->ctx.tinfo.addr.have = 0;
+ done_addr_type(parent, cans->type);
+ err = copy_cname_from_child(parent, child); if (err) goto x_err;
+ }
+
+ /* We've settled on the CNAME (now) associated with the parent, which
+ * already has appropriate address records. Build a query datagram for
+ * this name so that we can issue child queries for the missing address
+ * families. The child's vbuf looks handy for this.
+ */
+ err = adns__mkquery(ads, &child->vb, &id, pans->cname,
+ strlen(pans->cname), &tinfo_addrsub,
+ adns_r_addr, parent->flags);
+ if (err) goto x_err;
+
+ /* Now cancel the remaining children, and try again with the CNAME we've
+ * settled on.
+ */
+ adns__cancel_children(parent);
+ if (gettimeofday(&now, 0)) goto x_gtod;
+ addr_subqueries(parent, now, adns__qf_addr_cname,
+ child->vb.buf, child->vb.used);
+ return;
+ }
+
+ if (cans->cname && !pans->cname) {
+ err = copy_cname_from_child(parent, child);
+ if (err) goto x_err;
+ }
+
+ if ((parent->flags & adns_qf_search) &&
+ !pans->cname && cans->status == adns_s_nxdomain) {
+ /* We're searching a list of suffixes, this is the first answer, and it
+ * tells us that the name doesn't exist. Try the next one.
+ */
+
+ 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;
+ if (gettimeofday(&now, 0)) goto x_gtod;
+ adns__search_next(ads, parent, now);
+ return;
+ }
+
+ if (cans->status && cans->status != adns_s_nodata)
+ { err = cans->status; goto x_err; }
+
+ assert(pans->rrsz == cans->rrsz);
+ err = append_addrs(parent, child, pans->rrsz,
+ &pans->rrs.addr, &pans->nrrs,
+ cans->rrs.addr, cans->nrrs);
+ if (err) goto x_err;
+ done_addr_type(parent, cans->type);
+
+ 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);
+ parent->flags |= adns__qf_addr_answer;
+ return;
+
+x_gtod:
+ adns__diag(ads, -1, parent, "gettimeofday failed: %s", strerror(errno));
+ err = adns_s_systemfail;
+ goto x_err;
+
+x_err:
+ adns__query_fail(parent, err);
+}
+
+static void qs_addr(adns_query qu, struct timeval now)
+{
+ if (!qu->ctx.tinfo.addr.want) {
+ qu->ctx.tinfo.addr.want =
+ addr_rrtypes(qu->ads, qu->answer->type, qu->flags);
+ qu->ctx.tinfo.addr.have = 0;
+ }
+ addr_subqueries(qu, now, 0, qu->query_dgram, qu->query_dglen);
+}
+
+/*
+ * _domain (pap,csp,cs)
+ * _dom_raw (pa)
+ */
+