Skip to content

Commit 410d712

Browse files
committed
changes from feedback
1 parent c005f3e commit 410d712

File tree

2 files changed

+111
-22
lines changed

2 files changed

+111
-22
lines changed

src/sys/socket/sockopt.rs

+93-22
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Socket options as used by `setsockopt` and `getsockopt`.
2-
#[cfg(linux_android)]
2+
#[cfg(any(linux_android, target_os = "illumos"))]
33
use super::SetSockOpt;
44
use crate::sys::time::TimeVal;
5-
#[cfg(linux_android)]
5+
#[cfg(any(linux_android, target_os = "illumos"))]
66
use crate::{errno::Errno, Result};
77
use cfg_if::cfg_if;
88
use libc::{self, c_int, c_void, socklen_t};
@@ -11,7 +11,7 @@ use std::ffi::CString;
1111
use std::ffi::{CStr, OsStr, OsString};
1212
use std::mem::{self, MaybeUninit};
1313
use std::os::unix::ffi::OsStrExt;
14-
#[cfg(linux_android)]
14+
#[cfg(any(linux_android, target_os = "illumos"))]
1515
use std::os::unix::io::{AsFd, AsRawFd};
1616

1717
// Constants
@@ -987,25 +987,6 @@ sockopt_impl!(
987987
libc::SO_ACCEPTFILTER,
988988
libc::accept_filter_arg
989989
);
990-
#[cfg(target_os = "illumos")]
991-
sockopt_impl!(
992-
/// Attach a named filter to this socket to be able to
993-
/// defer when anough byte had been buffered by the kernel
994-
FilAttach,
995-
SetOnly,
996-
libc::SOL_FILTER,
997-
libc::FIL_ATTACH,
998-
SetOsString<'static>
999-
);
1000-
#[cfg(target_os = "illumos")]
1001-
sockopt_impl!(
1002-
/// Detach a socket filter previously attached with FIL_ATTACH
1003-
FilDetach,
1004-
SetOnly,
1005-
libc::SOL_FILTER,
1006-
libc::FIL_DETACH,
1007-
SetOsString<'static>
1008-
);
1009990
#[cfg(target_os = "linux")]
1010991
sockopt_impl!(
1011992
/// Set the mark for each packet sent through this socket (similar to the
@@ -1501,6 +1482,96 @@ impl SetSockOpt for TcpTlsRx {
15011482
}
15021483
}
15031484

1485+
#[cfg(target_os = "illumos")]
1486+
#[derive(Copy, Clone, Debug)]
1487+
/// Attach a named filter to this socket to be able to
1488+
/// defer when anough byte had been buffered by the kernel
1489+
pub struct FilterAttach<'a> {
1490+
_marker: std::marker::PhantomData<&'a ()>,
1491+
}
1492+
1493+
#[cfg(target_os = "illumos")]
1494+
impl<'a> FilterAttach<'a> {
1495+
#[allow(missing_docs)]
1496+
pub fn new() -> Self {
1497+
Self {
1498+
_marker: std::marker::PhantomData,
1499+
}
1500+
}
1501+
}
1502+
1503+
#[cfg(target_os = "illumos")]
1504+
impl<'a> Default for FilterAttach<'a> {
1505+
fn default() -> Self {
1506+
Self::new()
1507+
}
1508+
}
1509+
1510+
#[cfg(target_os = "illumos")]
1511+
impl<'a> SetSockOpt for FilterAttach<'a> {
1512+
type Val = &'a OsStr;
1513+
1514+
fn set<F: AsFd>(&self, fd: &F, val: &Self::Val) -> Result<()> {
1515+
if val.len() > libc::FILNAME_MAX as usize {
1516+
return Err(Errno::EINVAL);
1517+
}
1518+
unsafe {
1519+
let res = libc::setsockopt(
1520+
fd.as_fd().as_raw_fd(),
1521+
libc::SOL_FILTER,
1522+
libc::FIL_ATTACH,
1523+
val.to_string_lossy().as_ptr().cast(),
1524+
val.len() as libc::socklen_t,
1525+
);
1526+
Errno::result(res).map(drop)
1527+
}
1528+
}
1529+
}
1530+
1531+
#[cfg(target_os = "illumos")]
1532+
#[derive(Copy, Clone, Debug)]
1533+
/// Detach a socket filter previously attached with FIL_ATTACH
1534+
pub struct FilterDetach<'a> {
1535+
_marker: std::marker::PhantomData<&'a ()>,
1536+
}
1537+
1538+
#[cfg(target_os = "illumos")]
1539+
impl<'a> FilterDetach<'a> {
1540+
#[allow(missing_docs)]
1541+
pub fn new() -> Self {
1542+
Self {
1543+
_marker: std::marker::PhantomData,
1544+
}
1545+
}
1546+
}
1547+
1548+
#[cfg(target_os = "illumos")]
1549+
impl<'a> Default for FilterDetach<'a> {
1550+
fn default() -> Self {
1551+
Self::new()
1552+
}
1553+
}
1554+
1555+
#[cfg(target_os = "illumos")]
1556+
impl<'a> SetSockOpt for FilterDetach<'a> {
1557+
type Val = &'a OsStr;
1558+
1559+
fn set<F: AsFd>(&self, fd: &F, val: &Self::Val) -> Result<()> {
1560+
if val.len() > libc::FILNAME_MAX as usize {
1561+
return Err(Errno::EINVAL);
1562+
}
1563+
unsafe {
1564+
let res = libc::setsockopt(
1565+
fd.as_fd().as_raw_fd(),
1566+
libc::SOL_FILTER,
1567+
libc::FIL_DETACH,
1568+
val.to_string_lossy().as_ptr().cast(),
1569+
val.len() as libc::socklen_t,
1570+
);
1571+
Errno::result(res).map(drop)
1572+
}
1573+
}
1574+
}
15041575
/*
15051576
*
15061577
* ===== Accessor helpers =====

test/sys/test_sockopt.rs

+18
Original file line numberDiff line numberDiff line change
@@ -1168,3 +1168,21 @@ fn test_exclbind() {
11681168
Err(Errno::EADDRINUSE)
11691169
);
11701170
}
1171+
1172+
#[cfg(target_os = "illumos")]
1173+
#[test]
1174+
fn test_solfilter() {
1175+
use nix::errno::Errno;
1176+
let s = socket(
1177+
AddressFamily::Inet,
1178+
SockType::Stream,
1179+
SockFlag::empty(),
1180+
SockProtocol::Tcp,
1181+
)
1182+
.unwrap();
1183+
let data = std::ffi::OsStr::new("httpf");
1184+
let attach = sockopt::FilterAttach::<'static>::new();
1185+
let detach = sockopt::FilterDetach::<'static>::new();
1186+
assert_eq!(Err(Errno::ENOENT), setsockopt(&s, attach, &data));
1187+
assert_eq!(Err(Errno::ENOENT), setsockopt(&s, detach, &data));
1188+
}

0 commit comments

Comments
 (0)