authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-20 17:03:06-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-23 02:37:11-07:00
log12a9e0f4150f0d30c851727f4dc2354fa6c90682
treec22f047d48dd4a9a71a2dbe96504d444bbd97eeb
parent6395ba852a88f0e0b2a2f0659f1daf9d08e90157

std.net.listen: fix Windows API use

In a previous commit I removed a load-bearing use of `@hasDecl` to detect whether the SO.REUSEPORT option should be set. `@hasDecl` should not be used for OS feature detection because it can hide bugs. The new logic checks for the operating system specifically and then does the thing that is supposed to be done on that operating system directly.

1 files changed, 12 insertions(+), 9 deletions(-)

lib/std/net.zig+12-9
...@@ -217,7 +217,10 @@ pub const Address = extern union {...@@ -217,7 +217,10 @@ pub const Address = extern union {
217 /// If more than this many connections pool in the kernel, clients will start217 /// If more than this many connections pool in the kernel, clients will start
218 /// seeing "Connection refused".218 /// seeing "Connection refused".
219 kernel_backlog: u31 = 128,219 kernel_backlog: u31 = 128,
220 /// Sets SO_REUSEADDR and SO_REUSEPORT on POSIX.
221 /// Sets SO_REUSEADDR on Windows, which is roughly equivalent.
220 reuse_address: bool = false,222 reuse_address: bool = false,
223 /// Deprecated. Does nothing.
221 reuse_port: bool = false,224 reuse_port: bool = false,
222 force_nonblocking: bool = false,225 force_nonblocking: bool = false,
223 };226 };
...@@ -242,15 +245,15 @@ pub const Address = extern union {...@@ -242,15 +245,15 @@ pub const Address = extern union {
242 posix.SO.REUSEADDR,245 posix.SO.REUSEADDR,
243 &mem.toBytes(@as(c_int, 1)),246 &mem.toBytes(@as(c_int, 1)),
244 );247 );
245 }248 switch (builtin.os.tag) {
246249 .windows => {},
247 if (options.reuse_port) {250 else => try posix.setsockopt(
248 try posix.setsockopt(251 sockfd,
249 sockfd,252 posix.SOL.SOCKET,
250 posix.SOL.SOCKET,253 posix.SO.REUSEPORT,
251 posix.SO.REUSEPORT,254 &mem.toBytes(@as(c_int, 1)),
252 &mem.toBytes(@as(c_int, 1)),255 ),
253 );256 }
254 }257 }
255258
256 var socklen = address.getOsSockLen();259 var socklen = address.getOsSockLen();