@@ -6,8 +6,7 @@ use std::ffi::OsStr;
66
77use std:: borrow:: Cow ;
88use std:: fmt;
9- use std:: io;
10- use std:: net:: { self , SocketAddr , ToSocketAddrs } ;
9+ use std:: net:: { self , SocketAddr } ;
1110use std:: path:: { Path , PathBuf } ;
1211
1312/// Type of forwarding
@@ -44,12 +43,20 @@ pub enum Socket<'a> {
4443 } ,
4544
4645 /// Tcp socket.
47- TcpSocket ( SocketAddr ) ,
46+ TcpSocket {
47+ /// Hostname.
48+ host : Cow < ' a , str > ,
49+ /// Port.
50+ port : u16 ,
51+ } ,
4852}
4953
5054impl From < SocketAddr > for Socket < ' static > {
5155 fn from ( addr : SocketAddr ) -> Self {
52- Socket :: TcpSocket ( addr)
56+ Socket :: TcpSocket {
57+ host : addr. ip ( ) . to_string ( ) . into ( ) ,
58+ port : addr. port ( ) ,
59+ }
5360 }
5461}
5562
@@ -99,19 +106,22 @@ impl From<Box<Path>> for Socket<'static> {
99106
100107impl Socket < ' _ > {
101108 /// Create a new TcpSocket
102- pub fn new < T : ToSocketAddrs > ( addr : & T ) -> Result < Socket < ' static > , io:: Error > {
103- addr. to_socket_addrs ( ) ?
104- . next ( )
105- . ok_or_else ( || io:: Error :: new ( io:: ErrorKind :: Other , "no more socket addresses to try" ) )
106- . map ( Socket :: TcpSocket )
109+ pub fn new < ' a , S > ( host : S , port : u16 ) -> Socket < ' a >
110+ where
111+ S : Into < Cow < ' a , str > > ,
112+ {
113+ Socket :: TcpSocket {
114+ host : host. into ( ) ,
115+ port,
116+ }
107117 }
108118
109119 #[ cfg( feature = "process-mux" ) ]
110120 pub ( crate ) fn as_os_str ( & self ) -> Cow < ' _ , OsStr > {
111121 match self {
112122 #[ cfg( unix) ]
113123 Socket :: UnixSocket { path } => Cow :: Borrowed ( path. as_os_str ( ) ) ,
114- Socket :: TcpSocket ( socket ) => Cow :: Owned ( format ! ( "{}" , socket ) . into ( ) ) ,
124+ Socket :: TcpSocket { host , port } => Cow :: Owned ( format ! ( "{host}:{port}" ) . into ( ) ) ,
115125 }
116126 }
117127}
@@ -124,9 +134,9 @@ impl<'a> From<Socket<'a>> for native_mux_impl::Socket<'a> {
124134 match socket {
125135 #[ cfg( unix) ]
126136 Socket :: UnixSocket { path } => UnixSocket { path } ,
127- Socket :: TcpSocket ( socket ) => TcpSocket {
128- port : socket . port ( ) as u32 ,
129- host : socket . ip ( ) . to_string ( ) . into ( ) ,
137+ Socket :: TcpSocket { host , port } => TcpSocket {
138+ host ,
139+ port : port as u32 ,
130140 } ,
131141 }
132142 }
@@ -137,9 +147,9 @@ impl<'a> fmt::Display for Socket<'a> {
137147 match self {
138148 #[ cfg( unix) ]
139149 Socket :: UnixSocket { path } => {
140- write ! ( f, "{}" , path. to_string_lossy ( ) )
150+ write ! ( f, "{}" , path. display ( ) )
141151 }
142- Socket :: TcpSocket ( socket ) => write ! ( f, "{}" , socket ) ,
152+ Socket :: TcpSocket { host , port } => write ! ( f, "{host}:{port}" ) ,
143153 }
144154 }
145155}
0 commit comments