@@ -6,8 +6,7 @@ use std::ffi::OsStr;
6
6
7
7
use std:: borrow:: Cow ;
8
8
use std:: fmt;
9
- use std:: io;
10
- use std:: net:: { self , SocketAddr , ToSocketAddrs } ;
9
+ use std:: net:: { self , SocketAddr } ;
11
10
use std:: path:: { Path , PathBuf } ;
12
11
13
12
/// Type of forwarding
@@ -44,12 +43,20 @@ pub enum Socket<'a> {
44
43
} ,
45
44
46
45
/// Tcp socket.
47
- TcpSocket ( SocketAddr ) ,
46
+ TcpSocket {
47
+ /// Hostname.
48
+ host : Cow < ' a , str > ,
49
+ /// Port.
50
+ port : u16 ,
51
+ } ,
48
52
}
49
53
50
54
impl From < SocketAddr > for Socket < ' static > {
51
55
fn from ( addr : SocketAddr ) -> Self {
52
- Socket :: TcpSocket ( addr)
56
+ Socket :: TcpSocket {
57
+ host : addr. ip ( ) . to_string ( ) . into ( ) ,
58
+ port : addr. port ( ) ,
59
+ }
53
60
}
54
61
}
55
62
@@ -99,19 +106,22 @@ impl From<Box<Path>> for Socket<'static> {
99
106
100
107
impl Socket < ' _ > {
101
108
/// 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
+ }
107
117
}
108
118
109
119
#[ cfg( feature = "process-mux" ) ]
110
120
pub ( crate ) fn as_os_str ( & self ) -> Cow < ' _ , OsStr > {
111
121
match self {
112
122
#[ cfg( unix) ]
113
123
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 ( ) ) ,
115
125
}
116
126
}
117
127
}
@@ -124,9 +134,9 @@ impl<'a> From<Socket<'a>> for native_mux_impl::Socket<'a> {
124
134
match socket {
125
135
#[ cfg( unix) ]
126
136
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 ,
130
140
} ,
131
141
}
132
142
}
@@ -137,9 +147,9 @@ impl<'a> fmt::Display for Socket<'a> {
137
147
match self {
138
148
#[ cfg( unix) ]
139
149
Socket :: UnixSocket { path } => {
140
- write ! ( f, "{}" , path. to_string_lossy ( ) )
150
+ write ! ( f, "{}" , path. display ( ) )
141
151
}
142
- Socket :: TcpSocket ( socket ) => write ! ( f, "{}" , socket ) ,
152
+ Socket :: TcpSocket { host , port } => write ! ( f, "{host}:{port}" ) ,
143
153
}
144
154
}
145
155
}
0 commit comments