Access control for wasi:sockets.
A gate consults a latch before performing each socket operation. The latch either defers, raising no objection, or denies the operation. A latch never grants an operation, an operation proceeds when the latch defers. Several latches can be aggregated into one, the operation is denied when any of them denies it.
Deciding and acting on a decision are separate steps. authorize decides without side
effects, then the gate reports the final decision to observe-decision, where a latch updates
any state it keeps. A latch aggregated with others may not be asked to authorize an operation
another latch already denied, but it always observes the final decision.
Types
use variant ip-addresssince 0.1.0
ipv4:ipv4-addressipv6:ipv6-address
use enum ip-address-familysince 0.1.0
ipv4Similar to
AF_INETin POSIX.ipv6Similar to
AF_INET6in POSIX.
use variant ip-socket-addresssince 0.1.0
ipv4:ipv4-socket-addressipv6:ipv6-socket-address
use resource tcp-socketsince 0.1.0
A TCP socket resource.
The socket can be in one of the following states:
unboundbound(See note below)listeningconnectingconnectedclosedSee https://github.com/WebAssembly/WASI/blob/main/proposals/sockets/TcpSocketOperationalSemantics-0.3.0.md for more information.
Note: Except where explicitly mentioned, whenever this documentation uses
the term "bound" without backticks it actually means: in the bound state or higher.
(i.e. bound, listening, connecting or connected)
WASI uses shared ownership semantics: the tcp-socket handle and all
derived stream and future values reference a single underlying OS
socket:
- Send/receive streams remain functional after the original
tcp-sockethandle is dropped. - The stream returned by
listenbehaves similarly. - Client sockets returned by
tcp-socket::listenare independent and do not keep the listening socket alive.
The OS socket is closed only after the last handle is dropped. This model has observable effects; for example, it affects when the local port binding is released.
In addition to the general error codes documented on the
types::error-code type, TCP socket methods may always return
error(invalid-state) when in the closed state.
create: static func( address-family: ip-address-family, ) -> result<tcp-socket, error-code>since 0.3.0Create a new TCP socket.
Similar to
socket(AF_INET or AF_INET6, SOCK_STREAM, IPPROTO_TCP)in POSIX. On IPv6 sockets, IPV6_V6ONLY is enabled by default and can't be configured otherwise.Unlike POSIX, WASI sockets have no notion of a socket-level
O_NONBLOCKflag. Instead they fully rely on the Component Model's async support.Typical errors
not-supported: Theaddress-familyis not supported. (EAFNOSUPPORT)
References
bind: func(local-address: ip-socket-address) -> result<_, error-code>since 0.3.0Bind the socket to the provided IP address and port.
If the IP address is zero (
0.0.0.0in IPv4,::in IPv6), it is left to the implementation to decide which network interface(s) to bind to. If the TCP/UDP port is zero, the socket will be bound to a random free port.Bind can be attempted multiple times on the same socket, even with different arguments on each iteration. But never concurrently and only as long as the previous bind failed. Once a bind succeeds, the binding can't be changed anymore.
Typical errors
invalid-argument: Thelocal-addresshas the wrong address family. (EAFNOSUPPORT, EFAULT on Windows)invalid-argument:local-addressis not a unicast address. (EINVAL)invalid-argument:local-addressis an IPv4-mapped IPv6 address. (EINVAL)invalid-state: The socket is already bound. (EINVAL)address-in-use: No ephemeral ports available. (EADDRINUSE, ENOBUFS on Windows)address-in-use: Address is already in use. (EADDRINUSE)address-not-bindable:local-addressis not an address that can be bound to. (EADDRNOTAVAIL)
Implementors note
The bind operation shouldn't be affected by the TIME_WAIT state of a recently closed socket on the same local address. In practice this means that the SO_REUSEADDR socket option should be set implicitly on all platforms, except on Windows where this is the default behavior and SO_REUSEADDR performs something different.
References
connect: async func( remote-address: ip-socket-address, ) -> result<_, error-code>since 0.3.0Connect to a remote endpoint.
On success, the socket is transitioned into the
connectedstate and theremote-addressof the socket is updated. Thelocal-addressmay be updated as well, based on the best network path toremote-address. If the socket was not already explicitly bound, this function will implicitly bind the socket to a random free port.After a failed connection attempt, the socket will be in the
closedstate and the only valid action left is todropthe socket. A single socket can not be used to connect more than once.Typical errors
invalid-argument: Theremote-addresshas the wrong address family. (EAFNOSUPPORT)invalid-argument:remote-addressis not a unicast address. (EINVAL, ENETUNREACH on Linux, EAFNOSUPPORT on MacOS)invalid-argument:remote-addressis an IPv4-mapped IPv6 address. (EINVAL, EADDRNOTAVAIL on Illumos)invalid-argument: The IP address inremote-addressis set to INADDR_ANY (0.0.0.0/::). (EADDRNOTAVAIL on Windows)invalid-argument: The port inremote-addressis set to 0. (EADDRNOTAVAIL on Windows)invalid-state: The socket is already in theconnectingstate. (EALREADY)invalid-state: The socket is already in theconnectedstate. (EISCONN)invalid-state: The socket is already in thelisteningstate. (EOPNOTSUPP, EINVAL on Windows)timeout: Connection timed out. (ETIMEDOUT)connection-refused: The connection was forcefully rejected. (ECONNREFUSED)connection-reset: The connection was reset. (ECONNRESET)connection-aborted: The connection was aborted. (ECONNABORTED)remote-unreachable: The remote address is not reachable. (EHOSTUNREACH, EHOSTDOWN, ENETUNREACH, ENETDOWN, ENONET)address-in-use: Tried to perform an implicit bind, but there were no ephemeral ports available. (EADDRINUSE, EADDRNOTAVAIL on Linux, EAGAIN on BSD)
References
listen: func() -> result<stream<tcp-socket>, error-code>since 0.3.0Start listening and return a stream of new inbound connections.
Transitions the socket into the
listeningstate. This can be called at most once per socket.If the socket is not already explicitly bound, this function will implicitly bind the socket to a random free port.
Normally, the returned sockets are bound, in the
connectedstate and immediately ready for I/O. Though, depending on exact timing and circumstances, a newly accepted connection may already beclosedby the time the server attempts to perform its first I/O on it. This is true regardless of whether the WASI implementation uses "synthesized" sockets or not (see Implementors Notes below).The following properties are inherited from the listener socket:
address-familykeep-alive-enabledkeep-alive-idle-timekeep-alive-intervalkeep-alive-counthop-limitreceive-buffer-sizesend-buffer-size
Typical errors
invalid-state: The socket is already in theconnectedstate. (EISCONN, EINVAL on BSD)invalid-state: The socket is already in thelisteningstate.address-in-use: Tried to perform an implicit bind, but there were no ephemeral ports available. (EADDRINUSE)
Implementors note
This method returns a single perpetual stream that should only close on fatal errors (if any). Yet, the POSIX'
acceptfunction may also return transient errors (e.g. ECONNABORTED). The exact details differ per operation system. For example, the Linux manual mentions:Linux accept() passes already-pending network errors on the new socket as an error code from accept(). This behavior differs from other BSD socket implementations. For reliable operation the application should detect the network errors defined for the protocol after accept() and treat them like EAGAIN by retrying. In the case of TCP/IP, these are ENETDOWN, EPROTO, ENOPROTOOPT, EHOSTDOWN, ENONET, EHOSTUNREACH, EOPNOTSUPP, and ENETUNREACH. Source: https://man7.org/linux/man-pages/man2/accept.2.html
WASI implementations have two options to handle this:
- Optionally log it and then skip over non-fatal errors returned by
accept. Guest code never gets to see these failures. Or: - Synthesize a
tcp-socketresource that exposes the error when attempting to send or receive on it. Guest code then sees these failures as regular I/O errors.
In either case, the stream returned by this
listenmethod remains operational.WASI requires
listento perform an implicit bind if the socket has not already been bound. Not all platforms (notably Windows) exhibit this behavior out of the box. On platforms that require it, the WASI implementation can emulate this behavior by performing the bind itself if the guest hasn't already done so.References
- https://pubs.opengroup.org/onlinepubs/9699919799/functions/listen.html
- https://pubs.opengroup.org/onlinepubs/9699919799/functions/accept.html
- https://man7.org/linux/man-pages/man2/listen.2.html
- https://man7.org/linux/man-pages/man2/accept.2.html
- https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-listen
- https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-accept
- https://man.freebsd.org/cgi/man.cgi?query=listen&sektion=2
- https://man.freebsd.org/cgi/man.cgi?query=accept&sektion=2
send: func(data: stream<u8>) -> future<result<_, error-code>>since 0.3.0Transmit data to peer.
The caller should close the stream when it has no more data to send to the peer. Under normal circumstances this will cause a FIN packet to be sent out. Closing the stream is equivalent to calling
shutdown(SHUT_WR)in POSIX.This function may be called at most once and returns once the full contents of the stream are transmitted or an error is encountered.
Typical errors
invalid-state: The socket is not in theconnectedstate. (ENOTCONN)invalid-state:sendhas already been called on this socket.connection-broken: The connection is not writable anymore. (EPIPE, ECONNABORTED on Windows)connection-reset: The connection was reset. (ECONNRESET)remote-unreachable: The remote address is not reachable. (EHOSTUNREACH, EHOSTDOWN, ENETUNREACH, ENETDOWN, ENONET)
References
receive: func() -> tuple<stream<u8>, future<result<_, error-code>>>since 0.3.0Read data from peer.
Returns a
streamof data sent by the peer. The implementation drops the stream once no more data is available. At that point, the returnedfutureresolves to:okafter a graceful shutdown from the peer (i.e. a FIN packet), orerrif the socket was closed abnormally.
receivemay be called only once per socket. Subsequent calls return a closed stream and a future resolved toerr(invalid-state).If the caller is not expecting to receive any more data from the peer, they should drop the stream. Any data still in the receive queue will be discarded. This is equivalent to calling
shutdown(SHUT_RD)in POSIX.Typical errors
invalid-state: The socket is not in theconnectedstate. (ENOTCONN)invalid-state:receivehas already been called on this socket.connection-reset: The connection was reset. (ECONNRESET)remote-unreachable: The remote address is not reachable. (EHOSTUNREACH, EHOSTDOWN, ENETUNREACH, ENETDOWN, ENONET)
References
get-local-address: func() -> result<ip-socket-address, error-code>since 0.3.0Get the bound local address.
POSIX mentions:
If the socket has not been bound to a local name, the value stored in the object pointed to by
addressis unspecified.WASI is stricter and requires
get-local-addressto returninvalid-statewhen the socket hasn't been bound yet.Typical errors
invalid-state: The socket is not bound to any local address.
References
get-remote-address: func() -> result<ip-socket-address, error-code>since 0.3.0Get the remote address.
Typical errors
invalid-state: The socket is not connected to a remote address. (ENOTCONN)
References
get-is-listening: func() -> boolsince 0.3.0Whether the socket is in the
listeningstate.Equivalent to the SO_ACCEPTCONN socket option.
get-address-family: func() -> ip-address-familysince 0.3.0Whether this is a IPv4 or IPv6 socket.
This is the value passed to the constructor.
Equivalent to the SO_DOMAIN socket option.
set-listen-backlog-size: func(value: u64) -> result<_, error-code>since 0.3.0Hints the desired listen queue size. Implementations are free to ignore this.
If the provided value is 0, an
invalid-argumenterror is returned. Any other value will never cause an error, but it might be silently clamped and/or rounded.Typical errors
not-supported: (set) The platform does not support changing the backlog size after the initial listen.invalid-argument: (set) The provided value was 0.invalid-state: (set) The socket is in theconnectingorconnectedstate.
get-keep-alive-enabled: func() -> result<bool, error-code>since 0.3.0Enables or disables keepalive.
The keepalive behavior can be adjusted using:
keep-alive-idle-timekeep-alive-intervalkeep-alive-countThese properties can be configured whilekeep-alive-enabledis false, but only come into effect whenkeep-alive-enabledis true.
Equivalent to the SO_KEEPALIVE socket option.
set-keep-alive-enabled: func(value: bool) -> result<_, error-code>since 0.3.0get-keep-alive-idle-time: func() -> result<duration, error-code>since 0.3.0Amount of time the connection has to be idle before TCP starts sending keepalive packets.
If the provided value is 0, an
invalid-argumenterror is returned. All other values are accepted without error, but may be clamped or rounded. As a result, the value read back from this setting may differ from the value that was set.Equivalent to the TCP_KEEPIDLE socket option. (TCP_KEEPALIVE on MacOS)
Typical errors
invalid-argument: (set) The provided value was 0.
set-keep-alive-idle-time: func(value: duration) -> result<_, error-code>since 0.3.0get-keep-alive-interval: func() -> result<duration, error-code>since 0.3.0The time between keepalive packets.
If the provided value is 0, an
invalid-argumenterror is returned. All other values are accepted without error, but may be clamped or rounded. As a result, the value read back from this setting may differ from the value that was set.Equivalent to the TCP_KEEPINTVL socket option.
Typical errors
invalid-argument: (set) The provided value was 0.
set-keep-alive-interval: func(value: duration) -> result<_, error-code>since 0.3.0get-keep-alive-count: func() -> result<u32, error-code>since 0.3.0The maximum amount of keepalive packets TCP should send before aborting the connection.
If the provided value is 0, an
invalid-argumenterror is returned. All other values are accepted without error, but may be clamped or rounded. As a result, the value read back from this setting may differ from the value that was set.Equivalent to the TCP_KEEPCNT socket option.
Typical errors
invalid-argument: (set) The provided value was 0.
set-keep-alive-count: func(value: u32) -> result<_, error-code>since 0.3.0get-hop-limit: func() -> result<u8, error-code>since 0.3.0Equivalent to the IP_TTL & IPV6_UNICAST_HOPS socket options.
If the provided value is 0, an
invalid-argumenterror is returned.Typical errors
invalid-argument: (set) The TTL value must be 1 or higher.
set-hop-limit: func(value: u8) -> result<_, error-code>since 0.3.0get-receive-buffer-size: func() -> result<u64, error-code>since 0.3.0Kernel buffer space reserved for sending/receiving on this socket. Implementations usually treat this as a cap the buffer can grow to, rather than allocating the full amount immediately.
If the provided value is 0, an
invalid-argumenterror is returned. All other values are accepted without error, but may be clamped or rounded. As a result, the value read back from this setting may differ from the value that was set.This is only a performance hint. The implementation may ignore it or tweak it based on real traffic patterns. Linux and macOS appear to behave differently depending on whether a buffer size was explicitly set. When set, they tend to honor it; when not set, they dynamically adjust the buffer size as the connection progresses. This is especially noticeable when comparing the values from before and after connection establishment.
Equivalent to the SO_RCVBUF and SO_SNDBUF socket options.
Typical errors
invalid-argument: (set) The provided value was 0.
set-receive-buffer-size: func(value: u64) -> result<_, error-code>since 0.3.0get-send-buffer-size: func() -> result<u64, error-code>since 0.3.0set-send-buffer-size: func(value: u64) -> result<_, error-code>since 0.3.0
use resource udp-socketsince 0.1.0
A UDP socket handle.
create: static func( address-family: ip-address-family, ) -> result<udp-socket, error-code>since 0.3.0Create a new UDP socket.
Similar to
socket(AF_INET or AF_INET6, SOCK_DGRAM, IPPROTO_UDP)in POSIX. On IPv6 sockets, IPV6_V6ONLY is enabled by default and can't be configured otherwise.Unlike POSIX, WASI sockets have no notion of a socket-level
O_NONBLOCKflag. Instead they fully rely on the Component Model's async support.References:
bind: func(local-address: ip-socket-address) -> result<_, error-code>since 0.3.0Bind the socket to the provided IP address and port.
If the IP address is zero (
0.0.0.0in IPv4,::in IPv6), it is left to the implementation to decide which network interface(s) to bind to. If the port is zero, the socket will be bound to a random free port.Typical errors
invalid-argument: Thelocal-addresshas the wrong address family. (EAFNOSUPPORT, EFAULT on Windows)invalid-state: The socket is already bound. (EINVAL)address-in-use: No ephemeral ports available. (EADDRINUSE, ENOBUFS on Windows)address-in-use: Address is already in use. (EADDRINUSE)address-not-bindable:local-addressis not an address that can be bound to. (EADDRNOTAVAIL)
References
connect: func( remote-address: ip-socket-address, ) -> result<_, error-code>since 0.3.0Associate this socket with a specific peer address.
On success, the
remote-addressof the socket is updated. Thelocal-addressmay be updated as well, based on the best network path toremote-address. If the socket was not already explicitly bound, this function will implicitly bind the socket to a random free port.When a UDP socket is "connected", the
sendandreceivemethods are limited to communicating with that peer only:sendcan only be used to send to this destination.receivewill only return datagrams sent from the providedremote-address.
The name "connect" was kept to align with the existing POSIX terminology. Other than that, this function only changes the local socket configuration and does not generate any network traffic. The peer is not aware of this "connection".
This method may be called multiple times on the same socket to change its association, but only the most recent one will be effective.
Typical errors
invalid-argument: Theremote-addresshas the wrong address family. (EAFNOSUPPORT)invalid-argument: The IP address inremote-addressis set to INADDR_ANY (0.0.0.0/::). (EDESTADDRREQ, EADDRNOTAVAIL)invalid-argument: The port inremote-addressis set to 0. (EDESTADDRREQ, EADDRNOTAVAIL)address-in-use: Tried to perform an implicit bind, but there were no ephemeral ports available. (EADDRINUSE, EADDRNOTAVAIL on Linux, EAGAIN on BSD)
Implementors note
If the socket is already connected, some platforms (e.g. Linux) require a disconnect before connecting to a different peer address.
References
disconnect: func() -> result<_, error-code>since 0.3.0Dissociate this socket from its peer address.
After calling this method,
send&receiveare free to communicate with any remote address again.The POSIX equivalent of this is calling
connectwith anAF_UNSPECaddress.Typical errors
invalid-state: The socket is not connected.
References
send: async func( data: list<u8>, remote-address: option<ip-socket-address>, ) -> result<_, error-code>since 0.3.0Send a message on the socket to a particular peer.
If the socket is connected, the peer address may be left empty. In that case this is equivalent to
sendin POSIX. Otherwise it is equivalent tosendto.Additionally, if the socket is connected, a
remote-addressargument may be provided but then it must be identical to the address passed toconnect.If the socket has not been explicitly bound, it will be implicitly bound to a random free port.
Implementations may trap if the
datalength exceeds 64 KiB.Typical errors
invalid-argument: Theremote-addresshas the wrong address family. (EAFNOSUPPORT)invalid-argument: The IP address inremote-addressis set to INADDR_ANY (0.0.0.0/::). (EDESTADDRREQ, EADDRNOTAVAIL)invalid-argument: The port inremote-addressis set to 0. (EDESTADDRREQ, EADDRNOTAVAIL)invalid-argument: The socket is in "connected" mode andremote-addressissomevalue that does not match the address passed toconnect. (EISCONN)invalid-argument: The socket is not "connected" and no value forremote-addresswas provided. (EDESTADDRREQ)remote-unreachable: The remote address is not reachable. (ECONNRESET, ENETRESET on Windows, EHOSTUNREACH, EHOSTDOWN, ENETUNREACH, ENETDOWN, ENONET)connection-refused: The connection was refused. (ECONNREFUSED)datagram-too-large: The datagram is too large. (EMSGSIZE)address-in-use: Tried to perform an implicit bind, but there were no ephemeral ports available. (EADDRINUSE)
Implementors note
WASI requires
sendto perform an implicit bind if the socket has not been bound. Not all platforms (notably Windows) exhibit this behavior natively. On such platforms, the WASI implementation should emulate it by performing the bind if the guest has not already done so.References
- https://pubs.opengroup.org/onlinepubs/9699919799/functions/sendto.html
- https://pubs.opengroup.org/onlinepubs/9699919799/functions/sendmsg.html
- https://man7.org/linux/man-pages/man2/send.2.html
- https://man7.org/linux/man-pages/man2/sendmmsg.2.html
- https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-send
- https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-sendto
- https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsasendmsg
- https://man.freebsd.org/cgi/man.cgi?query=send&sektion=2
receive: async func() -> result<tuple<list<u8>, ip-socket-address>, error-code>since 0.3.0Receive a message on the socket.
On success, the return value contains a tuple of the received data and the address of the sender. Theoretical maximum length of the data is 64 KiB. Though in practice, it will typically be less than 1500 bytes.
If the socket is connected, the sender address is guaranteed to match the remote address passed to
connect.Typical errors
invalid-state: The socket has not been bound yet.remote-unreachable: The remote address is not reachable. (ECONNRESET, ENETRESET on Windows, EHOSTUNREACH, EHOSTDOWN, ENETUNREACH, ENETDOWN, ENONET)connection-refused: The connection was refused. (ECONNREFUSED)
References
- https://pubs.opengroup.org/onlinepubs/9699919799/functions/recvfrom.html
- https://pubs.opengroup.org/onlinepubs/9699919799/functions/recvmsg.html
- https://man7.org/linux/man-pages/man2/recv.2.html
- https://man7.org/linux/man-pages/man2/recvmmsg.2.html
- https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-recvfrom
- https://learn.microsoft.com/en-us/windows/win32/api/mswsock/nc-mswsock-lpfn_wsarecvmsg
- https://man.freebsd.org/cgi/man.cgi?query=recv&sektion=2
get-local-address: func() -> result<ip-socket-address, error-code>since 0.3.0Get the current bound address.
POSIX mentions:
If the socket has not been bound to a local name, the value stored in the object pointed to by
addressis unspecified.WASI is stricter and requires
get-local-addressto returninvalid-statewhen the socket hasn't been bound yet.Typical errors
invalid-state: The socket is not bound to any local address.
References
get-remote-address: func() -> result<ip-socket-address, error-code>since 0.3.0Get the address the socket is currently "connected" to.
Typical errors
invalid-state: The socket is not "connected" to a specific remote address. (ENOTCONN)
References
get-address-family: func() -> ip-address-familysince 0.3.0Whether this is a IPv4 or IPv6 socket.
This is the value passed to the constructor.
Equivalent to the SO_DOMAIN socket option.
get-unicast-hop-limit: func() -> result<u8, error-code>since 0.3.0Equivalent to the IP_TTL & IPV6_UNICAST_HOPS socket options.
If the provided value is 0, an
invalid-argumenterror is returned.Typical errors
invalid-argument: (set) The TTL value must be 1 or higher.
set-unicast-hop-limit: func(value: u8) -> result<_, error-code>since 0.3.0get-receive-buffer-size: func() -> result<u64, error-code>since 0.3.0Kernel buffer space reserved for sending/receiving on this socket. Implementations usually treat this as a cap the buffer can grow to, rather than allocating the full amount immediately.
If the provided value is 0, an
invalid-argumenterror is returned. All other values are accepted without error, but may be clamped or rounded. As a result, the value read back from this setting may differ from the value that was set.Equivalent to the SO_RCVBUF and SO_SNDBUF socket options.
Typical errors
invalid-argument: (set) The provided value was 0.
set-receive-buffer-size: func(value: u64) -> result<_, error-code>since 0.3.0get-send-buffer-size: func() -> result<u64, error-code>since 0.3.0set-send-buffer-size: func(value: u64) -> result<_, error-code>since 0.3.0
record resolve-addresses-argssince 0.1.0
Arguments to wasi:sockets/ip-name-lookup#resolve-addresses.
name:stringThe name to look up, as passed by the caller. DNS search paths are not applied.
record resolve-addresses-returns-itemsince 0.1.0
Items returned from calls to wasi:sockets/ip-name-lookup#resolve-addresses.
ip-address:ip-addressAn address the lookup returned.
variant ip-name-lookup-operationsince 0.1.0
Operations of the wasi:sockets/ip-name-lookup interface.
resolve-addresses:resolve-addresses-argsAuthorized before the name is looked up. A denial fails the lookup.
resolve-addresses-return:resolve-addresses-returns-itemAuthorized for each address the lookup returned. A denied address is removed from the result, the lookup succeeds with the remaining addresses.
record tcp-socket-create-argssince 0.1.0
Arguments to wasi:sockets/types#tcp-socket.create.
address-family:ip-address-family
record tcp-socket-bind-argssince 0.1.0
Arguments to wasi:sockets/types#tcp-socket.bind.
local-address:ip-socket-address
record tcp-socket-connect-argssince 0.1.0
Arguments to wasi:sockets/types#tcp-socket.connect.
remote-address:ip-socket-address
variant tcp-socket-operationsince 0.1.0
Operations of the wasi:sockets/types#tcp-socket interface.
The borrowed socket is the socket the operation is performed on, a latch may query it, for example for its local or remote address.
create:tcp-socket-create-argsAuthorized before the socket is created. A denial fails the create.
bind:tuple<borrow<tcp-socket>, tcp-socket-bind-args>Authorized before the socket is bound. A denial fails the bind.
connect:tuple<borrow<tcp-socket>, tcp-socket-connect-args>Authorized before the socket connects. A denial fails the connect, nothing is sent to the remote address.
listen:tuple<borrow<tcp-socket>>Authorized before the socket starts listening. A denial fails the listen.
listen-connection:tuple<borrow<tcp-socket>>Authorized for each connection a listening socket accepts, the borrowed socket is the accepted connection. A denied connection is closed before it reaches the caller, the listening socket keeps accepting other connections.
send:tuple<borrow<tcp-socket>>Authorized once per socket before data is sent,
sendcan only be called once and streams all of its data. A denial fails the send.receive:tuple<borrow<tcp-socket>>Authorized once per socket before data is received,
receivecan only be called once and streams all of its data. A denial fails the receive.
record udp-socket-create-argssince 0.1.0
Arguments to wasi:sockets/types#udp-socket.create.
address-family:ip-address-family
record udp-socket-bind-argssince 0.1.0
Arguments to wasi:sockets/types#udp-socket.bind.
local-address:ip-socket-address
record udp-socket-connect-argssince 0.1.0
Arguments to wasi:sockets/types#udp-socket.connect.
remote-address:ip-socket-address
record udp-socket-send-argssince 0.1.0
Arguments to wasi:sockets/types#udp-socket.send.
data-length:u64The length of the datagram in bytes.
remote-address:option<ip-socket-address>Where the datagram is sent.
nonefor a connected socket sending to the remote address it is connected to.
record udp-socket-receive-returnssince 0.1.0
Returned value from calls to wasi:sockets/types#udp-socket.receive.
data-length:u64The length of the datagram in bytes.
remote-address:ip-socket-addressWhere the datagram was sent from. UDP source addresses are not authenticated.
variant udp-socket-operationsince 0.1.0
Operations of wasi:sockets/types#udp-socket.
The borrowed socket is the socket the operation is performed on, a latch may query it, for example for its local or remote address.
create:udp-socket-create-argsAuthorized before the socket is created. A denial fails the create.
bind:tuple<borrow<udp-socket>, udp-socket-bind-args>Authorized before the socket is bound. A denial fails the bind.
connect:tuple<borrow<udp-socket>, udp-socket-connect-args>Authorized before the socket connects. A denial fails the connect.
send:tuple<borrow<udp-socket>, udp-socket-send-args>Authorized for each datagram before it is sent. A denial fails the send, nothing is sent.
receive:tuple<borrow<udp-socket>, udp-socket-receive-returns>Authorized for each datagram after it is received, before it reaches the caller. A denied datagram is dropped and the receive continues with the next datagram, so a sender cannot fail the caller's receive.
variant operationsince 0.1.0
Operations for the wasi:sockets package.
ip-name-lookup:ip-name-lookup-operationtcp-socket:tcp-socket-operationudp-socket:udp-socket-operation
type latch-namesince 0.1.0
The name of a latch component, identifying which latch reported an error.
string
variant error-codesince 0.1.0
A latch was unable to decide or observe. The gate fails the operation, except for an accepted connection, which is closed as if it were denied.
invalid-config:latch-nameThe latch's configuration is invalid, the cause is logged by the latch.
observation-failed:latch-nameThe latch was unable to act on the final decision.
other:option<string>Any other error.
variant sockets-error-codesince 0.1.0
The reason an operation is denied, returned to the caller as the wasi:sockets error code.
access-deniedAccess denied.
POSIX equivalent: EACCES, EPERM
invalid-argumentOne of the arguments is invalid.
POSIX equivalent: EINVAL, EDESTADDRREQ, EAFNOSUPPORT
other:option<string>A catch-all for errors not captured by the existing variants. Implementations can use this to extend the error type without breaking existing code.
variant decisionsince 0.1.0
Whether the operation is allowed to proceed.
deferredNo objection to the operation.
denied:sockets-error-codeThe operation must not proceed. The error code is returned to the caller for operations that fail. For traffic that is dropped instead, like an accepted connection or a received datagram, it is only logged.
Functions
authorize: func(operation: operation) -> result<decision, error-code>since 0.1.0
Decide whether the operation may proceed.
Must not have side effects. When latches are aggregated, a latch is not asked about an
operation another latch already denied, so state changed here could disagree with what
actually happened. Act on decisions in observe-decision instead.
observe-decision: func(
final-decision: decision,
operation: operation,
) -> result<_, error-code>since 0.1.0
Observe the final decision for an operation, after authorize returned a decision.
Called for both deferred and denied operations, including operations this latch was not
asked to authorize because an aggregated latch denied them first. The final decision is
what the gate enforces, so this is where a latch updates any state it keeps, for example
remembering peers the caller is allowed to exchange traffic with. Not called when
authorize returned an error.
An error fails the operation, since the latch could not act on the decision.
WIT
/// Access control for wasi:sockets.
///
/// A gate consults a latch before performing each socket operation. The latch either defers,
/// raising no objection, or denies the operation. A latch never grants an operation, an
/// operation proceeds when the latch defers. Several latches can be aggregated into one, the
/// operation is denied when any of them denies it.
///
/// Deciding and acting on a decision are separate steps. `authorize` decides without side
/// effects, then the gate reports the final decision to `observe-decision`, where a latch updates
/// any state it keeps. A latch aggregated with others may not be asked to authorize an operation
/// another latch already denied, but it always observes the final decision.
@since(version = 0.1.0)
interface latch {
@since(version = 0.1.0)
use wasi:sockets/types@0.3.0.{ip-address, ip-address-family, ip-socket-address, tcp-socket, udp-socket};
/// Arguments to wasi:sockets/ip-name-lookup#resolve-addresses.
@since(version = 0.1.0)
record resolve-addresses-args {
/// The name to look up, as passed by the caller. DNS search paths are not applied.
name: string,
}
/// Items returned from calls to wasi:sockets/ip-name-lookup#resolve-addresses.
@since(version = 0.1.0)
record resolve-addresses-returns-item {
/// An address the lookup returned.
ip-address: ip-address,
}
/// Operations of the wasi:sockets/ip-name-lookup interface.
@since(version = 0.1.0)
variant ip-name-lookup-operation {
/// Authorized before the name is looked up. A denial fails the lookup.
resolve-addresses(resolve-addresses-args),
/// Authorized for each address the lookup returned. A denied address is removed from the
/// result, the lookup succeeds with the remaining addresses.
resolve-addresses-return(resolve-addresses-returns-item),
}
/// Arguments to wasi:sockets/types#tcp-socket.create.
@since(version = 0.1.0)
record tcp-socket-create-args {
address-family: ip-address-family,
}
/// Arguments to wasi:sockets/types#tcp-socket.bind.
@since(version = 0.1.0)
record tcp-socket-bind-args {
local-address: ip-socket-address,
}
/// Arguments to wasi:sockets/types#tcp-socket.connect.
@since(version = 0.1.0)
record tcp-socket-connect-args {
remote-address: ip-socket-address,
}
/// Operations of the wasi:sockets/types#tcp-socket interface.
///
/// The borrowed socket is the socket the operation is performed on, a latch may query it, for
/// example for its local or remote address.
@since(version = 0.1.0)
variant tcp-socket-operation {
/// Authorized before the socket is created. A denial fails the create.
create(tcp-socket-create-args),
/// Authorized before the socket is bound. A denial fails the bind.
bind(tuple<borrow<tcp-socket>, tcp-socket-bind-args>),
/// Authorized before the socket connects. A denial fails the connect, nothing is sent to
/// the remote address.
connect(tuple<borrow<tcp-socket>, tcp-socket-connect-args>),
/// Authorized before the socket starts listening. A denial fails the listen.
listen(tuple<borrow<tcp-socket>>),
/// Authorized for each connection a listening socket accepts, the borrowed socket is the
/// accepted connection. A denied connection is closed before it reaches the caller, the
/// listening socket keeps accepting other connections.
listen-connection(tuple<borrow<tcp-socket>>),
/// Authorized once per socket before data is sent, `send` can only be called once and
/// streams all of its data. A denial fails the send.
send(tuple<borrow<tcp-socket>>),
/// Authorized once per socket before data is received, `receive` can only be called once
/// and streams all of its data. A denial fails the receive.
receive(tuple<borrow<tcp-socket>>),
}
/// Arguments to wasi:sockets/types#udp-socket.create.
@since(version = 0.1.0)
record udp-socket-create-args {
address-family: ip-address-family,
}
/// Arguments to wasi:sockets/types#udp-socket.bind.
@since(version = 0.1.0)
record udp-socket-bind-args {
local-address: ip-socket-address,
}
/// Arguments to wasi:sockets/types#udp-socket.connect.
@since(version = 0.1.0)
record udp-socket-connect-args {
remote-address: ip-socket-address,
}
/// Arguments to wasi:sockets/types#udp-socket.send.
@since(version = 0.1.0)
record udp-socket-send-args {
/// The length of the datagram in bytes.
data-length: u64,
/// Where the datagram is sent. `none` for a connected socket sending to the remote
/// address it is connected to.
remote-address: option<ip-socket-address>,
}
/// Returned value from calls to wasi:sockets/types#udp-socket.receive.
@since(version = 0.1.0)
record udp-socket-receive-returns {
/// The length of the datagram in bytes.
data-length: u64,
/// Where the datagram was sent from. UDP source addresses are not authenticated.
remote-address: ip-socket-address,
}
/// Operations of wasi:sockets/types#udp-socket.
///
/// The borrowed socket is the socket the operation is performed on, a latch may query it, for
/// example for its local or remote address.
@since(version = 0.1.0)
variant udp-socket-operation {
/// Authorized before the socket is created. A denial fails the create.
create(udp-socket-create-args),
/// Authorized before the socket is bound. A denial fails the bind.
bind(tuple<borrow<udp-socket>, udp-socket-bind-args>),
/// Authorized before the socket connects. A denial fails the connect.
connect(tuple<borrow<udp-socket>, udp-socket-connect-args>),
/// Authorized for each datagram before it is sent. A denial fails the send, nothing is
/// sent.
send(tuple<borrow<udp-socket>, udp-socket-send-args>),
/// Authorized for each datagram after it is received, before it reaches the caller. A
/// denied datagram is dropped and the receive continues with the next datagram, so a
/// sender cannot fail the caller's receive.
receive(tuple<borrow<udp-socket>, udp-socket-receive-returns>),
}
/// Operations for the wasi:sockets package.
@since(version = 0.1.0)
variant operation {
ip-name-lookup(ip-name-lookup-operation),
tcp-socket(tcp-socket-operation),
udp-socket(udp-socket-operation),
}
/// The name of a latch component, identifying which latch reported an error.
@since(version = 0.1.0)
type latch-name = string;
/// A latch was unable to decide or observe. The gate fails the operation, except for an
/// accepted connection, which is closed as if it were denied.
@since(version = 0.1.0)
variant error-code {
/// The latch's configuration is invalid, the cause is logged by the latch.
invalid-config(latch-name),
/// The latch was unable to act on the final decision.
observation-failed(latch-name),
/// Any other error.
other(option<string>),
}
/// The reason an operation is denied, returned to the caller as the wasi:sockets error code.
@since(version = 0.1.0)
variant sockets-error-code {
/// Access denied.
///
/// POSIX equivalent: EACCES, EPERM
access-denied,
/// One of the arguments is invalid.
///
/// POSIX equivalent: EINVAL, EDESTADDRREQ, EAFNOSUPPORT
invalid-argument,
/// A catch-all for errors not captured by the existing variants.
/// Implementations can use this to extend the error type without
/// breaking existing code.
other(option<string>),
}
/// Whether the operation is allowed to proceed.
@since(version = 0.1.0)
variant decision {
/// No objection to the operation.
deferred,
/// The operation must not proceed. The error code is returned to the caller for
/// operations that fail. For traffic that is dropped instead, like an accepted connection
/// or a received datagram, it is only logged.
denied(sockets-error-code),
}
/// Decide whether the operation may proceed.
///
/// Must not have side effects. When latches are aggregated, a latch is not asked about an
/// operation another latch already denied, so state changed here could disagree with what
/// actually happened. Act on decisions in `observe-decision` instead.
@since(version = 0.1.0)
authorize: func(operation: operation) -> result<decision, error-code>;
/// Observe the final decision for an operation, after `authorize` returned a decision.
///
/// Called for both deferred and denied operations, including operations this latch was not
/// asked to authorize because an aggregated latch denied them first. The final decision is
/// what the gate enforces, so this is where a latch updates any state it keeps, for example
/// remembering peers the caller is allowed to exchange traffic with. Not called when
/// `authorize` returned an error.
///
/// An error fails the operation, since the latch could not act on the decision.
@since(version = 0.1.0)
observe-decision: func(final-decision: decision, operation: operation) -> result<_, error-code>;
}