Skip to content

std.net.socket: Listening Sockets

std.net.socket is the low-level listening socket layer beneath higher network APIs. It follows the tri-signature pattern:

ProfileEntry pointContract
:corelisten(address, allocator)Parse and bind a listening socket.
:servicelisten_with_context(address, ctx, allocator)Honor cancellation and expired deadlines before and during creation.
:sovereignlisten_with_capability(address, cap, allocator)Require NetBind authority and emit bind audit records.

The parser accepts these bind forms:

FormMeaning
:8080IPv4 wildcard bind on port 8080.
127.0.0.1:9000IPv4 host and port.
[::1]:443Bracketed IPv6 host and port.
unix:/tmp/janus.sockUnix socket path.
/tmp/janus.sockBare Unix socket path.

Malformed addresses return SocketError.InvalidAddress; malformed ports return SocketError.InvalidPort.

The context-aware path treats a cancelled context or an expired deadline as SocketError.Timeout. Creation checks the context before socket creation and again between the socket, option, sockaddr, bind, and listen steps.

That means service code can rely on the advertised signature:

let socket = net.listen_with_context(":8080", ctx, allocator) catch |err| do
match err {
SocketError.Timeout => return err,
_ => return err,
}
end

The capability path checks NetBind before any socket syscall is issued. Default NetBind values grant net.bind and allow ports 8080, 3000, and 8000. Add explicit ports with allow_port when a service owns another bind surface.

Denied binds return SocketError.CapabilityDenied.

NetBind can carry an audit sink. The socket layer emits:

OperationResult
check_bind_addressallowed or denied before socket creation.
bind_socketallowed after the OS bind succeeds.

The record contains the capability id, operation, optional port, and result. Unix socket paths have no port, so their audit record uses null for the port.

The closure target for this surface is:

Terminal window
cd janus
./scripts/zb test-net-socket

That target covers address parsing, owned sockaddr layout, a port-zero listen/close smoke, cancelled and expired contexts, capability-denied binds, and bind audit records.