Skip to content

Commit b54ce23

Browse files
authoredMar 31, 2023
feat(transport): Addlocal_addr to Request o (#1327)
1 parent 44ccf49 commit b54ce23

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed
 

‎tests/integration_tests/tests/connect_info.rs

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ async fn getting_connect_info() {
1414
#[tonic::async_trait]
1515
impl test_server::Test for Svc {
1616
async fn unary_call(&self, req: Request<Input>) -> Result<Response<Output>, Status> {
17+
assert!(req.local_addr().is_some());
1718
assert!(req.remote_addr().is_some());
1819
assert!(req.extensions().get::<TcpConnectInfo>().is_some());
1920

@@ -73,6 +74,7 @@ pub mod unix {
7374
let conn_info = req.extensions().get::<UdsConnectInfo>().unwrap();
7475

7576
// Client-side unix sockets are unnamed.
77+
assert!(req.local_addr().is_none());
7678
assert!(req.remote_addr().is_none());
7779
assert!(conn_info.peer_addr.as_ref().unwrap().is_unnamed());
7880
// This should contain process credentials for the client socket.

‎tonic/src/request.rs

+34
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,40 @@ impl<T> Request<T> {
203203
}
204204
}
205205

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+
206240
/// Get the remote address of this connection.
207241
///
208242
/// This will return `None` if the `IO` type used

‎tonic/src/transport/server/conn.rs

+8
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,16 @@ pub trait Connected {
6868
/// [ext]: crate::Request::extensions
6969
#[derive(Debug, Clone)]
7070
pub struct TcpConnectInfo {
71+
local_addr: Option<SocketAddr>,
7172
remote_addr: Option<SocketAddr>,
7273
}
7374

7475
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+
7581
/// Return the remote address the IO resource is connected too.
7682
pub fn remote_addr(&self) -> Option<SocketAddr> {
7783
self.remote_addr
@@ -83,6 +89,7 @@ impl Connected for AddrStream {
8389

8490
fn connect_info(&self) -> Self::ConnectInfo {
8591
TcpConnectInfo {
92+
local_addr: Some(self.local_addr()),
8693
remote_addr: Some(self.remote_addr()),
8794
}
8895
}
@@ -93,6 +100,7 @@ impl Connected for TcpStream {
93100

94101
fn connect_info(&self) -> Self::ConnectInfo {
95102
TcpConnectInfo {
103+
local_addr: self.local_addr().ok(),
96104
remote_addr: self.peer_addr().ok(),
97105
}
98106
}

0 commit comments

Comments
 (0)
Please sign in to comment.