runtime_raw/
udp.rs

1use std::fmt::Debug;
2use std::io;
3use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr};
4use std::pin::Pin;
5use std::task::{Context, Poll};
6
7/// A UDP socket.
8pub trait UdpSocket: Debug + Send + Sync {
9    /// Returns the local address that this listener is bound to.
10    ///
11    /// This can be useful, for example, when binding to port 0 to figure out
12    /// which port was actually bound.
13    fn local_addr(&self) -> io::Result<SocketAddr>;
14
15    /// Sends data on the IO interface to the specified target.
16    ///
17    /// On success, returns the number of bytes written.
18    fn poll_send_to(
19        self: Pin<&mut Self>,
20        cx: &mut Context<'_>,
21        buf: &[u8],
22        receiver: &SocketAddr,
23    ) -> Poll<io::Result<usize>>;
24
25    /// Receives data from the IO interface.
26    ///
27    /// On success, returns the number of bytes read and the target from whence
28    /// the data came.
29    fn poll_recv_from(
30        self: Pin<&mut Self>,
31        cx: &mut Context<'_>,
32        buf: &mut [u8],
33    ) -> Poll<io::Result<(usize, SocketAddr)>>;
34
35    /// Gets the value of the `SO_BROADCAST` option for this socket.
36    fn broadcast(&self) -> io::Result<bool>;
37
38    /// Sets the value of the `SO_BROADCAST` option for this socket.
39    fn set_broadcast(&self, on: bool) -> io::Result<()>;
40
41    /// Gets the value of the `IP_MULTICAST_LOOP` option for this socket.
42    fn multicast_loop_v4(&self) -> io::Result<bool>;
43
44    /// Sets the value of the `IP_MULTICAST_LOOP` option for this socket.
45    fn set_multicast_loop_v4(&self, on: bool) -> io::Result<()>;
46
47    /// Gets the value of the `IP_MULTICAST_TTL` option for this socket.
48    fn multicast_ttl_v4(&self) -> io::Result<u32>;
49
50    /// Sets the value of the `IP_MULTICAST_TTL` option for this socket.
51    fn set_multicast_ttl_v4(&self, ttl: u32) -> io::Result<()>;
52
53    /// Gets the value of the `IPV6_MULTICAST_LOOP` option for this socket.
54    fn multicast_loop_v6(&self) -> io::Result<bool>;
55
56    /// Sets the value of the `IPV6_MULTICAST_LOOP` option for this socket.
57    fn set_multicast_loop_v6(&self, on: bool) -> io::Result<()>;
58
59    /// Gets the value of the `IP_TTL` option for this socket.
60    fn ttl(&self) -> io::Result<u32>;
61
62    /// Sets the value for the `IP_TTL` option on this socket.
63    fn set_ttl(&self, ttl: u32) -> io::Result<()>;
64
65    /// Executes an operation of the `IP_ADD_MEMBERSHIP` type.
66    fn join_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr) -> io::Result<()>;
67
68    /// Executes an operation of the `IPV6_ADD_MEMBERSHIP` type.
69    fn join_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32) -> io::Result<()>;
70
71    /// Executes an operation of the `IP_DROP_MEMBERSHIP` type.
72    fn leave_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr) -> io::Result<()>;
73
74    /// Executes an operation of the `IPV6_DROP_MEMBERSHIP` type.
75    fn leave_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32) -> io::Result<()>;
76
77    /// Extracts the raw file descriptor.
78    #[cfg(unix)]
79    fn as_raw_fd(&self) -> std::os::unix::io::RawFd;
80}