Trait wasmtime_wasi::preview2::bindings::wasi::sockets::udp::HostUdpSocket
source · pub trait HostUdpSocket {
Show 19 methods
// Required methods
fn start_bind(
&mut self,
self_: Resource<UdpSocket>,
network: Resource<Network>,
local_address: IpSocketAddress
) -> Result<(), SocketError>;
fn finish_bind(
&mut self,
self_: Resource<UdpSocket>
) -> Result<(), SocketError>;
fn start_connect(
&mut self,
self_: Resource<UdpSocket>,
network: Resource<Network>,
remote_address: IpSocketAddress
) -> Result<(), SocketError>;
fn finish_connect(
&mut self,
self_: Resource<UdpSocket>
) -> Result<(), SocketError>;
fn receive(
&mut self,
self_: Resource<UdpSocket>,
max_results: u64
) -> Result<Vec<Datagram>, SocketError>;
fn send(
&mut self,
self_: Resource<UdpSocket>,
datagrams: Vec<Datagram>
) -> Result<u64, SocketError>;
fn local_address(
&mut self,
self_: Resource<UdpSocket>
) -> Result<IpSocketAddress, SocketError>;
fn remote_address(
&mut self,
self_: Resource<UdpSocket>
) -> Result<IpSocketAddress, SocketError>;
fn address_family(
&mut self,
self_: Resource<UdpSocket>
) -> Result<IpAddressFamily>;
fn ipv6_only(
&mut self,
self_: Resource<UdpSocket>
) -> Result<bool, SocketError>;
fn set_ipv6_only(
&mut self,
self_: Resource<UdpSocket>,
value: bool
) -> Result<(), SocketError>;
fn unicast_hop_limit(
&mut self,
self_: Resource<UdpSocket>
) -> Result<u8, SocketError>;
fn set_unicast_hop_limit(
&mut self,
self_: Resource<UdpSocket>,
value: u8
) -> Result<(), SocketError>;
fn receive_buffer_size(
&mut self,
self_: Resource<UdpSocket>
) -> Result<u64, SocketError>;
fn set_receive_buffer_size(
&mut self,
self_: Resource<UdpSocket>,
value: u64
) -> Result<(), SocketError>;
fn send_buffer_size(
&mut self,
self_: Resource<UdpSocket>
) -> Result<u64, SocketError>;
fn set_send_buffer_size(
&mut self,
self_: Resource<UdpSocket>,
value: u64
) -> Result<(), SocketError>;
fn subscribe(
&mut self,
self_: Resource<UdpSocket>
) -> Result<Resource<Pollable>>;
fn drop(&mut self, rep: Resource<UdpSocket>) -> Result<()>;
}
Required Methods§
sourcefn start_bind(
&mut self,
self_: Resource<UdpSocket>,
network: Resource<Network>,
local_address: IpSocketAddress
) -> Result<(), SocketError>
fn start_bind( &mut self, self_: Resource<UdpSocket>, network: Resource<Network>, local_address: IpSocketAddress ) -> Result<(), SocketError>
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.
When a socket is not explicitly bound, the first invocation to connect will implicitly bind the socket.
Unlike in POSIX, this function is async. This enables interactive WASI hosts to inject permission prompts.
Typical start
errors
invalid-argument
: Thelocal-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 thenetwork
can bind to. (EADDRNOTAVAIL)not-in-progress
: Abind
operation is not in progress.would-block
: Can’t finish the operation, it is still in progress. (EWOULDBLOCK, EAGAIN)
References
fn finish_bind(&mut self, self_: Resource<UdpSocket>) -> Result<(), SocketError>
sourcefn start_connect(
&mut self,
self_: Resource<UdpSocket>,
network: Resource<Network>,
remote_address: IpSocketAddress
) -> Result<(), SocketError>
fn start_connect( &mut self, self_: Resource<UdpSocket>, network: Resource<Network>, remote_address: IpSocketAddress ) -> Result<(), SocketError>
Set the destination address.
The local-address is updated based on the best network path to remote-address
.
When a destination address is set:
- all receive operations will only return datagrams sent from the provided
remote-address
. - the
send
function can only be used to send to this destination.
Note that this function does not generate any network traffic and the peer is not aware of this “connection”.
Unlike in POSIX, this function is async. This enables interactive WASI hosts to inject permission prompts.
Typical start
errors
invalid-argument
: Theremote-address
has the wrong address family. (EAFNOSUPPORT)invalid-argument
:remote-address
is a non-IPv4-mapped IPv6 address, but the socket was bound to a specific IPv4-mapped IPv6 address. (or vice versa)invalid-argument
: The IP address inremote-address
is set to INADDR_ANY (0.0.0.0
/::
). (EDESTADDRREQ, EADDRNOTAVAIL)invalid-argument
: The port inremote-address
is set to 0. (EDESTADDRREQ, EADDRNOTAVAIL)invalid-argument
: The socket is already bound to a different network. Thenetwork
passed toconnect
must be identical to the one passed tobind
.
Typical finish
errors
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
: Aconnect
operation is not in progress.would-block
: Can’t finish the operation, it is still in progress. (EWOULDBLOCK, EAGAIN)
References
fn finish_connect( &mut self, self_: Resource<UdpSocket> ) -> Result<(), SocketError>
sourcefn receive(
&mut self,
self_: Resource<UdpSocket>,
max_results: u64
) -> Result<Vec<Datagram>, SocketError>
fn receive( &mut self, self_: Resource<UdpSocket>, max_results: u64 ) -> Result<Vec<Datagram>, SocketError>
Receive messages on the socket.
This function attempts to receive up to max-results
datagrams on the socket without blocking.
The returned list may contain fewer elements than requested, but never more.
If max-results
is 0, this function returns successfully with an empty list.
Typical errors
invalid-state
: The socket is not bound to any local address. (EINVAL)remote-unreachable
: The remote address is not reachable. (ECONNREFUSED, ECONNRESET, ENETRESET on Windows, EHOSTUNREACH, EHOSTDOWN, ENETUNREACH, ENETDOWN)would-block
: There is no pending data available to be read at the moment. (EWOULDBLOCK, EAGAIN)
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-recv
- https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-recvfrom
- https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms741687(v=vs.85)
- https://man.freebsd.org/cgi/man.cgi?query=recv&sektion=2
sourcefn send(
&mut self,
self_: Resource<UdpSocket>,
datagrams: Vec<Datagram>
) -> Result<u64, SocketError>
fn send( &mut self, self_: Resource<UdpSocket>, datagrams: Vec<Datagram> ) -> Result<u64, SocketError>
Send messages on the socket.
This function attempts to send all provided datagrams
on the socket without blocking and
returns how many messages were actually sent (or queued for sending).
This function semantically behaves the same as iterating the datagrams
list and sequentially
sending each individual datagram until either the end of the list has been reached or the first error occurred.
If at least one datagram has been sent successfully, this function never returns an error.
If the input list is empty, the function returns ok(0)
.
The remote address option is required. To send a message to the “connected” peer,
call remote-address
to get their address.
Typical errors
invalid-argument
: Theremote-address
has the wrong address family. (EAFNOSUPPORT)invalid-argument
:remote-address
is a non-IPv4-mapped IPv6 address, but the socket was bound to a specific IPv4-mapped IPv6 address. (or vice versa)invalid-argument
: The IP address inremote-address
is set to INADDR_ANY (0.0.0.0
/::
). (EDESTADDRREQ, EADDRNOTAVAIL)invalid-argument
: The port inremote-address
is set to 0. (EDESTADDRREQ, EADDRNOTAVAIL)invalid-argument
: The socket is in “connected” mode and thedatagram.remote-address
does not match the address passed toconnect
. (EISCONN)invalid-state
: The socket is not bound to any local address. Unlike POSIX, this function does not perform an implicit bind.remote-unreachable
: The remote address is not reachable. (ECONNREFUSED, ECONNRESET, ENETRESET on Windows, EHOSTUNREACH, EHOSTDOWN, ENETUNREACH, ENETDOWN)datagram-too-large
: The datagram is too large. (EMSGSIZE)would-block
: The send buffer is currently full. (EWOULDBLOCK, EAGAIN)
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
sourcefn local_address(
&mut self,
self_: Resource<UdpSocket>
) -> Result<IpSocketAddress, SocketError>
fn local_address( &mut self, self_: Resource<UdpSocket> ) -> Result<IpSocketAddress, SocketError>
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
sourcefn remote_address(
&mut self,
self_: Resource<UdpSocket>
) -> Result<IpSocketAddress, SocketError>
fn remote_address( &mut self, self_: Resource<UdpSocket> ) -> Result<IpSocketAddress, SocketError>
Get the address set with connect
.
Typical errors
invalid-state
: The socket is not connected to a remote address. (ENOTCONN)
References
sourcefn address_family(
&mut self,
self_: Resource<UdpSocket>
) -> Result<IpAddressFamily>
fn address_family( &mut self, self_: Resource<UdpSocket> ) -> Result<IpAddressFamily>
Whether this is a IPv4 or IPv6 socket.
Equivalent to the SO_DOMAIN socket option.
sourcefn ipv6_only(&mut self, self_: Resource<UdpSocket>) -> Result<bool, SocketError>
fn ipv6_only(&mut self, self_: Resource<UdpSocket>) -> Result<bool, SocketError>
Whether IPv4 compatibility (dual-stack) mode is disabled or not.
Equivalent to the IPV6_V6ONLY socket option.
Typical errors
not-supported
: (get/set)this
socket is an IPv4 socket.invalid-state
: (set) The socket is already bound.not-supported
: (set) Host does not support dual-stack sockets. (Implementations are not required to.)
fn set_ipv6_only( &mut self, self_: Resource<UdpSocket>, value: bool ) -> Result<(), SocketError>
sourcefn unicast_hop_limit(
&mut self,
self_: Resource<UdpSocket>
) -> Result<u8, SocketError>
fn unicast_hop_limit( &mut self, self_: Resource<UdpSocket> ) -> Result<u8, SocketError>
Equivalent to the IP_TTL & IPV6_UNICAST_HOPS socket options.
fn set_unicast_hop_limit( &mut self, self_: Resource<UdpSocket>, value: u8 ) -> Result<(), SocketError>
sourcefn receive_buffer_size(
&mut self,
self_: Resource<UdpSocket>
) -> Result<u64, SocketError>
fn receive_buffer_size( &mut self, self_: Resource<UdpSocket> ) -> Result<u64, SocketError>
The kernel buffer space reserved for sends/receives on this socket.
Note #1: an implementation may choose to cap or round the buffer size when setting the value. In other words, after setting a value, reading the same setting back may return a different value.
Note #2: there is not necessarily a direct relationship between the kernel buffer size and the bytes of actual data to be sent/received by the application, because the kernel might also use the buffer space for internal metadata structures.
Equivalent to the SO_RCVBUF and SO_SNDBUF socket options.
fn set_receive_buffer_size( &mut self, self_: Resource<UdpSocket>, value: u64 ) -> Result<(), SocketError>
fn send_buffer_size( &mut self, self_: Resource<UdpSocket> ) -> Result<u64, SocketError>
fn set_send_buffer_size( &mut self, self_: Resource<UdpSocket>, value: u64 ) -> Result<(), SocketError>
sourcefn subscribe(
&mut self,
self_: Resource<UdpSocket>
) -> Result<Resource<Pollable>>
fn subscribe( &mut self, self_: Resource<UdpSocket> ) -> Result<Resource<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.