Tun/Tap interfaces
This crate allows the creation and usage of Tun/Tap interfaces, the aim is to make this cross-platform.
Usage
First, add the following to your Cargo.toml:
[]
= "3"
If you want to use the TUN interface with mio/tokio, you need to enable the async feature:
[]
= { = "3", = ["async"] }
Example
The following example creates and configures a TUN interface and starts reading packets from it.
use Read;
Platforms
Supported Platforms
- Windows
- Linux
- macOS
- FreeBSD
- Android
- iOS
Linux
You will need the tun2 module to be loaded and root is required to create
interfaces.
macOS & FreeBSD
tun2 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 tun2 to create the interface.
Here is an example to create the TUN device on iOS and pass the fd to tun2:
// 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)
}
}
}
pub extern "C"
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.