Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std::net: adding acceptfilter feature for netbsd/freebsd. #121881

Merged
merged 1 commit into from Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions library/std/src/os/freebsd/net.rs
Expand Up @@ -2,6 +2,7 @@

#![unstable(feature = "unix_socket_ancillary_data", issue = "76915")]

use crate::ffi::CStr;
use crate::io;
use crate::os::unix::net;
use crate::sealed::Sealed;
Expand Down Expand Up @@ -40,6 +41,15 @@ pub trait UnixSocketExt: Sealed {
/// ```
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
fn set_local_creds_persistent(&self, local_creds_persistent: bool) -> io::Result<()>;

/// Get a filter name if one had been set previously on the socket.
#[unstable(feature = "acceptfilter", issue = "121891")]
fn acceptfilter(&self) -> io::Result<&CStr>;

/// Set or disable a filter on the socket to filter incoming connections
/// to defer it before accept(2)
#[unstable(feature = "acceptfilter", issue = "121891")]
fn set_acceptfilter(&self, name: &CStr) -> io::Result<()>;
}

#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
Expand All @@ -51,6 +61,14 @@ impl UnixSocketExt for net::UnixDatagram {
fn set_local_creds_persistent(&self, local_creds_persistent: bool) -> io::Result<()> {
self.as_inner().set_local_creds_persistent(local_creds_persistent)
}

fn acceptfilter(&self) -> io::Result<&CStr> {
self.as_inner().acceptfilter()
}

fn set_acceptfilter(&self, name: &CStr) -> io::Result<()> {
self.as_inner().set_acceptfilter(name)
}
}

#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
Expand All @@ -62,4 +80,12 @@ impl UnixSocketExt for net::UnixStream {
fn set_local_creds_persistent(&self, local_creds_persistent: bool) -> io::Result<()> {
self.as_inner().set_local_creds_persistent(local_creds_persistent)
}

fn acceptfilter(&self) -> io::Result<&CStr> {
self.as_inner().acceptfilter()
}

fn set_acceptfilter(&self, name: &CStr) -> io::Result<()> {
self.as_inner().set_acceptfilter(name)
}
}
26 changes: 26 additions & 0 deletions library/std/src/os/netbsd/net.rs
Expand Up @@ -2,6 +2,7 @@

#![unstable(feature = "unix_socket_ancillary_data", issue = "76915")]

use crate::ffi::CStr;
use crate::io;
use crate::os::unix::net;
use crate::sealed::Sealed;
Expand Down Expand Up @@ -40,6 +41,15 @@ pub trait UnixSocketExt: Sealed {
/// ```
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
fn set_local_creds(&self, local_creds: bool) -> io::Result<()>;

/// Get a filter name if one had been set previously on the socket.
#[unstable(feature = "acceptfilter", issue = "121891")]
fn acceptfilter(&self) -> io::Result<&CStr>;

/// Set or disable a filter on the socket to filter incoming connections
/// to defer it before accept(2)
#[unstable(feature = "acceptfilter", issue = "121891")]
fn set_acceptfilter(&self, name: &CStr) -> io::Result<()>;
}

#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
Expand All @@ -51,6 +61,14 @@ impl UnixSocketExt for net::UnixDatagram {
fn set_local_creds(&self, local_creds: bool) -> io::Result<()> {
self.as_inner().set_local_creds(local_creds)
}

fn acceptfilter(&self) -> io::Result<&CStr> {
self.as_inner().acceptfilter()
}

fn set_acceptfilter(&self, name: &CStr) -> io::Result<()> {
self.as_inner().set_acceptfilter(name)
}
}

#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
Expand All @@ -62,4 +80,12 @@ impl UnixSocketExt for net::UnixStream {
fn set_local_creds(&self, local_creds: bool) -> io::Result<()> {
self.as_inner().set_local_creds(local_creds)
}

fn acceptfilter(&self) -> io::Result<&CStr> {
self.as_inner().acceptfilter()
}

fn set_acceptfilter(&self, name: &CStr) -> io::Result<()> {
self.as_inner().set_acceptfilter(name)
}
}
31 changes: 31 additions & 0 deletions library/std/src/sys/pal/unix/net.rs
Expand Up @@ -453,6 +453,37 @@ impl Socket {
Ok(raw as u32)
}

#[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
pub fn set_acceptfilter(&self, name: &CStr) -> io::Result<()> {
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;
Copy link
Member

@taiki-e taiki-e Mar 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be as libc::c_char. (c_char is u8 by default on most non-Apple/non-Windows ARM/AArch64/PowerPC/RISC-V/s390x/Hexagon targets)

# with empty crate
$ cargo check -Z build-std --target aarch64-unknown-freebsd
error[E0308]: mismatched types
   --> /Users/taiki/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/sys/pal/unix/net.rs:465:27
    |
461 |             for (src, dst) in name.to_bytes().iter().zip(&mut buf[..AF_NAME_MAX - 1]) {
    |                                                               --- here the type of `buf` is inferred to be `[i8; 16]`
...
465 |             arg.af_name = buf;
    |             -----------   ^^^ expected `[u8; 16]`, found `[i8; 16]`
    |             |
    |             expected due to the type of this binding
    |
    = note: expected array `[u8; 16]`
               found array `[i8; 16]`

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened #122983 to fix this.

}
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,
)
}
}

#[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
pub fn acceptfilter(&self) -> io::Result<&CStr> {
let arg: libc::accept_filter_arg =
getsockopt(self, libc::SOL_SOCKET, libc::SO_ACCEPTFILTER)?;
let s: &[u8] =
unsafe { core::slice::from_raw_parts(arg.af_name.as_ptr() as *const u8, 16) };
let name = CStr::from_bytes_with_nul(s).unwrap();
Ok(name)
}

#[cfg(any(target_os = "android", target_os = "linux",))]
pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
setsockopt(self, libc::SOL_SOCKET, libc::SO_PASSCRED, passcred as libc::c_int)
Expand Down