pub struct UdpSocket { /* private fields */ }Expand description
A UDP socket handle.
Implementations§
Source§impl UdpSocket
impl UdpSocket
Sourcepub fn start_bind(
&self,
network: &Network,
local_address: IpSocketAddress,
) -> Result<(), ErrorCode>
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: 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 thenetworkcan bind to. (EADDRNOTAVAIL)not-in-progress: Abindoperation 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
impl UdpSocket
Sourcepub fn stream(
&self,
remote_address: Option<IpSocketAddress>,
) -> Result<(IncomingDatagramStream, OutgoingDatagramStream), ErrorCode>
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:
sendcan only be used to send to this destination.receivewill only return datagrams sent from the providedremote-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: 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-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
impl UdpSocket
Sourcepub fn local_address(&self) -> Result<IpSocketAddress, ErrorCode>
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
addressis 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
impl UdpSocket
Sourcepub fn remote_address(&self) -> Result<IpSocketAddress, ErrorCode>
pub fn remote_address(&self) -> Result<IpSocketAddress, ErrorCode>
Source§impl UdpSocket
impl UdpSocket
Sourcepub fn address_family(&self) -> IpAddressFamily
pub fn address_family(&self) -> IpAddressFamily
Whether this is a IPv4 or IPv6 socket.
Equivalent to the SO_DOMAIN socket option.
Source§impl UdpSocket
impl UdpSocket
Sourcepub fn receive_buffer_size(&self) -> Result<u64, ErrorCode>
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.