Skip to content

Commit 53598e3

Browse files
authored
std.posix: adding getsockopt (#22335)
1 parent c0d85cd commit 53598e3

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

lib/std/posix.zig

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4276,6 +4276,35 @@ pub fn connect(sock: socket_t, sock_addr: *const sockaddr, len: socklen_t) Conne
42764276
}
42774277
}
42784278

4279+
pub const GetSockOptError = error{
4280+
/// The calling process does not have the appropriate privileges.
4281+
AccessDenied,
4282+
4283+
/// The option is not supported by the protocol.
4284+
InvalidProtocolOption,
4285+
4286+
/// Insufficient resources are available in the system to complete the call.
4287+
SystemResources,
4288+
} || UnexpectedError;
4289+
4290+
pub fn getsockopt(fd: socket_t, level: i32, optname: u32, opt: []u8) GetSockOptError!void {
4291+
var len: socklen_t = undefined;
4292+
switch (errno(system.getsockopt(fd, level, optname, opt.ptr, &len))) {
4293+
.SUCCESS => {
4294+
std.debug.assert(len == opt.len);
4295+
},
4296+
.BADF => unreachable,
4297+
.NOTSOCK => unreachable,
4298+
.INVAL => unreachable,
4299+
.FAULT => unreachable,
4300+
.NOPROTOOPT => return error.InvalidProtocolOption,
4301+
.NOMEM => return error.SystemResources,
4302+
.NOBUFS => return error.SystemResources,
4303+
.ACCES => return error.AccessDenied,
4304+
else => |err| return unexpectedErrno(err),
4305+
}
4306+
}
4307+
42794308
pub fn getsockoptError(sockfd: fd_t) ConnectError!void {
42804309
var err_code: i32 = undefined;
42814310
var size: u32 = @sizeOf(u32);

0 commit comments

Comments
 (0)