authorgravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2020-12-02 11:13:59+11:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-12-01 17:14:43-07:00
logd15a0ec7b28d7f87b93b2a4fa627ebfebcff2a01
tree657a9d640a15c9a43535f6d5d0bcc3247a155881
parent0858d7b0df97abc409370df5094cc93f1f3532ff

Localhost is special (#6955)

* std: always return loopback address when looking up localhost * std: also return Ipv6 loopback * std: remove commented out obsolete code

1 files changed, 14 insertions(+), 3 deletions(-)

lib/std/net.zig+14-3
...@@ -19,9 +19,6 @@ pub const Address = extern union {...@@ -19,9 +19,6 @@ pub const Address = extern union {
19 in6: Ip6Address,19 in6: Ip6Address,
20 un: if (has_unix_sockets) os.sockaddr_un else void,20 un: if (has_unix_sockets) os.sockaddr_un else void,
2121
22 // TODO this crashed the compiler. https://github.com/ziglang/zig/issues/3512
23 //pub const localhost = initIp4(parseIp4("127.0.0.1") catch unreachable, 0);
24
25 /// Parse the given IP address string into an Address value.22 /// Parse the given IP address string into an Address value.
26 /// It is recommended to use `resolveIp` instead, to handle23 /// It is recommended to use `resolveIp` instead, to handle
27 /// IPv6 link-local unix addresses.24 /// IPv6 link-local unix addresses.
...@@ -839,6 +836,20 @@ fn linuxLookupName(...@@ -839,6 +836,20 @@ fn linuxLookupName(
839 if (addrs.items.len == 0) {836 if (addrs.items.len == 0) {
840 try linuxLookupNameFromDnsSearch(addrs, canon, name, family, port);837 try linuxLookupNameFromDnsSearch(addrs, canon, name, family, port);
841 }838 }
839 if (addrs.items.len == 0) {
840 // RFC 6761 Section 6.3
841 // Name resolution APIs and libraries SHOULD recognize localhost
842 // names as special and SHOULD always return the IP loopback address
843 // for address queries and negative responses for all other query
844 // types.
845
846 // Check for equal to "localhost" or ends in ".localhost"
847 if (mem.endsWith(u8, name, "localhost") and (name.len == "localhost".len or name[name.len - "localhost".len] == '.')) {
848 try addrs.append(LookupAddr{ .addr = .{ .in = Ip4Address.parse("127.0.0.1", port) catch unreachable } });
849 try addrs.append(LookupAddr{ .addr = .{ .in6 = Ip6Address.parse("::1", port) catch unreachable } });
850 return;
851 }
852 }
842 }853 }
843 } else {854 } else {
844 try canon.resize(0);855 try canon.resize(0);