wintun: Auto-calculate TUN exchange buffer size

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2019-02-08 15:21:24 +01:00
parent f05f52637f
commit b719a09a26

View File

@ -18,7 +18,6 @@ import (
const ( const (
packetSizeMax = 1600 packetSizeMax = 1600
packetExchangeMax = 256 // Number of packets that can be exchanged at a time packetExchangeMax = 256 // Number of packets that can be exchanged at a time
exchangeBufferSize = 410632
) )
const ( const (
@ -252,10 +251,10 @@ func (tun *nativeTun) Read(buff []byte, offset int) (int, error) {
} }
// Fill queue. // Fill queue.
data := (*[exchangeBufferSize]byte)(unsafe.Pointer(&tun.rdBuff)) const bufSize = int(unsafe.Sizeof(tun.rdBuff))
n, err := tun.tunFile.Read(data[:]) n, err := tun.tunFile.Read((*[bufSize]byte)(unsafe.Pointer(&tun.rdBuff))[:])
tun.rdNextPacket = 0 tun.rdNextPacket = 0
if n != exchangeBufferSize || err != nil { if n != bufSize || err != nil {
// TUN interface stopped, returned incomplete data, etc. // TUN interface stopped, returned incomplete data, etc.
// Retry. // Retry.
tun.rdBuff.numPackets = 0 tun.rdBuff.numPackets = 0
@ -269,14 +268,14 @@ func (tun *nativeTun) Read(buff []byte, offset int) (int, error) {
func (tun *nativeTun) flush() error { func (tun *nativeTun) flush() error {
// Flush write buffer. // Flush write buffer.
data := (*[exchangeBufferSize]byte)(unsafe.Pointer(&tun.wrBuff)) const bufSize = int(unsafe.Sizeof(tun.wrBuff))
n, err := tun.tunFile.Write(data[:]) n, err := tun.tunFile.Write((*[bufSize]byte)(unsafe.Pointer(&tun.wrBuff))[:])
tun.wrBuff.numPackets = 0 tun.wrBuff.numPackets = 0
if err != nil { if err != nil {
return err return err
} }
if n != exchangeBufferSize { if n != bufSize {
return fmt.Errorf("%d byte(s) written, %d byte(s) expected", n, exchangeBufferSize) return fmt.Errorf("%d byte(s) written, %d byte(s) expected", n, bufSize)
} }
return nil return nil