aboutsummaryrefslogtreecommitdiff
path: root/lib/std/net.zig
diff options
context:
space:
mode:
authorLuna <git@l4.pm>2019-11-28 22:19:42 -0300
committerAndrew Kelley <andrew@ziglang.org>2019-12-30 19:26:29 -0500
commit631eb6783d1dbc64f031426436ab685008c31ca8 (patch)
treead25a1ccb42800f1c814399f1e811bab027a4c90 /lib/std/net.zig
parent73e535e1125d76bcd4e85123defea8b76412ab09 (diff)
downloadzig-631eb6783d1dbc64f031426436ab685008c31ca8.tar.gz
zig-631eb6783d1dbc64f031426436ab685008c31ca8.zip
add StreamServer.Options.reuse_address
this uses a bad direct interface with std.os.linux, this should add setsockopt to std.os.
Diffstat (limited to 'lib/std/net.zig')
-rw-r--r--lib/std/net.zig16
1 files changed, 16 insertions, 0 deletions
diff --git a/lib/std/net.zig b/lib/std/net.zig
index 53b75d8578..cd456c46c6 100644
--- a/lib/std/net.zig
+++ b/lib/std/net.zig
@@ -1290,6 +1290,9 @@ pub const StreamServer = struct {
/// If more than this many connections pool in the kernel, clients will start
/// seeing "Connection refused".
kernel_backlog: u32 = 128,
+
+ /// Enable SO_REUSEADDR on the socket.
+ reuse_address: bool = false,
};
/// After this call succeeds, resources have been acquired and must
@@ -1320,6 +1323,19 @@ pub const StreamServer = struct {
self.sockfd = null;
}
+ // TODO proper interface with errors in std.os
+ var optval: c_int = 1;
+
+ if (self.options.reuse_address) {
+ _ = os.linux.setsockopt(
+ server.sockfd.?,
+ os.linux.SOL_SOCKET,
+ os.linux.SO_REUSEADDR,
+ @ptrCast([*]const u8, &optval),
+ @sizeOf(c_int),
+ );
+ }
+
var socklen = address.getOsSockLen();
try os.bind(sockfd, &address.any, socklen);
try os.listen(sockfd, self.kernel_backlog);