#![warn(missing_docs, clippy::pedantic)]
#![cfg_attr(not(target_os = "linux"), allow(unused_imports, dead_code))]
#![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::expect_used))]
#![allow(
missing_docs,
unused_variables,
clippy::wildcard_imports,
clippy::cast_possible_truncation,
clippy::cast_possible_wrap,
clippy::cast_lossless,
clippy::cast_sign_loss,
clippy::cast_ptr_alignment,
clippy::borrow_as_ptr,
clippy::ptr_as_ptr,
clippy::ref_as_ptr,
clippy::semicolon_if_nothing_returned,
clippy::missing_errors_doc
)]
pub mod assembler;
pub mod compiler;
pub mod loader;
#[cfg(unix)]
use std::os::unix::io::RawFd;
#[cfg(unix)]
pub fn attach_kernel_page_discard(pattern: &[u8], target_socket: RawFd) -> Result<(), AttachError> {
let insns = compiler::compile_literal_search(pattern)?;
let filter_fd = loader::load_filter(&insns)?;
loader::attach_to_socket(filter_fd, target_socket)?;
Ok(())
}
#[cfg(not(unix))]
pub fn attach_kernel_page_discard(pattern: &[u8], _target_socket: i32) -> Result<(), AttachError> {
let _ = pattern;
Err(AttachError::Io(std::io::Error::new(
std::io::ErrorKind::Unsupported,
"eBPF is only available on Linux",
)))
}
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum AttachError {
#[error(transparent)]
Compile(#[from] compiler::CompileError),
#[error("kernel operation failed: {0}")]
Io(#[from] std::io::Error),
}