The system's network sockets (Berkeley sockets on Unix, Winsock on Windows) for TCP, UDP and Unix-domain use.
The module exposes the native calls (socket, bind, listen, connect, send, recv, select, ...) with their
native signatures, and a small set of portable helpers around them: socket_init, set_blocking, set_keepalive,
close_and_reset, the accept and bind overloads, FD_SET and its siblings, and to_string for addresses. For
HTTP, TLS or an event loop use Extensions/Http, Extensions/Tls and Extensions/Io instead.
Calls report failure the way the system does: -1 (or INVALID_SOCKET) and an error code from
get_last_socket_error(). Nothing here allocates except the to_string calls, which use context.allocator.
A socket is owned by whoever created or accepted it and is released with close_and_reset. Sockets start blocking;
set_blocking(s, false) makes recv, send, accept and connect return at once with SOCKET_WOULDBLOCK, and
reads and writes may then transfer fewer bytes than requested. Addresses go through sockaddr_in (IPv4) and
sockaddr_in6 (IPv6); ports in these records are in network order (htons) while the helper bind overloads take host order.
Windows needs socket_init before the first call, and the descriptor-set and control-message helpers differ by
platform (CMSG_* exist on Unix only).
Under WASI only accept, recv, send and shutdown on a socket the host pre-opened work; the calls that would create or set up a
socket return -1 with ENOSYS.
Builds a Winsock control code for ioctlsocket from the group character x, the command number y and the type of the
argument T (its size is encoded in the code). FIONBIO is IOW(102, 126, u32).
Adds fd to set. A socket already in the set is not added twice, and a full set (fd_array.count sockets, 64 by
default) ignores further sockets without an error.
Returns 1 when descriptor fd is in set and 0 when it is not. A null set, a negative descriptor and one past the
set's capacity (usually 1024) count as not in the set. After select returns, use it to ask which descriptors are ready.
Rounds size up to the alignment of control message data: 4 bytes on macOS, 8 elsewhere. A negative size, or one too
large to round, fails an assertion.
The value for a control message's cmsg_len when its payload is size bytes: the aligned header plus size, without
padding after the payload. Fails an assertion for a negative size or one that does not fit in a u32.
// A descriptor-passing message carries one s32.assert(CMSG_LEN(size_of(s32)) == cast(u32) (CMSG_ALIGN(size_of(cmsghdr)) + size_of(s32)));
Doesn't run in the browser (only on linux, macos).
The buffer space one control message with a size-byte payload needs: the aligned header plus the aligned payload. Use
it for msg_controllen and for sizing the buffer. Fails an assertion for a negative size or one that does not fit.
The directions shutdown can close: RD stops receiving, WR stops sending (the peer then reads end of stream) and
RDWR closes both. The SHUT_-prefixed names are the same values.
The generic socket address header: a family and 14 bytes of family-specific data. Cast a sockaddr_in or sockaddr_in6
to *sockaddr for calls that take any address.
The ancillary data IP_PKTINFO reports for a received datagram: the interface it arrived on, the local address it was
sent to and the header's destination address.
A socket handle: Winsock's unsigned pointer-sized SOCKET. Unix uses an s32 file descriptor. Compare a handle against
INVALID_SOCKET, never against a number.
The error get_last_socket_error() reports when a non-blocking connect has started and is still in progress. Winsock
uses WSAEWOULDBLOCK for this, so it equals SOCKET_WOULDBLOCK here, unlike the Unix targets.