Skip to content

Commit 023089c

Browse files
committed
Add SockFilter
A wrapper around libc::sock_filter, so we don't use any libc types in our API.
1 parent dd97053 commit 023089c

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,6 @@ compile_error!("Socket2 doesn't support the compile target");
186186
use sys::c_int;
187187

188188
pub use sockaddr::{sa_family_t, socklen_t, SockAddr, SockAddrStorage};
189-
pub use socket::Socket;
190-
pub use sockref::SockRef;
191-
192189
#[cfg(not(any(
193190
target_os = "haiku",
194191
target_os = "illumos",
@@ -197,6 +194,10 @@ pub use sockref::SockRef;
197194
target_os = "solaris",
198195
)))]
199196
pub use socket::InterfaceIndexOrAddress;
197+
pub use socket::Socket;
198+
pub use sockref::SockRef;
199+
#[cfg(all(feature = "all", any(target_os = "linux", target_os = "android")))]
200+
pub use sys::SockFilter;
200201

201202
/// Specification of the communication domain for a socket.
202203
///

src/sys/unix.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2459,16 +2459,17 @@ impl crate::Socket {
24592459
}
24602460
}
24612461

2462-
/// Attach Berkeley Packet Filter(BPF) on this socket.
2462+
/// Attach Berkeley Packet Filter (BPF) on this socket.
24632463
///
24642464
/// BPF allows a user-space program to attach a filter onto any socket
24652465
/// and allow or disallow certain types of data to come through the socket.
24662466
///
24672467
/// For more information about this option, see [filter](https://www.kernel.org/doc/html/v5.12/networking/filter.html)
24682468
#[cfg(all(feature = "all", any(target_os = "linux", target_os = "android")))]
2469-
pub fn attach_filter(&self, filters: &[libc::sock_filter]) -> io::Result<()> {
2469+
pub fn attach_filter(&self, filters: &[SockFilter]) -> io::Result<()> {
24702470
let prog = libc::sock_fprog {
24712471
len: filters.len() as u16,
2472+
// SAFETY: this is safe due to `repr(transparent)`.
24722473
filter: filters.as_ptr() as *mut _,
24732474
};
24742475

@@ -2814,6 +2815,34 @@ impl crate::Socket {
28142815
}
28152816
}
28162817

2818+
/// Berkeley Packet Filter (BPF).
2819+
///
2820+
/// See [`Socket::attach_filter`].
2821+
///
2822+
/// [`Socket::attach_filter`]: crate::Socket::attach_filter
2823+
#[cfg(all(feature = "all", any(target_os = "linux", target_os = "android")))]
2824+
#[repr(transparent)]
2825+
pub struct SockFilter {
2826+
filter: libc::sock_filter,
2827+
}
2828+
2829+
#[cfg(all(feature = "all", any(target_os = "linux", target_os = "android")))]
2830+
impl SockFilter {
2831+
/// Create new `SockFilter`.
2832+
pub fn new(code: u16, jt: u8, jf: u8, k: u32) -> SockFilter {
2833+
SockFilter {
2834+
filter: libc::sock_filter { code, jt, jf, k },
2835+
}
2836+
}
2837+
}
2838+
2839+
#[cfg(all(feature = "all", any(target_os = "linux", target_os = "android")))]
2840+
impl std::fmt::Debug for SockFilter {
2841+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2842+
f.debug_struct("SockFilter").finish_non_exhaustive()
2843+
}
2844+
}
2845+
28172846
/// See [`Socket::dccp_available_ccids`].
28182847
#[cfg(all(feature = "all", target_os = "linux"))]
28192848
#[derive(Debug)]

0 commit comments

Comments
 (0)