Skip to content

Commit

Permalink
Add Socket::passcred/set_passcred for working with SO_PASSCRED.
Browse files Browse the repository at this point in the history
  • Loading branch information
tobz committed Apr 15, 2024
1 parent c93cdcc commit 4581dba
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/socket.rs
Expand Up @@ -965,6 +965,37 @@ impl Socket {
}
}

/// Get value for the `SO_PASSCRED` option on this socket.
///
/// For more information about this option, see [`set_passcred`].
///
/// [`set_passcred`]: Socket::set_passcred
#[cfg(all(unix, target_os = "linux"))]
#[cfg_attr(docsrs, doc(cfg(all(unix, target_os = "linux"))))]
pub fn passcred(&self) -> io::Result<bool> {
unsafe {
getsockopt::<c_int>(self.as_raw(), sys::SOL_SOCKET, sys::SO_PASSCRED)
.map(|passcred| passcred != 0)
}
}

/// Set value for the `SO_PASSCRED` option on this socket.
///
/// If this option is enabled, enables the receiving of the `SCM_CREDENTIALS`
/// control messages.
#[cfg(all(unix, target_os = "linux"))]
#[cfg_attr(docsrs, doc(cfg(all(unix, target_os = "linux"))))]
pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
unsafe {
setsockopt(
self.as_raw(),
sys::SOL_SOCKET,
sys::SO_PASSCRED,
passcred as c_int,
)
}
}

/// Get value for the `SO_RCVBUF` option on this socket.
///
/// For more information about this option, see [`set_recv_buffer_size`].
Expand Down
2 changes: 2 additions & 0 deletions src/sys/unix.rs
Expand Up @@ -179,6 +179,8 @@ pub(crate) use libc::{
SO_BROADCAST, SO_ERROR, SO_KEEPALIVE, SO_RCVBUF, SO_RCVTIMEO, SO_REUSEADDR, SO_SNDBUF,
SO_SNDTIMEO, SO_TYPE, TCP_NODELAY,
};
#[cfg(target_os = "linux")]
pub(crate) use libc::SO_PASSCRED;
#[cfg(not(any(
target_os = "dragonfly",
target_os = "haiku",
Expand Down

0 comments on commit 4581dba

Please sign in to comment.