Skip to content

Commit 1109e5f

Browse files
committed
std::net: adding acceptfilter feature for netbsd/freebsd.
similar to linux's ext deferaccept, to filter incoming connections before accept.
1 parent a128516 commit 1109e5f

File tree

1 file changed

+22
-0
lines changed
  • library/std/src/sys/pal/unix

1 file changed

+22
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,28 @@ impl Socket {
453453
Ok(raw as u32)
454454
}
455455

456+
#[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
457+
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;
462+
}
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)
466+
}
467+
468+
#[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
469+
pub fn acceptfilter(&self) -> io::Result<&CStr> {
470+
let arg: libc::accept_filter_arg =
471+
getsockopt(self, libc::SOL_SOCKET, libc::SO_ACCEPTFILTER)?;
472+
let s: &[u8] =
473+
unsafe { core::slice::from_raw_parts(arg.af_name.as_ptr() as *const u8, 16) };
474+
let name = CStr::from_bytes_with_nul(s).unwrap();
475+
Ok(name)
476+
}
477+
456478
#[cfg(any(target_os = "android", target_os = "linux",))]
457479
pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
458480
setsockopt(self, libc::SOL_SOCKET, libc::SO_PASSCRED, passcred as libc::c_int)

0 commit comments

Comments
 (0)