Module sys/sockinfo

sys/sockinfo
Stability: unstable — may still change; see below.

Socket address and option queries — the raw syscall boundary.

The four synchronous socket-metadata calls: getsockname, getpeername, getsockopt, setsockopt, and own_stream_socket, the one option std/net sets on every stream it creates. They are here rather than in std/sys/tcp because they do not submit I/O and so need no IoFuture — the kernel answers from the socket's own state. std/net/tcp.yo and std/net/udp.yo are the public surface (local_addr, peer_addr, and the SO_* setters behind TcpListener/UdpSocket).

Stability

unstable — the option API is fully untyped — level, optname, an unsized *u8 and a byte length, with no check that the three agree. Freezing needs a typed option surface; std/net is the stable layer meanwhile. Failures are a negative errno on every platform (the Windows runtime maps Winsock's codes to errno since 2026-10-03).

Functions

getsockname function
fn(sockfd : i32, addr : *u8, addrlen : *u32) -> i32

Read the LOCAL address sockfd is bound to into addr — POSIX getsockname(2). addrlen is in/out: set it to the capacity of addr (sockaddr_storage_size() is the safe choice) and it comes back holding the size actually written. Returns 0 on success, a negative errno on failure.

This is how you learn the port the kernel picked after binding to port 0. Use std/sys/tcp's get_family to decide whether to read the buffer as sockaddr_in or sockaddr_in6. wasm returns -ENOSYS.

Parameters

NameTypeNotes
sockfdi32
addr*u8
addrlen*u32

Returns: i32

getpeername function
fn(sockfd : i32, addr : *u8, addrlen : *u32) -> i32

Read the REMOTE address sockfd is connected to into addr — POSIX getpeername(2). Same in/out addrlen contract as getsockname. Returns 0 on success, a negative errno on failure; an unconnected socket is -ENOTCONN on every platform (the Windows runtime translates the winsock code). wasm returns -ENOSYS.

Parameters

NameTypeNotes
sockfdi32
addr*u8
addrlen*u32

Returns: i32

getsockopt function
fn(sockfd : i32, level : i32, optname : i32, optval : *u8, optlen : *u32) -> i32

Read socket option optname at level into optval — POSIX getsockopt(2). optlen is in/out: set it to the capacity of optval (4 bytes for the many i32-valued options) and read back the size written. Returns 0 on success, a negative errno on failure.

Nothing checks that optval's size matches what the option actually returns, so a too-small buffer is a kernel-side truncation or -EINVAL depending on the option, not a Yo-side error.

Parameters

NameTypeNotes
sockfdi32
leveli32
optnamei32
optval*u8
optlen*u32

Returns: i32

setsockopt function
fn(sockfd : i32, level : i32, optname : i32, optval : *u8, optlen : u32) -> i32

Set socket option optname at level from the optlen bytes at optval — POSIX setsockopt(2). Returns 0 on success, a negative errno on failure. This is the synchronous form; std/sys/tcp and std/sys/udp also expose an IoFuture-returning setsockopt, which is the same syscall wrapped for use inside an async block.

Parameters

NameTypeNotes
sockfdi32
leveli32
optnamei32
optval*u8
optlenu32

Returns: i32

fn(sockfd : i32) -> i32

Make a stream socket safe for std/sys/tcp's and std/sys/unix's stream_write: on macOS, set SO_NOSIGPIPE, so a write to a closed peer returns EPIPE instead of raising SIGPIPE. Nothing else is needed: the runtime makes every socket it creates or accepts non-blocking, Linux's stream_write passes MSG_NOSIGNAL, and Windows has no SIGPIPE.

Returns 0, or the negative errno of the failed setsockopt. A caller must not write such a socket with stream_write; std/net fails the connect or accept instead, as libuv's uv__stream_open does.

Parameters

NameTypeNotes
sockfdi32

Returns: i32