Skip to content
New issue

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

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

Already on GitHub? # to your account

socket::sockopt adding SOL_FILTER level options for illumos. #2611

Merged
merged 3 commits into from
Mar 25, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/2611.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `FilAttach` and `FilDetach` to socket::sockopt for Illumos
2 changes: 1 addition & 1 deletion src/sys/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2466,7 +2466,7 @@ pub trait GetSockOpt: Copy {

/// Represents a socket option that can be set.
pub trait SetSockOpt: Clone {
type Val;
type Val: ?Sized;

/// Set the value of this socket option on the given socket.
fn set<F: AsFd>(&self, fd: &F, val: &Self::Val) -> Result<()>;
Expand Down
60 changes: 56 additions & 4 deletions src/sys/socket/sockopt.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Socket options as used by `setsockopt` and `getsockopt`.
#[cfg(linux_android)]
#[cfg(any(linux_android, target_os = "illumos"))]
use super::SetSockOpt;
use crate::sys::time::TimeVal;
#[cfg(linux_android)]
#[cfg(any(linux_android, target_os = "illumos"))]
use crate::{errno::Errno, Result};
use cfg_if::cfg_if;
use libc::{self, c_int, c_void, socklen_t};
Expand All @@ -11,7 +11,7 @@ use std::ffi::CString;
use std::ffi::{CStr, OsStr, OsString};
use std::mem::{self, MaybeUninit};
use std::os::unix::ffi::OsStrExt;
#[cfg(linux_android)]
#[cfg(any(linux_android, target_os = "illumos"))]
use std::os::unix::io::{AsFd, AsRawFd};

// Constants
Expand Down Expand Up @@ -1482,6 +1482,58 @@ impl SetSockOpt for TcpTlsRx {
}
}

#[cfg(target_os = "illumos")]
#[derive(Copy, Clone, Debug)]
/// Attach a named filter to this socket to be able to
/// defer when anough byte had been buffered by the kernel
pub struct FilterAttach;

#[cfg(target_os = "illumos")]
impl SetSockOpt for FilterAttach {
type Val = OsStr;

fn set<F: AsFd>(&self, fd: &F, val: &Self::Val) -> Result<()> {
if val.len() > libc::FILNAME_MAX as usize {
return Err(Errno::EINVAL);
}
unsafe {
let res = libc::setsockopt(
fd.as_fd().as_raw_fd(),
libc::SOL_FILTER,
libc::FIL_ATTACH,
val.as_bytes().as_ptr().cast(),
val.len() as libc::socklen_t,
);
Errno::result(res).map(drop)
}
}
}

#[cfg(target_os = "illumos")]
#[derive(Copy, Clone, Debug)]
/// Detach a socket filter previously attached with FIL_ATTACH
pub struct FilterDetach;

#[cfg(target_os = "illumos")]
impl SetSockOpt for FilterDetach {
type Val = OsStr;

fn set<F: AsFd>(&self, fd: &F, val: &Self::Val) -> Result<()> {
if val.len() > libc::FILNAME_MAX as usize {
return Err(Errno::EINVAL);
}
unsafe {
let res = libc::setsockopt(
fd.as_fd().as_raw_fd(),
libc::SOL_FILTER,
libc::FIL_DETACH,
val.as_bytes().as_ptr().cast(),
val.len() as libc::socklen_t,
);
Errno::result(res).map(drop)
}
}
}
/*
*
* ===== Accessor helpers =====
Expand Down Expand Up @@ -1799,7 +1851,7 @@ pub struct SetOsString<'a> {
val: &'a OsStr,
}

#[cfg(any(target_os = "freebsd", linux_android))]
#[cfg(any(target_os = "freebsd", linux_android, target_os = "illumos"))]
impl<'a> Set<'a, OsString> for SetOsString<'a> {
fn new(val: &OsString) -> SetOsString {
SetOsString {
Expand Down
23 changes: 23 additions & 0 deletions test/sys/test_sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1168,3 +1168,26 @@ fn test_exclbind() {
Err(Errno::EADDRINUSE)
);
}

#[cfg(target_os = "illumos")]
#[test]
fn test_solfilter() {
use nix::errno::Errno;
let s = socket(
AddressFamily::Inet,
SockType::Stream,
SockFlag::empty(),
SockProtocol::Tcp,
)
.unwrap();
let data = std::ffi::OsStr::new("httpf");
let attach = sockopt::FilterAttach;
let detach = sockopt::FilterDetach;

// These 2 options won't work unless the needed kernel module is installed:
// https://github.com/nix-rust/nix/pull/2611#issuecomment-2750237782
//
// So we only test the binding here
assert_eq!(Err(Errno::ENOENT), setsockopt(&s, attach, data));
assert_eq!(Err(Errno::ENOENT), setsockopt(&s, detach, data));
}