tun: windows: delay initial write

Otherwise we provoke Wintun 0.3.
This commit is contained in:
Jason A. Donenfeld 2019-07-04 22:41:42 +02:00
parent f361e59001
commit 13abbdf14b

View File

@ -11,6 +11,7 @@ import (
"io" "io"
"os" "os"
"sync" "sync"
"sync/atomic"
"time" "time"
"unsafe" "unsafe"
@ -38,16 +39,17 @@ type exchgBufWrite struct {
} }
type NativeTun struct { type NativeTun struct {
wt *wintun.Wintun wt *wintun.Wintun
tunFileRead *os.File tunFileRead *os.File
tunFileWrite *os.File tunFileWrite *os.File
tunLock sync.Mutex haveRegisteredWriteBuffer int32
close bool tunLock sync.Mutex
rdBuff *exchgBufRead close bool
wrBuff *exchgBufWrite rdBuff *exchgBufRead
events chan Event wrBuff *exchgBufWrite
errors chan error events chan Event
forcedMTU int errors chan error
forcedMTU int
} }
func packetAlign(size uint32) uint32 { func packetAlign(size uint32) uint32 {
@ -140,12 +142,7 @@ func (tun *NativeTun) openTUN() error {
} }
return err return err
} }
firstSize := (*uint32)(unsafe.Pointer(&tun.wrBuff.data[0])) atomic.StoreInt32(&tun.haveRegisteredWriteBuffer, 0)
saved := *firstSize
*firstSize = 0
// Set the maximum buffer length with an invalid write.
tun.tunFileWrite.Write(tun.wrBuff.data[:])
*firstSize = saved
} }
return nil return nil
} }
@ -324,6 +321,15 @@ func (tun *NativeTun) Flush() error {
return err return err
} }
if atomic.CompareAndSwapInt32(&tun.haveRegisteredWriteBuffer, 0, 1) {
firstSize := (*uint32)(unsafe.Pointer(&tun.wrBuff.data[0]))
saved := *firstSize
*firstSize = 0
// Set the maximum buffer length with an invalid write.
tun.tunFileWrite.Write(tun.wrBuff.data[:])
*firstSize = saved
}
for { for {
_, err = file.Write(tun.wrBuff.data[:tun.wrBuff.offset]) _, err = file.Write(tun.wrBuff.data[:tun.wrBuff.offset])
if err != nil { if err != nil {