authorgravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2020-12-02 11:13:59+11:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-12-01 19:13:59-05:00
logdb0cb54f4ea023869d955449ba5e3eb86981d413
treefe0a68988e6eee4b084729718de5870f70991653
parentc91c4dc25696224fd1d15d5b5fbf9698c8a77272
signature Signed by PGP key 4AEE18F83AFDEB23

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);