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
This commit is contained in:
Pi Lanningham 2023-02-09 21:58:08 -05:00 committed by GitHub
parent 2e24dc53dc
commit c8f08fe94c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 9 deletions

View file

@ -460,13 +460,14 @@ mod tests {
message: Vec<u8>,
) -> 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)
}

View file

@ -123,11 +123,11 @@ impl Bearer {
Ok(Bearer::Tcp(bearer))
}
pub fn accept_tcp(server: TcpListener) -> Result<Self, std::io::Error> {
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")]

View file

@ -12,7 +12,7 @@ fn setup_passive_muxer<const P: u16>() -> JoinHandle<StdPlexer> {
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)
})