Skip to content

Commit

Permalink
Rollup merge of rust-lang#96334 - devnexen:socket_mark, r=dtolnay
Browse files Browse the repository at this point in the history
socket `set_mark` addition.

to be able to set a marker/id on the socket for network filtering
 (iptables/ipfw here) purpose.
  • Loading branch information
matthiaskrgr committed Aug 28, 2022
2 parents 7830b82 + f6efb0b commit 830d518
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
25 changes: 25 additions & 0 deletions library/std/src/os/unix/net/datagram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,31 @@ impl UnixDatagram {
self.0.passcred()
}

/// Set the id of the socket for network filtering purpose
///
#[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;
///
/// fn main() -> std::io::Result<()> {
/// let sock = UnixDatagram::unbound()?;
/// sock.set_mark(32)?;
/// Ok(())
/// }
/// ```
#[cfg(any(doc, target_os = "linux", target_os = "freebsd", target_os = "openbsd",))]
#[unstable(feature = "unix_set_mark", issue = "96467")]
pub fn set_mark(&self, mark: u32) -> io::Result<()> {
self.0.set_mark(mark)
}

/// Returns the value of the `SO_ERROR` option.
///
/// # Examples
Expand Down
25 changes: 25 additions & 0 deletions library/std/src/os/unix/net/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,31 @@ impl UnixStream {
self.0.passcred()
}

/// Set the id of the socket for network filtering purpose
///
#[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;
///
/// fn main() -> std::io::Result<()> {
/// let sock = UnixStream::connect("/tmp/sock")?;
/// sock.set_mark(32)?;
/// Ok(())
/// }
/// ```
#[cfg(any(doc, target_os = "linux", target_os = "freebsd", target_os = "openbsd",))]
#[unstable(feature = "unix_set_mark", issue = "96467")]
pub fn set_mark(&self, mark: u32) -> io::Result<()> {
self.0.set_mark(mark)
}

/// Returns the value of the `SO_ERROR` option.
///
/// # Examples
Expand Down
11 changes: 11 additions & 0 deletions library/std/src/sys/unix/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,17 @@ impl Socket {
self.0.set_nonblocking(nonblocking)
}

#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
pub fn set_mark(&self, mark: u32) -> io::Result<()> {
#[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<Option<io::Error>> {
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))) }
Expand Down

0 comments on commit 830d518

Please sign in to comment.