Struct wasi::sockets::udp::UdpSocket

source ·
pub struct UdpSocket { /* private fields */ }
Expand description

A UDP socket handle.

Implementations§

source§

impl UdpSocket

source

pub fn start_bind( &self, network: &Network, local_address: IpSocketAddress ) -> Result<(), ErrorCode>

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 port is zero, the socket will be bound to a random free port.

§Typical errors
  • invalid-argument: The local-address has 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-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
source§

impl UdpSocket

source§

impl UdpSocket

source

pub fn stream( &self, remote_address: Option<IpSocketAddress> ) -> Result<(IncomingDatagramStream, OutgoingDatagramStream), ErrorCode>

Set up inbound & outbound communication channels, optionally to a specific peer.

This function only changes the local socket configuration and does not generate any network traffic. On success, the remote-address of the socket is updated. The local-address may be updated as well, based on the best network path to remote-address.

When a remote-address is provided, the returned streams are limited to communicating with that specific peer:

  • send can only be used to send to this destination.
  • receive will only return datagrams sent from the provided remote-address.

This method may be called multiple times on the same socket to change its association, but only the most recently returned pair of streams will be operational. Implementations may trap if the streams returned by a previous invocation haven’t been dropped yet before calling stream again.

The POSIX equivalent in pseudo-code is:

if (was previously connected) {
connect(s, AF_UNSPEC)
}
if (remote_address is Some) {
connect(s, remote_address)
}

Unlike in POSIX, the socket must already be explicitly bound.

§Typical errors
  • invalid-argument: The remote-address has the wrong address family. (EAFNOSUPPORT)
  • invalid-argument: The IP address in remote-address is set to INADDR_ANY (0.0.0.0 / ::). (EDESTADDRREQ, EADDRNOTAVAIL)
  • invalid-argument: The port in remote-address is set to 0. (EDESTADDRREQ, EADDRNOTAVAIL)
  • invalid-state: The socket is not bound.
  • address-in-use: Tried to perform an implicit bind, but there were no ephemeral ports available. (EADDRINUSE, EADDRNOTAVAIL on Linux, EAGAIN on BSD)
  • remote-unreachable: The remote address is not reachable. (ECONNRESET, ENETRESET, EHOSTUNREACH, EHOSTDOWN, ENETUNREACH, ENETDOWN, ENONET)
  • connection-refused: The connection was refused. (ECONNREFUSED)
§References
source§

impl UdpSocket

source

pub fn local_address(&self) -> Result<IpSocketAddress, ErrorCode>

Get 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 address is unspecified.

WASI is stricter and requires local-address to return invalid-state when the socket hasn’t been bound yet.

§Typical errors
  • invalid-state: The socket is not bound to any local address.
§References
source§

impl UdpSocket

source§

impl UdpSocket

source

pub fn address_family(&self) -> IpAddressFamily

Whether this is a IPv4 or IPv6 socket.

Equivalent to the SO_DOMAIN socket option.

source§

impl UdpSocket

source

pub fn unicast_hop_limit(&self) -> Result<u8, ErrorCode>

Equivalent to the IP_TTL & IPV6_UNICAST_HOPS socket options.

If the provided value is 0, an invalid-argument error is returned.

§Typical errors
  • invalid-argument: (set) The TTL value must be 1 or higher.
source§

impl UdpSocket

source

pub fn set_unicast_hop_limit(&self, value: u8) -> Result<(), ErrorCode>

source§

impl UdpSocket

source

pub fn receive_buffer_size(&self) -> Result<u64, ErrorCode>

The kernel buffer space reserved for sends/receives on this socket.

If the provided value is 0, an invalid-argument error is returned. Any other value will never cause an error, but it might be silently clamped and/or rounded. I.e. after setting a value, reading the same setting back may return a different value.

Equivalent to the SO_RCVBUF and SO_SNDBUF socket options.

§Typical errors
  • invalid-argument: (set) The provided value was 0.
source§

impl UdpSocket

source§

impl UdpSocket

source§

impl UdpSocket

source

pub fn set_send_buffer_size(&self, value: u64) -> Result<(), ErrorCode>

source§

impl UdpSocket

source

pub fn subscribe(&self) -> Pollable

Create a pollable which will resolve once the socket is ready for I/O.

Note: this function is here for WASI Preview2 only. It’s planned to be removed when future is natively supported in Preview3.

Trait Implementations§

source§

impl Debug for UdpSocket

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.