From 4f35eb7591031a1e5ef9828f9304361f282f28b9 Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Wed, 11 Jul 2018 15:03:34 -0400 Subject: resolver: don't depend on v4mapped ipv6 to probe routability of v4 addrs to produce sorted results roughly corresponding to RFC 3484/6724, __lookup_name computes routability and choice of source address via dummy UDP connect operations (which do not produce any packets). since at the logical level, the properties fed into the sort key are computed on ipv6 addresses, the code was written to use the v4mapped ipv6 form of ipv4 addresses and share a common code path for them all. however, on kernels where ipv6 support has been completely omitted, this causes ipv4 to appear equally unroutable as ipv6, thereby putting unreachable ipv6 addresses before ipv4 addresses in the results. instead, use only ipv4 sockets to compute routability for ipv4 addresses. some gratuitous conversion back and forth is left so that the logic is not affected by these changes. it may be possible to simplify the ipv4 case considerably, thereby reducing code size and complexity. --- src/network/lookup_name.c | 47 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) (limited to 'src/network') diff --git a/src/network/lookup_name.c b/src/network/lookup_name.c index a1851f1f..0e6db9ef 100644 --- a/src/network/lookup_name.c +++ b/src/network/lookup_name.c @@ -355,36 +355,53 @@ int __lookup_name(struct address buf[static MAXADDRS], char canon[static 256], c * excessive runtime and code size cost and dubious benefit. * So far the label/precedence table cannot be customized. */ for (i=0; ilabel; int dprec = dpolicy->prec; int prefixlen = 0; - int fd = socket(AF_INET6, SOCK_DGRAM|SOCK_CLOEXEC, IPPROTO_UDP); + int fd = socket(family, SOCK_DGRAM|SOCK_CLOEXEC, IPPROTO_UDP); if (fd >= 0) { - if (!connect(fd, (void *)&da, sizeof da)) { + if (!connect(fd, da, dalen)) { key |= DAS_USABLE; - if (!getsockname(fd, (void *)&sa, - &(socklen_t){sizeof sa})) { - if (dscope == scopeof(&sa.sin6_addr)) + if (!getsockname(fd, sa, &salen)) { + if (family == AF_INET) memcpy( + &sa6.sin6_addr.s6_addr+12, + &sa4.sin_addr, 4); + if (dscope == scopeof(&sa6.sin6_addr)) key |= DAS_MATCHINGSCOPE; - if (dlabel == labelof(&sa.sin6_addr)) + if (dlabel == labelof(&sa6.sin6_addr)) key |= DAS_MATCHINGLABEL; - prefixlen = prefixmatch(&sa.sin6_addr, - &da.sin6_addr); + prefixlen = prefixmatch(&sa6.sin6_addr, + &da6.sin6_addr); } } close(fd); -- cgit v1.2.1