13个不稳定版本 (4个重大更改)
0.5.0 | 2024年7月22日 |
---|---|
0.4.0 | 2024年1月12日 |
0.3.2 | 2023年9月27日 |
0.2.1 | 2021年12月3日 |
0.1.3 | 2021年6月28日 |
#151 in 网络编程
每月18,330次下载
用于19个包 (6个直接使用)
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库的所有功能,使使用感觉更直观。
使用
在代码中加载来自https://wintun.net下载的wintun.dll签名驱动文件,使用load
、load_from_path
或load_from_library
。
然后调用Adapter::create
或Adapter::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::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::Adapter::open(&wintun, "Demo") {
Ok(a) => a,
Err(_) => {
//If loading failed (most likely it didn't exist), create a new one
wintun::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::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
:如果发送的数据包未发送就被丢弃,则会引发panic。对于调试数据包问题非常有用,因为未发送且被丢弃的数据包会阻塞wintun的内部环形缓冲区。
待办事项
- 添加异步支持 需要挂钩到Windows特定的reactor并在wintun的读取句柄上注册读取兴趣。通过tokio::spawn_blocking将其他慢速操作异步化。一如既往,欢迎提交PR!
许可证:MIT
依赖关系
~11–19MB
~248K SLoC