wintun: Migrate from unsafe buffer handling to encoding/binary
Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
parent
8091c6474a
commit
2491f9d454
@ -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,16 +230,18 @@ func (tun *nativeTun) Read(buff []byte, offset int) (int, error) {
|
|||||||
for {
|
for {
|
||||||
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.
|
||||||
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)
|
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.
|
||||||
tun.rdBuff.avail = 0
|
tun.rdBuff.avail = 0
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
packet = packet[:pSize]
|
||||||
|
|
||||||
// Copy data.
|
// Copy data.
|
||||||
copy(buff[offset:], tun.rdBuff.data[tun.rdBuff.offset+packetExchangeAlignment:][:size])
|
copy(buff[offset:], packet[packetExchangeAlignment:][:size])
|
||||||
tun.rdBuff.offset += pSize
|
tun.rdBuff.offset += pSize
|
||||||
return int(size), nil
|
return int(size), nil
|
||||||
}
|
}
|
||||||
@ -330,8 +332,9 @@ func (tun *nativeTun) putTunPacket(buff []byte) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Write packet to the exchange buffer.
|
// Write packet to the exchange buffer.
|
||||||
*(*uint32)(unsafe.Pointer(&tun.wrBuff.data[tun.wrBuff.offset])) = size
|
packet := tun.wrBuff.data[tun.wrBuff.offset:][:pSize]
|
||||||
copy(tun.wrBuff.data[tun.wrBuff.offset+packetExchangeAlignment:][:size], buff)
|
binary.LittleEndian.PutUint32(packet[:4], size)
|
||||||
|
copy(packet[packetExchangeAlignment:][:size], buff)
|
||||||
|
|
||||||
tun.wrBuff.packetNum++
|
tun.wrBuff.packetNum++
|
||||||
tun.wrBuff.offset += pSize
|
tun.wrBuff.offset += pSize
|
||||||
|
Loading…
Reference in New Issue
Block a user