merge boringtun into burrow

This commit is contained in:
Conrad Kramer 2023-09-16 10:45:53 -07:00
parent e643d9dd41
commit 28af9003d0
39 changed files with 3122 additions and 228 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)
@ -80,9 +80,10 @@ async fn download(directory: &std::path::Path) -> anyhow::Result<std::fs::File>
#[cfg(windows)]
fn parse(file: std::fs::File) -> anyhow::Result<(bindgen::Bindings, Vec<u8>)> {
use anyhow::Context;
use std::io::Read;
use anyhow::Context;
let reader = std::io::BufReader::new(file);
let mut archive = zip::ZipArchive::new(reader)?;

View file

@ -2,11 +2,11 @@
#[cfg(target_os = "windows")]
#[path = "windows/mod.rs"]
mod imp;
mod os_imp;
#[cfg(any(target_os = "linux", target_vendor = "apple"))]
#[path = "unix/mod.rs"]
pub(crate) mod imp;
pub(crate) mod os_imp;
mod options;
@ -14,5 +14,5 @@ mod options;
#[cfg(feature = "tokio")]
pub mod tokio;
pub use imp::{TunInterface, TunQueue};
pub use options::TunOptions;
pub use os_imp::{TunInterface, TunQueue};

View file

@ -1,6 +1,7 @@
use fehler::throws;
use std::io::Error;
use fehler::throws;
use super::TunInterface;
#[derive(Debug, Clone, Default)]
@ -15,25 +16,17 @@ pub struct TunOptions {
}
impl TunOptions {
pub fn new() -> Self {
Self::default()
}
pub fn new() -> Self { Self::default() }
pub fn name(mut self, name: &str) -> Self {
self.name = Some(name.to_owned());
self
}
pub fn no_pi(mut self, enable: bool) {
self.no_pi = enable.then_some(());
}
pub fn no_pi(mut self, enable: bool) { self.no_pi = enable.then_some(()); }
pub fn tun_excl(mut self, enable: bool) {
self.tun_excl = enable.then_some(());
}
pub fn tun_excl(mut self, enable: bool) { self.tun_excl = enable.then_some(()); }
#[throws]
pub fn open(self) -> TunInterface {
TunInterface::new_with_options(self)?
}
pub fn open(self) -> TunInterface { TunInterface::new_with_options(self)? }
}

View file

@ -1,4 +1,5 @@
use std::io;
use tokio::io::unix::AsyncFd;
use tracing::instrument;
@ -16,7 +17,7 @@ impl TunInterface {
}
#[instrument]
pub async fn write(&self, buf: &[u8]) -> io::Result<usize> {
pub async fn send(&self, buf: &[u8]) -> io::Result<usize> {
loop {
let mut guard = self.inner.writable().await?;
match guard.try_io(|inner| inner.get_ref().send(buf)) {
@ -27,7 +28,7 @@ impl TunInterface {
}
#[instrument]
pub async fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
pub async fn recv(&mut self, buf: &mut [u8]) -> io::Result<usize> {
loop {
let mut guard = self.inner.readable_mut().await?;
match guard.try_io(|inner| (*inner).get_mut().recv(buf)) {

View file

@ -1,7 +1,6 @@
use std::{io::Error, mem::size_of, os::unix::io::AsRawFd};
use fehler::throws;
use std::io::Error;
use std::mem::size_of;
use std::os::unix::io::AsRawFd;
use super::sys;
@ -16,10 +15,7 @@ pub trait SysControlSocket {
impl SysControlSocket for socket2::Socket {
#[throws]
fn resolve(&self, name: &str, index: u32) -> socket2::SockAddr {
let mut info = sys::ctl_info {
ctl_id: 0,
ctl_name: [0; 96],
};
let mut info = sys::ctl_info { ctl_id: 0, ctl_name: [0; 96] };
info.ctl_name[..name.len()].copy_from_slice(name.as_bytes());
unsafe { sys::resolve_ctl_info(self.as_raw_fd(), &mut info as *mut sys::ctl_info)? };
@ -28,7 +24,7 @@ impl SysControlSocket for socket2::Socket {
socket2::SockAddr::init(|addr_storage, len| {
*len = size_of::<sys::sockaddr_ctl>() as u32;
let mut addr: &mut sys::sockaddr_ctl = &mut *addr_storage.cast();
let addr: &mut sys::sockaddr_ctl = &mut *addr_storage.cast();
addr.sc_len = *len as u8;
addr.sc_family = sys::AF_SYSTEM as u8;
addr.ss_sysaddr = sys::AF_SYS_CONTROL as u16;

View file

@ -1,22 +1,24 @@
use std::{
io::{Error, IoSlice},
mem,
net::{Ipv4Addr, SocketAddrV4},
os::fd::{AsRawFd, RawFd},
};
use byteorder::{ByteOrder, NetworkEndian};
use fehler::throws;
use libc::{c_char, iovec, writev, AF_INET, AF_INET6};
use tracing::info;
use socket2::{Domain, SockAddr, Socket, Type};
use std::io::IoSlice;
use std::net::{Ipv4Addr, SocketAddrV4};
use std::os::fd::{AsRawFd, RawFd};
use std::{io::Error, mem};
use tracing::instrument;
use tracing::{self, instrument};
mod kern_control;
mod sys;
pub use super::queue::TunQueue;
use super::{ifname_to_string, string_to_ifname, TunOptions};
use kern_control::SysControlSocket;
pub use super::queue::TunQueue;
use super::{ifname_to_string, string_to_ifname, TunOptions};
#[derive(Debug)]
pub struct TunInterface {
pub(crate) socket: socket2::Socket,
@ -81,7 +83,7 @@ impl TunInterface {
let mut iff = self.ifreq()?;
iff.ifr_ifru.ifru_addr = unsafe { *addr.as_ptr() };
self.perform(|fd| unsafe { sys::if_set_addr(fd, &iff) })?;
info!("ipv4_addr_set: {:?} (fd: {:?})", addr, self.as_raw_fd())
tracing::info!("ipv4_addr_set: {:?} (fd: {:?})", addr, self.as_raw_fd())
}
#[throws]
@ -118,7 +120,7 @@ impl TunInterface {
let mut iff = self.ifreq()?;
iff.ifr_ifru.ifru_mtu = mtu;
self.perform(|fd| unsafe { sys::if_set_mtu(fd, &iff) })?;
info!("mtu_set: {:?} (fd: {:?})", mtu, self.as_raw_fd())
tracing::info!("mtu_set: {:?} (fd: {:?})", mtu, self.as_raw_fd())
}
#[throws]
@ -140,7 +142,7 @@ impl TunInterface {
let mut iff = self.ifreq()?;
iff.ifr_ifru.ifru_netmask = unsafe { *addr.as_ptr() };
self.perform(|fd| unsafe { sys::if_set_netmask(fd, &iff) })?;
info!(
tracing::info!(
"netmask_set: {:?} (fd: {:?})",
unsafe { iff.ifr_ifru.ifru_netmask },
self.as_raw_fd()

View file

@ -2,11 +2,20 @@ use std::mem;
use libc::{c_char, c_int, c_short, c_uint, c_ulong, sockaddr};
pub use libc::{
c_void, sockaddr_ctl, sockaddr_in, socklen_t, AF_SYSTEM, AF_SYS_CONTROL, IFNAMSIZ,
c_void,
sockaddr_ctl,
sockaddr_in,
socklen_t,
AF_SYSTEM,
AF_SYS_CONTROL,
IFNAMSIZ,
SYSPROTO_CONTROL,
};
use nix::{
ioctl_read_bad, ioctl_readwrite, ioctl_write_ptr_bad, request_code_readwrite,
ioctl_read_bad,
ioctl_readwrite,
ioctl_write_ptr_bad,
request_code_readwrite,
request_code_write,
};

View file

@ -1,16 +1,18 @@
use std::{
fs::OpenOptions,
io::{Error, Write},
mem,
net::{Ipv4Addr, Ipv6Addr, SocketAddrV4},
os::{
fd::RawFd,
unix::io::{AsRawFd, FromRawFd, IntoRawFd},
},
};
use fehler::throws;
use socket2::{Domain, SockAddr, Socket, Type};
use std::fs::OpenOptions;
use std::io::{Error, Write};
use std::mem;
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddrV4};
use std::os::fd::RawFd;
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd};
use tracing::{info, instrument};
use libc::in6_ifreq;
use socket2::{Domain, SockAddr, Socket, Type};
use tracing::{info, instrument};
use super::{ifname_to_string, string_to_ifname, TunOptions};
@ -24,9 +26,7 @@ pub struct TunInterface {
impl TunInterface {
#[throws]
#[instrument]
pub fn new() -> TunInterface {
Self::new_with_options(TunOptions::new())?
}
pub fn new() -> TunInterface { Self::new_with_options(TunOptions::new())? }
#[throws]
#[instrument]
@ -212,7 +212,5 @@ impl TunInterface {
#[throws]
#[instrument]
pub fn send(&self, buf: &[u8]) -> usize {
self.socket.send(buf)?
}
pub fn send(&self, buf: &[u8]) -> usize { self.socket.send(buf)? }
}

View file

@ -1,10 +1,7 @@
use nix::{ioctl_read_bad, ioctl_write_ptr_bad, request_code_read, request_code_write};
use std::mem::size_of;
pub use libc::ifreq;
pub use libc::sockaddr;
pub use libc::sockaddr_in;
pub use libc::sockaddr_in6;
pub use libc::{ifreq, sockaddr, sockaddr_in, sockaddr_in6};
use nix::{ioctl_read_bad, ioctl_write_ptr_bad, request_code_read, request_code_write};
ioctl_write_ptr_bad!(
tun_set_iff,

View file

@ -2,6 +2,7 @@ use std::{
io::{Error, Read},
os::fd::{AsRawFd, FromRawFd, IntoRawFd, RawFd},
};
use tracing::instrument;
use super::TunOptions;
@ -28,9 +29,8 @@ impl AsRawFd for TunInterface {
impl FromRawFd for TunInterface {
unsafe fn from_raw_fd(fd: RawFd) -> TunInterface {
TunInterface {
socket: socket2::Socket::from_raw_fd(fd),
}
let socket = socket2::Socket::from_raw_fd(fd);
TunInterface { socket }
}
}
@ -65,4 +65,4 @@ pub fn string_to_ifname(name: &str) -> [libc::c_char; libc::IFNAMSIZ] {
let len = name.len().min(buf.len());
buf[..len].copy_from_slice(unsafe { &*(name.as_bytes() as *const _ as *const [libc::c_char]) });
buf
}
}

View file

@ -1,10 +1,10 @@
use fehler::throws;
use std::{
io::{Error, Read, Write},
mem::MaybeUninit,
os::unix::io::{AsRawFd, IntoRawFd, RawFd},
};
use fehler::throws;
use tracing::instrument;
use crate::TunInterface;
@ -15,10 +15,9 @@ pub struct TunQueue {
}
impl TunQueue {
#[throws]
#[instrument]
pub fn recv(&self, buf: &mut [MaybeUninit<u8>]) -> usize {
self.socket.recv(buf)?
pub fn recv(&self, buf: &mut [MaybeUninit<u8>]) -> Result<usize, Error> {
self.socket.recv(buf)
}
}
@ -43,9 +42,7 @@ impl Write for TunQueue {
impl From<TunInterface> for TunQueue {
fn from(interface: TunInterface) -> TunQueue {
TunQueue {
socket: interface.socket,
}
TunQueue { socket: interface.socket }
}
}

View file

@ -1,15 +1,14 @@
use std::fmt::Debug;
use std::{fmt::Debug, io::Error, ptr};
use fehler::throws;
use std::io::Error;
use std::ptr;
use widestring::U16CString;
use windows::Win32::Foundation::GetLastError;
mod queue;
use super::TunOptions;
pub use queue::TunQueue;
use super::TunOptions;
pub struct TunInterface {
handle: sys::WINTUN_ADAPTER_HANDLE,
name: String,
@ -26,9 +25,7 @@ impl Debug for TunInterface {
impl TunInterface {
#[throws]
pub fn new() -> TunInterface {
Self::new_with_options(TunOptions::new())?
}
pub fn new() -> TunInterface { Self::new_with_options(TunOptions::new())? }
#[throws]
pub(crate) fn new_with_options(options: TunOptions) -> TunInterface {
@ -46,15 +43,11 @@ impl TunInterface {
}
}
pub fn name(&self) -> String {
self.name.clone()
}
pub fn name(&self) -> String { self.name.clone() }
}
impl Drop for TunInterface {
fn drop(&mut self) {
unsafe { sys::WINTUN.WintunCloseAdapter(self.handle) }
}
fn drop(&mut self) { unsafe { sys::WINTUN.WintunCloseAdapter(self.handle) } }
}
pub(crate) mod sys {

View file

@ -1,13 +1,11 @@
use std::{io::Error, net::Ipv4Addr};
use fehler::throws;
use std::io::Error;
use std::net::Ipv4Addr;
use tun::TunInterface;
#[test]
#[throws]
fn test_create() {
TunInterface::new()?;
}
fn test_create() { TunInterface::new()?; }
#[test]
#[throws]

View file

@ -1,7 +1,6 @@
use fehler::throws;
use std::io::Error;
use std::{io::Error, net::Ipv4Addr};
use std::net::Ipv4Addr;
use fehler::throws;
use tun::TunInterface;
#[throws]
@ -9,8 +8,8 @@ use tun::TunInterface;
#[ignore = "requires interactivity"]
#[cfg(not(target_os = "windows"))]
fn tst_read() {
// This test is interactive, you need to send a packet to any server through 192.168.1.10
// EG. `sudo route add 8.8.8.8 192.168.1.10`,
// This test is interactive, you need to send a packet to any server through
// 192.168.1.10 EG. `sudo route add 8.8.8.8 192.168.1.10`,
//`dig @8.8.8.8 hackclub.com`
let mut tun = TunInterface::new()?;
println!("tun name: {:?}", tun.name()?);

View file

@ -4,7 +4,7 @@ use std::net::Ipv4Addr;
#[cfg(all(feature = "tokio", not(target_os = "windows")))]
async fn test_create() {
let tun = tun::TunInterface::new().unwrap();
let async_tun = tun::tokio::TunInterface::new(tun).unwrap();
let _ = tun::tokio::TunInterface::new(tun).unwrap();
}
#[tokio::test]
@ -17,6 +17,6 @@ async fn test_write() {
let async_tun = tun::tokio::TunInterface::new(tun).unwrap();
let mut buf = [0u8; 1500];
buf[0] = 6 << 4;
let bytes_written = async_tun.write(&buf).await.unwrap();
let bytes_written = async_tun.send(&buf).await.unwrap();
assert!(bytes_written > 0);
}