Repository: tokio-rs/tokio
File: tokio/src/net/udp.rs (Rust)

Code before:
        self.io.multicast_loop_v6()
    }

    /// 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.
    pub fn set_multicast_loop_v6(&self, on: bool) -> io::Result<()> {
        self.io.set_multicast_loop_v6(on)
    }

    /// Gets the value of the `IP_TTL` option for this socket.
    ///
    /// For more information about this option, see [`set_ttl`].
    ///
    /// [`set_ttl`]: method@Self::set_ttl
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use tokio::net::UdpSocket;
    /// # use std::io;
    ///
    /// # async fn dox() -> io::Result<()> {
    /// let sock = UdpSocket::bind("127.0.0.1:8080").await?;
    ///
    /// println!("{:?}", sock.ttl()?);
    /// # Ok(())
    /// # }
    /// ```
    pub fn ttl(&self) -> io::Result<u32> {
        self.io.ttl()
    }

    /// Sets the value for the `IP_TTL` option on this socket.
    ///
    /// This value sets the time-to-live field that is used in every packet sent
    /// from this socket.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use tokio::net::UdpSocket;
    /// # use std::io;
    ///
    /// # async fn dox() -> io::Result<()> {
    /// let sock = UdpSocket::bind("127.0.0.1:8080").await?;
    /// sock.set_ttl(60)?;

Review comment (suggestion):
I understand if there's concerns about adding too many methods to `UdpSocket` at once. 

I believe the only one I need to support my use-case is this one, which will be paired with `ready()` and `try_send_to()`.

However, `try_peek_from()` is conspicuously absent from the API so the presence of this method might be confusing, especially if it's the only one.

Code after:
    /// 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.
    ///
    /// [`poll_peek_from`]: method@Self::poll_peek_from
    /// [packet injection attack]: https://en.wikipedia.org/wiki/Packet_injection
    pub fn poll_peek_sender(&self, cx: &mut Context<'_>) -> Poll<io::Result<SocketAddr>> {
        self.io
            .registration()
            .poll_read_io(cx, || self.peek_sender_inner())
    }

    /// Try to retrieve the sender of the data at the head of the input queue.
    ///
    /// When there is no pending data, `Err(io::ErrorKind::WouldBlock)` is
    /// returned. This function is usually paired with `readable()`.
    ///
    /// 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.
    ///
    /// [packet injection attack]: https://en.wikipedia.org/wiki/Packet_injection
    pub fn try_peek_sender(&self) -> io::Result<SocketAddr> {
        self.io
            .registration()
            .try_io(Interest::READABLE, || self.peek_sender_inner())
    }

    #[inline]
    fn peek_sender_inner(&self) -> io::Result<SocketAddr> {
        self.io.try_io(|| {
            self.as_socket()
                .peek_sender()?
                // May be `None` if the platform doesn't populate the sender for some reason.
                // In testing, that only occurred on macOS if you pass a zero-sized buffer,
                // but the implementation of `Socket::peek_sender()` covers that.
                .as_socket()
                .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "sender not available"))
        })
    }

    /// Gets the value of the `SO_BROADCAST` option for this socket.
    ///
    /// For more information about this option, see [`set_broadcast`].
    ///
    /// [`set_broadcast`]: method@Self::set_broadcast
    pub fn broadcast(&self) -> io::Result<bool> {
        self.io.broadcast()