From f924e44262ac9480bc749e3b6c1c602665c86ad1 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Sun, 20 Sep 2026 01:18:37 -0700 Subject: [PATCH] add dual stack support to Address listen --- net.zig | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/net.zig b/net.zig index d3efe46e92616303ecdddbf3457ba1f673d4658e..c32cb0e8c7a4e2e8f4f0c20b9bd823c17f6aa31d 100644 --- a/net.zig +++ b/net.zig @@ -77,7 +77,11 @@ pub const Address = extern union { }); } - pub fn listen(adr: Address, options: ListenOptions) !Server { + pub fn listen(adr_: Address, options: ListenOptions) !Server { + var adr = adr_; + if (options.dualstack_if_possible and adr_.any.family == .INET and adr_.in.sa.addr.addr == 0) { + adr = .{ .in6 = .init(@bitCast(@as([16]u8, @splat(0))), @byteSwap(adr_.in.sa.port)) }; + } const sockfd = try sys.socket( adr.any.family, sys.SOCK.STREAM | sys.SOCK.CLOEXEC, @@ -89,6 +93,9 @@ pub const Address = extern union { }; errdefer s.stream.close(); + if (options.dualstack_if_possible and adr_.any.family == .INET and adr_.in.sa.addr.addr == 0) { + try sys.setsockopt(sockfd, sys.IPPROTO.IPV6, sys.IPV6.V6ONLY, &std.mem.toBytes(@as(c_int, 0))); + } if (options.reuse_address) { try sys.setsockopt(sockfd, sys.SOL.SOCKET, sys.SO.REUSEADDR, &std.mem.toBytes(@as(c_int, 1))); @@ -110,6 +117,8 @@ pub const Address = extern union { kernel_backlog: u31 = 511, /// Sets SO_REUSEADDR and SO_REUSEPORT. reuse_address: bool = false, + /// When listening on 0.0.0.0, listens on [::] instead and disables IPV6_V6ONLY. + dualstack_if_possible: bool = true, }; pub fn tcpConnect(adr: Address) !Stream { -- 2.54.0