Commit remaining Burrow platform work

This commit is contained in:
Conrad Kramer 2026-03-31 23:35:36 -07:00
parent fff5475914
commit 7f280c08cf
48 changed files with 2508 additions and 1864 deletions

View file

@ -26,7 +26,7 @@ async fn generate(out_dir: &std::path::Path) -> anyhow::Result<()> {
println!("cargo:rerun-if-changed={}", binary_path.to_str().unwrap());
if let (Ok(..), Ok(..)) = (File::open(&bindings_path), File::open(&binary_path)) {
return Ok(())
return Ok(());
};
let archive = download(out_dir)

View file

@ -33,7 +33,7 @@ impl TunInterface {
Ok(result) => return result,
Err(_would_block) => {
tracing::debug!("WouldBlock");
continue
continue;
}
}
}

View file

@ -114,6 +114,10 @@ impl TunInterface {
ifname_to_string(buf)
}
pub(crate) fn packet_information_size(&self) -> usize {
4
}
#[throws]
#[instrument]
fn ifreq(&self) -> sys::ifreq {

View file

@ -73,6 +73,21 @@ impl TunInterface {
ifname_to_string(iff.ifr_name)
}
pub(crate) fn packet_information_size(&self) -> usize {
let mut iff = unsafe { mem::zeroed::<libc::ifreq>() };
match unsafe { sys::tun_get_iff(self.socket.as_raw_fd(), &mut iff) } {
Ok(_) => {
let flags = unsafe { iff.ifr_ifru.ifru_flags };
if flags & libc::IFF_NO_PI as i16 != 0 {
0
} else {
4
}
}
Err(_) => 4,
}
}
#[throws]
#[instrument]
fn ifreq(&self) -> sys::ifreq {
@ -283,6 +298,16 @@ impl TunInterface {
#[throws]
#[instrument]
pub fn send(&self, buf: &[u8]) -> usize {
self.socket.send(buf)?
let len = unsafe {
libc::write(
self.as_raw_fd(),
buf.as_ptr().cast::<libc::c_void>(),
buf.len(),
)
};
if len < 0 {
Err(Error::last_os_error())?;
}
len as usize
}
}

View file

@ -48,12 +48,26 @@ impl TunInterface {
#[throws]
#[instrument]
pub fn recv(&self, buf: &mut [u8]) -> usize {
// Use IoVec to read directly into target buffer
let mut tmp_buf = [MaybeUninit::uninit(); 1500];
let len = self.socket.recv(&mut tmp_buf)?;
let result_buf = unsafe { assume_init(&tmp_buf[4..len]) };
buf[..len - 4].copy_from_slice(result_buf);
len - 4
let packet_information_size = self.packet_information_size();
let mut tmp_buf = [MaybeUninit::uninit(); 1504];
let len = unsafe {
libc::read(
self.as_raw_fd(),
tmp_buf.as_mut_ptr().cast::<libc::c_void>(),
tmp_buf.len(),
)
};
if len < 0 {
Err(Error::last_os_error())?;
}
let len = len as usize;
if len < packet_information_size {
return 0;
}
let result_buf = unsafe { assume_init(&tmp_buf[packet_information_size..len]) };
buf[..len - packet_information_size].copy_from_slice(result_buf);
len - packet_information_size
}
#[throws]

View file

@ -3,17 +3,33 @@ use std::{io::Error, net::Ipv4Addr};
use fehler::throws;
use tun::TunInterface;
fn open_tun() -> Result<Option<TunInterface>, Error> {
match TunInterface::new() {
Ok(tun) => Ok(Some(tun)),
Err(err)
if err.kind() == std::io::ErrorKind::PermissionDenied
|| matches!(err.raw_os_error(), Some(1 | 13)) =>
{
eprintln!("skipping tun test without tunnel privileges: {err}");
Ok(None)
}
Err(err) => Err(err),
}
}
#[test]
#[throws]
fn test_create() {
TunInterface::new()?;
let _ = open_tun()?;
}
#[test]
#[throws]
#[cfg(not(any(target_os = "windows", target_vendor = "apple")))]
fn test_set_get_broadcast_addr() {
let tun = TunInterface::new()?;
let Some(tun) = open_tun()? else {
return Ok(());
};
let addr = Ipv4Addr::new(10, 0, 0, 1);
tun.set_ipv4_addr(addr)?;
@ -28,7 +44,9 @@ fn test_set_get_broadcast_addr() {
#[throws]
#[cfg(not(target_os = "windows"))]
fn test_set_get_ipv4() {
let tun = TunInterface::new()?;
let Some(tun) = open_tun()? else {
return Ok(());
};
let addr = Ipv4Addr::new(10, 0, 0, 1);
tun.set_ipv4_addr(addr)?;
@ -43,7 +61,9 @@ fn test_set_get_ipv4() {
fn test_set_get_ipv6() {
use std::net::Ipv6Addr;
let tun = TunInterface::new()?;
let Some(tun) = open_tun()? else {
return Ok(());
};
let addr = Ipv6Addr::new(1, 1, 1, 1, 1, 1, 1, 1);
tun.add_ipv6_addr(addr, 128)?;
@ -56,7 +76,9 @@ fn test_set_get_ipv6() {
#[throws]
#[cfg(not(target_os = "windows"))]
fn test_set_get_mtu() {
let interf = TunInterface::new()?;
let Some(interf) = open_tun()? else {
return Ok(());
};
interf.set_mtu(500)?;
@ -67,7 +89,9 @@ fn test_set_get_mtu() {
#[throws]
#[cfg(not(target_os = "windows"))]
fn test_set_get_netmask() {
let interf = TunInterface::new()?;
let Some(interf) = open_tun()? else {
return Ok(());
};
let netmask = Ipv4Addr::new(255, 0, 0, 0);
let addr = Ipv4Addr::new(192, 168, 1, 1);

View file

@ -1,10 +1,27 @@
#[cfg(all(feature = "tokio", not(target_os = "windows")))]
use std::net::Ipv4Addr;
#[cfg(all(feature = "tokio", not(target_os = "windows")))]
fn open_tun() -> Option<tun::TunInterface> {
match tun::TunInterface::new() {
Ok(tun) => Some(tun),
Err(err)
if err.kind() == std::io::ErrorKind::PermissionDenied
|| matches!(err.raw_os_error(), Some(1 | 13)) =>
{
eprintln!("skipping tokio tun test without tunnel privileges: {err}");
None
}
Err(err) => panic!("failed to create tun interface: {err}"),
}
}
#[tokio::test]
#[cfg(all(feature = "tokio", not(target_os = "windows")))]
async fn test_create() {
let tun = tun::TunInterface::new().unwrap();
let Some(tun) = open_tun() else {
return;
};
let _ = tun::tokio::TunInterface::new(tun).unwrap();
}
@ -12,7 +29,9 @@ async fn test_create() {
#[ignore = "requires interactivity"]
#[cfg(all(feature = "tokio", not(target_os = "windows")))]
async fn test_write() {
let tun = tun::TunInterface::new().unwrap();
let Some(tun) = open_tun() else {
return;
};
tun.set_ipv4_addr(Ipv4Addr::from([192, 168, 1, 10]))
.unwrap();
let async_tun = tun::tokio::TunInterface::new(tun).unwrap();