diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/knownhosts.rs | 46 | ||||
-rw-r--r-- | src/session.rs | 4 |
2 files changed, 24 insertions, 26 deletions
diff --git a/src/knownhosts.rs b/src/knownhosts.rs index 737ff52..d2e507e 100644 --- a/src/knownhosts.rs +++ b/src/knownhosts.rs @@ -2,10 +2,11 @@ use libc::{c_int, size_t}; use std::ffi::CString; use std::marker; use std::path::Path; +use std::rc::Rc; use std::str; -use util::{self, Binding, SessionBinding}; -use {raw, CheckResult, Error, KnownHostFileKind, Session}; +use util::{self, Binding}; +use {raw, CheckResult, Error, KnownHostFileKind, SessionInner}; /// A set of known hosts which can be used to verify the identity of a remote /// server. @@ -43,15 +44,15 @@ use {raw, CheckResult, Error, KnownHostFileKind, Session}; /// known_hosts.write_file(&file, KnownHostFileKind::OpenSSH).unwrap(); /// } /// ``` -pub struct KnownHosts<'sess> { +pub struct KnownHosts { raw: *mut raw::LIBSSH2_KNOWNHOSTS, - sess: &'sess Session, + sess: Rc<SessionInner>, } /// Iterator over the hosts in a `KnownHosts` structure. pub struct Hosts<'kh> { prev: *mut raw::libssh2_knownhost, - hosts: &'kh KnownHosts<'kh>, + hosts: &'kh KnownHosts, } /// Structure representing a known host as part of a `KnownHosts` structure. @@ -60,7 +61,21 @@ pub struct Host<'kh> { _marker: marker::PhantomData<&'kh str>, } -impl<'sess> KnownHosts<'sess> { +impl KnownHosts { + pub(crate) fn from_raw_opt( + raw: *mut raw::LIBSSH2_KNOWNHOSTS, + sess: &Rc<SessionInner>, + ) -> Result<Self, Error> { + if raw.is_null() { + Err(Error::last_error_raw(sess.raw).unwrap_or_else(Error::unknown)) + } else { + Ok(Self { + raw, + sess: Rc::clone(sess), + }) + } + } + /// Reads a collection of known hosts from a specified file and adds them to /// the collection of known hosts. pub fn read_file(&mut self, file: &Path, kind: KnownHostFileKind) -> Result<u32, Error> { @@ -207,24 +222,7 @@ impl<'sess> KnownHosts<'sess> { } } -impl<'sess> SessionBinding<'sess> for KnownHosts<'sess> { - type Raw = raw::LIBSSH2_KNOWNHOSTS; - - unsafe fn from_raw( - sess: &'sess Session, - raw: *mut raw::LIBSSH2_KNOWNHOSTS, - ) -> KnownHosts<'sess> { - KnownHosts { - raw: raw, - sess: sess, - } - } - fn raw(&self) -> *mut raw::LIBSSH2_KNOWNHOSTS { - self.raw - } -} - -impl<'sess> Drop for KnownHosts<'sess> { +impl Drop for KnownHosts { fn drop(&mut self) { unsafe { raw::libssh2_knownhost_free(self.raw) } } diff --git a/src/session.rs b/src/session.rs index 7228e6e..edc6898 100644 --- a/src/session.rs +++ b/src/session.rs @@ -10,7 +10,7 @@ use std::rc::Rc; use std::slice; use std::str; -use util::{self, SessionBinding}; +use util; use {raw, ByApplication, DisconnectCode, Error, HostKeyType}; use {Agent, Channel, HashType, KnownHosts, Listener, MethodType, Sftp}; @@ -439,7 +439,7 @@ impl Session { pub fn known_hosts(&self) -> Result<KnownHosts, Error> { unsafe { let ptr = raw::libssh2_knownhost_init(self.inner.raw); - SessionBinding::from_raw_opt(self, ptr) + KnownHosts::from_raw_opt(ptr, &self.inner) } } |