Skip to content

Add SockFilter #581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,6 @@ compile_error!("Socket2 doesn't support the compile target");
use sys::c_int;

pub use sockaddr::{sa_family_t, socklen_t, SockAddr, SockAddrStorage};
pub use socket::Socket;
pub use sockref::SockRef;

#[cfg(not(any(
target_os = "haiku",
target_os = "illumos",
Expand All @@ -197,6 +194,12 @@ pub use sockref::SockRef;
target_os = "solaris",
)))]
pub use socket::InterfaceIndexOrAddress;
pub use socket::Socket;
pub use sockref::SockRef;
#[cfg(all(feature = "all", target_os = "linux"))]
pub use sys::CcidEndpoints;
#[cfg(all(feature = "all", any(target_os = "linux", target_os = "android")))]
pub use sys::SockFilter;

/// Specification of the communication domain for a socket.
///
Expand Down
35 changes: 33 additions & 2 deletions src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2459,16 +2459,17 @@ impl crate::Socket {
}
}

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

Expand Down Expand Up @@ -2814,7 +2815,37 @@ impl crate::Socket {
}
}

/// Berkeley Packet Filter (BPF).
///
/// See [`Socket::attach_filter`].
///
/// [`Socket::attach_filter`]: crate::Socket::attach_filter
#[cfg(all(feature = "all", any(target_os = "linux", target_os = "android")))]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Cygwin?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm using the same cfg as for the attach_filter method where this is used.

#[repr(transparent)]
pub struct SockFilter {
filter: libc::sock_filter,
}

#[cfg(all(feature = "all", any(target_os = "linux", target_os = "android")))]
impl SockFilter {
/// Create new `SockFilter`.
pub fn new(code: u16, jt: u8, jf: u8, k: u32) -> SockFilter {
SockFilter {
filter: libc::sock_filter { code, jt, jf, k },
}
}
}

#[cfg(all(feature = "all", any(target_os = "linux", target_os = "android")))]
impl std::fmt::Debug for SockFilter {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SockFilter").finish_non_exhaustive()
}
}

/// See [`Socket::dccp_available_ccids`].
///
/// [`Socket::dccp_available_ccids`]: crate::Socket::dccp_available_ccids
#[cfg(all(feature = "all", target_os = "linux"))]
#[derive(Debug)]
pub struct CcidEndpoints<const N: usize> {
Expand Down
Loading