Add C Swift Bindings

This adds C Swift bindings for burrow via compiling burrow
with a matching header file.
This commit is contained in:
Jett Chen 2023-08-06 11:51:50 +08:00 committed by Conrad Kramer
parent d821f3d03c
commit 3ef13b09a3
4 changed files with 85 additions and 4 deletions

View file

@ -1 +1,32 @@
pub mod ensureroot;
#[cfg(any(target_os = "linux", target_vendor = "apple"))]
use std::{
mem,
os::fd::{AsRawFd, FromRawFd},
};
use tun::TunInterface;
// TODO Separate start and retrieve functions
#[cfg(any(target_os = "linux", target_vendor = "apple"))]
#[no_mangle]
pub extern "C" fn retrieve() -> i32 {
let iface2 = (1..100)
.filter_map(|i| {
let iface = unsafe { TunInterface::from_raw_fd(i) };
match iface.name() {
Ok(_name) => Some(iface),
Err(_) => {
mem::forget(iface);
None
}
}
})
.next();
match iface2 {
Some(iface) => iface.as_raw_fd(),
None => -1,
}
}

View file

@ -1,5 +1,11 @@
use std::mem;
#[cfg(any(target_os = "linux", target_vendor = "apple"))]
use std::os::fd::FromRawFd;
use clap::{Args, Parser, Subcommand};
use tokio::io::Result;
#[cfg(any(target_os = "linux", target_vendor = "apple"))]
use burrow::retrieve;
use tun::TunInterface;
#[derive(Parser)]
@ -22,17 +28,41 @@ struct Cli {
enum Commands {
/// Start Burrow
Start(StartArgs),
/// Retrieve the file descriptor of the tun interface
Retrieve(RetrieveArgs),
}
#[derive(Args)]
struct StartArgs {}
async fn try_main() -> Result<()> {
burrow::ensureroot::ensure_root();
#[derive(Args)]
struct RetrieveArgs {}
#[cfg(any(target_os = "linux", target_vendor = "apple"))]
async fn try_start() -> Result<()> {
burrow::ensureroot::ensure_root();
let iface = TunInterface::new()?;
println!("{:?}", iface.name());
let iface2 = retrieve();
println!("{}", iface2);
Ok(())
}
#[cfg(any(target_os = "linux", target_vendor = "apple"))]
async fn try_retrieve() -> Result<()> {
burrow::ensureroot::ensure_root();
let iface2 = retrieve();
println!("{}", iface2);
Ok(())
}
#[cfg(not(any(target_os = "linux", target_vendor = "apple")))]
async fn try_start() -> Result<()> {
Ok(())
}
#[cfg(not(any(target_os = "linux", target_vendor = "apple")))]
async fn try_retrieve() -> Result<()> {
Ok(())
}
@ -43,7 +73,12 @@ async fn main() {
let cli = Cli::parse();
match &cli.command {
Commands::Start(..) => {
try_main().await.unwrap();
try_start().await.unwrap();
println!("FINISHED");
}
Commands::Retrieve(..) => {
try_retrieve().await.unwrap();
println!("FINISHED");
}
}
}