Skip to content

Commit

Permalink
Update to the latest wasi-sockets.
Browse files Browse the repository at this point in the history
Update to the latest wasi-sockets. This just contains documentation
changes, and no interface changes.
  • Loading branch information
sunfishcode committed Jan 23, 2024
1 parent a64be9c commit c87dbf7
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 43 deletions.
4 changes: 2 additions & 2 deletions wit/deps.lock
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ sha512 = "cc4fa3d178559a89d9d6a376e3359b892158d1e73317c5db1f797ebc6b0b57abf24227

[sockets]
url = "https://github.com/WebAssembly/wasi-sockets/archive/main.tar.gz"
sha256 = "40863017f355ac90c57630cc00b94518804e8e2c5694a7870b7a54dbdcda0e08"
sha512 = "2d6a919247430e869bf85a06a6a1d198f04368951e76c1fec7961b2b07af381c58c8e8b9079c91925dfbf80976971213329be57d59a90bae6e4e6460b073dc88"
sha256 = "9a3816bfa5a8b0673e5651024d8f9a1540f169f6d70f2bde6ee0e8240cd177ee"
sha512 = "bfcce89127510e16e9e7d7ac058ba3108953067e2d53f05cb1020dadf552738753b7fc9d10797236f2ced089063de7ac853fd9526079758fab1419d9b58a5212"
116 changes: 80 additions & 36 deletions wit/deps/sockets/tcp.wit
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,45 @@ interface tcp {
/// Similar to `SHUT_RDWR` in POSIX.
both,
}


/// A TCP socket handle.

