File tree 3 files changed +44
-0
lines changed
tests/integration_tests/tests
3 files changed +44
-0
lines changed Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ async fn getting_connect_info() {
14
14
#[ tonic:: async_trait]
15
15
impl test_server:: Test for Svc {
16
16
async fn unary_call ( & self , req : Request < Input > ) -> Result < Response < Output > , Status > {
17
+ assert ! ( req. local_addr( ) . is_some( ) ) ;
17
18
assert ! ( req. remote_addr( ) . is_some( ) ) ;
18
19
assert ! ( req. extensions( ) . get:: <TcpConnectInfo >( ) . is_some( ) ) ;
19
20
@@ -73,6 +74,7 @@ pub mod unix {
73
74
let conn_info = req. extensions ( ) . get :: < UdsConnectInfo > ( ) . unwrap ( ) ;
74
75
75
76
// Client-side unix sockets are unnamed.
77
+ assert ! ( req. local_addr( ) . is_none( ) ) ;
76
78
assert ! ( req. remote_addr( ) . is_none( ) ) ;
77
79
assert ! ( conn_info. peer_addr. as_ref( ) . unwrap( ) . is_unnamed( ) ) ;
78
80
// This should contain process credentials for the client socket.
Original file line number Diff line number Diff line change @@ -203,6 +203,40 @@ impl<T> Request<T> {
203
203
}
204
204
}
205
205
206
+ /// Get the local address of this connection.
207
+ ///
208
+ /// This will return `None` if the `IO` type used
209
+ /// does not implement `Connected` or when using a unix domain socket.
210
+ /// This currently only works on the server side.
211
+ pub fn local_addr ( & self ) -> Option < SocketAddr > {
212
+ #[ cfg( feature = "transport" ) ]
213
+ {
214
+ #[ cfg( feature = "tls" ) ]
215
+ {
216
+ self . extensions ( )
217
+ . get :: < TcpConnectInfo > ( )
218
+ . and_then ( |i| i. local_addr ( ) )
219
+ . or_else ( || {
220
+ self . extensions ( )
221
+ . get :: < TlsConnectInfo < TcpConnectInfo > > ( )
222
+ . and_then ( |i| i. get_ref ( ) . local_addr ( ) )
223
+ } )
224
+ }
225
+
226
+ #[ cfg( not( feature = "tls" ) ) ]
227
+ {
228
+ self . extensions ( )
229
+ . get :: < TcpConnectInfo > ( )
230
+ . and_then ( |i| i. local_addr ( ) )
231
+ }
232
+ }
233
+
234
+ #[ cfg( not( feature = "transport" ) ) ]
235
+ {
236
+ None
237
+ }
238
+ }
239
+
206
240
/// Get the remote address of this connection.
207
241
///
208
242
/// This will return `None` if the `IO` type used
Original file line number Diff line number Diff line change @@ -68,10 +68,16 @@ pub trait Connected {
68
68
/// [ext]: crate::Request::extensions
69
69
#[ derive( Debug , Clone ) ]
70
70
pub struct TcpConnectInfo {
71
+ local_addr : Option < SocketAddr > ,
71
72
remote_addr : Option < SocketAddr > ,
72
73
}
73
74
74
75
impl TcpConnectInfo {
76
+ /// Return the local address the IO resource is connected.
77
+ pub fn local_addr ( & self ) -> Option < SocketAddr > {
78
+ self . local_addr
79
+ }
80
+
75
81
/// Return the remote address the IO resource is connected too.
76
82
pub fn remote_addr ( & self ) -> Option < SocketAddr > {
77
83
self . remote_addr
@@ -83,6 +89,7 @@ impl Connected for AddrStream {
83
89
84
90
fn connect_info ( & self ) -> Self :: ConnectInfo {
85
91
TcpConnectInfo {
92
+ local_addr : Some ( self . local_addr ( ) ) ,
86
93
remote_addr : Some ( self . remote_addr ( ) ) ,
87
94
}
88
95
}
@@ -93,6 +100,7 @@ impl Connected for TcpStream {
93
100
94
101
fn connect_info ( & self ) -> Self :: ConnectInfo {
95
102
TcpConnectInfo {
103
+ local_addr : self . local_addr ( ) . ok ( ) ,
96
104
remote_addr : self . peer_addr ( ) . ok ( ) ,
97
105
}
98
106
}
You can’t perform that action at this time.
0 commit comments