Skip to content

Commit 630c8a7

Browse files
tobzThomasdezeeuw
authored andcommitted
Add Socket::passcred/set_passcred for working with SO_PASSCRED.
1 parent 21ba660 commit 630c8a7

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/socket.rs

+31
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,37 @@ impl Socket {
965965
}
966966
}
967967

968+
/// Get value for the `SO_PASSCRED` option on this socket.
969+
///
970+
/// For more information about this option, see [`set_passcred`].
971+
///
972+
/// [`set_passcred`]: Socket::set_passcred
973+
#[cfg(all(unix, target_os = "linux"))]
974+
#[cfg_attr(docsrs, doc(cfg(all(unix, target_os = "linux"))))]
975+
pub fn passcred(&self) -> io::Result<bool> {
976+
unsafe {
977+
getsockopt::<c_int>(self.as_raw(), sys::SOL_SOCKET, sys::SO_PASSCRED)
978+
.map(|passcred| passcred != 0)
979+
}
980+
}
981+
982+
/// Set value for the `SO_PASSCRED` option on this socket.
983+
///
984+
/// If this option is enabled, enables the receiving of the `SCM_CREDENTIALS`
985+
/// control messages.
986+
#[cfg(all(unix, target_os = "linux"))]
987+
#[cfg_attr(docsrs, doc(cfg(all(unix, target_os = "linux"))))]
988+
pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
989+
unsafe {
990+
setsockopt(
991+
self.as_raw(),
992+
sys::SOL_SOCKET,
993+
sys::SO_PASSCRED,
994+
passcred as c_int,
995+
)
996+
}
997+
}
998+
968999
/// Get value for the `SO_RCVBUF` option on this socket.
9691000
///
9701001
/// For more information about this option, see [`set_recv_buffer_size`].

src/sys/unix.rs

+2
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ pub(crate) use libc::{
179179
SO_BROADCAST, SO_ERROR, SO_KEEPALIVE, SO_RCVBUF, SO_RCVTIMEO, SO_REUSEADDR, SO_SNDBUF,
180180
SO_SNDTIMEO, SO_TYPE, TCP_NODELAY,
181181
};
182+
#[cfg(target_os = "linux")]
183+
pub(crate) use libc::SO_PASSCRED;
182184
#[cfg(not(any(
183185
target_os = "dragonfly",
184186
target_os = "haiku",

0 commit comments

Comments
 (0)