Skip to content

Commit 7b06dba

Browse files
authored
Unrolled build for rust-lang#121881
Rollup merge of rust-lang#121881 - devnexen:bsd_acceptfilter, r=Amanieu std::net: adding acceptfilter feature for netbsd/freebsd. similar to linux's ext deferaccept, to filter incoming connections before accept.
2 parents 0ad927c + 19cb05f commit 7b06dba

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

library/std/src/os/freebsd/net.rs

+26
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
#![unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
44

5+
use crate::ffi::CStr;
56
use crate::io;
67
use crate::os::unix::net;
78
use crate::sealed::Sealed;
@@ -40,6 +41,15 @@ pub trait UnixSocketExt: Sealed {
4041
/// ```
4142
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
4243
fn set_local_creds_persistent(&self, local_creds_persistent: bool) -> io::Result<()>;
44+
45+
/// Get a filter name if one had been set previously on the socket.
46+
#[unstable(feature = "acceptfilter", issue = "121891")]
47+
fn acceptfilter(&self) -> io::Result<&CStr>;
48+
49+
/// Set or disable a filter on the socket to filter incoming connections
50+
/// to defer it before accept(2)
51+
#[unstable(feature = "acceptfilter", issue = "121891")]
52+
fn set_acceptfilter(&self, name: &CStr) -> io::Result<()>;
4353
}
4454

4555
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
@@ -51,6 +61,14 @@ impl UnixSocketExt for net::UnixDatagram {
5161
fn set_local_creds_persistent(&self, local_creds_persistent: bool) -> io::Result<()> {
5262
self.as_inner().set_local_creds_persistent(local_creds_persistent)
5363
}
64+
65+
fn acceptfilter(&self) -> io::Result<&CStr> {
66+
self.as_inner().acceptfilter()
67+
}
68+
69+
fn set_acceptfilter(&self, name: &CStr) -> io::Result<()> {
70+
self.as_inner().set_acceptfilter(name)
71+
}
5472
}
5573

5674
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
@@ -62,4 +80,12 @@ impl UnixSocketExt for net::UnixStream {
6280
fn set_local_creds_persistent(&self, local_creds_persistent: bool) -> io::Result<()> {
6381
self.as_inner().set_local_creds_persistent(local_creds_persistent)
6482
}
83+
84+
fn acceptfilter(&self) -> io::Result<&CStr> {
85+
self.as_inner().acceptfilter()
86+
}
87+
88+
fn set_acceptfilter(&self, name: &CStr) -> io::Result<()> {
89+
self.as_inner().set_acceptfilter(name)
90+
}
6591
}

library/std/src/os/netbsd/net.rs

+26
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
#![unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
44

5+
use crate::ffi::CStr;
56
use crate::io;
67
use crate::os::unix::net;
78
use crate::sealed::Sealed;
@@ -40,6 +41,15 @@ pub trait UnixSocketExt: Sealed {
4041
/// ```
4142
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
4243
fn set_local_creds(&self, local_creds: bool) -> io::Result<()>;
44+
45+
/// Get a filter name if one had been set previously on the socket.
46+
#[unstable(feature = "acceptfilter", issue = "121891")]
47+
fn acceptfilter(&self) -> io::Result<&CStr>;
48+
49+
/// Set or disable a filter on the socket to filter incoming connections
50+
/// to defer it before accept(2)
51+
#[unstable(feature = "acceptfilter", issue = "121891")]
52+
fn set_acceptfilter(&self, name: &CStr) -> io::Result<()>;
4353
}
4454

4555
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
@@ -51,6 +61,14 @@ impl UnixSocketExt for net::UnixDatagram {
5161
fn set_local_creds(&self, local_creds: bool) -> io::Result<()> {
5262
self.as_inner().set_local_creds(local_creds)
5363
}
64+
65+
fn acceptfilter(&self) -> io::Result<&CStr> {
66+
self.as_inner().acceptfilter()
67+
}
68+
69+
fn set_acceptfilter(&self, name: &CStr) -> io::Result<()> {
70+
self.as_inner().set_acceptfilter(name)
71+
}
5472
}
5573

5674
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
@@ -62,4 +80,12 @@ impl UnixSocketExt for net::UnixStream {
6280
fn set_local_creds(&self, local_creds: bool) -> io::Result<()> {
6381
self.as_inner().set_local_creds(local_creds)
6482
}
83+
84+
fn acceptfilter(&self) -> io::Result<&CStr> {
85+
self.as_inner().acceptfilter()
86+
}
87+
88+
fn set_acceptfilter(&self, name: &CStr) -> io::Result<()> {
89+
self.as_inner().set_acceptfilter(name)
90+
}
6591
}

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

+31
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,37 @@ 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+
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+
)
474+
}
475+
}
476+
477+
#[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
478+
pub fn acceptfilter(&self) -> io::Result<&CStr> {
479+
let arg: libc::accept_filter_arg =
480+
getsockopt(self, libc::SOL_SOCKET, libc::SO_ACCEPTFILTER)?;
481+
let s: &[u8] =
482+
unsafe { core::slice::from_raw_parts(arg.af_name.as_ptr() as *const u8, 16) };
483+
let name = CStr::from_bytes_with_nul(s).unwrap();
484+
Ok(name)
485+
}
486+
456487
#[cfg(any(target_os = "android", target_os = "linux",))]
457488
pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
458489
setsockopt(self, libc::SOL_SOCKET, libc::SO_PASSCRED, passcred as libc::c_int)

0 commit comments

Comments
 (0)