wintun: migrate to wintun.dll API

Rather than having every application using Wintun driver reinvent the
wheel, the Wintun device/adapter/interface management has been moved
from wireguard-go to wintun.dll deployed with Wintun itself.

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman
2020-07-22 09:15:49 +02:00
parent 5ca1218a5c
commit 3e08b8aee0
25 changed files with 183 additions and 3725 deletions

View File

@@ -33,7 +33,7 @@ type rateJuggler struct {
}
type NativeTun struct {
wt *wintun.Interface
wt *wintun.Adapter
handle windows.Handle
close bool
events chan Event
@@ -44,7 +44,15 @@ type NativeTun struct {
writeLock sync.Mutex
}
const WintunPool = wintun.Pool("WireGuard")
var WintunPool *wintun.Pool
func init() {
var err error
WintunPool, err = wintun.MakePool("WireGuard")
if err != nil {
panic(fmt.Errorf("Failed to make pool: %v", err))
}
}
//go:linkname procyield runtime.procyield
func procyield(cycles uint32)
@@ -66,18 +74,18 @@ func CreateTUN(ifname string, mtu int) (Device, error) {
//
func CreateTUNWithRequestedGUID(ifname string, requestedGUID *windows.GUID, mtu int) (Device, error) {
var err error
var wt *wintun.Interface
var wt *wintun.Adapter
// Does an interface with this name already exist?
wt, err = WintunPool.GetInterface(ifname)
wt, err = WintunPool.OpenAdapter(ifname)
if err == nil {
// If so, we delete it, in case it has weird residual configuration.
_, err = wt.DeleteInterface()
_, err = wt.Delete(true)
if err != nil {
return nil, fmt.Errorf("Error deleting already existing interface: %v", err)
}
}
wt, _, err = WintunPool.CreateInterface(ifname, requestedGUID)
wt, _, err = WintunPool.CreateAdapter(ifname, requestedGUID)
if err != nil {
return nil, fmt.Errorf("Error creating interface: %v", err)
}
@@ -132,7 +140,7 @@ func (tun *NativeTun) Close() error {
tun.rings.Close()
var err error
if tun.wt != nil {
_, err = tun.wt.DeleteInterface()
_, err = tun.wt.Delete(false)
}
close(tun.events)
return err
@@ -254,9 +262,9 @@ func (tun *NativeTun) LUID() uint64 {
return tun.wt.LUID()
}
// Version returns the version of the Wintun driver and NDIS system currently loaded.
func (tun *NativeTun) Version() (driverVersion string, ndisVersion string, err error) {
return tun.wt.Version()
// RunningVersion returns the running version of the Wintun driver.
func (tun *NativeTun) RunningVersion() (version uint32, err error) {
return wintun.RunningVersion()
}
func (rate *rateJuggler) update(packetLen uint64) {