componentized/directory

wasi:sockets

interface dependency

A WASI API for TCP & UDP sockets and domain name lookup.

interface udp-create-socket wasi:sockets/udp-create-socket@0.2.3 since 0.2.0

Types

use resource networksince 0.2.0

from wasi:sockets/network@0.2.3

An opaque resource that represents access to (a subset of) the network. This enables context-based security for networking. There is no need for this to map 1:1 to a physical network interface.

use enum error-codesince 0.2.0

from wasi:sockets/network@0.2.3

Error codes.

In theory, every API can return any error code. In practice, API's typically only return the errors documented per API combined with a couple of errors that are always possible:

  • unknown
  • access-denied
  • not-supported
  • out-of-memory
  • concurrency-conflict

See each individual API for what the POSIX equivalents are. They sometimes differ per API.

  • unknown

    Unknown error

  • access-denied

    Access denied.

    POSIX equivalent: EACCES, EPERM

  • not-supported

    The operation is not supported.

    POSIX equivalent: EOPNOTSUPP

  • invalid-argument

    One of the arguments is invalid.

    POSIX equivalent: EINVAL

  • out-of-memory

    Not enough memory to complete the operation.

    POSIX equivalent: ENOMEM, ENOBUFS, EAI_MEMORY

  • timeout

    The operation timed out before it could finish completely.

  • concurrency-conflict

    This operation is incompatible with another asynchronous operation that is already in progress.

    POSIX equivalent: EALREADY

  • not-in-progress

    Trying to finish an asynchronous operation that:

    • has not been started yet, or:
    • was already finished by a previous finish-* call.

    Note: this is scheduled to be removed when futures are natively supported.

  • would-block

    The operation has been aborted because it could not be completed immediately.

    Note: this is scheduled to be removed when futures are natively supported.

  • invalid-state

    The operation is not valid in the socket's current state.

  • new-socket-limit

    A new socket resource could not be created because of a system limit.

  • address-not-bindable

    A bind operation failed because the provided address is not an address that the network can bind to.

  • address-in-use

    A bind operation failed because the provided address is already in use or because there are no ephemeral ports available.

  • remote-unreachable

    The remote address is not reachable

  • connection-refused

    The TCP connection was forcefully rejected

  • connection-reset

    The TCP connection was reset.

  • connection-aborted

    A TCP connection was aborted.

  • datagram-too-large

    The size of a datagram sent to a UDP socket exceeded the maximum supported size.

  • name-unresolvable

    Name does not exist or has no suitable associated IP addresses.

  • temporary-resolver-failure

    A temporary failure in name resolution occurred.

  • permanent-resolver-failure

    A permanent failure in name resolution occurred.

use enum ip-address-familysince 0.2.0

from wasi:sockets/network@0.2.3

  • ipv4

    Similar to AF_INET in POSIX.

  • ipv6

    Similar to AF_INET6 in POSIX.

use resource udp-socketsince 0.2.0

from wasi:sockets/udp@0.2.3

A UDP socket handle.

Functions

functioncreate-udp-socket: func( address-family: ip-address-family, ) -> result<udp-socket, error-code>since 0.2.0

Create 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.

This function does not require a network capability handle. This is considered to be safe because at time of creation, the socket is not bound to any network yet. Up to the moment bind is called, the socket is effectively an in-memory configuration object, unable to communicate with the outside world.

All sockets are non-blocking. Use the wasi-poll interface to block on asynchronous operations.

Typical errors

  • not-supported: The specified address-family is not supported. (EAFNOSUPPORT)
  • new-socket-limit: The new socket resource could not be created because of a system limit. (EMFILE, ENFILE)

References:

WIT

@since(version = 0.2.0)
interface udp-create-socket {
  @since(version = 0.2.0)
  use network.{network, error-code, ip-address-family};
  @since(version = 0.2.0)
  use udp.{udp-socket};

  /// Create 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.
  ///
  /// This function does not require a network capability handle. This is considered to be safe because
  /// at time of creation, the socket is not bound to any `network` yet. Up to the moment `bind` is called,
  /// the socket is effectively an in-memory configuration object, unable to communicate with the outside world.
  ///
  /// All sockets are non-blocking. Use the wasi-poll interface to block on asynchronous operations.
  ///
  /// # Typical errors
  /// - `not-supported`:     The specified `address-family` is not supported. (EAFNOSUPPORT)
  /// - `new-socket-limit`:  The new socket resource could not be created because of a system limit. (EMFILE, ENFILE)
  ///
  /// # References:
  /// - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/socket.html>
  /// - <https://man7.org/linux/man-pages/man2/socket.2.html>
  /// - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsasocketw>
  /// - <https://man.freebsd.org/cgi/man.cgi?query=socket&sektion=2>
  @since(version = 0.2.0)
  create-udp-socket: func(address-family: ip-address-family) -> result<udp-socket, error-code>;
}