WinTun driver integration for high-performance virtual network adapters on Windows.
This module provides integration with the WinTun driver, a modern, lightweight virtual network adapter driver maintained by the WireGuard project. WinTun enables creating TUN (Layer 3) interfaces on Windows for VPN applications, custom network stacks, and tunnel implementations.
WinTun is the modern replacement for TAP-Windows and other legacy virtual adapter solutions. It is:
- Lightweight: Driver binary is less than 50KB
- Modern: Designed for Windows 7 through Windows 11
- Fast: Optimized for high throughput and low latency
- Maintained: Actively developed by the WireGuard team
- Signed: Microsoft-signed driver (WHQL certified)
- Open Source: MIT licensed
- Windows 7 or newer
- WinTun driver installed (download from https://www.wintun.net/)
- Administrator privileges to create virtual adapters
Get the latest WinTun driver from the official website:
- Official Site: https://www.wintun.net/
- GitHub: https://git.zx2c4.com/wintun/about/
WinTun can be deployed in several ways:
Option A: System-wide installation
# Extract wintun.dll to C:\Windows\System32
# The driver is automatically loaded when neededOption B: Application bundle
# Include wintun.dll with your application
# Load from application directory
Use the WinTun API to create a virtual network adapter programmatically, or use network configuration tools.
User Space Application (Magma.WinTun)
│
↓
WinTun API (wintun.dll)
│
↓ (IOCTL/DeviceIoControl)
WinTun Driver (wintun.sys)
│
↓
Windows Network Stack
│
↓
Virtual Network Adapter
The current Magma.WinTun implementation uses TAP-Windows compatible interface:
- Device Access: Uses Windows device handles via
CreateFile - I/O Operations: FileStream-based read/write operations
- Registry Integration: Discovers adapters via Windows registry
- Async I/O: Overlapped I/O for efficient packet processing
- WinTunPort: Main port abstraction managing file handles and packet flow
- WinTunTransitter: Implements
IPacketTransmitterfor packet transmission - WinTunTransportReceiver: Implements
IPacketReceiverfor packet reception - WinTunMemoryPool: Memory pooling for efficient buffer management
For best performance and modern Windows compatibility, consider upgrading to the native WinTun API:
- Ring Buffer Interface: Direct access to shared memory rings (similar to AF_XDP)
- Zero-copy: Avoid data copies between user/kernel space
- Better Performance: Optimized for modern Windows versions
- Cleaner API: Purpose-built for TUN interfaces
// Create adapter
WintunCreateAdapter(name, tunnelType, requestedGUID)
// Get read/write handles
WintunStartSession(adapter, capacity)
// Receive packets
WintunReceivePacket(session, out packetSize)
WintunReleaseReceivePacket(session, packet)
// Send packets
WintunAllocateSendPacket(session, packetSize)
WintunSendPacket(session, packet)
// Cleanup
WintunEndSession(session)
WintunDeleteAdapter(adapter)// Current implementation (TAP-compatible)
var port = new WinTunPort<TcpTransportReceiver<WinTunTransitter>>(
adapterName: "Magma TAP",
packetReceiverFactory: transmitter =>
new TcpTransportReceiver<WinTunTransitter>(
endpoint, transmitter, dispatcher
)
);| Feature | WinTun | TAP-Windows (Legacy) |
|---|---|---|
| Driver Size | < 50KB | > 100KB |
| Performance | Optimized | Moderate |
| Maintenance | Active | Deprecated |
| Windows 10+ | Native support | Compatibility mode |
| API | Modern C API | IOCTL-based |
| License | MIT | GPL (complex) |
After creating a WinTun adapter, configure it using standard Windows networking:
# Set IP address
netsh interface ip set address "Magma TAP" static 10.0.0.1 255.255.255.0
# Set DNS (optional)
netsh interface ip set dns "Magma TAP" static 8.8.8.8
# Enable interface
netsh interface set interface "Magma TAP" admin=enabled- Ensure wintun.dll is in System32 or application directory
- Check Windows version compatibility (Windows 7+)
- Run application as Administrator
- Check User Account Control (UAC) settings
- Disable hardware offload on physical adapter if routing through WinTun
- Use native WinTun API instead of TAP compatibility layer
- Consider increasing buffer sizes
Potential improvements for Magma.WinTun:
- Native WinTun API integration (ring buffer based)
- Zero-copy packet processing
- Multi-queue support for parallel processing
- Better async/await patterns
- Improved error handling and diagnostics