use std::io;
use std::mem;
use std::os::unix::io::{AsRawFd, RawFd};
use libc;
use super::{ifreq, ifreq_for};
use crate::phy::Medium;
use crate::wire::ETHERNET_HEADER_LEN;
#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "netbsd",
target_os = "openbsd",
target_os = "freebsd"
))]
const BIOCSETIF: libc::c_ulong = 0x8020426c;
#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "netbsd",
target_os = "openbsd",
target_os = "freebsd"
))]
const BIOCGBLEN: libc::c_ulong = 0x40044266;
#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "netbsd",
target_os = "openbsd",
target_os = "freebsd"
))]
const BIOCIMMEDIATE: libc::c_ulong = 0x80044270;
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "netbsd"))]
const SIZEOF_BPF_HDR: usize = 18;
#[cfg(any(target_os = "openbsd", target_os = "freebsd"))]
const SIZEOF_BPF_HDR: usize = 24;
#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "netbsd",
target_os = "openbsd",
target_os = "freebsd"
))]
const BPF_HDRLEN: usize = (((SIZEOF_BPF_HDR + ETHERNET_HEADER_LEN) + mem::align_of::<u32>() - 1)
& !(mem::align_of::<u32>() - 1))
- ETHERNET_HEADER_LEN;
macro_rules! try_ioctl {
($fd:expr,$cmd:expr,$req:expr) => {
unsafe {
if libc::ioctl($fd, $cmd, $req) == -1 {
return Err(io::Error::last_os_error());
}
}
};
}
#[derive(Debug)]
pub struct BpfDevice {
fd: libc::c_int,
ifreq: ifreq,
}
impl AsRawFd for BpfDevice {
fn as_raw_fd(&self) -> RawFd {
self.fd
}
}
fn open_device() -> io::Result<libc::c_int> {
unsafe {
for i in 0..256 {
let dev = format!("/dev/bpf{}\0", i);
match libc::open(
dev.as_ptr() as *const libc::c_char,
libc::O_RDWR | libc::O_NONBLOCK,
) {
-1 => continue,
fd => return Ok(fd),
};
}
}
Err(io::Error::last_os_error())
}
impl BpfDevice {
pub fn new(name: &str, _medium: Medium) -> io::Result<BpfDevice> {
Ok(BpfDevice {
fd: open_device()?,
ifreq: ifreq_for(name),
})
}
pub fn bind_interface(&mut self) -> io::Result<()> {
try_ioctl!(self.fd, BIOCSETIF, &mut self.ifreq);
Ok(())
}
pub fn interface_mtu(&mut self) -> io::Result<usize> {
let mut bufsize: libc::c_int = 1;
try_ioctl!(self.fd, BIOCIMMEDIATE, &mut bufsize as *mut libc::c_int);
try_ioctl!(self.fd, BIOCGBLEN, &mut bufsize as *mut libc::c_int);
Ok(bufsize as usize)
}
pub fn recv(&mut self, buffer: &mut [u8]) -> io::Result<usize> {
unsafe {
let len = libc::read(
self.fd,
buffer.as_mut_ptr() as *mut libc::c_void,
buffer.len(),
);
if len == -1 || len < BPF_HDRLEN as isize {
return Err(io::Error::last_os_error());
}
let len = len as usize;
libc::memmove(
buffer.as_mut_ptr() as *mut libc::c_void,
&buffer[BPF_HDRLEN] as *const u8 as *const libc::c_void,
len - BPF_HDRLEN,
);
Ok(len)
}
}
pub fn send(&mut self, buffer: &[u8]) -> io::Result<usize> {
unsafe {
let len = libc::write(
self.fd,
buffer.as_ptr() as *const libc::c_void,
buffer.len(),
);
if len == -1 {
panic!("{:?}", io::Error::last_os_error())
}
Ok(len as usize)
}
}
}
impl Drop for BpfDevice {
fn drop(&mut self) {
unsafe {
libc::close(self.fd);
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
#[cfg(any(target_os = "macos", target_os = "netbsd"))]
fn test_aligned_bpf_hdr_len() {
assert_eq!(18, BPF_HDRLEN);
}
#[test]
#[cfg(target_os = "openbsd")]
fn test_aligned_bpf_hdr_len() {
assert_eq!(26, BPF_HDRLEN);
}
}