From 14d288fe125813b130a6571bbf2ae49c5f247174 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sat, 23 Apr 2022 11:05:31 +0100 Subject: [PATCH 1/4] socket `set_mark` addition. to be able to set a marker/id on the socket for network filtering (iptables/ipfw here) purpose. --- library/std/src/os/unix/net/stream.rs | 6 ++++++ library/std/src/sys/unix/net.rs | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/library/std/src/os/unix/net/stream.rs b/library/std/src/os/unix/net/stream.rs index 1d6083e66e172..7eb06be3e0907 100644 --- a/library/std/src/os/unix/net/stream.rs +++ b/library/std/src/os/unix/net/stream.rs @@ -424,6 +424,12 @@ impl UnixStream { self.0.passcred() } + #[cfg(any(doc, target_os = "linux", target_os = "freebsd",))] + #[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/sys/unix/net.rs b/library/std/src/sys/unix/net.rs index a1bbc2d87b640..60ee52528c59a 100644 --- a/library/std/src/sys/unix/net.rs +++ b/library/std/src/sys/unix/net.rs @@ -427,6 +427,16 @@ impl Socket { self.0.set_nonblocking(nonblocking) } + #[cfg(target_os = "linux")] + pub fn set_mark(&self, mark: u32) -> io::Result<()> { + setsockopt(self, libc::SOL_SOCKET, libc::SO_MARK, mark as libc::c_int) + } + + #[cfg(target_os = "freebsd")] + pub fn set_mark(&self, mark: u32) -> io::Result<()> { + setsockopt(self, libc::SOL_SOCKET, libc::SO_USER_COOKIE, mark) + } + 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))) } From 48ef00e36f58c1debaec8d5612297b8819f7a690 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Wed, 27 Apr 2022 06:01:05 +0100 Subject: [PATCH 2/4] 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 8008acfd1c96f..7f5d760481b86 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 7eb06be3e0907..7ecb81340ac8d 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 60ee52528c59a..30667edafbaef 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))) } From 10f5a19a4deac4a7300ed6bfad11731d451713b0 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Wed, 6 Jul 2022 20:01:25 +0100 Subject: [PATCH 3/4] changes from feedback --- library/std/src/os/unix/net/datagram.rs | 5 ++--- library/std/src/os/unix/net/stream.rs | 5 ++--- library/std/src/sys/unix/net.rs | 20 ++++++++------------ 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/library/std/src/os/unix/net/datagram.rs b/library/std/src/os/unix/net/datagram.rs index 7f5d760481b86..02d0f24cd6526 100644 --- a/library/std/src/os/unix/net/datagram.rs +++ b/library/std/src/os/unix/net/datagram.rs @@ -839,7 +839,6 @@ impl UnixDatagram { } /// Set the id of the socket for network filtering purpose - /// and is only a setter. /// /// ```no_run /// #![feature(unix_set_mark)] @@ -847,12 +846,12 @@ impl UnixDatagram { /// /// fn main() -> std::io::Result<()> { /// let sock = UnixDatagram::unbound()?; - /// sock.set_mark(32 as u32).expect("set_mark function failed"); + /// sock.set_mark(32)?; /// Ok(()) /// } /// ``` #[cfg(any(doc, target_os = "linux", target_os = "freebsd", target_os = "openbsd",))] - #[unstable(feature = "unix_set_mark", issue = "none")] + #[unstable(feature = "unix_set_mark", issue = "96467")] pub fn set_mark(&self, mark: u32) -> io::Result<()> { self.0.set_mark(mark) } diff --git a/library/std/src/os/unix/net/stream.rs b/library/std/src/os/unix/net/stream.rs index 7ecb81340ac8d..ece0f91dad029 100644 --- a/library/std/src/os/unix/net/stream.rs +++ b/library/std/src/os/unix/net/stream.rs @@ -425,7 +425,6 @@ impl UnixStream { } /// Set the id of the socket for network filtering purpose - /// and is only a setter. /// /// ```no_run /// #![feature(unix_set_mark)] @@ -433,12 +432,12 @@ impl UnixStream { /// /// fn main() -> std::io::Result<()> { /// let sock = UnixStream::connect("/tmp/sock")?; - /// sock.set_mark(32 as u32).expect("set_mark function failed"); + /// sock.set_mark(32)?; /// Ok(()) /// } /// ``` #[cfg(any(doc, target_os = "linux", target_os = "freebsd", target_os = "openbsd",))] - #[unstable(feature = "unix_set_mark", issue = "none")] + #[unstable(feature = "unix_set_mark", issue = "96467")] 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 30667edafbaef..c942689eddf48 100644 --- a/library/std/src/sys/unix/net.rs +++ b/library/std/src/sys/unix/net.rs @@ -427,19 +427,15 @@ impl Socket { self.0.set_nonblocking(nonblocking) } - #[cfg(target_os = "linux")] + #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))] pub fn set_mark(&self, mark: u32) -> io::Result<()> { - setsockopt(self, libc::SOL_SOCKET, libc::SO_MARK, mark as libc::c_int) - } - - #[cfg(target_os = "freebsd")] - pub fn set_mark(&self, mark: u32) -> io::Result<()> { - 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) + #[cfg(target_os = "linux")] + let option = libc::SO_MARK; + #[cfg(target_os = "freebsd")] + let option = libc::SO_USER_COOKIE; + #[cfg(target_os = "openbsd")] + let option = libc::SO_RTABLE; + setsockopt(self, libc::SOL_SOCKET, option, mark as libc::c_int) } pub fn take_error(&self) -> io::Result> { From f6efb0b74f286dc806b2fb46b3bd880606533c64 Mon Sep 17 00:00:00 2001 From: David CARLIER Date: Thu, 7 Jul 2022 13:45:05 +0100 Subject: [PATCH 4/4] Fix doc build on unsupported oses --- library/std/src/os/unix/net/datagram.rs | 9 ++++++++- library/std/src/os/unix/net/stream.rs | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/library/std/src/os/unix/net/datagram.rs b/library/std/src/os/unix/net/datagram.rs index 02d0f24cd6526..f758f88d0a370 100644 --- a/library/std/src/os/unix/net/datagram.rs +++ b/library/std/src/os/unix/net/datagram.rs @@ -840,7 +840,14 @@ impl UnixDatagram { /// Set the id of the socket for network filtering purpose /// - /// ```no_run + #[cfg_attr( + any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"), + doc = "```no_run" + )] + #[cfg_attr( + not(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd")), + doc = "```ignore" + )] /// #![feature(unix_set_mark)] /// use std::os::unix::net::UnixDatagram; /// diff --git a/library/std/src/os/unix/net/stream.rs b/library/std/src/os/unix/net/stream.rs index ece0f91dad029..240c5a77105d5 100644 --- a/library/std/src/os/unix/net/stream.rs +++ b/library/std/src/os/unix/net/stream.rs @@ -426,7 +426,14 @@ impl UnixStream { /// Set the id of the socket for network filtering purpose /// - /// ```no_run + #[cfg_attr( + any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"), + doc = "```no_run" + )] + #[cfg_attr( + not(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd")), + doc = "```ignore" + )] /// #![feature(unix_set_mark)] /// use std::os::unix::net::UnixStream; ///