From 14d288fe125813b130a6571bbf2ae49c5f247174 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sat, 23 Apr 2022 11:05:31 +0100 Subject: [PATCH] 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 1d6083e66e17..7eb06be3e090 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 a1bbc2d87b64..60ee52528c59 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))) }