diff options
| author | Luna <git@l4.pm> | 2019-11-29 17:17:09 -0300 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2019-12-30 19:26:29 -0500 |
| commit | 4a4d2c0d80443a00945beeff4e3acaa9e7ea59cb (patch) | |
| tree | bcb4f1f7202f51f03d6f40a1df295e952533f71b /lib | |
| parent | 631eb6783d1dbc64f031426436ab685008c31ca8 (diff) | |
| download | zig-4a4d2c0d80443a00945beeff4e3acaa9e7ea59cb.tar.gz zig-4a4d2c0d80443a00945beeff4e3acaa9e7ea59cb.zip | |
os: add setsockopt
- net: use os.setsockopt()
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/std/net.zig | 12 | ||||
| -rw-r--r-- | lib/std/os.zig | 20 |
2 files changed, 25 insertions, 7 deletions
diff --git a/lib/std/net.zig b/lib/std/net.zig index cd456c46c6..effe8e74cd 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -1323,14 +1323,12 @@ 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, + var optval: c_int = 1; + try os.setsockopt( + self.sockfd.?, + os.SOL_SOCKET, + os.SO_REUSEADDR, @ptrCast([*]const u8, &optval), @sizeOf(c_int), ); diff --git a/lib/std/os.zig b/lib/std/os.zig index 3d1b3540b6..27327c1016 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -3250,3 +3250,23 @@ pub fn sched_yield() SchedYieldError!void { else => return error.SystemCannotYield, } } + +/// Set a socket's options. +pub fn setsockopt(fd: fd_t, level: u32, optname: u32, optval: [*]const u8, optlen: socklen_t) SetSockOptError!void { + const rc = system.setsockopt(); + + switch (errno(system.setsockopt(fd, level, optname, optval, optlen))) { + 0 => {}, + EBADF => unreachable, + EINVAL => unreachable, + EDOM => return error.TimeoutTooBig, + EISCONN => return error.AlreadyConnected, + ENOPROTOOOPT => return error.InvalidProtocolOption, + ENOTSOCK => return error.NotSocket, + + ENOMEM => return error.OutOfMemory, + ENOBUFS => return error.SystemResources, + + else => |err| return std.os.unexpectedErrno(err), + } +} |
