easy-tun (2.0.1-dev.20260516-053918.d18cb98)

Published 2026-05-16 05:39:19 +00:00 by Evan Pratten

Installation

[registries.forgejo]
index = "sparse+" # Sparse index
# index = "" # Git

[net]
git-fetch-with-cli = true
cargo add easy-tun@2.0.1-dev.20260516-053918.d18cb98 --registry forgejo

About this package

A pure-rust TUN interface library

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.

Dependencies

ID Version
ioctl-gen ^0.1.1
libc ^0.2
log ^0.4
env_logger ^0.10

Keywords

tun network vpn interface linux
Details
Cargo
2026-05-16 05:39:19 +00:00
9
Evan Pratten <ewpratten@gmail.com>
MIT
6.6 KiB
Assets (1)
Versions (4) View all