Skip to content

Commit

Permalink
Don't attempt to resolve TCP sockets client side (#120)
Browse files Browse the repository at this point in the history
It may be important to have the SSH server perform DNS resolution, as
the client often does not use the same DNS server as the server.
  • Loading branch information
benesch authored Jun 3, 2023
1 parent a471aa2 commit 3319a81
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions src/port_forwarding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use std::ffi::OsStr;

use std::borrow::Cow;
use std::fmt;
use std::io;
use std::net::{self, SocketAddr, ToSocketAddrs};
use std::net::{self, SocketAddr};
use std::path::{Path, PathBuf};

/// Type of forwarding
Expand Down Expand Up @@ -44,12 +43,20 @@ pub enum Socket<'a> {
},

/// Tcp socket.
TcpSocket(SocketAddr),
TcpSocket {
/// Hostname.
host: Cow<'a, str>,
/// Port.
port: u16,
},
}

impl From<SocketAddr> for Socket<'static> {
fn from(addr: SocketAddr) -> Self {
Socket::TcpSocket(addr)
Socket::TcpSocket {
host: addr.ip().to_string().into(),
port: addr.port(),
}
}
}

Expand Down Expand Up @@ -99,19 +106,22 @@ impl From<Box<Path>> for Socket<'static> {

impl Socket<'_> {
/// Create a new TcpSocket
pub fn new<T: ToSocketAddrs>(addr: &T) -> Result<Socket<'static>, io::Error> {
addr.to_socket_addrs()?
.next()
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "no more socket addresses to try"))
.map(Socket::TcpSocket)
pub fn new<'a, S>(host: S, port: u16) -> Socket<'a>
where
S: Into<Cow<'a, str>>,
{
Socket::TcpSocket {
host: host.into(),
port,
}
}

#[cfg(feature = "process-mux")]
pub(crate) fn as_os_str(&self) -> Cow<'_, OsStr> {
match self {
#[cfg(unix)]
Socket::UnixSocket { path } => Cow::Borrowed(path.as_os_str()),
Socket::TcpSocket(socket) => Cow::Owned(format!("{}", socket).into()),
Socket::TcpSocket { host, port } => Cow::Owned(format!("{host}:{port}").into()),
}
}
}
Expand All @@ -124,9 +134,9 @@ impl<'a> From<Socket<'a>> for native_mux_impl::Socket<'a> {
match socket {
#[cfg(unix)]
Socket::UnixSocket { path } => UnixSocket { path },
Socket::TcpSocket(socket) => TcpSocket {
port: socket.port() as u32,
host: socket.ip().to_string().into(),
Socket::TcpSocket { host, port } => TcpSocket {
host,
port: port as u32,
},
}
}
Expand All @@ -137,9 +147,9 @@ impl<'a> fmt::Display for Socket<'a> {
match self {
#[cfg(unix)]
Socket::UnixSocket { path } => {
write!(f, "{}", path.to_string_lossy())
write!(f, "{}", path.display())
}
Socket::TcpSocket(socket) => write!(f, "{}", socket),
Socket::TcpSocket { host, port } => write!(f, "{host}:{port}"),
}
}
}

0 comments on commit 3319a81

Please sign in to comment.