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 {
217217 /// If more than this many connections pool in the kernel, clients will start
218218 /// seeing "Connection refused".
219219 kernel_backlog: u31 = 128,
220 /// Sets SO_REUSEADDR and SO_REUSEPORT on POSIX.
221 /// Sets SO_REUSEADDR on Windows, which is roughly equivalent.
220222 reuse_address: bool = false,
223 /// Deprecated. Does nothing.
221224 reuse_port: bool = false,
222225 force_nonblocking: bool = false,
223226 };
......@@ -242,15 +245,15 @@ pub const Address = extern union {
242245 posix.SO.REUSEADDR,
243246 &mem.toBytes(@as(c_int, 1)),
244247 );
245 }
246
247 if (options.reuse_port) {
248 try posix.setsockopt(
249 sockfd,
250 posix.SOL.SOCKET,
251 posix.SO.REUSEPORT,
252 &mem.toBytes(@as(c_int, 1)),
253 );
248 switch (builtin.os.tag) {
249 .windows => {},
250 else => try posix.setsockopt(
251 sockfd,
252 posix.SOL.SOCKET,
253 posix.SO.REUSEPORT,
254 &mem.toBytes(@as(c_int, 1)),
255 ),
256 }
254257 }
255258
256259 var socklen = address.getOsSockLen();