Skip to content

Commit 82fa52d

Browse files
authored
Merge pull request #39 from cyphernet-dao/peer
Make NetConnection local and remote address failable
2 parents 6394e80 + c12f11e commit 82fa52d

File tree

3 files changed

+21
-27
lines changed

3 files changed

+21
-27
lines changed

src/connection.rs

+12-18
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ pub trait NetConnection: NetStream + AsRawFd + Debug {
5959

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

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

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

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

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

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

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

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

232-
fn remote_addr(&self) -> Self::Addr {
233-
socket2::Socket::peer_addr(self)
234-
.expect("net stream must use only connections")
228+
fn remote_addr(&self) -> io::Result<Self::Addr> {
229+
Ok(socket2::Socket::peer_addr(self)?
235230
.as_socket()
236-
.expect("net stream must use only connections")
237-
.into()
231+
.ok_or::<io::Error>(io::ErrorKind::NotFound.into())?
232+
.into())
238233
}
239234

240-
fn local_addr(&self) -> Self::Addr {
241-
socket2::Socket::local_addr(self)
242-
.expect("net stream doesn't has local socket")
235+
fn local_addr(&self) -> io::Result<Self::Addr> {
236+
Ok(socket2::Socket::local_addr(self)?
243237
.as_socket()
244-
.expect("net stream doesn't has local socket")
245-
.into()
238+
.ok_or::<io::Error>(io::ErrorKind::NotFound.into())?
239+
.into())
246240
}
247241

248242
fn set_read_timeout(&mut self, dur: Option<Duration>) -> io::Result<()> {

src/resource.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ impl<S: NetSession> NetTransport<S> {
312312
pub fn is_outbound(&self) -> bool { self.link_direction() == Direction::Outbound }
313313
pub fn link_direction(&self) -> Direction { self.link_direction }
314314

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

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

498498
fn flush(&mut self) -> io::Result<()> {
499499
let res = self.flush_buffer();
500-
self.session.flush().and_then(|_| res)
500+
self.session.flush().and(res)
501501
}
502502
}
503503

src/session.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,16 @@ impl<I: EcSign, D: Digest> CypherSession<I, D> {
150150
cert: Cert<I::Sig>,
151151
allowed_ids: Vec<I::Pk>,
152152
signer: I,
153-
) -> Self {
154-
Self::with_config::<HASHLEN>(
155-
connection.remote_addr().into(),
153+
) -> io::Result<Self> {
154+
Ok(Self::with_config::<HASHLEN>(
155+
connection.remote_addr()?.into(),
156156
connection,
157157
Direction::Inbound,
158158
cert,
159159
allowed_ids,
160160
signer,
161161
false,
162-
)
162+
))
163163
}
164164

165165
fn with_config<const HASHLEN: usize>(
@@ -610,9 +610,9 @@ mod impl_noise {
610610
pub fn to_vec(&self) -> Vec<u8> {
611611
let mut vec = Vec::<u8>::with_capacity(D::OUTPUT_LEN + E::Pk::COMPRESSED_LEN);
612612
vec.extend_from_slice(self.handshake_hash.as_ref());
613-
self.remote_static_key
614-
.as_ref()
615-
.map(|pk| vec.extend_from_slice(pk.to_pk_compressed().as_ref()));
613+
if let Some(pk) = self.remote_static_key.as_ref() {
614+
vec.extend_from_slice(pk.to_pk_compressed().as_ref())
615+
}
616616
vec
617617
}
618618
}

0 commit comments

Comments
 (0)