From f56f3c5824af17516bcf3a0559b98cdad20bd416 Mon Sep 17 00:00:00 2001 From: hequn Date: Sat, 18 Mar 2023 03:58:02 +0800 Subject: [PATCH] Enable IPv4 mapped address conversion in linux version getAddressList (#14916) It seems like the original code of setsockopt is not effective because i catch the EINVAL branch when uncomment this code, it should call setsockopt before the bind call. This should fix issue #14900. Co-authored-by: Qun He --- lib/std/net.zig | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/lib/std/net.zig b/lib/std/net.zig index 7222433fd5..8800bbe6be 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -741,7 +741,7 @@ pub fn tcpConnectToAddress(address: Address) TcpConnectToAddressError!Stream { return Stream{ .handle = sockfd }; } -const GetAddressListError = std.mem.Allocator.Error || std.fs.File.OpenError || std.fs.File.ReadError || std.os.SocketError || std.os.BindError || error{ +const GetAddressListError = std.mem.Allocator.Error || std.fs.File.OpenError || std.fs.File.ReadError || std.os.SocketError || std.os.BindError || std.os.SetSockOptError || error{ // TODO: break this up into error sets from the various underlying functions TemporaryNameServerFailure, @@ -1534,15 +1534,10 @@ fn resMSendRc( ns[i] = iplit.addr; assert(ns[i].getPort() == 53); if (iplit.addr.any.family != os.AF.INET) { - sl = @sizeOf(os.sockaddr.in6); family = os.AF.INET6; } } - // Get local address and open/bind a socket - var sa: Address = undefined; - @memset(@ptrCast([*]u8, &sa), 0, @sizeOf(Address)); - sa.any.family = family; const flags = os.SOCK.DGRAM | os.SOCK.CLOEXEC | os.SOCK.NONBLOCK; const fd = os.socket(family, flags, 0) catch |err| switch (err) { error.AddressFamilyNotSupported => blk: { @@ -1556,27 +1551,35 @@ fn resMSendRc( else => |e| return e, }; defer os.closeSocket(fd); - try os.bind(fd, &sa.any, sl); // Past this point, there are no errors. Each individual query will // yield either no reply (indicated by zero length) or an answer // packet which is up to the caller to interpret. // Convert any IPv4 addresses in a mixed environment to v4-mapped - // TODO - //if (family == AF.INET6) { - // setsockopt(fd, IPPROTO.IPV6, IPV6_V6ONLY, &(int){0}, sizeof 0); - // for (i=0; i