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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

devnexen
Copy link
Contributor

@devnexen devnexen commented Mar 2, 2025

Respectively FIL_ATTACH/FIL_DETACH to bind/unbind a filter to a socket to for example delay connection acceptance until data (datafilt) is received.

devnexen added 2 commits March 2, 2025 07:43
Respectively FIL_ATTACH/FIL_DETACH to bind/unbind a filter
to a socket to for example delay connection acceptance
until data (datafilt) is received.
@devnexen devnexen marked this pull request as ready for review March 2, 2025 09:29
SetOnly,
libc::SOL_FILTER,
libc::FIL_ATTACH,
SetOsString<'static>
Copy link
Member

Choose a reason for hiding this comment

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

I am thinking if this 'static lifetime would be too restrictive? I do not see a way to specify lifetime parameter using the macro, maybe we can implement the nix::sys::socket::SetSockOpt trait manually?

@devnexen devnexen force-pushed the illumos_filter_socket branch from f3c875b to 366ca97 Compare March 24, 2025 14:52
Comment on lines 1493 to 1521
type Val = [u8; libc::FILNAME_MAX as usize];

fn set<F: AsFd>(&self, fd: &F, val: &[u8; libc::FILNAME_MAX as usize]) -> Result<()> {
unsafe {
let res = libc::setsockopt(
fd.as_fd().as_raw_fd(),
libc::SOL_FILTER,
libc::FIL_ATTACH,
val.as_ptr().cast(),
val.len() as libc::socklen_t,
Copy link
Member

Choose a reason for hiding this comment

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

We are passing the whole buffer to the syscall, is this something expected?

BTW, the below code should mirror the original implementation:

#[derive(Clone)]
pub struct FilterAttach<'a> {
    _marker: std::marker::PhantomData<&'a ()>
}

impl<'a> FilterAttach<'a> {
    pub fn new() -> Self {
        Self {
            _marker: std::marker::PhantomData,
        }
    }
}

impl<'a> SetSockOpt for FilterAttach<'a> {
    type Val = &'a OsStr;

    fn set<F: std::os::unix::prelude::AsFd>(&self, fd: &F, val: &Self::Val) -> nix::Result<()> {
        unsafe {
            let res = libc::setsockopt(
                fd.as_fd().as_raw_fd(),
                libc::SOL_FILTER,
                libc::FIL_ATTACH,
                val.as_ptr().cast(),
                val.len() as libc::socklen_t,
            );
            Errno::result(res).map(drop)
        }
    }
}

@devnexen devnexen force-pushed the illumos_filter_socket branch 2 times, most recently from 915fcf8 to 21690ce Compare March 25, 2025 06:10
@@ -20,6 +20,10 @@ use std::os::unix::io::{AsFd, AsRawFd};
#[cfg(feature = "net")]
const TCP_CA_NAME_MAX: usize = 16;

#[cfg(target_os = "illumos")]
#[cfg(feature = "net")]
const FILNAME_MAX: usize = 32;
Copy link
Member

Choose a reason for hiding this comment

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

Please use the constant from the libc crate:)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes I tried to avoid the cast ;) but ok

@SteveLauC
Copy link
Member

Can we have tests for this?

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants