authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-09-20 01:18:37-07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-09-20 01:18:37-07:00
logf924e44262ac9480bc749e3b6c1c602665c86ad1
tree97489cfb23a96f7e3c95fe83413582f476fbd4eb
parent0bb9548662a6721b7fff23259b608e03e7740215
signature Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

add dual stack support to Address listen


1 files changed, 10 insertions(+), 1 deletions(-)

net.zig+10-1
......@@ -77,7 +77,11 @@ pub const Address = extern union {
7777 });
7878 }
7979
80 pub fn listen(adr: Address, options: ListenOptions) !Server {
80 pub fn listen(adr_: Address, options: ListenOptions) !Server {
81 var adr = adr_;
82 if (options.dualstack_if_possible and adr_.any.family == .INET and adr_.in.sa.addr.addr == 0) {
83 adr = .{ .in6 = .init(@bitCast(@as([16]u8, @splat(0))), @byteSwap(adr_.in.sa.port)) };
84 }
8185 const sockfd = try sys.socket(
8286 adr.any.family,
8387 sys.SOCK.STREAM | sys.SOCK.CLOEXEC,
......@@ -89,6 +93,9 @@ pub const Address = extern union {
8993 };
9094 errdefer s.stream.close();
9195
96 if (options.dualstack_if_possible and adr_.any.family == .INET and adr_.in.sa.addr.addr == 0) {
97 try sys.setsockopt(sockfd, sys.IPPROTO.IPV6, sys.IPV6.V6ONLY, &std.mem.toBytes(@as(c_int, 0)));
98 }
9299 if (options.reuse_address) {
93100 try sys.setsockopt(sockfd, sys.SOL.SOCKET, sys.SO.REUSEADDR, &std.mem.toBytes(@as(c_int, 1)));
94101
......@@ -110,6 +117,8 @@ pub const Address = extern union {
110117 kernel_backlog: u31 = 511,
111118 /// Sets SO_REUSEADDR and SO_REUSEPORT.
112119 reuse_address: bool = false,
120 /// When listening on 0.0.0.0, listens on [::] instead and disables IPV6_V6ONLY.
121 dualstack_if_possible: bool = true,
113122 };
114123
115124 pub fn tcpConnect(adr: Address) !Stream {