authorgravatar for stel@comfy.monsterpraschke <stel@comfy.monster> 2023-01-10 15:26:01+00:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-10 16:10:15-07:00
log0a03d68594374698b5d751530d6ee7f47a2f75b3
treeeb48cc503c65299c19d8a3e95610b8c46176f3dc
parentc8a7dc22b1b224c036f285910fdd3fccfb2a9075

std.net: check for localhost names before asking DNS

the old implementation asks DNS before checking if it shouldn't. additionally, it did not catch absolute 'localhost.' names.

1 files changed, 6 insertions(+), 6 deletions(-)

lib/std/net.zig+6-6
......@@ -869,21 +869,21 @@ fn linuxLookupName(
869869 } else {
870870 try linuxLookupNameFromHosts(addrs, canon, name, family, port);
871871 if (addrs.items.len == 0) {
872 try linuxLookupNameFromDnsSearch(addrs, canon, name, family, port);
873 }
874 if (addrs.items.len == 0) {
875 // RFC 6761 Section 6.3
872 // RFC 6761 Section 6.3.3
876873 // Name resolution APIs and libraries SHOULD recognize localhost
877874 // names as special and SHOULD always return the IP loopback address
878875 // for address queries and negative responses for all other query
879876 // types.
880877
881 // Check for equal to "localhost" or ends in ".localhost"
882 if (mem.endsWith(u8, name, "localhost") and (name.len == "localhost".len or name[name.len - "localhost".len] == '.')) {
878 // Check for equal to "localhost(.)" or ends in ".localhost(.)"
879 const localhost = if (name[name.len - 1] == '.') "localhost." else "localhost";
880 if (mem.endsWith(u8, name, localhost) and (name.len == localhost.len or name[name.len - localhost.len] == '.')) {
883881 try addrs.append(LookupAddr{ .addr = .{ .in = Ip4Address.parse("127.0.0.1", port) catch unreachable } });
884882 try addrs.append(LookupAddr{ .addr = .{ .in6 = Ip6Address.parse("::1", port) catch unreachable } });
885883 return;
886884 }
885
886 try linuxLookupNameFromDnsSearch(addrs, canon, name, family, port);
887887 }
888888 }
889889 } else {