Skip to content

Commit 915fcf8

Browse files
committed
changes from feedback
1 parent c005f3e commit 915fcf8

File tree

1 file changed

+79
-22
lines changed

1 file changed

+79
-22
lines changed

src/sys/socket/sockopt.rs

Lines changed: 79 additions & 22 deletions
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
@@ -20,6 +20,10 @@ use std::os::unix::io::{AsFd, AsRawFd};
2020
#[cfg(feature = "net")]
2121
const TCP_CA_NAME_MAX: usize = 16;
2222

23+
#[cfg(target_os = "illumos")]
24+
#[cfg(feature = "net")]
25+
const FILNAME_MAX: usize = 32;
26+
2327
/// Helper for implementing `SetSockOpt` for a given socket option. See
2428
/// [`::sys::socket::SetSockOpt`](sys/socket/trait.SetSockOpt.html).
2529
///
@@ -987,25 +991,6 @@ sockopt_impl!(
987991
libc::SO_ACCEPTFILTER,
988992
libc::accept_filter_arg
989993
);
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-
);
1009994
#[cfg(target_os = "linux")]
1010995
sockopt_impl!(
1011996
/// Set the mark for each packet sent through this socket (similar to the
@@ -1501,6 +1486,78 @@ impl SetSockOpt for TcpTlsRx {
15011486
}
15021487
}
15031488

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

0 commit comments

Comments
 (0)