From 48ef00e36f58c1debaec8d5612297b8819f7a690 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Wed, 27 Apr 2022 06:01:05 +0100 Subject: [PATCH] doc additions --- library/std/src/os/unix/net/datagram.rs | 19 +++++++++++++++++++ library/std/src/os/unix/net/stream.rs | 15 ++++++++++++++- library/std/src/sys/unix/net.rs | 5 +++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/library/std/src/os/unix/net/datagram.rs b/library/std/src/os/unix/net/datagram.rs index 8008acfd1c96..7f5d760481b8 100644 --- a/library/std/src/os/unix/net/datagram.rs +++ b/library/std/src/os/unix/net/datagram.rs @@ -838,6 +838,25 @@ impl UnixDatagram { self.0.passcred() } + /// Set the id of the socket for network filtering purpose + /// and is only a setter. + /// + /// ```no_run + /// #![feature(unix_set_mark)] + /// use std::os::unix::net::UnixDatagram; + /// + /// fn main() -> std::io::Result<()> { + /// let sock = UnixDatagram::unbound()?; + /// sock.set_mark(32 as u32).expect("set_mark function failed"); + /// Ok(()) + /// } + /// ``` + #[cfg(any(doc, target_os = "linux", target_os = "freebsd", target_os = "openbsd",))] + #[unstable(feature = "unix_set_mark", issue = "none")] + pub fn set_mark(&self, mark: u32) -> io::Result<()> { + self.0.set_mark(mark) + } + /// Returns the value of the `SO_ERROR` option. /// /// # Examples diff --git a/library/std/src/os/unix/net/stream.rs b/library/std/src/os/unix/net/stream.rs index 7eb06be3e090..7ecb81340ac8 100644 --- a/library/std/src/os/unix/net/stream.rs +++ b/library/std/src/os/unix/net/stream.rs @@ -424,7 +424,20 @@ impl UnixStream { self.0.passcred() } - #[cfg(any(doc, target_os = "linux", target_os = "freebsd",))] + /// Set the id of the socket for network filtering purpose + /// and is only a setter. + /// + /// ```no_run + /// #![feature(unix_set_mark)] + /// use std::os::unix::net::UnixStream; + /// + /// fn main() -> std::io::Result<()> { + /// let sock = UnixStream::connect("/tmp/sock")?; + /// sock.set_mark(32 as u32).expect("set_mark function failed"); + /// Ok(()) + /// } + /// ``` + #[cfg(any(doc, target_os = "linux", target_os = "freebsd", target_os = "openbsd",))] #[unstable(feature = "unix_set_mark", issue = "none")] pub fn set_mark(&self, mark: u32) -> io::Result<()> { self.0.set_mark(mark) diff --git a/library/std/src/sys/unix/net.rs b/library/std/src/sys/unix/net.rs index 60ee52528c59..30667edafbae 100644 --- a/library/std/src/sys/unix/net.rs +++ b/library/std/src/sys/unix/net.rs @@ -437,6 +437,11 @@ impl Socket { setsockopt(self, libc::SOL_SOCKET, libc::SO_USER_COOKIE, mark) } + #[cfg(target_os = "openbsd")] + pub fn set_mark(&self, mark: u32) -> io::Result<()> { + setsockopt(self, libc::SOL_SOCKET, libc::SO_RTABLE, mark as libc::c_int) + } + pub fn take_error(&self) -> io::Result> { let raw: c_int = getsockopt(self, libc::SOL_SOCKET, libc::SO_ERROR)?; if raw == 0 { Ok(None) } else { Ok(Some(io::Error::from_raw_os_error(raw as i32))) }