From 421c1f9143347d279c5a296a302720754b2a21b2 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Wed, 3 Apr 2019 05:40:35 +0200 Subject: [PATCH] tun: windows: Attempt to reopen handle on all errors Signed-off-by: Simon Rozman --- tun/tun_windows.go | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/tun/tun_windows.go b/tun/tun_windows.go index 948f08d..4d26dce 100644 --- a/tun/tun_windows.go +++ b/tun/tun_windows.go @@ -188,13 +188,6 @@ func (tun *NativeTun) getTUN() (read *os.File, write *os.File, err error) { return } -func (tun *NativeTun) shouldReopenHandle(err error) bool { - if pe, ok := err.(*os.PathError); ok && pe.Err == windows.ERROR_OPERATION_ABORTED { - return !tun.close - } - return false -} - func (tun *NativeTun) Name() (string, error) { return tun.wt.GetInterfaceName() } @@ -267,12 +260,13 @@ func (tun *NativeTun) Read(buff []byte, offset int) (int, error) { // Fill queue. n, err := file.Read(tun.rdBuff.data[:]) if err != nil { + tun.rdBuff.offset = 0 tun.rdBuff.avail = 0 - if tun.shouldReopenHandle(err) { - tun.closeTUN() - continue + if tun.close { + return 0, err } - return 0, err + tun.closeTUN() + continue } tun.rdBuff.offset = 0 tun.rdBuff.avail = uint32(n) @@ -298,11 +292,11 @@ func (tun *NativeTun) Flush() error { tun.wrBuff.packetNum = 0 tun.wrBuff.offset = 0 if err != nil { - if tun.shouldReopenHandle(err) { - tun.closeTUN() - continue + if tun.close { + return err } - return err + tun.closeTUN() + continue } return nil }