Skip to content

Commit

Permalink
tonic: implement the server Connected trait for tokio UnixStream
Browse files Browse the repository at this point in the history
This impl is needed in order to use a tokio UnixStream as the
`incoming` argument in methods like
`tonic::transport::server::Router::serve_with_incoming_shutdown`

Fixes: hyperium#856

Signed-off-by: Anthony Green <agreen@starry.com>
  • Loading branch information
agreen17 committed Feb 14, 2022
1 parent e78a2d8 commit 7fea6fb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions tonic/src/request.rs
Expand Up @@ -202,8 +202,8 @@ impl<T> Request<T> {
/// Get the remote address of this connection.
///
/// This will return `None` if the `IO` type used
/// does not implement `Connected`. This currently,
/// only works on the server side.
/// does not implement `Connected` or when using a unix domain socket.
/// This currently only works on the server side.
pub fn remote_addr(&self) -> Option<SocketAddr> {
#[cfg(feature = "transport")]
{
Expand Down
5 changes: 5 additions & 0 deletions tonic/src/transport/server/mod.rs
Expand Up @@ -6,6 +6,8 @@ mod recover_error;
#[cfg(feature = "tls")]
#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
mod tls;
#[cfg(unix)]
mod unix;

pub use conn::{Connected, TcpConnectInfo};
#[cfg(feature = "tls")]
Expand All @@ -17,6 +19,9 @@ pub use conn::TlsConnectInfo;
#[cfg(feature = "tls")]
use super::service::TlsAcceptor;

#[cfg(unix)]
pub use unix::UdsConnectInfo;

use incoming::TcpIncoming;

#[cfg(feature = "tls")]
Expand Down
31 changes: 31 additions & 0 deletions tonic/src/transport/server/unix.rs
@@ -0,0 +1,31 @@
use super::Connected;
use std::sync::Arc;

/// Connection info for Unix domain socket streams.
///
/// This type will be accessible through [request extensions][ext] if you're using
/// a unix stream.
///
/// See [Connected] for more details.
///
/// [ext]: crate::Request::extensions
/// [Connected]: crate::transport::server::Connected
#[cfg_attr(docsrs, doc(cfg(unix)))]
#[derive(Clone, Debug)]
pub struct UdsConnectInfo {
/// Peer address. This will be "unnamed" for client unix sockets.
pub peer_addr: Option<Arc<tokio::net::unix::SocketAddr>>,
/// Process credentials for the unix socket.
pub peer_cred: Option<tokio::net::unix::UCred>,
}

impl Connected for tokio::net::UnixStream {
type ConnectInfo = UdsConnectInfo;

fn connect_info(&self) -> Self::ConnectInfo {
UdsConnectInfo {
peer_addr: self.peer_addr().ok().map(Arc::new),
peer_cred: self.peer_cred().ok(),
}
}
}

0 comments on commit 7fea6fb

Please sign in to comment.