update setsockopt error set according to POSIX

In the code review I accidentally encouraged Luna to remove some
handling of errors that are possible according to POSIX, but I think how
Luna had it before was better, so I fixed it, and now the branch should
be good to merge.
This commit is contained in:
Andrew Kelley 2019-12-30 19:35:05 -05:00
parent 22f6297157
commit 99f6f8ead9
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9

View File

@ -3251,16 +3251,33 @@ pub fn sched_yield() SchedYieldError!void {
}
}
pub const SetSockOptError = error{
/// The socket is already connected, and a specified option cannot be set while the socket is connected.
AlreadyConnected,
/// The option is not supported by the protocol.
InvalidProtocolOption,
/// The send and receive timeout values are too big to fit into the timeout fields in the socket structure.
TimeoutTooBig,
/// Insufficient resources are available in the system to complete the call.
SystemResources,
} || UnexpectedError;
/// Set a socket's options.
pub fn setsockopt(fd: fd_t, level: u32, optname: u32, opt: []const u8) !void {
pub fn setsockopt(fd: fd_t, level: u32, optname: u32, opt: []const u8) SetSockOptError!void {
switch (errno(system.setsockopt(fd, level, optname, opt.ptr, @intCast(socklen_t, opt.len)))) {
0 => {},
EBADF => unreachable,
EBADF => unreachable, // always a race condition
ENOTSOCK => unreachable, // always a race condition
EINVAL => unreachable,
EFAULT => unreachable,
EDOM => return error.TimeoutTooBig,
EISCONN => return error.AlreadyConnected,
ENOPROTOOPT => return error.InvalidProtocolOption,
ENOTSOCK => unreachable,
ENOMEM => return error.SystemResources,
ENOBUFS => return error.SystemResources,
else => |err| return unexpectedErrno(err),
}
}