Skip to content

Commit 754cf92

Browse files
committed
adding the possiblity to remove the filter.
fixing build too.
1 parent 1109e5f commit 754cf92

File tree

3 files changed

+100
-7
lines changed

3 files changed

+100
-7
lines changed

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

+2
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;

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

+82
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,88 @@ impl UnixStream {
386386
self.0.set_nonblocking(nonblocking)
387387
}
388388

389+
<<<<<<< HEAD
390+
=======
391+
/// Moves the socket to pass unix credentials as control message in [`SocketAncillary`].
392+
///
393+
/// Set the socket option `SO_PASSCRED`.
394+
///
395+
/// # Examples
396+
///
397+
#[cfg_attr(
398+
any(
399+
target_os = "android",
400+
target_os = "linux",
401+
target_os = "netbsd",
402+
target_os = "freebsd"
403+
),
404+
doc = "```no_run"
405+
)]
406+
#[cfg_attr(
407+
not(any(
408+
target_os = "android",
409+
target_os = "linux",
410+
target_os = "netbsd",
411+
target_os = "freebsd"
412+
)),
413+
doc = "```ignore"
414+
)]
415+
/// #![feature(unix_socket_ancillary_data)]
416+
/// use std::os::unix::net::UnixStream;
417+
///
418+
/// fn main() -> std::io::Result<()> {
419+
/// let socket = UnixStream::connect("/tmp/sock")?;
420+
/// socket.set_passcred(true).expect("Couldn't set passcred");
421+
/// Ok(())
422+
/// }
423+
/// ```
424+
#[cfg(any(
425+
doc,
426+
target_os = "android",
427+
target_os = "linux",
428+
target_os = "netbsd",
429+
target_os = "freebsd"
430+
))]
431+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
432+
pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
433+
self.0.set_passcred(passcred)
434+
}
435+
436+
/// Get the current value of the socket for passing unix credentials in [`SocketAncillary`].
437+
/// This value can be change by [`set_passcred`].
438+
///
439+
/// Get the socket option `SO_PASSCRED`.
440+
///
441+
/// [`set_passcred`]: UnixStream::set_passcred
442+
#[cfg(any(
443+
doc,
444+
target_os = "android",
445+
target_os = "linux",
446+
target_os = "netbsd",
447+
target_os = "freebsd"
448+
))]
449+
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
450+
pub fn passcred(&self) -> io::Result<bool> {
451+
self.0.passcred()
452+
}
453+
454+
/// Set a filter name on the socket to filter incoming connections to defer it before accept(2)
455+
///
456+
/// an empty name allows to remove this connection's filter
457+
#[cfg(any(doc, target_os = "netbsd", target_os = "freebsd"))]
458+
#[unstable(feature = "acceptfilter", issue = "none")]
459+
pub fn set_acceptfilter(&self, name: &CStr) -> io::Result<()> {
460+
self.0.set_acceptfilter(name)
461+
}
462+
463+
/// Get a filter name if one had been set previously on the socket.
464+
///
465+
#[cfg(any(doc, target_os = "netbsd", target_os = "freebsd"))]
466+
#[unstable(feature = "acceptfilter", issue = "none")]
467+
pub fn acceptfilter(&self) -> io::Result<&CStr> {
468+
self.0.acceptfilter()
469+
}
470+
>>>>>>> 3257f18365a (adding the possiblity to remove the filter.)
389471
/// Set the id of the socket for network filtering purpose
390472
///
391473
#[cfg_attr(

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)