Commit 2aa5f45b authored by Nick Mathewson's avatar Nick Mathewson 🤹
Browse files

UdpSocket: remove support for connect().

Currently, Arti doesn't need this.  But once it does, it will be
way better to have a separate type for connected sockets, rather
than having to error-check every time somebody gives us a socket.

Part of #410
parent 0fdbe701
Loading
Loading
Loading
Loading
+3 −20
Original line number Diff line number Diff line
@@ -117,7 +117,7 @@ mod net {
        async fn bind(&self, addr: &std::net::SocketAddr) -> IoResult<Self::UdpSocket> {
            StdUdpSocket::bind(*addr)
                .await
                .map(|socket| UdpSocket { socket, addr: None })
                .map(|socket| UdpSocket { socket })
        }
    }

@@ -125,38 +125,21 @@ mod net {
    pub struct UdpSocket {
        /// The underlying UdpSocket
        socket: StdUdpSocket,
        /// The remote address if the socket is connected
        addr: Option<SocketAddr>,
    }

    #[async_trait]
    impl traits::UdpSocket for UdpSocket {
        async fn recv(&self, buf: &mut [u8]) -> IoResult<(usize, SocketAddr)> {
            if let Some(addr) = self.addr {
                self.socket.recv(buf).await.map(|r| (r, addr))
            } else {
            self.socket.recv_from(buf).await
        }
        }

        async fn send(&self, buf: &[u8], target: &SocketAddr) -> IoResult<usize> {
            if let Some(addr) = self.addr {
                debug_assert!(addr == *target);
                self.socket.send(buf).await
            } else {
            self.socket.send_to(buf, target).await
        }
        }

        fn local_addr(&self) -> IoResult<SocketAddr> {
            self.socket.local_addr()
        }

        async fn connect(&mut self, addr: &SocketAddr) -> IoResult<()> {
            self.socket.connect(addr).await?;
            self.addr = Some(*addr);
            Ok(())
        }
    }
}

+3 −20
Original line number Diff line number Diff line
@@ -103,8 +103,6 @@ pub(crate) mod net {
    pub struct UdpSocket {
        /// The underelying UdpSocket
        socket: TokioUdpSocket,
        /// The remote address if the socket is connected
        addr: Option<SocketAddr>,
    }

    impl UdpSocket {
@@ -112,38 +110,23 @@ pub(crate) mod net {
        pub async fn bind(addr: SocketAddr) -> IoResult<Self> {
            TokioUdpSocket::bind(addr)
                .await
                .map(|socket| UdpSocket { socket, addr: None })
                .map(|socket| UdpSocket { socket })
        }
    }

    #[async_trait]
    impl traits::UdpSocket for UdpSocket {
        async fn recv(&self, buf: &mut [u8]) -> IoResult<(usize, SocketAddr)> {
            if let Some(addr) = self.addr {
                self.socket.recv(buf).await.map(|r| (r, addr))
            } else {
            self.socket.recv_from(buf).await
        }
        }

        async fn send(&self, buf: &[u8], target: &SocketAddr) -> IoResult<usize> {
            if let Some(addr) = self.addr {
                debug_assert!(addr == *target);
                self.socket.send(buf).await
            } else {
            self.socket.send_to(buf, target).await
        }
        }

        fn local_addr(&self) -> IoResult<SocketAddr> {
            self.socket.local_addr()
        }

        async fn connect(&mut self, addr: &SocketAddr) -> IoResult<()> {
            self.socket.connect(addr).await?;
            self.addr = Some(*addr);
            Ok(())
        }
    }
}

+4 −7
Original line number Diff line number Diff line
@@ -195,6 +195,10 @@ pub trait UdpProvider {
/// Trait for a localy bound Udp socket that can send and receive datagrams.
///
/// These objects are returned by instances of [`UdpProvider`].
//
// NOTE that UdpSocket objects are _necessarily_ un-connected.  If you need to
// implement a connected Udp socket in the future, please make a new trait (and
// a new type.)
#[async_trait]
pub trait UdpSocket {
    /// Wait for an incoming datagram; return it along its address.
@@ -203,13 +207,6 @@ pub trait UdpSocket {
    async fn send(&self, buf: &[u8], target: &SocketAddr) -> IoResult<usize>;
    /// Return the local address that this socket is bound to.
    fn local_addr(&self) -> IoResult<SocketAddr>;
    /// Connect to a remote address. After calling this [`UdpSocket::recv`] may only
    /// return that same address, and the target provided to [`UdpSocket::send`] must
    /// be this address.
    // rationale for taking &mut self: this changes the behavior of the whole socket,
    // so it should probably only be used when you have ownership of the socket and
    // not when sharing it.
    async fn connect(&mut self, addr: &SocketAddr) -> IoResult<()>;
}

/// An object with a peer certificate: typically a TLS connection.