From 0a03d68594374698b5d751530d6ee7f47a2f75b3 Mon Sep 17 00:00:00 2001 From: praschke Date: Tue, 10 Jan 2023 15:26:01 +0000 Subject: [PATCH] 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. --- lib/std/net.zig | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/std/net.zig b/lib/std/net.zig index 21fa36c4eb..436d510c6c 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -869,21 +869,21 @@ fn linuxLookupName( } else { try linuxLookupNameFromHosts(addrs, canon, name, family, port); if (addrs.items.len == 0) { - try linuxLookupNameFromDnsSearch(addrs, canon, name, family, port); - } - if (addrs.items.len == 0) { - // RFC 6761 Section 6.3 + // RFC 6761 Section 6.3.3 // Name resolution APIs and libraries SHOULD recognize localhost // names as special and SHOULD always return the IP loopback address // for address queries and negative responses for all other query // types. - // Check for equal to "localhost" or ends in ".localhost" - if (mem.endsWith(u8, name, "localhost") and (name.len == "localhost".len or name[name.len - "localhost".len] == '.')) { + // Check for equal to "localhost(.)" or ends in ".localhost(.)" + const localhost = if (name[name.len - 1] == '.') "localhost." else "localhost"; + if (mem.endsWith(u8, name, localhost) and (name.len == localhost.len or name[name.len - localhost.len] == '.')) { try addrs.append(LookupAddr{ .addr = .{ .in = Ip4Address.parse("127.0.0.1", port) catch unreachable } }); try addrs.append(LookupAddr{ .addr = .{ .in6 = Ip6Address.parse("::1", port) catch unreachable } }); return; } + + try linuxLookupNameFromDnsSearch(addrs, canon, name, family, port); } } } else {