clean up dns_parse_callback
authorRich Felker <dalias@aerifal.cx>
Wed, 19 Oct 2022 18:02:48 +0000 (14:02 -0400)
committerRich Felker <dalias@aerifal.cx>
Wed, 19 Oct 2022 18:02:48 +0000 (14:02 -0400)
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

index be0c0bd..5f6867c 100644 (file)
@@ -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;
 }