From d29b2f7a9fcc6908f5f1e48af15e057d77b3e2ee Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Tue, 23 Jul 2019 17:52:31 -0700 Subject: Agent no longer borrows session either --- src/agent.rs | 43 ++++++++++++++++++++++--------------------- src/session.rs | 2 +- 2 files changed, 23 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/agent.rs b/src/agent.rs index b1f35f5..ad1b863 100644 --- a/src/agent.rs +++ b/src/agent.rs @@ -1,23 +1,24 @@ use std::ffi::CString; use std::marker; +use std::rc::Rc; use std::slice; use std::str; -use util::{Binding, SessionBinding}; -use {raw, Error, Session}; +use util::Binding; +use {raw, Error, SessionInner}; /// A structure representing a connection to an SSH agent. /// /// Agents can be used to authenticate a session. -pub struct Agent<'sess> { +pub struct Agent { raw: *mut raw::LIBSSH2_AGENT, - sess: &'sess Session, + sess: Rc, } /// An iterator over the identities found in an SSH agent. pub struct Identities<'agent> { prev: *mut raw::libssh2_agent_publickey, - agent: &'agent Agent<'agent>, + agent: &'agent Agent, } /// A public key which is extracted from an SSH agent. @@ -26,7 +27,21 @@ pub struct PublicKey<'agent> { _marker: marker::PhantomData<&'agent [u8]>, } -impl<'sess> Agent<'sess> { +impl Agent { + pub(crate) fn from_raw_opt( + raw: *mut raw::LIBSSH2_AGENT, + sess: &Rc, + ) -> Result { + if raw.is_null() { + Err(Error::last_error_raw(sess.raw).unwrap_or_else(Error::unknown)) + } else { + Ok(Self { + raw, + sess: Rc::clone(sess), + }) + } + } + /// Connect to an ssh-agent running on the system. pub fn connect(&mut self) -> Result<(), Error> { unsafe { self.sess.rc(raw::libssh2_agent_connect(self.raw)) } @@ -66,21 +81,7 @@ impl<'sess> Agent<'sess> { } } -impl<'sess> SessionBinding<'sess> for Agent<'sess> { - type Raw = raw::LIBSSH2_AGENT; - - unsafe fn from_raw(sess: &'sess Session, raw: *mut raw::LIBSSH2_AGENT) -> Agent<'sess> { - Agent { - raw: raw, - sess: sess, - } - } - fn raw(&self) -> *mut raw::LIBSSH2_AGENT { - self.raw - } -} - -impl<'a> Drop for Agent<'a> { +impl Drop for Agent { fn drop(&mut self) { unsafe { raw::libssh2_agent_free(self.raw) } } diff --git a/src/session.rs b/src/session.rs index 47329aa..7228e6e 100644 --- a/src/session.rs +++ b/src/session.rs @@ -429,7 +429,7 @@ impl Session { /// /// The returned agent will still need to be connected manually before use. pub fn agent(&self) -> Result { - unsafe { SessionBinding::from_raw_opt(self, raw::libssh2_agent_init(self.inner.raw)) } + unsafe { Agent::from_raw_opt(raw::libssh2_agent_init(self.inner.raw), &self.inner) } } /// Init a collection of known hosts for this session. -- cgit v1.2.3