summaryrefslogtreecommitdiff
path: root/src/listener.rs
blob: e0dc315234de11e59a79e14d0322180bce81ec8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use parking_lot::Mutex;
use std::sync::Arc;
use {raw, Channel, Error, SessionInner};

/// A listener represents a forwarding port from the remote server.
///
/// New channels can be accepted from a listener which represent connections on
/// the remote server's port.
pub struct Listener {
    raw: *mut raw::LIBSSH2_LISTENER,
    sess: Arc<Mutex<SessionInner>>,
}

// Listener is both Send and Sync; the compiler can't see it because it
// is pessimistic about the raw pointer.  We use Arc/Mutex to guard accessing
// the raw pointer so we are safe for both.
unsafe impl Send for Listener {}
unsafe impl Sync for Listener {}

impl Listener {
    /// Accept a queued connection from this listener.
    pub fn accept(&mut self) -> Result<Channel, Error> {
        let sess = self.sess.lock();
        unsafe {
            let chan = raw::libssh2_channel_forward_accept(self.raw);
            let err = sess.last_error();
            Channel::from_raw_opt(chan, err, &self.sess)
        }
    }

    pub(crate) fn from_raw_opt(
        raw: *mut raw::LIBSSH2_LISTENER,
        err: Option<Error>,
        sess: &Arc<Mutex<SessionInner>>,
    ) -> Result<Self, Error> {
        if raw.is_null() {
            Err(err.unwrap_or_else(Error::unknown))
        } else {
            Ok(Self {
                raw,
                sess: Arc::clone(sess),
            })
        }
    }
}

impl Drop for Listener {
    fn drop(&mut self) {
        let _sess = self.sess.lock();
        unsafe {
            let _ = raw::libssh2_channel_forward_cancel(self.raw);
        }
    }
}