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(...@@ -869,21 +869,21 @@ fn linuxLookupName(
869 } else {869 } else {
870 try linuxLookupNameFromHosts(addrs, canon, name, family, port);870 try linuxLookupNameFromHosts(addrs, canon, name, family, port);
871 if (addrs.items.len == 0) {871 if (addrs.items.len == 0) {
872 try linuxLookupNameFromDnsSearch(addrs, canon, name, family, port);872 // RFC 6761 Section 6.3.3
873 }
874 if (addrs.items.len == 0) {
875 // RFC 6761 Section 6.3
876 // Name resolution APIs and libraries SHOULD recognize localhost873 // Name resolution APIs and libraries SHOULD recognize localhost
877 // names as special and SHOULD always return the IP loopback address874 // names as special and SHOULD always return the IP loopback address
878 // for address queries and negative responses for all other query875 // for address queries and negative responses for all other query
879 // types.876 // types.
880877
881 // Check for equal to "localhost" or ends in ".localhost"878 // 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] == '.')) {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] == '.')) {
883 try addrs.append(LookupAddr{ .addr = .{ .in = Ip4Address.parse("127.0.0.1", port) catch unreachable } });881 try addrs.append(LookupAddr{ .addr = .{ .in = Ip4Address.parse("127.0.0.1", port) catch unreachable } });
884 try addrs.append(LookupAddr{ .addr = .{ .in6 = Ip6Address.parse("::1", port) catch unreachable } });882 try addrs.append(LookupAddr{ .addr = .{ .in6 = Ip6Address.parse("::1", port) catch unreachable } });
885 return;883 return;
886 }884 }
885
886 try linuxLookupNameFromDnsSearch(addrs, canon, name, family, port);
887 }887 }
888 }888 }
889 } else {889 } else {