From e43929d52795a047942a5c14281707bf8c69d5c4 Mon Sep 17 00:00:00 2001 From: bold Date: Mon, 27 Jan 2020 06:27:42 +0100 Subject: replace tcp stream accessor with std traits to obtain raw fds --- src/session.rs | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/session.rs b/src/session.rs index 6364020..21a137b 100644 --- a/src/session.rs +++ b/src/session.rs @@ -298,22 +298,6 @@ impl Session { let _ = inner.tcp.replace(Box::new(stream)); } - /// Returns the raw fd of the stream that was associated with the Session by - /// the Session::handshake method. - #[cfg(unix)] - pub fn tcp_stream(&self) -> Option { - let inner = self.inner(); - inner.tcp.as_ref().map(|tcp| tcp.as_raw_fd()) - } - - /// Returns the raw socket of the stream that was associated with the - /// Session by the Session::handshake method. - #[cfg(windows)] - pub fn tcp_stream(&self) -> Option { - let inner = self.inner(); - inner.tcp.as_ref().map(|tcp| tcp.as_raw_socket()) - } - /// Attempt basic password authentication. /// /// Note that many SSH servers which appear to support ordinary password @@ -1020,6 +1004,28 @@ impl Session { } } +#[cfg(unix)] +impl AsRawFd for Session { + fn as_raw_fd(&self) -> RawFd { + let inner = self.inner(); + match inner.tcp.as_ref() { + Some(tcp) => tcp.as_raw_fd(), + None => panic!("tried to obtain raw fd without tcp stream set"), + } + } +} + +#[cfg(windows)] +impl AsRawSocket for Session { + fn as_raw_socket(&self) -> RawSocket { + let inner = self.inner(); + match inner.tcp.as_ref() { + Some(tcp) => tcp.as_raw_socket(), + None => panic!("tried to obtain raw socket without tcp stream set"), + } + } +} + impl SessionInner { /// Translate a return code into a Rust-`Result`. pub fn rc(&self, rc: c_int) -> Result<(), Error> { -- cgit v1.2.3