From c8f08fe94cd2eeeeb620ae016357c713c8f471be Mon Sep 17 00:00:00 2001 From: Pi Lanningham Date: Thu, 9 Feb 2023 21:58:08 -0500 Subject: [PATCH] Return the remote socket address when accepting a connection (#227) feat!: Return the socket address when accepting a connection BREAKING CHANGE: The signature for Bearer.accept_tcp now returns the bearer, and the address that connected. This can, for example, be used to implement allow and deny lists for accepting or rejecting incoming connections. * Return the remote address from accept_unix * cargo fmt * Fix comment formatting --- pallas-crypto/src/key/ed25519.rs | 11 ++++++----- pallas-multiplexer/src/bearers.rs | 6 +++--- pallas-multiplexer/tests/integration.rs | 2 +- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/pallas-crypto/src/key/ed25519.rs b/pallas-crypto/src/key/ed25519.rs index e40d2fd..2af0e83 100644 --- a/pallas-crypto/src/key/ed25519.rs +++ b/pallas-crypto/src/key/ed25519.rs @@ -460,13 +460,14 @@ mod tests { message: Vec, ) -> bool { // NOTE: this test may fail but it is impossible to see this happening in normal - // condition. we are generating 32 random bytes of public key and - // 64 random bytes of signature with an randomly generated message - // of a random number of bytes in. If the message were empty, the - // probability to have a signature that matches the verify key + // condition. We are generating 32 random bytes of public key and + // 64 random bytes of signature with an randomly generated message + // of a random number of bytes in. If the message were empty, the + // probability to have a signature that matches the verify key + // would still be 1 out of 2^96. // - // if this test fails and it is not a bug, go buy a lottery ticket. + // if this test fails and it is not a bug, go buy a lottery ticket. !public_key.verify(message, &signature) } diff --git a/pallas-multiplexer/src/bearers.rs b/pallas-multiplexer/src/bearers.rs index ef5ddc1..64d1d20 100644 --- a/pallas-multiplexer/src/bearers.rs +++ b/pallas-multiplexer/src/bearers.rs @@ -123,11 +123,11 @@ impl Bearer { Ok(Bearer::Tcp(bearer)) } - pub fn accept_tcp(server: TcpListener) -> Result { - let (bearer, _) = server.accept().unwrap(); + pub fn accept_tcp(server: TcpListener) -> Result<(Self, SocketAddr), std::io::Error> { + let (bearer, remote_addr) = server.accept().unwrap(); bearer.set_nodelay(true)?; - Ok(Bearer::Tcp(bearer)) + Ok((Bearer::Tcp(bearer), remote_addr)) } #[cfg(target_family = "unix")] diff --git a/pallas-multiplexer/tests/integration.rs b/pallas-multiplexer/tests/integration.rs index e309dd2..bfec2b8 100644 --- a/pallas-multiplexer/tests/integration.rs +++ b/pallas-multiplexer/tests/integration.rs @@ -12,7 +12,7 @@ fn setup_passive_muxer() -> JoinHandle { let server = TcpListener::bind(SocketAddrV4::new(Ipv4Addr::LOCALHOST, P)).unwrap(); info!("listening for connections on port {}", P); - let bearer = Bearer::accept_tcp(server).unwrap(); + let (bearer, _) = Bearer::accept_tcp(server).unwrap(); StdPlexer::new(bearer) })