Skip to content

Commit

Permalink
adding the possiblity to remove the filter.
Browse files Browse the repository at this point in the history
fixing build too.
  • Loading branch information
devnexen committed Mar 11, 2024
1 parent 42def76 commit 44102a8
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 7 deletions.
82 changes: 82 additions & 0 deletions library/std/src/os/unix/net/datagram.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#[cfg(any(doc, target_os = "android", target_os = "linux"))]
use super::{recv_vectored_with_ancillary_from, send_vectored_with_ancillary_to, SocketAncillary};
use super::{sockaddr_un, SocketAddr};
#[cfg(any(doc, target_os = "netbsd", target_os = "freebsd"))]
use crate::ffi::CStr;
#[cfg(any(doc, target_os = "android", target_os = "linux"))]
use crate::io::{IoSlice, IoSliceMut};
use crate::net::Shutdown;
Expand Down Expand Up @@ -807,6 +809,86 @@ impl UnixDatagram {
self.0.set_nonblocking(nonblocking)
}

/// Moves the socket to pass unix credentials as control message in [`SocketAncillary`].
///
/// Set the socket option `SO_PASSCRED`.
///
/// # Examples
///
#[cfg_attr(
any(
target_os = "android",
target_os = "linux",
target_os = "netbsd",
target_os = "freebsd",
),
doc = "```no_run"
)]
#[cfg_attr(
not(any(
target_os = "android",
target_os = "linux",
target_os = "netbsd",
target_os = "freebsd"
)),
doc = "```ignore"
)]
/// #![feature(unix_socket_ancillary_data)]
/// use std::os::unix::net::UnixDatagram;
///
/// fn main() -> std::io::Result<()> {
/// let sock = UnixDatagram::unbound()?;
/// sock.set_passcred(true).expect("set_passcred function failed");
/// Ok(())
/// }
/// ```
#[cfg(any(
doc,
target_os = "android",
target_os = "linux",
target_os = "netbsd",
target_os = "freebsd"
))]
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
self.0.set_passcred(passcred)
}

/// Get the current value of the socket for passing unix credentials in [`SocketAncillary`].
/// This value can be change by [`set_passcred`].
///
/// Get the socket option `SO_PASSCRED`.
///
/// [`set_passcred`]: UnixDatagram::set_passcred
#[cfg(any(
doc,
target_os = "android",
target_os = "linux",
target_os = "netbsd",
target_os = "freebsd"
))]
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
pub fn passcred(&self) -> io::Result<bool> {
self.0.passcred()
}

/// Set a filter name on the socket to filter incoming connections to defer it before accept(2)
///
/// an empty name allows to remove this connection's filter
#[cfg(any(doc, target_os = "netbsd", target_os = "freebsd"))]
#[unstable(feature = "acceptfilter", issue = "none")]
pub fn set_acceptfilter(&self, name: &CStr) -> io::Result<()> {
self.0.set_acceptfilter(name)
}

/// Get a filter name if one had been set previously on the socket.
///
#[cfg(any(doc, target_os = "netbsd", target_os = "freebsd"))]
#[unstable(feature = "acceptfilter", issue = "none")]
pub fn acceptfilter(&self) -> io::Result<&CStr> {
self.0.acceptfilter()
}

/// Set the id of the socket for network filtering purpose
///
#[cfg_attr(
Expand Down
2 changes: 2 additions & 0 deletions library/std/src/os/unix/net/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use super::{peer_cred, UCred};
#[cfg(any(doc, target_os = "android", target_os = "linux"))]
use super::{recv_vectored_with_ancillary_from, send_vectored_with_ancillary_to, SocketAncillary};
use super::{sockaddr_un, SocketAddr};
#[cfg(any(doc, target_os = "netbsd", target_os = "freebsd"))]
use crate::ffi::CStr;
use crate::fmt;
use crate::io::{self, IoSlice, IoSliceMut};
Expand Down Expand Up @@ -450,6 +451,7 @@ impl UnixStream {

/// Set a filter name on the socket to filter incoming connections to defer it before accept(2)
///
/// an empty name allows to remove this connection's filter
#[cfg(any(doc, target_os = "netbsd", target_os = "freebsd"))]
#[unstable(feature = "acceptfilter", issue = "none")]
pub fn set_acceptfilter(&self, name: &CStr) -> io::Result<()> {
Expand Down
23 changes: 16 additions & 7 deletions library/std/src/sys/pal/unix/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,14 +455,23 @@ impl Socket {

#[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
pub fn set_acceptfilter(&self, name: &CStr) -> io::Result<()> {
const AF_NAME_MAX: usize = 16;
let mut buf = [0; AF_NAME_MAX];
for (src, dst) in name.to_bytes().iter().zip(&mut buf[..AF_NAME_MAX - 1]) {
*dst = *src as i8;
if !name.to_bytes().is_empty() {
const AF_NAME_MAX: usize = 16;
let mut buf = [0; AF_NAME_MAX];
for (src, dst) in name.to_bytes().iter().zip(&mut buf[..AF_NAME_MAX - 1]) {
*dst = *src as i8;
}
let mut arg: libc::accept_filter_arg = unsafe { mem::zeroed() };
arg.af_name = buf;
setsockopt(self, libc::SOL_SOCKET, libc::SO_ACCEPTFILTER, &mut arg)
} else {
setsockopt(
self,
libc::SOL_SOCKET,
libc::SO_ACCEPTFILTER,
core::ptr::null_mut() as *mut c_void,
)
}
let mut arg: libc::accept_filter_arg = unsafe { mem::zeroed() };
arg.af_name = buf;
setsockopt(self, libc::SOL_SOCKET, libc::SO_ACCEPTFILTER, &mut arg)
}

#[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
Expand Down

0 comments on commit 44102a8

Please sign in to comment.