diff options
author | Simon Rozman <simon@rozman.si> | 2019-02-20 20:10:24 +0100 |
---|---|---|
committer | Simon Rozman <simon@rozman.si> | 2019-02-20 20:10:24 +0100 |
commit | 2491f9d4540cac7d38805531ab120e9f2d1cdfb3 (patch) | |
tree | 9690251cfc19372aac6512bfbbbf4dc3884f243c /tun | |
parent | 8091c6474a545fec806fd1a3611d7cb50a099746 (diff) |
wintun: Migrate from unsafe buffer handling to encoding/binary
Signed-off-by: Simon Rozman <simon@rozman.si>
Diffstat (limited to 'tun')
-rw-r--r-- | tun/tun_windows.go | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/tun/tun_windows.go b/tun/tun_windows.go index 838fdd1..5640a5d 100644 --- a/tun/tun_windows.go +++ b/tun/tun_windows.go @@ -6,10 +6,10 @@ package tun import ( + "encoding/binary" "errors" "os" "sync" - "unsafe" "golang.org/x/sys/windows" "golang.zx2c4.com/wireguard/tun/wintun" @@ -230,16 +230,18 @@ func (tun *nativeTun) Read(buff []byte, offset int) (int, error) { for { if tun.rdBuff.offset+packetExchangeAlignment <= tun.rdBuff.avail { // Get packet from the exchange buffer. - size := *(*uint32)(unsafe.Pointer(&tun.rdBuff.data[tun.rdBuff.offset])) + packet := tun.rdBuff.data[tun.rdBuff.offset:] + size := binary.LittleEndian.Uint32(packet[:4]) pSize := packetAlign(packetExchangeAlignment + size) if packetSizeMax < size || tun.rdBuff.avail < tun.rdBuff.offset+pSize { // Invalid packet size. tun.rdBuff.avail = 0 continue } + packet = packet[:pSize] // Copy data. - copy(buff[offset:], tun.rdBuff.data[tun.rdBuff.offset+packetExchangeAlignment:][:size]) + copy(buff[offset:], packet[packetExchangeAlignment:][:size]) tun.rdBuff.offset += pSize return int(size), nil } @@ -330,8 +332,9 @@ func (tun *nativeTun) putTunPacket(buff []byte) error { } // Write packet to the exchange buffer. - *(*uint32)(unsafe.Pointer(&tun.wrBuff.data[tun.wrBuff.offset])) = size - copy(tun.wrBuff.data[tun.wrBuff.offset+packetExchangeAlignment:][:size], buff) + packet := tun.wrBuff.data[tun.wrBuff.offset:][:pSize] + binary.LittleEndian.PutUint32(packet[:4], size) + copy(packet[packetExchangeAlignment:][:size], buff) tun.wrBuff.packetNum++ tun.wrBuff.offset += pSize |