From 63402be229facae2d0de9c5943a6ed25246fd021 Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Wed, 19 Oct 2022 14:02:48 -0400 Subject: clean up dns_parse_callback the only functional change here should be that MAXADDRS is only checked for RRs that provide address results, so that a CNAME which appears after an excessive number of address RRs does not get ignored. I'm not aware of any servers that order the RRs this way, and it may even be forbidden to do so, but I prefer having the callback logic not be order dependent. other than that, the motivation for this change is that the A and AAAA cases were mostly duplicate code that could be combined as a single code path. --- src/network/lookup_name.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'src/network/lookup_name.c') diff --git a/src/network/lookup_name.c b/src/network/lookup_name.c index be0c0bdd..5f6867cb 100644 --- a/src/network/lookup_name.c +++ b/src/network/lookup_name.c @@ -114,29 +114,29 @@ struct dpc_ctx { static int dns_parse_callback(void *c, int rr, const void *data, int len, const void *packet) { char tmp[256]; + int family; struct dpc_ctx *ctx = c; + if (rr == RR_CNAME) { + if (__dn_expand(packet, (const unsigned char *)packet + ABUF_SIZE, + data, tmp, sizeof tmp) > 0 && is_valid_hostname(tmp)) + strcpy(ctx->canon, tmp); + return 0; + } if (ctx->cnt >= MAXADDRS) return 0; + if (rr != ctx->rrtype) return 0; switch (rr) { case RR_A: - if (rr != ctx->rrtype) return 0; if (len != 4) return -1; - ctx->addrs[ctx->cnt].family = AF_INET; - ctx->addrs[ctx->cnt].scopeid = 0; - memcpy(ctx->addrs[ctx->cnt++].addr, data, 4); + family = AF_INET; break; case RR_AAAA: - if (rr != ctx->rrtype) return 0; if (len != 16) return -1; - ctx->addrs[ctx->cnt].family = AF_INET6; - ctx->addrs[ctx->cnt].scopeid = 0; - memcpy(ctx->addrs[ctx->cnt++].addr, data, 16); - break; - case RR_CNAME: - if (__dn_expand(packet, (const unsigned char *)packet + ABUF_SIZE, - data, tmp, sizeof tmp) > 0 && is_valid_hostname(tmp)) - strcpy(ctx->canon, tmp); + family = AF_INET6; break; } + ctx->addrs[ctx->cnt].family = family; + ctx->addrs[ctx->cnt].scopeid = 0; + memcpy(ctx->addrs[ctx->cnt++].addr, data, len); return 0; } -- cgit v1.2.3-70-g09d2