Skip to content

Commit 3ab4398

Browse files
praschkeandrewrk
authored andcommitted
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 parent 9b807f9 commit 3ab4398

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

lib/std/net.zig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -882,21 +882,21 @@ fn linuxLookupName(
882882
} else {
883883
try linuxLookupNameFromHosts(addrs, canon, name, family, port);
884884
if (addrs.items.len == 0) {
885-
try linuxLookupNameFromDnsSearch(addrs, canon, name, family, port);
886-
}
887-
if (addrs.items.len == 0) {
888-
// RFC 6761 Section 6.3
885+
// RFC 6761 Section 6.3.3
889886
// Name resolution APIs and libraries SHOULD recognize localhost
890887
// names as special and SHOULD always return the IP loopback address
891888
// for address queries and negative responses for all other query
892889
// types.
893890

894-
// Check for equal to "localhost" or ends in ".localhost"
895-
if (mem.endsWith(u8, name, "localhost") and (name.len == "localhost".len or name[name.len - "localhost".len] == '.')) {
891+
// Check for equal to "localhost(.)" or ends in ".localhost(.)"
892+
const localhost = if (name[name.len - 1] == '.') "localhost." else "localhost";
893+
if (mem.endsWith(u8, name, localhost) and (name.len == localhost.len or name[name.len - localhost.len] == '.')) {
896894
try addrs.append(LookupAddr{ .addr = .{ .in = Ip4Address.parse("127.0.0.1", port) catch unreachable } });
897895
try addrs.append(LookupAddr{ .addr = .{ .in6 = Ip6Address.parse("::1", port) catch unreachable } });
898896
return;
899897
}
898+
899+
try linuxLookupNameFromDnsSearch(addrs, canon, name, family, port);
900900
}
901901
}
902902
} else {

0 commit comments

Comments
 (0)