about summary refs log tree commit diff
path: root/library/std/src/sys/unix/ext/net.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-09-15 17:05:57 +0000
committerbors <bors@rust-lang.org>2020-09-15 17:05:57 +0000
commita874956d940ecb3ed524b6176a171219ac4787ea (patch)
treee0217ab89c57ffb1e64ee52fd7ac8aa744982bb0 /library/std/src/sys/unix/ext/net.rs
parent27a45d0aabbc19b635c858033d6e87f86c663570 (diff)
parent68ff495afa7687677cf9facf83c5130db24d3acd (diff)
downloadrust-a874956d940ecb3ed524b6176a171219ac4787ea.tar.gz
rust-a874956d940ecb3ed524b6176a171219ac4787ea.zip
Auto merge of #75148 - joechrisellis:master, r=Amanieu
Implementation of peer credentials for Unix sockets

The code in `ucred.rs` is based on the work done in [PR 13](https://github.com/tokio-rs/tokio-uds/pull/13) in the tokio-uds repository on GitHub.

This commit is effectively a port to the stdlib, so credit to Martin Habovštiak (`@Kixunil)` and contributors for the meat of this work. 🥇

Happy to make changes as needed. 🙂
Diffstat (limited to 'library/std/src/sys/unix/ext/net.rs')
-rw-r--r--library/std/src/sys/unix/ext/net.rs51
1 files changed, 51 insertions, 0 deletions
diff --git a/library/std/src/sys/unix/ext/net.rs b/library/std/src/sys/unix/ext/net.rs
index 320378e30cc..f3da8f9f584 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,34 @@ 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
+    /// #![feature(peer_credentials_unix_socket)]
+    /// 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")]
+    #[cfg(any(
+        target_os = "android",
+        target_os = "linux",
+        target_os = "dragonfly",
+        target_os = "freebsd",
+        target_os = "ios",
+        target_os = "macos",
+        target_os = "openbsd"
+    ))]
+    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