Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make NetConnection local and remote address failable #39

Merged
merged 2 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ pub trait NetConnection: NetStream + AsRawFd + Debug {

fn shutdown(&mut self, how: Shutdown) -> io::Result<()>;

fn remote_addr(&self) -> Self::Addr;
fn local_addr(&self) -> Self::Addr;
fn remote_addr(&self) -> io::Result<Self::Addr>;
fn local_addr(&self) -> io::Result<Self::Addr>;

fn set_read_timeout(&mut self, dur: Option<Duration>) -> io::Result<()>;
fn set_write_timeout(&mut self, dur: Option<Duration>) -> io::Result<()>;
Expand Down Expand Up @@ -104,13 +104,9 @@ impl NetConnection for TcpStream {

fn shutdown(&mut self, how: Shutdown) -> io::Result<()> { TcpStream::shutdown(self, how) }

fn remote_addr(&self) -> Self::Addr {
TcpStream::peer_addr(self).expect("TCP stream doesn't know remote peer address").into()
}
fn remote_addr(&self) -> io::Result<Self::Addr> { Ok(TcpStream::peer_addr(self)?.into()) }

fn local_addr(&self) -> Self::Addr {
TcpStream::local_addr(self).expect("TCP stream doesn't has local address").into()
}
fn local_addr(&self) -> io::Result<Self::Addr> { Ok(TcpStream::local_addr(self)?.into()) }

fn set_read_timeout(&mut self, dur: Option<Duration>) -> io::Result<()> {
TcpStream::set_read_timeout(self, dur)
Expand Down Expand Up @@ -229,20 +225,18 @@ impl NetConnection for socket2::Socket {

fn shutdown(&mut self, how: Shutdown) -> io::Result<()> { socket2::Socket::shutdown(self, how) }

fn remote_addr(&self) -> Self::Addr {
socket2::Socket::peer_addr(self)
.expect("net stream must use only connections")
fn remote_addr(&self) -> io::Result<Self::Addr> {
Ok(socket2::Socket::peer_addr(self)?
.as_socket()
.expect("net stream must use only connections")
.into()
.ok_or::<io::Error>(io::ErrorKind::NotFound.into())?
.into())
}

fn local_addr(&self) -> Self::Addr {
socket2::Socket::local_addr(self)
.expect("net stream doesn't has local socket")
fn local_addr(&self) -> io::Result<Self::Addr> {
Ok(socket2::Socket::local_addr(self)?
.as_socket()
.expect("net stream doesn't has local socket")
.into()
.ok_or::<io::Error>(io::ErrorKind::NotFound.into())?
.into())
}

fn set_read_timeout(&mut self, dur: Option<Duration>) -> io::Result<()> {
Expand Down
4 changes: 2 additions & 2 deletions src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ impl<S: NetSession> NetTransport<S> {
pub fn is_outbound(&self) -> bool { self.link_direction() == Direction::Outbound }
pub fn link_direction(&self) -> Direction { self.link_direction }

pub fn local_addr(&self) -> <S::Connection as NetConnection>::Addr {
pub fn local_addr(&self) -> io::Result<<S::Connection as NetConnection>::Addr> {
self.session.as_connection().local_addr()
}

Expand Down Expand Up @@ -497,7 +497,7 @@ impl<S: NetSession> Write for NetTransport<S> {

fn flush(&mut self) -> io::Result<()> {
let res = self.flush_buffer();
self.session.flush().and_then(|_| res)
self.session.flush().and(res)
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,16 @@ impl<I: EcSign, D: Digest> CypherSession<I, D> {
cert: Cert<I::Sig>,
allowed_ids: Vec<I::Pk>,
signer: I,
) -> Self {
Self::with_config::<HASHLEN>(
connection.remote_addr().into(),
) -> io::Result<Self> {
Ok(Self::with_config::<HASHLEN>(
connection.remote_addr()?.into(),
connection,
Direction::Inbound,
cert,
allowed_ids,
signer,
false,
)
))
}

fn with_config<const HASHLEN: usize>(
Expand Down Expand Up @@ -610,9 +610,9 @@ mod impl_noise {
pub fn to_vec(&self) -> Vec<u8> {
let mut vec = Vec::<u8>::with_capacity(D::OUTPUT_LEN + E::Pk::COMPRESSED_LEN);
vec.extend_from_slice(self.handshake_hash.as_ref());
self.remote_static_key
.as_ref()
.map(|pk| vec.extend_from_slice(pk.to_pk_compressed().as_ref()));
if let Some(pk) = self.remote_static_key.as_ref() {
vec.extend_from_slice(pk.to_pk_compressed().as_ref())
}
vec
}
}
Expand Down