wintun: Read/write packet size from/to exchange buffer directly

Driver <-> user-space communication is local and using native endian.

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2019-02-22 16:16:14 +01:00
parent e06a8f8f9f
commit d002eff155

View file

@ -6,10 +6,10 @@
package tun package tun
import ( import (
"encoding/binary"
"errors" "errors"
"os" "os"
"sync" "sync"
"unsafe"
"golang.org/x/sys/windows" "golang.org/x/sys/windows"
"golang.zx2c4.com/wireguard/tun/wintun" "golang.zx2c4.com/wireguard/tun/wintun"
@ -230,7 +230,7 @@ func (tun *nativeTun) Read(buff []byte, offset int) (int, error) {
if tun.rdBuff.offset+packetExchangeAlignment <= tun.rdBuff.avail { if tun.rdBuff.offset+packetExchangeAlignment <= tun.rdBuff.avail {
// Get packet from the exchange buffer. // Get packet from the exchange buffer.
packet := tun.rdBuff.data[tun.rdBuff.offset:] packet := tun.rdBuff.data[tun.rdBuff.offset:]
size := binary.LittleEndian.Uint32(packet[:4]) size := *(*uint32)(unsafe.Pointer(&packet[0]))
pSize := packetAlign(packetExchangeAlignment + size) pSize := packetAlign(packetExchangeAlignment + size)
if packetSizeMax < size || tun.rdBuff.avail < tun.rdBuff.offset+pSize { if packetSizeMax < size || tun.rdBuff.avail < tun.rdBuff.offset+pSize {
// Invalid packet size. // Invalid packet size.
@ -332,7 +332,7 @@ func (tun *nativeTun) putTunPacket(buff []byte) error {
// Write packet to the exchange buffer. // Write packet to the exchange buffer.
packet := tun.wrBuff.data[tun.wrBuff.offset : tun.wrBuff.offset+pSize] packet := tun.wrBuff.data[tun.wrBuff.offset : tun.wrBuff.offset+pSize]
binary.LittleEndian.PutUint32(packet[:4], size) *(*uint32)(unsafe.Pointer(&packet[0])) = size
copy(packet[packetExchangeAlignment:packetExchangeAlignment+size], buff) copy(packet[packetExchangeAlignment:packetExchangeAlignment+size], buff)
tun.wrBuff.packetNum++ tun.wrBuff.packetNum++