2 个版本
0.1.2 | 2024 年 4 月 14 日 |
---|---|
0.1.1 | 2024 年 4 月 10 日 |
0.1.0 |
|
#1055 in 网络编程
49KB
1K SLoC
Netstack Smoltcp
一个专门将数据包从/到 TUN 接口转换为 TCP 流和 UDP 数据包的 netstack。它使用 smoltcp-rs 作为后端 netstack。
特性
- 支持 Future Send 和非 Send,大多数人使用 Send。
- 支持由 TCP 运行程序驱动的 ICMP 协议来使用 ICMP ping。
- 支持通过源和目的 IP 地址过滤数据包。
- 可以从 netstack 读取 IP 数据包,将 IP 数据包写入 netstack。
- 可以接收来自 netstack 暴露的 TcpListener 的 TcpStream。
- 可以接收来自 netstack 暴露的 UdpSocket 的 UDP 数据报。
- 实现了流行的 future 流特性异步 IO 特性
- TcpListener 实现 futures Stream/Sink 特性
- TcpStream 实现 tokio AsyncRead/AsyncWrite 特性
- UdpSocket(ReadHalf/WriteHalf) 实现 futures Stream/Sink 特性。
平台
此 crate 为 Linux、iOS、macOS、Android 和 Windows 提供轻量级的 netstack 支持。目前,它适用于大多数目标,但主要测试了以下流行的平台:
- linux-amd64: x86_64-unknown-linux-gnu
- android-arm64: aarch64-linux-android
- android-amd64: x86_64-linux-android
- macos-amd64: x86_64-apple-darwin
- macos-arm64: aarch64-apple-darwin
- ios-arm64: aarch64-apple-ios
- windows-amd64: x86_64-pc-windows-msvc
- windows-arm64: aarch64-pc-windows-msvc
示例
// let device = tun2::create_as_async(&cfg)?;
// let framed = device.into_framed();
// let mut builder = StackBuilder::default();
// let (runner, udp_socket, tcp_listener, stack) = builder.build();
// tokio::task::spawn(runner);
let (udp_socket, tcp_listener, stack) = StackBuilder::default().run();
let (mut stack_sink, mut stack_stream) = stack.split();
let (mut tun_sink, mut tun_stream) = framed.split();
// Reads packet from stack and sends to TUN.
tokio::spawn(async move {
while let Some(pkt) = stack_stream.next().await {
if let Ok(pkt) = pkt {
tun_sink.send(pkt).await.unwrap();
}
}
});
// Reads packet from TUN and sends to stack.
tokio::spawn(async move {
while let Some(pkt) = tun_stream.next().await {
if let Ok(pkt) = pkt {
stack_sink.send(pkt).await.unwrap();
}
}
});
// Extracts TCP connections from stack and sends them to the dispatcher.
tokio::spawn(async move {
handle_inbound_stream(tcp_listener).await;
});
// Receive and send UDP packets between netstack and NAT manager. The NAT
// manager would maintain UDP sessions and send them to the dispatcher.
tokio::spawn(async move {
handle_inbound_datagram(udp_socket).await;
});
许可
本项目采用 Apache License 2.0 许可或 MIT 许可。
- Apache License 2.0 (LICENSE-APACHE 或 https://apache.ac.cn/licenses/LICENSE-2.0)
- MIT 许可 (LICENSE-MIT 或 https://opensource.org/licenses/MIT)
任选其一。
贡献
除非您明确声明,否则任何有意提交以包含在 netstack-smoltcp 中的贡献,根据 Apache-2.0 许可证定义,应双许可如上,无任何额外条款或条件。
灵感来源
特别感谢以下这些启发 netstack-smoltcp 的优秀项目
星标历史
依赖
~11MB
~203K SLoC