A simple way to open a TUN interface
Find a file
Evan Pratten 5f125937fb
Some checks failed
Publish / Rust Crate (push) Has been cancelled
Build / Rust project (push) Has been cancelled
Appease Clippy
2026-05-16 01:39:56 -04:00
.forgejo/workflows Expand CI with tests, clippy, rustfmt, and updated actions 2026-05-15 19:08:39 -04:00
examples Implement Clippy recommendations 2024-04-24 11:31:07 -04:00
src Appease Clippy 2026-05-16 01:39:56 -04:00
.gitignore Add CI workflow 2026-01-14 14:30:03 -05:00
Cargo.toml Add crate keywords, categories, and MSRV to Cargo.toml 2026-05-15 19:10:47 -04:00
LICENSE Clean up Cargo.toml 2026-01-14 15:07:50 -05:00
README.md Add queue_count() method and update README example 2026-05-15 19:11:35 -04:00

easy-tun

Crates.io Docs.rs

easy-tun is a pure-Rust library that can bring up and manage a TUN interface by directly interacting with the Universal TUN/TAP Driver.

Requirements

  • Linux (glibc or musl)
  • Root privileges or CAP_NET_ADMIN capability

Installation

Add easy-tun to your Cargo.toml:

[dependencies]
easy-tun = "2"

Usage

Single queue

use std::io::Read;
use easy_tun::Tun;

fn main() -> std::io::Result<()> {
    let tun = Tun::new("tun%d", 1)?;
    println!("Created device: {}", tun.name());

    let mut buffer = [0u8; 1500];
    let n = tun.fd(0).unwrap().read(&mut buffer)?;
    println!("Read {} bytes", n);
    Ok(())
}

Multi-queue

use std::{io::Read, sync::Arc};
use easy_tun::Tun;

fn main() -> std::io::Result<()> {
    let tun = Arc::new(Tun::new("tun%d", 5)?);
    println!("Created device: {}", tun.name());

    let mut handles = Vec::new();
    for i in 0..tun.queue_count() {
        let tun = Arc::clone(&tun);
        handles.push(std::thread::spawn(move || {
            let mut buffer = [0u8; 1500];
            loop {
                let n = tun.fd(i).unwrap().read(&mut buffer).unwrap();
                println!("Queue {}: read {} bytes", i, n);
            }
        }));
    }

    for h in handles {
        h.join().unwrap();
    }
    Ok(())
}

Platform Support

This crate currently supports Linux with glibc or musl. Other platforms are not supported.