about summary refs log tree commit diff
path: root/library/std/src/sys/unix/ext/net.rs
diff options
context:
space:
mode:
authorJoe Ellis <joe.ellis@arm.com>2020-08-04 11:18:13 +0100
committerJoe Ellis <joe.ellis@arm.com>2020-09-14 10:31:44 +0100
commited20eff92be7bcd29ddc74f6bfa603f6698c9504 (patch)
tree90a06f39d0603d7c431c116bddf6aaddfc519f18 /library/std/src/sys/unix/ext/net.rs
parent5bc8b181954e3a4cbce91466e44027600d4c94ef (diff)
downloadrust-ed20eff92be7bcd29ddc74f6bfa603f6698c9504.tar.gz
rust-ed20eff92be7bcd29ddc74f6bfa603f6698c9504.zip
Implementation of peer credentials for Unix sockets
The code in `ucred.rs` is based on the work done in PR 13 in the
tokio-uds repository on GitHub. Link below for reference:

    https://github.com/tokio-rs/tokio-uds/pull/13

Credit to Martin Habovštiak (GitHub username Kixunil) and contributors
for this work!
Diffstat (limited to 'library/std/src/sys/unix/ext/net.rs')
-rw-r--r--library/std/src/sys/unix/ext/net.rs41
1 files changed, 41 insertions, 0 deletions
diff --git a/library/std/src/sys/unix/ext/net.rs b/library/std/src/sys/unix/ext/net.rs
index 0e07106f5ce..930a6797000 100644
--- a/library/std/src/sys/unix/ext/net.rs
+++ b/library/std/src/sys/unix/ext/net.rs
@@ -31,6 +31,29 @@ use crate::sys_common::{self, AsInner, FromInner, IntoInner};
 use crate::time::Duration;
 
 #[cfg(any(
+    target_os = "android",
+    target_os = "linux",
+    target_os = "dragonfly",
+    target_os = "freebsd",
+    target_os = "ios",
+    target_os = "macos",
+    target_os = "openbsd"
+))]
+use crate::os::unix::ucred;
+
+#[unstable(feature = "peer_credentials_unix_socket", issue = "42839", reason = "unstable")]
+#[cfg(any(
+    target_os = "android",
+    target_os = "linux",
+    target_os = "dragonfly",
+    target_os = "freebsd",
+    target_os = "ios",
+    target_os = "macos",
+    target_os = "openbsd"
+))]
+pub use ucred::UCred;
+
+#[cfg(any(
     target_os = "linux",
     target_os = "android",
     target_os = "dragonfly",
@@ -405,6 +428,24 @@ impl UnixStream {
         SocketAddr::new(|addr, len| unsafe { libc::getpeername(*self.0.as_inner(), addr, len) })
     }
 
+    /// Gets the peer credentials for this Unix domain socket.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixStream;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let socket = UnixStream::connect("/tmp/sock")?;
+    ///     let peer_cred = socket.peer_cred().expect("Couldn't get peer credentials");
+    ///     Ok(())
+    /// }
+    /// ```
+    #[unstable(feature = "peer_credentials_unix_socket", issue = "42839", reason = "unstable")]
+    pub fn peer_cred(&self) -> io::Result<UCred> {
+        ucred::peer_cred(self)
+    }
+
     /// Sets the read timeout for the socket.
     ///
     /// If the provided value is [`None`], then [`read`] calls will block