Skip to content

Commit e1e3edc

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

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/sys/unix.rs

Lines changed: 20 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,23 @@ impl crate::Socket {
28142815
}
28152816
}
28162817

2818+
/// Berkeley Packet Filter (BPF).
2819+
///
2820+
/// See [`Socket::attach_filter[
2821+
#[cfg(all(feature = "all", any(target_os = "linux", target_os = "android")))]
2822+
#[repr(transparent)]
2823+
pub struct SockFilter {
2824+
filter: libc::sock_filter,
2825+
}
2826+
2827+
#[cfg(all(feature = "all", any(target_os = "linux", target_os = "android")))]
2828+
impl SockFilter {
2829+
/// Create new `SockFilter`.
2830+
pub fn new(code: u16, jt: u8, jf: u8, k: u32) -> SockFilter {
2831+
SockFilter { code, jt, jf, k }
2832+
}
2833+
}
2834+
28172835
/// See [`Socket::dccp_available_ccids`].
28182836
#[cfg(all(feature = "all", target_os = "linux"))]
28192837
#[derive(Debug)]

0 commit comments

Comments
 (0)