aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2019-04-29 11:37:02 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2019-04-29 11:37:02 +0200
commit4280e7ee4d8d461ec868970bf52846f3aad38b2a (patch)
treef7054f4c64a8f0f37c8433184b5f44c5792c13a8
parentgo.mod: use vendored winio (diff)
downloadwireguard-go-jd/single-if-test.tar.xz
wireguard-go-jd/single-if-test.zip
tun: windows: allow not deleting wintun on shutdownjd/single-if-test
-rw-r--r--tun/tun_windows.go67
1 files changed, 38 insertions, 29 deletions
diff --git a/tun/tun_windows.go b/tun/tun_windows.go
index 2fa6901..c69bbd5 100644
--- a/tun/tun_windows.go
+++ b/tun/tun_windows.go
@@ -38,31 +38,37 @@ type exchgBufWrite struct {
}
type NativeTun struct {
- wt *wintun.Wintun
- tunFileRead *os.File
- tunFileWrite *os.File
- tunLock sync.Mutex
- close bool
- rdBuff *exchgBufRead
- wrBuff *exchgBufWrite
- events chan TUNEvent
- errors chan error
- forcedMtu int
+ wt *wintun.Wintun
+ tunFileRead *os.File
+ tunFileWrite *os.File
+ tunLock sync.Mutex
+ close bool
+ rdBuff *exchgBufRead
+ wrBuff *exchgBufWrite
+ events chan TUNEvent
+ errors chan error
+ forcedMtu int
+ deleteOnClose bool
}
func packetAlign(size uint32) uint32 {
return (size + (packetExchangeAlignment - 1)) &^ (packetExchangeAlignment - 1)
}
+func CreateTUN(ifname string, deleteOnClose bool) (TUNDevice, error) {
+ return CreateTUNEx(ifname, true)
+}
+
//
-// CreateTUN creates a Wintun adapter with the given name. Should a Wintun
-// adapter with the same name exist, it is reused.
+// CreateTUNEx creates a Wintun adapter with the given name. Should a Wintun
+// adapter with the same name exist, it is reused. The function name is a
+// Windows joke...
//
-func CreateTUN(ifname string) (TUNDevice, error) {
+func CreateTUNEx(ifname string, deleteOnClose bool) (TUNDevice, error) {
// Does an interface with this name already exist?
wt, err := wintun.GetInterface(ifname, 0)
if wt == nil {
- // Interface does not exist or an error occured. Create one.
+ // Interface does not exist or an error occurred. Create one.
wt, _, err = wintun.CreateInterface("WireGuard Tunnel Adapter", 0)
if err != nil {
return nil, errors.New("Creating Wintun adapter failed: " + err.Error())
@@ -73,12 +79,12 @@ func CreateTUN(ifname string) (TUNDevice, error) {
// proces die without deleting this interface first, the interface would remain
// orphaned.
return nil, err
- }
-
- err = wt.SetInterfaceName(ifname)
- if err != nil {
- wt.DeleteInterface(0)
- return nil, err
+ } else {
+ err = wt.SetInterfaceName(ifname)
+ if err != nil {
+ wt.DeleteInterface(0)
+ return nil, err
+ }
}
err = wt.FlushInterface()
@@ -88,12 +94,13 @@ func CreateTUN(ifname string) (TUNDevice, error) {
}
return &NativeTun{
- wt: wt,
- rdBuff: &exchgBufRead{},
- wrBuff: &exchgBufWrite{},
- events: make(chan TUNEvent, 10),
- errors: make(chan error, 1),
- forcedMtu: 1500,
+ wt: wt,
+ rdBuff: &exchgBufRead{},
+ wrBuff: &exchgBufWrite{},
+ events: make(chan TUNEvent, 10),
+ errors: make(chan error, 1),
+ forcedMtu: 1500,
+ deleteOnClose: deleteOnClose,
}, nil
}
@@ -208,9 +215,11 @@ func (tun *NativeTun) Close() error {
close(tun.events)
}
- _, _, err2 := tun.wt.DeleteInterface(0)
- if err1 == nil {
- err1 = err2
+ if tun.deleteOnClose {
+ _, _, err2 := tun.wt.DeleteInterface(0)
+ if err1 == nil {
+ err1 = err2
+ }
}
return err1