20 releases
0.10.1 | Jul 11, 2022 |
---|---|
0.10.0 | Oct 27, 2021 |
0.9.0 | Jul 3, 2021 |
0.8.4 | Feb 24, 2021 |
0.8.1 | Oct 28, 2020 |
#2131 in Asynchronous
58 downloads per month
25KB
590 lines
Async TUN/TAP
Asynchronous allocation of TUN/TAP devices in Rust using async-std
. Use tokio-tun for tokio
version.
Getting Started
- Create a tun device using
TunBuilder
and read from it in a loop:
async fn async_main() -> Result<()> {
let tun = TunBuilder::new()
.name("") // if name is empty, then it is set by kernel.
.tap(false) // false (default): TUN, true: TAP.
.packet_info(false) // false: IFF_NO_PI, default is true.
.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.
.await?;
println!("tun created, name: {}, fd: {}", tun.name(), tun.as_raw_fd());
let mut reader = tun.reader();
let mut buf = [0u8; 1024];
loop {
let n = reader.read(&mut buf).await?;
println!("reading {} bytes: {:?}", n, &buf[..n]);
}
}
fn main() -> Result<()> {
task::block_on(async_main())
}
- Run the code using
sudo
:
➜ sudo -E /path/to/cargo run
- Set the address of device (address and netmask could also be set using
TunBuilder
):
➜ sudo ip a add 10.0.0.1/24 dev <tun-name>
- Ping to read packets:
➜ ping 10.0.0.2
- Display devices and analyze the network traffic:
➜ ip tuntap
➜ sudo tshark -i <tun-name>
Supported Platforms
- Linux
- FreeBSD
- Android
- OSX
- iOS
- Windows
Examples
Dependencies
~6–14MB
~195K SLoC