#wintun #tun #vpn #tap #wireguard #api-bindings #ring-buffer

winTun 绑定

对 WinTun C 库的安全且符合 Rust 习惯的绑定以及更多增强功能

3 个版本

0.6.2 2024 年 8 月 23 日
0.6.1 2024 年 8 月 22 日
0.6.0 2024 年 8 月 22 日

#130 in 硬件支持

Download history 172/week @ 2024-08-17

172 每月下载次数
7 个 Crates 中使用(通过 tun2

MIT 许可证

1.5MB
1.5K SLoC

包含(Windows DLL,550KB)wintun/bin/x86/wintun.dll,(Windows DLL,430KB)wintun/bin/amd64/wintun.dll,(Windows DLL,365KB)wintun/bin/arm/wintun.dll,(Windows DLL,225KB)wintun/bin/arm64/wintun.dll

winTun 绑定

为 Wintun C 库提供安全的 Rust 风格绑定:https://wintun.net

使用纯 Rust 类型和方法封装了 Wintun 库的所有功能,以便于使用。

Version Documentation Download License

用法

在您的代码中,使用 loadload_from_pathload_from_library 加载从 https://wintun.net 下载的 wintun.dll 签名驱动文件。

然后,调用 Adapter::createAdapter::open 获取一个 wintun 适配器。使用 Adapter::start_session 开始会话。

示例

use std::sync::Arc;

//Must be run as Administrator because we create network adapters
//Load the wintun dll file so that we can call the underlying C functions
//Unsafe because we are loading an arbitrary dll file
let wintun = unsafe { wintun_bindings::load_from_path("path/to/wintun.dll") }
    .expect("Failed to load wintun dll");

//Try to open an adapter with the name "Demo"
let adapter = match wintun_bindings::Adapter::open(&wintun, "Demo") {
    Ok(a) => a,
    Err(_) => {
        //If loading failed (most likely it didn't exist), create a new one
        wintun_bindings::Adapter::create(&wintun, "Demo", "Example", None)
            .expect("Failed to create wintun adapter!")
    }
};
//Specify the size of the ring buffer the wintun driver should use.
let session = Arc::new(adapter.start_session(wintun_bindings::MAX_RING_CAPACITY).unwrap());

//Get a 20 byte packet from the ring buffer
let mut packet = session.allocate_send_packet(20).unwrap();
let bytes: &mut [u8] = packet.bytes_mut();
//Write IPV4 version and header length
bytes[0] = 0x40;

//Finish writing IP header
bytes[9] = 0x69;
bytes[10] = 0x04;
bytes[11] = 0x20;
//...

//Send the packet to wintun virtual adapter for processing by the system
session.send_packet(packet);

//Stop any readers blocking for data on other threads
//Only needed when a blocking reader is preventing shutdown Ie. it holds an Arc to the
//session, blocking it from being dropped
session.shutdown();

//the session is stopped on drop
//drop(session);

//drop(adapter)
//And the adapter closes its resources when dropped

请参阅 examples/wireshark.rs,以获取将接收到的数据包写入 pcap 文件的更完整示例。

功能

  • panic_on_unsent_packets:如果发送的数据包未发送而被丢弃,则引发恐慌。这对于调试数据包问题很有用,因为未发送且被丢弃的数据包会阻塞 wintun 的内部环形缓冲区。

  • verify_binary_signature:在加载之前验证wintun dll文件的签名。

待办事项

  • 添加异步支持 需要挂钩到特定于Windows的反应器并注册wintun读取句柄的读取兴趣。通过tokio::spawn_blocking异步化其他慢速操作。一如既往,欢迎提交PR!

许可证:MIT

依赖项

约15-21MB
约292K SLoC