tapd-0.1.4 has been yanked.
TAPD interfaces
Usage
First, add the following to your Cargo.toml:
[dependencies]
tapd = "0.1"
Example
The following example creates and configures a TAP interface and starts reading
packets from it.
use anyhow::{Result, bail};
use nix::{
fcntl::{self, FcntlArg},
sys::stat::{self, SFlag},
};
use std::{
env::args,
io::{IsTerminal, stdin, stdout},
os::fd::AsFd,
};
use tapd::{pipe_std, pipe_tun};
#[tokio::main(worker_threads = 2)]
async fn main() -> Result<()> {
let mut argv = args();
let app = argv.next().unwrap();
match argv.len() {
2 => pipe_tun(&argv.next().unwrap(), &argv.next().unwrap()).await?,
1 => {
if setup_stdio(stdin()) && setup_stdio(stdout()) {
pipe_std(&argv.next().unwrap()).await?
}
}
_ => {}
}
bail!("Usage: {app:?} [dev1 dev2 | dev]");
}
fn setup_stdio<T: AsFd + IsTerminal>(fd: T) -> bool {
if fd.is_terminal() {
return false;
}
return match stat::fstat(fd.as_fd()) {
Ok(stat) => {
if SFlag::from_bits_truncate(stat.st_mode).contains(SFlag::S_IFIFO) {
let _ = fcntl::fcntl(fd.as_fd(), FcntlArg::F_SETPIPE_SZ(1 << 20));
}
true
}
Err(_) => false,
};
}
Linux
You will need the tun module to be loaded and root is required to create
interfaces.