Skip to content

Commit

Permalink
🩹 zb: implement peer_credentials() for tokio win32
Browse files Browse the repository at this point in the history
  • Loading branch information
elmarco committed Feb 1, 2024
1 parent 98c854a commit 0d74cfb
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions zbus/src/connection/socket/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ impl ReadHalf for tokio::net::tcp::OwnedReadHalf {
ret
})
}

#[cfg(windows)]
async fn peer_credentials(&mut self) -> io::Result<crate::fdo::ConnectionCredentials> {
let peer_addr = self.peer_addr()?.clone();
crate::Task::spawn_blocking(
move || win32_credentials_from_addr(&peer_addr),
"peer credentials",
)
.await
}
}

#[cfg(feature = "tokio")]
Expand All @@ -141,4 +151,29 @@ impl WriteHalf for tokio::net::tcp::OwnedWriteHalf {
async fn close(&mut self) -> io::Result<()> {
tokio::io::AsyncWriteExt::shutdown(self).await
}

#[cfg(windows)]
async fn peer_credentials(&mut self) -> io::Result<crate::fdo::ConnectionCredentials> {
let peer_addr = self.peer_addr()?.clone();
crate::Task::spawn_blocking(
move || win32_credentials_from_addr(&peer_addr),
"peer credentials",
)
.await
}
}

#[cfg(feature = "tokio")]
#[cfg(windows)]
fn win32_credentials_from_addr(
addr: &std::net::SocketAddr,
) -> io::Result<crate::fdo::ConnectionCredentials> {
use crate::win32::{socket_addr_get_pid, ProcessToken};

let pid = socket_addr_get_pid(addr)? as _;
let sid = ProcessToken::open(if pid != 0 { Some(pid as _) } else { None })
.and_then(|process_token| process_token.sid())?;
Ok(crate::fdo::ConnectionCredentials::default()
.set_process_id(pid)
.set_windows_sid(sid))
}

0 comments on commit 0d74cfb

Please sign in to comment.