Skip to content

Commit 0a03d68

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 c8a7dc2 commit 0a03d68

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
@@ -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 {

0 commit comments

Comments
 (0)