use std::io;
#[cfg(doc)]
use std::marker::PhantomData;
#[cfg(not(target_os = "windows"))]
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, RawFd};
#[cfg(target_os = "openbsd")]
use crate::bpf::RxFrameImpl;
#[cfg(any(
target_os = "dragonfly",
target_os = "freebsd",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"
))]
use crate::bpf::SnifferImpl;
#[cfg(target_os = "linux")]
use crate::linux::{RxFrameImpl, SnifferImpl};
#[cfg(all(target_os = "windows", feature = "npcap"))]
use crate::npcap::SnifferImpl;
use crate::{filter::PacketFilter, Interface};
pub struct Sniffer {
#[cfg(not(doc))]
inner: SnifferImpl,
}
impl Sniffer {
#[inline]
pub fn new(iface: Interface) -> io::Result<Self> {
Ok(Self {
inner: SnifferImpl::new(iface)?,
})
}
#[cfg(any(doc, target_os = "freebsd", target_os = "linux"))]
#[inline]
pub fn new_with_size(iface: Interface, ring_size: usize) -> io::Result<Self> {
Ok(Self {
inner: SnifferImpl::new_with_size(iface, ring_size)?,
})
}
#[inline]
pub fn activate(&mut self, filter: Option<PacketFilter>) -> io::Result<()> {
self.inner.activate(filter)
}
pub fn deactivate(&mut self) -> io::Result<()> {
self.inner.deactivate()
}
#[inline]
pub fn nonblocking(&self) -> io::Result<bool> {
self.inner.nonblocking()
}
#[inline]
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
self.inner.set_nonblocking(nonblocking)
}
#[inline]
pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
self.inner.send(buf)
}
#[inline]
pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.recv(buf)
}
#[cfg(any(doc, target_os = "linux", target_os = "freebsd"))]
#[inline]
pub fn mapped_recv(&mut self) -> Option<RxFrame<'_>> {
Some(RxFrame {
inner: self.inner.mapped_recv()?,
})
}
}
#[cfg(not(target_os = "windows"))]
impl AsRawFd for Sniffer {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.inner.as_raw_fd()
}
}
#[cfg(not(target_os = "windows"))]
impl AsFd for Sniffer {
fn as_fd(&self) -> BorrowedFd<'_> {
self.inner.as_fd()
}
}
#[cfg(any(doc, target_os = "linux", target_os = "freebsd"))]
pub struct RxFrame<'a> {
#[cfg(not(doc))]
inner: RxFrameImpl<'a>,
#[cfg(doc)]
_phantom: PhantomData<&'a ()>,
}
#[cfg(any(doc, target_os = "linux", target_os = "freebsd"))]
impl RxFrame<'_> {
pub fn data(&self) -> &[u8] {
self.inner.data()
}
pub fn data_mut(&mut self) -> &mut [u8] {
self.inner.data_mut()
}
}