diff options
author | Simon Rozman <simon@rozman.si> | 2019-02-08 15:21:24 +0100 |
---|---|---|
committer | Simon Rozman <simon@rozman.si> | 2019-02-08 15:21:24 +0100 |
commit | b719a09a26041c696e213213cebdc8ecd30ad4f3 (patch) | |
tree | 4b5b1ac4072ca6a02782a3f1bfd6ea172aa5104a | |
parent | f05f52637f32319564a1be9c0564b4fab3e9093b (diff) |
wintun: Auto-calculate TUN exchange buffer size
Signed-off-by: Simon Rozman <simon@rozman.si>
-rw-r--r-- | tun/tun_windows.go | 15 |
1 files changed, 7 insertions, 8 deletions
diff --git a/tun/tun_windows.go b/tun/tun_windows.go index 57be3dc..0bfd847 100644 --- a/tun/tun_windows.go +++ b/tun/tun_windows.go @@ -18,7 +18,6 @@ import ( const ( packetSizeMax = 1600 packetExchangeMax = 256 // Number of packets that can be exchanged at a time - exchangeBufferSize = 410632 ) const ( @@ -252,10 +251,10 @@ func (tun *nativeTun) Read(buff []byte, offset int) (int, error) { } // Fill queue. - data := (*[exchangeBufferSize]byte)(unsafe.Pointer(&tun.rdBuff)) - n, err := tun.tunFile.Read(data[:]) + const bufSize = int(unsafe.Sizeof(tun.rdBuff)) + n, err := tun.tunFile.Read((*[bufSize]byte)(unsafe.Pointer(&tun.rdBuff))[:]) tun.rdNextPacket = 0 - if n != exchangeBufferSize || err != nil { + if n != bufSize || err != nil { // TUN interface stopped, returned incomplete data, etc. // Retry. tun.rdBuff.numPackets = 0 @@ -269,14 +268,14 @@ func (tun *nativeTun) Read(buff []byte, offset int) (int, error) { func (tun *nativeTun) flush() error { // Flush write buffer. - data := (*[exchangeBufferSize]byte)(unsafe.Pointer(&tun.wrBuff)) - n, err := tun.tunFile.Write(data[:]) + const bufSize = int(unsafe.Sizeof(tun.wrBuff)) + n, err := tun.tunFile.Write((*[bufSize]byte)(unsafe.Pointer(&tun.wrBuff))[:]) tun.wrBuff.numPackets = 0 if err != nil { return err } - if n != exchangeBufferSize { - return fmt.Errorf("%d byte(s) written, %d byte(s) expected", n, exchangeBufferSize) + if n != bufSize { + return fmt.Errorf("%d byte(s) written, %d byte(s) expected", n, bufSize) } return nil |