Skip to content

Commit 44102a8

Browse files
committed
adding the possiblity to remove the filter.
fixing build too.
1 parent 42def76 commit 44102a8

File tree

3 files changed

+100
-7
lines changed

3 files changed

+100
-7
lines changed

library/std/src/os/unix/net/datagram.rs

+82
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#[cfg(any(doc, target_os = "android", target_os = "linux"))]
22
use super::{recv_vectored_with_ancillary_from, send_vectored_with_ancillary_to, SocketAncillary};
33
use super::{sockaddr_un, SocketAddr};
4+
#[cfg(any(doc, target_os = "netbsd", target_os = "freebsd"))]
5+
use crate::ffi::CStr;
46
#[cfg(any(doc, target_os = "android", target_os = "linux"))]
57
use crate::io::{IoSlice, IoSliceMut};
68
use crate::net::Shutdown;
@@ -807,6 +809,86 @@ impl UnixDatagram {
807809
self.0.set_nonblocking(nonblocking)
808810
}
809811

812+
/// Moves the socket to pass unix credentials as control message in [`SocketAncillary`].
813+
///
814+
/// Set the socket option `SO_PASSCRED`.
815+
///
816+
/// # Examples
817+
///
818+
#[cfg_attr(
819+
any(
820+
target_os = "android",
821+
target_os = "linux",
822+
target_os = "netbsd",
823+
target_os = "freebsd",
824+
),
825+
doc = "```no_run"
826+
)]
827+
#[cfg_attr(
828+
not(any(
829+
target_os = "android",
830+
target_os = "linux",
831+
target_os = "netbsd",
832+
target_os = "freebsd"
833+
)),
834+
doc = "```ignore"
835+
)]
836+
/// #![feature(unix_socket_ancillary_data)]
837+
/// use std::os::unix::net::UnixDatagram;
838+
///
839+
/// fn main() -> std::io::Result<()> {
840+
/// let sock = UnixDatagram::unbound()?;
841+
/// sock.set_passcred(true).expect("set_passcred function failed");
842+
/// Ok(())
843+
/// }
844+
/// ```
845+
#[cfg(any(
846+
doc,
847+
target_os = "android",
848+
target_os = "linux",
849+
target_os = "netbsd",
850+
target_os = "freebsd"
851+
))]
852+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
853+
pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
854+
self.0.set_passcred(passcred)
855+
}
856+
857+
/// Get the current value of the socket for passing unix credentials in [`SocketAncillary`].
858+
/// This value can be change by [`set_passcred`].
859+
///
860+
/// Get the socket option `SO_PASSCRED`.
861+
///
862+
/// [`set_passcred`]: UnixDatagram::set_passcred
863+
#[cfg(any(
864+
doc,
865+
target_os = "android",
866+
target_os = "linux",
867+
target_os = "netbsd",
868+
target_os = "freebsd"
869+
))]
870+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
871+
pub fn passcred(&self) -> io::Result<bool> {
872+
self.0.passcred()
873+
}
874+
875+
/// Set a filter name on the socket to filter incoming connections to defer it before accept(2)
876+
///
877+
/// an empty name allows to remove this connection's filter
878+
#[cfg(any(doc, target_os = "netbsd", target_os = "freebsd"))]
879+
#[unstable(feature = "acceptfilter", issue = "none")]
880+
pub fn set_acceptfilter(&self, name: &CStr) -> io::Result<()> {
881+
self.0.set_acceptfilter(name)
882+
}
883+
884+
/// Get a filter name if one had been set previously on the socket.
885+
///
886+
#[cfg(any(doc, target_os = "netbsd", target_os = "freebsd"))]
887+
#[unstable(feature = "acceptfilter", issue = "none")]
888+
pub fn acceptfilter(&self) -> io::Result<&CStr> {
889+
self.0.acceptfilter()
890+
}
891+
810892
/// Set the id of the socket for network filtering purpose
811893
///
812894
#[cfg_attr(

library/std/src/os/unix/net/stream.rs

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use super::{peer_cred, UCred};
1414
#[cfg(any(doc, target_os = "android", target_os = "linux"))]
1515
use super::{recv_vectored_with_ancillary_from, send_vectored_with_ancillary_to, SocketAncillary};
1616
use super::{sockaddr_un, SocketAddr};
17+
#[cfg(any(doc, target_os = "netbsd", target_os = "freebsd"))]
1718
use crate::ffi::CStr;
1819
use crate::fmt;
1920
use crate::io::{self, IoSlice, IoSliceMut};
@@ -450,6 +451,7 @@ impl UnixStream {
450451

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

library/std/src/sys/pal/unix/net.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -455,14 +455,23 @@ impl Socket {
455455

456456
#[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
457457
pub fn set_acceptfilter(&self, name: &CStr) -> io::Result<()> {
458-
const AF_NAME_MAX: usize = 16;
459-
let mut buf = [0; AF_NAME_MAX];
460-
for (src, dst) in name.to_bytes().iter().zip(&mut buf[..AF_NAME_MAX - 1]) {
461-
*dst = *src as i8;
458+
if !name.to_bytes().is_empty() {
459+
const AF_NAME_MAX: usize = 16;
460+
let mut buf = [0; AF_NAME_MAX];
461+
for (src, dst) in name.to_bytes().iter().zip(&mut buf[..AF_NAME_MAX - 1]) {
462+
*dst = *src as i8;
463+
}
464+
let mut arg: libc::accept_filter_arg = unsafe { mem::zeroed() };
465+
arg.af_name = buf;
466+
setsockopt(self, libc::SOL_SOCKET, libc::SO_ACCEPTFILTER, &mut arg)
467+
} else {
468+
setsockopt(
469+
self,
470+
libc::SOL_SOCKET,
471+
libc::SO_ACCEPTFILTER,
472+
core::ptr::null_mut() as *mut c_void,
473+
)
462474
}
463-
let mut arg: libc::accept_filter_arg = unsafe { mem::zeroed() };
464-
arg.af_name = buf;
465-
setsockopt(self, libc::SOL_SOCKET, libc::SO_ACCEPTFILTER, &mut arg)
466475
}
467476

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

0 commit comments

Comments
 (0)