pub trait UdpSocket:
TryFrom<UdpSocket, Error = Error>
+ Fd
+ Unpin
+ Send
+ Sync
+ 'static {
type Runtime: RuntimeLite;
Show 32 methods
// Required methods
fn bind<A: ToSocketAddrs<Self::Runtime>>(
addr: A,
) -> impl Future<Output = Result<Self>> + Send
where Self: Sized;
fn connect<A: ToSocketAddrs<Self::Runtime>>(
&self,
addr: A,
) -> impl Future<Output = Result<()>> + Send;
fn local_addr(&self) -> Result<SocketAddr>;
fn peer_addr(&self) -> Result<SocketAddr>;
fn recv(&self, buf: &mut [u8]) -> impl Future<Output = Result<usize>> + Send;
fn recv_from(
&self,
buf: &mut [u8],
) -> impl Future<Output = Result<(usize, SocketAddr)>> + Send;
fn send(&self, buf: &[u8]) -> impl Future<Output = Result<usize>> + Send;
fn send_to<A: ToSocketAddrs<Self::Runtime>>(
&self,
buf: &[u8],
target: A,
) -> impl Future<Output = Result<usize>> + Send;
fn peek(&self, buf: &mut [u8]) -> impl Future<Output = Result<usize>> + Send;
fn peek_from(
&self,
buf: &mut [u8],
) -> impl Future<Output = Result<(usize, SocketAddr)>> + Send;
fn join_multicast_v4(
&self,
multiaddr: Ipv4Addr,
interface: Ipv4Addr,
) -> Result<()>;
fn join_multicast_v6(
&self,
multiaddr: &Ipv6Addr,
interface: u32,
) -> Result<()>;
fn leave_multicast_v4(
&self,
multiaddr: Ipv4Addr,
interface: Ipv4Addr,
) -> Result<()>;
fn leave_multicast_v6(
&self,
multiaddr: &Ipv6Addr,
interface: u32,
) -> Result<()>;
fn multicast_loop_v4(&self) -> Result<bool>;
fn set_multicast_loop_v4(&self, on: bool) -> Result<()>;
fn multicast_ttl_v4(&self) -> Result<u32>;
fn set_multicast_ttl_v4(&self, ttl: u32) -> Result<()>;
fn multicast_loop_v6(&self) -> Result<bool>;
fn set_multicast_loop_v6(&self, on: bool) -> Result<()>;
fn set_ttl(&self, ttl: u32) -> Result<()>;
fn ttl(&self) -> Result<u32>;
fn set_broadcast(&self, broadcast: bool) -> Result<()>;
fn broadcast(&self) -> Result<bool>;
fn poll_recv_from(
&self,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<Result<(usize, SocketAddr)>>;
fn poll_send_to(
&self,
cx: &mut Context<'_>,
buf: &[u8],
target: SocketAddr,
) -> Poll<Result<usize>>;
// Provided methods
fn try_clone(&self) -> Result<Self> { ... }
fn only_v6(&self) -> Result<bool> { ... }
fn set_recv_buffer_size(&self, size: usize) -> Result<()> { ... }
fn recv_buffer_size(&self) -> Result<usize> { ... }
fn set_send_buffer_size(&self, size: usize) -> Result<()> { ... }
fn send_buffer_size(&self) -> Result<usize> { ... }
}
Expand description
The abstraction of a UDP socket.
Required Associated Types§
Sourcetype Runtime: RuntimeLite
type Runtime: RuntimeLite
The async runtime.
Required Methods§
Sourcefn bind<A: ToSocketAddrs<Self::Runtime>>(
addr: A,
) -> impl Future<Output = Result<Self>> + Sendwhere
Self: Sized,
fn bind<A: ToSocketAddrs<Self::Runtime>>(
addr: A,
) -> impl Future<Output = Result<Self>> + Sendwhere
Self: Sized,
Binds this socket to the specified address.
Sourcefn connect<A: ToSocketAddrs<Self::Runtime>>(
&self,
addr: A,
) -> impl Future<Output = Result<()>> + Send
fn connect<A: ToSocketAddrs<Self::Runtime>>( &self, addr: A, ) -> impl Future<Output = Result<()>> + Send
Connects this socket to the specified address.
Sourcefn local_addr(&self) -> Result<SocketAddr>
fn local_addr(&self) -> Result<SocketAddr>
Returns the local address that this listener is bound to.
This can be useful, for example, when binding to port 0
to figure out which port was actually bound.
Sourcefn peer_addr(&self) -> Result<SocketAddr>
fn peer_addr(&self) -> Result<SocketAddr>
Returns the peer address that this listener is connected to.
This can be useful, for example, when connect to port 0
to figure out which port was actually connected.
Sourcefn recv(&self, buf: &mut [u8]) -> impl Future<Output = Result<usize>> + Send
fn recv(&self, buf: &mut [u8]) -> impl Future<Output = Result<usize>> + Send
Receives data from the socket. Returns the number of bytes read and the source address.
Sourcefn recv_from(
&self,
buf: &mut [u8],
) -> impl Future<Output = Result<(usize, SocketAddr)>> + Send
fn recv_from( &self, buf: &mut [u8], ) -> impl Future<Output = Result<(usize, SocketAddr)>> + Send
Receives data from the socket, returning the number of bytes read and the source address.
Sourcefn send(&self, buf: &[u8]) -> impl Future<Output = Result<usize>> + Send
fn send(&self, buf: &[u8]) -> impl Future<Output = Result<usize>> + Send
Sends data by the socket.
Sourcefn send_to<A: ToSocketAddrs<Self::Runtime>>(
&self,
buf: &[u8],
target: A,
) -> impl Future<Output = Result<usize>> + Send
fn send_to<A: ToSocketAddrs<Self::Runtime>>( &self, buf: &[u8], target: A, ) -> impl Future<Output = Result<usize>> + Send
Sends data by the socket to the given address.
Sourcefn peek(&self, buf: &mut [u8]) -> impl Future<Output = Result<usize>> + Send
fn peek(&self, buf: &mut [u8]) -> impl Future<Output = Result<usize>> + Send
Receives data from the socket without removing it from the queue.
On success, returns the number of bytes peeked.
Sourcefn peek_from(
&self,
buf: &mut [u8],
) -> impl Future<Output = Result<(usize, SocketAddr)>> + Send
fn peek_from( &self, buf: &mut [u8], ) -> impl Future<Output = Result<(usize, SocketAddr)>> + Send
Receives data from socket without removing it from the queue.
On success, returns the number of bytes peeked and the origin.
Sourcefn join_multicast_v4(
&self,
multiaddr: Ipv4Addr,
interface: Ipv4Addr,
) -> Result<()>
fn join_multicast_v4( &self, multiaddr: Ipv4Addr, interface: Ipv4Addr, ) -> Result<()>
Executes an operation of the IP_ADD_MEMBERSHIP
type.
This function specifies a new multicast group for this socket to join.
The address must be a valid multicast address, and interface
is the
address of the local interface with which the system should join the
multicast group. If it’s equal to INADDR_ANY
then an appropriate
interface is chosen by the system.
Sourcefn join_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32) -> Result<()>
fn join_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32) -> Result<()>
Executes an operation of the IPV6_ADD_MEMBERSHIP
type.
This function specifies a new multicast group for this socket to join.
The address must be a valid multicast address, and interface
is the
index of the interface to join/leave (or 0 to indicate any interface).
Sourcefn leave_multicast_v4(
&self,
multiaddr: Ipv4Addr,
interface: Ipv4Addr,
) -> Result<()>
fn leave_multicast_v4( &self, multiaddr: Ipv4Addr, interface: Ipv4Addr, ) -> Result<()>
Executes an operation of the IP_DROP_MEMBERSHIP
type.
For more information about this option, see join_multicast_v4
.
Sourcefn leave_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32) -> Result<()>
fn leave_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32) -> Result<()>
Executes an operation of the IPV6_DROP_MEMBERSHIP
type.
For more information about this option, see join_multicast_v6
.
Sourcefn multicast_loop_v4(&self) -> Result<bool>
fn multicast_loop_v4(&self) -> Result<bool>
Gets the value of the IP_MULTICAST_LOOP
option for this socket.
For more information about this option, see set_multicast_loop_v4
.
Sourcefn set_multicast_loop_v4(&self, on: bool) -> Result<()>
fn set_multicast_loop_v4(&self, on: bool) -> Result<()>
Sets the value of the IP_MULTICAST_LOOP
option for this socket.
If enabled, multicast packets will be looped back to the local socket.
§Note
This may not have any affect on IPv6 sockets.
Sourcefn multicast_ttl_v4(&self) -> Result<u32>
fn multicast_ttl_v4(&self) -> Result<u32>
Gets the value of the IP_MULTICAST_TTL
option for this socket.
For more information about this option, see set_multicast_ttl_v4
.
Sourcefn set_multicast_ttl_v4(&self, ttl: u32) -> Result<()>
fn set_multicast_ttl_v4(&self, ttl: u32) -> Result<()>
Sets the value of the IP_MULTICAST_TTL
option for this socket.
Indicates the time-to-live value of outgoing multicast packets for this socket. The default value is 1 which means that multicast packets don’t leave the local network unless explicitly requested.
§Note
This may not have any affect on IPv6 sockets.
Sourcefn multicast_loop_v6(&self) -> Result<bool>
fn multicast_loop_v6(&self) -> Result<bool>
Gets the value of the IPV6_MULTICAST_LOOP
option for this socket.
For more information about this option, see set_multicast_loop_v6
.
Sourcefn set_multicast_loop_v6(&self, on: bool) -> Result<()>
fn set_multicast_loop_v6(&self, on: bool) -> Result<()>
Sets the value of the IPV6_MULTICAST_LOOP
option for this socket.
Controls whether this socket sees the multicast packets it sends itself.
§Note
This may not have any affect on IPv4 sockets.
Sourcefn set_broadcast(&self, broadcast: bool) -> Result<()>
fn set_broadcast(&self, broadcast: bool) -> Result<()>
Sets the broadcast flag for this UDP socket.
Sourcefn poll_recv_from(
&self,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<Result<(usize, SocketAddr)>>
fn poll_recv_from( &self, cx: &mut Context<'_>, buf: &mut [u8], ) -> Poll<Result<(usize, SocketAddr)>>
Attempts to receive a single datagram on the socket.
Note that on multiple calls to a poll_*
method in the recv direction, only the
Waker
from the Context
passed to the most recent call will be scheduled to
receive a wakeup.
§Return value
The function returns:
Poll::Pending
if the socket is not ready to readPoll::Ready(Ok(addr))
reads data fromaddr
intoReadBuf
if the socket is readyPoll::Ready(Err(e))
if an error is encountered.
§Errors
This function may encounter any standard I/O error except WouldBlock
.
§Notes
Note that the socket address cannot be implicitly trusted, because it is relatively trivial to send a UDP datagram with a spoofed origin in a packet injection attack. Because UDP is stateless and does not validate the origin of a packet, the attacker does not need to be able to intercept traffic in order to interfere. It is important to be aware of this when designing your application-level protocol.
Sourcefn poll_send_to(
&self,
cx: &mut Context<'_>,
buf: &[u8],
target: SocketAddr,
) -> Poll<Result<usize>>
fn poll_send_to( &self, cx: &mut Context<'_>, buf: &[u8], target: SocketAddr, ) -> Poll<Result<usize>>
Attempts to send data on the socket to a given address.
Note that on multiple calls to a poll_*
method in the send direction, only the
Waker
from the Context
passed to the most recent call will be scheduled to
receive a wakeup.
§Return value
The function returns:
Poll::Pending
if the socket is not ready to writePoll::Ready(Ok(n))
n
is the number of bytes sent.Poll::Ready(Err(e))
if an error is encountered.
§Errors
This function may encounter any standard I/O error except WouldBlock
.
Provided Methods§
Sourcefn try_clone(&self) -> Result<Self>
fn try_clone(&self) -> Result<Self>
Creates a new independently owned handle to the underlying socket.
The returned UdpSocket
is a reference to the same socket that this
object references. Both handles will read and write the same port, and
options set on one socket will be propagated to the other.
Sourcefn set_recv_buffer_size(&self, size: usize) -> Result<()>
fn set_recv_buffer_size(&self, size: usize) -> Result<()>
Set value for the SO_RCVBUF
option on this socket.
Changes the size of the operating system’s receive buffer associated with the socket.
Sourcefn recv_buffer_size(&self) -> Result<usize>
fn recv_buffer_size(&self) -> Result<usize>
Get value for the SO_RCVBUF
option on this socket.
For more information about this option, see set_recv_buffer_size
.
Sourcefn set_send_buffer_size(&self, size: usize) -> Result<()>
fn set_send_buffer_size(&self, size: usize) -> Result<()>
Set value for the SO_SNDBUF
option on this socket.
Changes the size of the operating system’s send buffer associated with the socket.
Sourcefn send_buffer_size(&self) -> Result<usize>
fn send_buffer_size(&self) -> Result<usize>
Get the value of the SO_SNDBUF
option on this socket.
For more information about this option, see set_send_buffer_size
.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.