Crates.io | tokio-tun |
lib.rs | tokio-tun |
version | 0.12.1 |
source | src |
created_at | 2020-10-03 05:08:44.386441 |
updated_at | 2024-10-23 11:04:39.579054 |
description | Asynchronous allocation of TUN/TAP devices using tokio |
homepage | https://github.com/yaa110/tokio-tun |
repository | https://github.com/yaa110/tokio-tun |
max_upload_size | |
id | 295655 |
size | 58,909 |
Asynchronous allocation of TUN/TAP devices in Rust using tokio
. Use async-tun for async-std
version.
Tun::builder()
and read from it in a loop:#[tokio::main]
async fn main() {
let tun = Arc::new(
Tun::builder()
.name("") // if name is empty, then it is set by kernel.
.tap() // uses TAP instead of TUN (default).
.packet_info() // avoids setting IFF_NO_PI.
.up() // or set it up manually using `sudo ip link set <tun-name> up`.
.try_build() // or `.try_build_mq(queues)` for multi-queue support.
.unwrap(),
);
println!("tun created, name: {}, fd: {}", tun.name(), tun.as_raw_fd());
let (mut reader, mut _writer) = tokio::io::split(tun);
// Writer: simply clone Arced Tun.
let tun_c = tun.clone();
tokio::spawn(async move{
let buf = b"data to be written";
tun_c.send_all(buf).await.unwrap();
});
// Reader
let mut buf = [0u8; 1024];
loop {
let n = tun.recv(&mut buf).await.unwrap();
println!("reading {} bytes: {:?}", n, &buf[..n]);
}
}
sudo
:sudo -E $(which cargo) run
TunBuilder
):sudo ip a add 10.0.0.1/24 dev <tun-name>
ping 10.0.0.2
ip tuntap
sudo tshark -i <tun-name>
read
: Split tun to (reader, writer) pair and read packets from reader.read-mq
: Read from multi-queue tun using tokio::select!
.sudo -E $(which cargo) run --example read
sudo -E $(which cargo) run --example read-mq