/// A TCP socket resource.
///
/// The socket can be in one of the following states:
/// - `unbound`
/// - `bind-in-progress`
/// - `bound` (See note below)
/// - `listen-in-progress`
/// - `listening`
/// - `connect-in-progress`
/// - `connected`
/// - `closed`
/// See <https://github.com/WebAssembly/wasi-sockets/TcpSocketOperationalSemantics.md>
/// for a 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`, `listen-in-progress`, `listening`, `connect-in-progress` or `connected`)
///
/// In addition to the general error codes documented on the
/// `network::error-code` type, TCP socket methods may always return
/// `error(invalid-state)` when in the `closed` state.
resource tcp-socket {
/// Bind the socket to a specific network on the provided IP address and port.
///
/// If the IP address is zero (`0.0.0.0` in 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.
///
/// Unlike in POSIX, this function is async. This enables interactive WASI hosts to inject permission prompts.
/// 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 `start` errors
/// # Typical errors
/// - `invalid-argument`: The `local-address` has the wrong address family. (EAFNOSUPPORT, EFAULT on Windows)
/// - `invalid-argument`: `local-address` is not a unicast address. (EINVAL)
/// - `invalid-argument`: `local-address` is an IPv4-mapped IPv6 address. (EINVAL)
/// - `invalid-state`: The socket is already bound. (EINVAL)
///
/// # Typical `finish` errors
/// - `address-in-use`: No ephemeral ports available. (EADDRINUSE, ENOBUFS on Windows)
/// - `address-in-use`: Address is already in use. (EADDRINUSE)
/// - `address-not-bindable`: `local-address` is not an address that the `network` can bind to. (EADDRNOTAVAIL)
Expand All @@ -46,6 +66,11 @@ interface tcp {
/// socket option should be set implicitly on all platforms, except on Windows where this is the default behavior
/// and SO_REUSEADDR performs something different entirely.
///
/// Unlike in POSIX, in WASI the bind operation is async. This enables
/// interactive WASI hosts to inject permission prompts. Runtimes that
/// don't want to make use of this ability can simply call the native
/// `bind` as part of either `start-bind` or `finish-bind`.
///
/// # References
/// - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/bind.html>
/// - <https://man7.org/linux/man-pages/man2/bind.2.html>
Expand All @@ -57,32 +82,40 @@ interface tcp {
/// Connect to a remote endpoint.
///
/// On success:
/// - the socket is transitioned into the Connection state
/// - the socket is transitioned into the `connection` state.
/// - a pair of streams is returned that can be used to read & write to the connection
///
/// After a failed connection attempt, the only valid action left is to
/// `drop` the socket. A single socket can not be used to connect more than once.
/// After a failed connection attempt, the socket will be in the `closed`
/// state and the only valid action left is to `drop` the socket. A single
/// socket can not be used to connect more than once.
///
/// # Typical `start` errors
/// # Typical errors
/// - `invalid-argument`: The `remote-address` has the wrong address family. (EAFNOSUPPORT)
/// - `invalid-argument`: `remote-address` is not a unicast address. (EINVAL, ENETUNREACH on Linux, EAFNOSUPPORT on MacOS)
/// - `invalid-argument`: `remote-address` is an IPv4-mapped IPv6 address. (EINVAL, EADDRNOTAVAIL on Illumos)
/// - `invalid-argument`: The IP address in `remote-address` is set to INADDR_ANY (`0.0.0.0` / `::`). (EADDRNOTAVAIL on Windows)
/// - `invalid-argument`: The port in `remote-address` is set to 0. (EADDRNOTAVAIL on Windows)
/// - `invalid-argument`: The socket is already attached to a different network. The `network` passed to `connect` must be identical to the one passed to `bind`.
/// - `invalid-state`: The socket is already in the Connection state. (EISCONN)
/// - `invalid-state`: The socket is already in the Listener state. (EOPNOTSUPP, EINVAL on Windows)
///
/// # Typical `finish` errors
/// - `invalid-state`: The socket is already in the `connected` state. (EISCONN)
/// - `invalid-state`: The socket is already in the `listening` state. (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)
/// - `not-in-progress`: A `connect` operation is not in progress.
/// - `not-in-progress`: A connect operation is not in progress.
/// - `would-block`: Can't finish the operation, it is still in progress. (EWOULDBLOCK, EAGAIN)
///
/// # Implementors note
/// The POSIX equivalent of `start-connect` is the regular `connect` syscall.
/// Because all WASI sockets are non-blocking this is expected to return
/// EINPROGRESS, which should be translated to `ok()` in WASI.
///
/// The POSIX equivalent of `finish-connect` is a `poll` for event `POLLOUT`
/// with a timeout of 0 on the socket descriptor. Followed by a check for
/// the `SO_ERROR` socket option, in case the poll signaled readiness.
///
/// # References
/// - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/connect.html>
/// - <https://man7.org/linux/man-pages/man2/connect.2.html>
Expand All @@ -93,22 +126,24 @@ interface tcp {

/// Start listening for new connections.
///
/// Transitions the socket into the Listener state.
/// Transitions the socket into the `listening` state.
///
/// Unlike POSIX:
/// - this function is async. This enables interactive WASI hosts to inject permission prompts.
/// - the socket must already be explicitly bound.
/// Unlike POSIX, the socket must already be explicitly bound.
///
/// # Typical `start` errors
/// # Typical errors
/// - `invalid-state`: The socket is not bound to any local address. (EDESTADDRREQ)
/// - `invalid-state`: The socket is already in the Connection state. (EISCONN, EINVAL on BSD)
/// - `invalid-state`: The socket is already in the Listener state.
///
/// # Typical `finish` errors
/// - `invalid-state`: The socket is already in the `connected` state. (EISCONN, EINVAL on BSD)
/// - `invalid-state`: The socket is already in the `listening` state.
/// - `address-in-use`: Tried to perform an implicit bind, but there were no ephemeral ports available. (EADDRINUSE)
/// - `not-in-progress`: A `listen` operation is not in progress.
/// - `not-in-progress`: A listen operation is not in progress.
/// - `would-block`: Can't finish the operation, it is still in progress. (EWOULDBLOCK, EAGAIN)
///
/// # Implementors note
/// Unlike in POSIX, in WASI the listen operation is async. This enables
/// interactive WASI hosts to inject permission prompts. Runtimes that
/// don't want to make use of this ability can simply call the native
/// `listen` as part of either `start-listen` or `finish-listen`.
///
/// # References
/// - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/listen.html>
/// - <https://man7.org/linux/man-pages/man2/listen.2.html>
Expand All @@ -119,7 +154,7 @@ interface tcp {

/// Accept a new client socket.
///
/// The returned socket is bound and in the Connection state. The following properties are inherited from the listener socket:
/// The returned socket is bound and in the `connected` state. The following properties are inherited from the listener socket:
/// - `address-family`
/// - `keep-alive-enabled`
/// - `keep-alive-idle-time`
Expand All @@ -133,7 +168,7 @@ interface tcp {
/// a pair of streams that can be used to read & write to the connection.
///
/// # Typical errors
/// - `invalid-state`: Socket is not in the Listener state. (EINVAL)
/// - `invalid-state`: Socket is not in the `listening` state. (EINVAL)
/// - `would-block`: No pending connections at the moment. (EWOULDBLOCK, EAGAIN)
/// - `connection-aborted`: An incoming connection was pending, but was terminated by the client before this listener could accept it. (ECONNABORTED)
/// - `new-socket-limit`: The new socket resource could not be created because of a system limit. (EMFILE, ENFILE)
Expand Down Expand Up @@ -175,7 +210,7 @@ interface tcp {
/// - <https://man.freebsd.org/cgi/man.cgi?query=getpeername&sektion=2&n=1>
remote-address: func() -> result<ip-socket-address, error-code>;

/// Whether the socket is listening for new connections.
/// Whether the socket is in the `listening` state.
///
/// Equivalent to the SO_ACCEPTCONN socket option.
is-listening: func() -> bool;
Expand All @@ -193,7 +228,7 @@ interface tcp {
/// # 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 already in the Connection state.
/// - `invalid-state`: (set) The socket is in the `connect-in-progress` or `connected` state.
set-listen-backlog-size: func(value: u64) -> result<_, error-code>;

/// Enables or disables keepalive.
Expand Down Expand Up @@ -253,8 +288,6 @@ interface tcp {
///
/// # Typical errors
/// - `invalid-argument`: (set) The TTL value must be 1 or higher.
/// - `invalid-state`: (set) The socket is already in the Connection state.
/// - `invalid-state`: (set) The socket is already in the Listener state.
hop-limit: func() -> result<u8, error-code>;
set-hop-limit: func(value: u8) -> result<_, error-code>;

Expand All @@ -268,14 +301,25 @@ interface tcp {
///
/// # Typical errors
/// - `invalid-argument`: (set) The provided value was 0.
/// - `invalid-state`: (set) The socket is already in the Connection state.
/// - `invalid-state`: (set) The socket is already in the Listener state.
receive-buffer-size: func() -> result<u64, error-code>;
set-receive-buffer-size: func(value: u64) -> result<_, error-code>;
send-buffer-size: func() -> result<u64, error-code>;
set-send-buffer-size: func(value: u64) -> result<_, error-code>;

/// Create a `pollable` which will resolve once the socket is ready for I/O.
/// Create a `pollable` which can be used to poll for, or block on,
/// completion of any of the asynchronous operations of this socket.
///
/// When `finish-bind`, `finish-listen`, `finish-connect` or `accept`
/// return `error(would-block)`, this pollable can be used to wait for
/// their success or failure, after which the method can be retried.
///
/// The pollable is not limited to the async operation that happens to be
/// in progress at the time of calling `subscribe` (if any). Theoretically,
/// `subscribe` only has to be called once per socket and can then be
/// (re)used for the remainder of the socket's lifetime.
///
/// See <https://github.com/WebAssembly/wasi-sockets/TcpSocketOperationalSemantics.md#Pollable-readiness>
/// for a more information.
///
/// Note: this function is here for WASI Preview2 only.
/// It's planned to be removed when `future` is natively supported in Preview3.
Expand All @@ -297,7 +341,7 @@ interface tcp {
/// The shutdown function does not close (drop) the socket.
///
/// # Typical errors
/// - `invalid-state`: The socket is not in the Connection state. (ENOTCONN)
/// - `invalid-state`: The socket is not in the `connected` state. (ENOTCONN)
///
/// # References
/// - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/shutdown.html>
Expand Down
12 changes: 7 additions & 5 deletions wit/deps/sockets/udp.wit
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,21 @@ interface udp {
/// network interface(s) to bind to.
/// If the port is zero, the socket will be bound to a random free port.
///
/// Unlike in POSIX, this function is async. This enables interactive WASI hosts to inject permission prompts.
///
/// # Typical `start` errors
/// # Typical errors
/// - `invalid-argument`: The `local-address` has the wrong address family. (EAFNOSUPPORT, EFAULT on Windows)
/// - `invalid-state`: The socket is already bound. (EINVAL)
///
/// # Typical `finish` errors
/// - `address-in-use`: No ephemeral ports available. (EADDRINUSE, ENOBUFS on Windows)
/// - `address-in-use`: Address is already in use. (EADDRINUSE)
/// - `address-not-bindable`: `local-address` is not an address that the `network` can bind to. (EADDRNOTAVAIL)
/// - `not-in-progress`: A `bind` operation is not in progress.
/// - `would-block`: Can't finish the operation, it is still in progress. (EWOULDBLOCK, EAGAIN)
///
/// # Implementors note
/// Unlike in POSIX, in WASI the bind operation is async. This enables
/// interactive WASI hosts to inject permission prompts. Runtimes that
/// don't want to make use of this ability can simply call the native
/// `bind` as part of either `start-bind` or `finish-bind`.
///
/// # References
/// - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/bind.html>
/// - <https://man7.org/linux/man-pages/man2/bind.2.html>
Expand Down

0 comments on commit c87dbf7

Please sign in to comment.