Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Socket::passcred/set_passcred for working with SO_PASSCRED. #506

Merged
merged 2 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/socket.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ pub(crate) use libc::SO_LINGER;
target_os = "watchos",
))]
pub(crate) use libc::SO_LINGER_SEC as SO_LINGER;
#[cfg(target_os = "linux")]
pub(crate) use libc::SO_PASSCRED;
pub(crate) use libc::{
ip_mreq as IpMreq, linger, IPPROTO_IP, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, IPV6_MULTICAST_IF,
IPV6_MULTICAST_LOOP, IPV6_UNICAST_HOPS, IPV6_V6ONLY, IP_ADD_MEMBERSHIP, IP_DROP_MEMBERSHIP,
Expand Down
16 changes: 16 additions & 0 deletions tests/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1680,3 +1680,19 @@ fn cookie() {
Err(err) => panic!("Could not get socket cookie a second time, err: {err}"),
}
}

#[cfg(all(unix, target_os = "linux"))]
#[test]
fn set_passcred() {
let socket = Socket::new(Domain::UNIX, Type::DGRAM, None).unwrap();
assert!(!socket.passcred().unwrap());

socket.set_passcred(true).unwrap();
assert!(socket.passcred().unwrap());

let socket = Socket::new(Domain::UNIX, Type::STREAM, None).unwrap();
assert!(!socket.passcred().unwrap());

socket.set_passcred(true).unwrap();
assert!(socket.passcred().unwrap());
}