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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#[cfg(feature = "native-mux")]
use super::native_mux_impl;
#[cfg(feature = "process-mux")]
use std::ffi::OsStr;
use std::borrow::Cow;
use std::fmt;
use std::io;
use std::net::{SocketAddr, ToSocketAddrs};
use std::path::Path;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum ForwardType {
Local,
Remote,
}
#[cfg(feature = "native-mux")]
impl From<ForwardType> for native_mux_impl::ForwardType {
fn from(fwd_type: ForwardType) -> Self {
use native_mux_impl::ForwardType::*;
match fwd_type {
ForwardType::Local => Local,
ForwardType::Remote => Remote,
}
}
}
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub enum Socket<'a> {
UnixSocket {
path: Cow<'a, Path>,
},
TcpSocket(SocketAddr),
}
impl Socket<'_> {
pub fn new<T: ToSocketAddrs>(addr: &T) -> Result<Self, io::Error> {
let mut it = addr.to_socket_addrs()?;
let addr = it.next().ok_or_else(|| {
io::Error::new(io::ErrorKind::Other, "no more socket addresses to try")
})?;
Ok(Socket::TcpSocket(addr))
}
#[cfg(feature = "process-mux")]
pub(crate) fn as_os_str(&self) -> Cow<'_, OsStr> {
match self {
Socket::UnixSocket { path } => Cow::Borrowed(path.as_os_str()),
Socket::TcpSocket(socket) => Cow::Owned(format!("{}", socket).into()),
}
}
}
#[cfg(feature = "native-mux")]
impl<'a> From<Socket<'a>> for native_mux_impl::Socket<'a> {
fn from(socket: Socket<'a>) -> Self {
use native_mux_impl::Socket::*;
match socket {
Socket::UnixSocket { path } => UnixSocket { path },
Socket::TcpSocket(socket) => TcpSocket {
port: socket.port() as u32,
host: socket.ip().to_string().into(),
},
}
}
}
impl<'a> fmt::Display for Socket<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Socket::UnixSocket { path } => {
write!(f, "{}", path.to_string_lossy())
}
Socket::TcpSocket(socket) => write!(f, "{}", socket),
}
}
}