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 18:09:07-05:00
log3ab43988c1cc5e22c84faccd1816df6e8094d9a3
treea18a20aa1c5f1896918c580853b1c5dde7215521
parent9b807f9c171e9e5998447ab3846d29de921cf8dd

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
...@@ -882,21 +882,21 @@ fn linuxLookupName(...@@ -882,21 +882,21 @@ fn linuxLookupName(
882 } else {882 } else {
883 try linuxLookupNameFromHosts(addrs, canon, name, family, port);883 try linuxLookupNameFromHosts(addrs, canon, name, family, port);
884 if (addrs.items.len == 0) {884 if (addrs.items.len == 0) {
885 try linuxLookupNameFromDnsSearch(addrs, canon, name, family, port);885 // RFC 6761 Section 6.3.3
886 }
887 if (addrs.items.len == 0) {
888 // RFC 6761 Section 6.3
889 // Name resolution APIs and libraries SHOULD recognize localhost886 // Name resolution APIs and libraries SHOULD recognize localhost
890 // names as special and SHOULD always return the IP loopback address887 // names as special and SHOULD always return the IP loopback address
891 // for address queries and negative responses for all other query888 // for address queries and negative responses for all other query
892 // types.889 // types.
893890
894 // Check for equal to "localhost" or ends in ".localhost"891 // 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] == '.')) {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] == '.')) {
896 try addrs.append(LookupAddr{ .addr = .{ .in = Ip4Address.parse("127.0.0.1", port) catch unreachable } });894 try addrs.append(LookupAddr{ .addr = .{ .in = Ip4Address.parse("127.0.0.1", port) catch unreachable } });
897 try addrs.append(LookupAddr{ .addr = .{ .in6 = Ip6Address.parse("::1", port) catch unreachable } });895 try addrs.append(LookupAddr{ .addr = .{ .in6 = Ip6Address.parse("::1", port) catch unreachable } });
898 return;896 return;
899 }897 }
898
899 try linuxLookupNameFromDnsSearch(addrs, canon, name, family, port);
900 }900 }
901 }901 }
902 } else {902 } else {