Skip to content

Commit 3257f18

Browse files
committed
adding the possiblity to remove the filter.
fixing build too.
1 parent e319b9d commit 3257f18

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

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

+19
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;
@@ -865,6 +867,23 @@ impl UnixDatagram {
865867
self.0.passcred()
866868
}
867869

870+
/// Set a filter name on the socket to filter incoming connections to defer it before accept(2)
871+
///
872+
/// an empty name allows to remove this connection's filter
873+
#[cfg(any(doc, target_os = "netbsd", target_os = "freebsd"))]
874+
#[unstable(feature = "acceptfilter", issue = "none")]
875+
pub fn set_acceptfilter(&self, name: &CStr) -> io::Result<()> {
876+
self.0.set_acceptfilter(name)
877+
}
878+
879+
/// Get a filter name if one had been set previously on the socket.
880+
///
881+
#[cfg(any(doc, target_os = "netbsd", target_os = "freebsd"))]
882+
#[unstable(feature = "acceptfilter", issue = "none")]
883+
pub fn acceptfilter(&self) -> io::Result<&CStr> {
884+
self.0.acceptfilter()
885+
}
886+
868887
/// Set the id of the socket for network filtering purpose
869888
///
870889
#[cfg_attr(

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

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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"))]
45
use crate::ffi::CStr;
56
use crate::fmt;
67
use crate::io::{self, IoSlice, IoSliceMut};
@@ -460,6 +461,7 @@ impl UnixStream {
460461

461462
/// Set a filter name on the socket to filter incoming connections to defer it before accept(2)
462463
///
464+
/// an empty name allows to remove this connection's filter
463465
#[cfg(any(doc, target_os = "netbsd", target_os = "freebsd"))]
464466
#[unstable(feature = "acceptfilter", issue = "none")]
465467
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)