25 releases

0.7.5 Nov 30, 2024
0.7.1 Oct 27, 2024
0.6.1 Oct 10, 2023
0.5.5 Feb 21, 2023
0.3.0 Jun 2, 2017

#32 in Network programming

Download history 6688/week @ 2024-08-22 6409/week @ 2024-08-29 5082/week @ 2024-09-05 4047/week @ 2024-09-12 6772/week @ 2024-09-19 4451/week @ 2024-09-26 4742/week @ 2024-10-03 3952/week @ 2024-10-10 6398/week @ 2024-10-17 6089/week @ 2024-10-24 4302/week @ 2024-10-31 5781/week @ 2024-11-07 6049/week @ 2024-11-14 6745/week @ 2024-11-21 5982/week @ 2024-11-28 5685/week @ 2024-12-05

25,498 downloads per month
Used in 12 crates (11 directly)

WTFPL license

155KB
3.5K SLoC

TUN interfaces

Crates.io tun WTFPL

This crate allows the creation and usage of TUN interfaces, the aim is to make this cross-platform.

Usage

First, add the following to your Cargo.toml:

[dependencies]
tun = "0.7"

If you want to use the TUN interface with mio/tokio, you need to enable the async feature:

[dependencies]
tun = { version = "0.7", features = ["async"] }

Example

The following example creates and configures a TUN interface and starts reading packets from it.

use std::io::Read;

fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
    let mut config = tun::Configuration::default();
    config
        .address((10, 0, 0, 9))
        .netmask((255, 255, 255, 0))
        .destination((10, 0, 0, 1))
        .up();

    #[cfg(target_os = "linux")]
    config.platform_config(|config| {
        // requiring root privilege to acquire complete functions
        config.ensure_root_privileges(true);
    });

    let mut dev = tun::create(&config)?;
    let mut buf = [0; 4096];

    loop {
        let amount = dev.read(&mut buf)?;
        println!("{:?}", &buf[0..amount]);
    }
}

Platforms

Supported Platforms

  • Windows
  • Linux
  • macOS
  • FreeBSD
  • Android
  • iOS
  • OpenHarmony

Linux

You will need the tun module to be loaded and root is required to create interfaces.

macOS & FreeBSD

tun will automatically set up a route according to the provided configuration, which does a similar thing like this:

sudo route -n add -net 10.0.0.0/24 10.0.0.1

iOS

You can pass the file descriptor of the TUN device to tun to create the interface.

Here is an example to create the TUN device on iOS and pass the fd to tun:

// Swift
class PacketTunnelProvider: NEPacketTunnelProvider {
    override func startTunnel(options: [String : NSObject]?, completionHandler: @escaping (Error?) -> Void) {
        let tunnelNetworkSettings = createTunnelSettings() // Configure TUN address, DNS, mtu, routing...
        setTunnelNetworkSettings(tunnelNetworkSettings) { [weak self] error in
            // The tunnel of this tunFd is contains `Packet Information` prifix.
            let tunFd = self?.packetFlow.value(forKeyPath: "socket.fileDescriptor") as! Int32
            DispatchQueue.global(qos: .default).async {
                start_tun(tunFd)
            }
            completionHandler(nil)
        }
    }
}
#[no_mangle]
pub extern "C" fn start_tun(fd: std::os::raw::c_int) {
    let mut rt = tokio::runtime::Runtime::new().unwrap();
    rt.block_on(async {
        let mut cfg = tun::Configuration::default();
        cfg.raw_fd(fd);
        #[cfg(target_os = "ios")]
        cfg.platform_config(|p_cfg| {
            p_cfg.packet_information(true);
        });
        let mut tun = tun::create_as_async(&cfg).unwrap();
        let mut framed = tun.into_framed();
        while let Some(packet) = framed.next().await {
            ...
        }
    });
}

Windows

You need to copy the wintun.dll file which matches your architecture to the same directory as your executable and run your program as administrator.

Contributors ✨

Thanks goes to these wonderful people:

Dependencies

~2–12MB
~141K SLoC