From 0fa3db229ce2036ae779de8ba03d336b7aa826bd Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Tue, 6 Dec 2022 20:54:09 -0800 Subject: wintun: return errors instead of panicking where possible Apparently there are weird crashes related to this, and returning an err would help diagnose them. Signed-off-by: Brad Fitzpatrick Signed-off-by: Jason A. Donenfeld --- wintun.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/wintun.go b/wintun.go index 254dc1e..1ffd62f 100644 --- a/wintun.go +++ b/wintun.go @@ -89,6 +89,9 @@ func CreateAdapter(name string, tunnelType string, requestedGUID *windows.GUID) if err != nil { return } + if err := procWintunCreateAdapter.Find(); err != nil { + return nil, err + } r0, _, e1 := syscall.Syscall(procWintunCreateAdapter.Addr(), 3, uintptr(unsafe.Pointer(name16)), uintptr(unsafe.Pointer(tunnelType16)), uintptr(unsafe.Pointer(requestedGUID))) if r0 == 0 { err = e1 @@ -106,6 +109,9 @@ func OpenAdapter(name string) (wintun *Adapter, err error) { if err != nil { return } + if err := procWintunOpenAdapter.Find(); err != nil { + return nil, err + } r0, _, e1 := syscall.Syscall(procWintunOpenAdapter.Addr(), 1, uintptr(unsafe.Pointer(name16)), 0, 0) if r0 == 0 { err = e1 @@ -118,6 +124,9 @@ func OpenAdapter(name string) (wintun *Adapter, err error) { // Close closes a Wintun adapter. func (wintun *Adapter) Close() (err error) { + if err := procWintunCloseAdapter.Find(); err != nil { + return err + } runtime.SetFinalizer(wintun, nil) r1, _, e1 := syscall.Syscall(procWintunCloseAdapter.Addr(), 1, wintun.handle, 0, 0) if r1 == 0 { @@ -128,6 +137,9 @@ func (wintun *Adapter) Close() (err error) { // Uninstall removes the driver from the system if no drivers are currently in use. func Uninstall() (err error) { + if err := procWintunDeleteDriver.Find(); err != nil { + return err + } r1, _, e1 := syscall.Syscall(procWintunDeleteDriver.Addr(), 0, 0, 0, 0) if r1 == 0 { err = e1 @@ -137,6 +149,9 @@ func Uninstall() (err error) { // RunningVersion returns the version of the loaded driver. func RunningVersion() (version uint32, err error) { + if err := procWintunGetRunningDriverVersion.Find(); err != nil { + return 0, err + } r0, _, e1 := syscall.Syscall(procWintunGetRunningDriverVersion.Addr(), 0, 0, 0, 0) version = uint32(r0) if version == 0 { -- cgit v1.2.3-59-g8ed1b