Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions endian/big.go

This file was deleted.

8 changes: 0 additions & 8 deletions endian/little.go

This file was deleted.

11 changes: 11 additions & 0 deletions native/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Package native provides easy access to native byte order.
//
// Usage: use native.Endian where you need the native binary.ByteOrder.
//
// Please think twice before using this package.
// It can break program portability.
// Native byte order is usually not the right answer.
// This package is a drop-in of github.com/josharian/native, with attribution in
// the file 'license'. This package will likely be replaced by something similar
// in the standard library, see https://github.com/golang/go/issues/57237.
package native
14 changes: 14 additions & 0 deletions native/endian_big.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build mips || mips64 || ppc64 || s390x
// +build mips mips64 ppc64 s390x

package native

import "encoding/binary"

// Endian is the encoding/binary.ByteOrder implementation for the
// current CPU's native byte order.
var Endian = binary.BigEndian

// IsBigEndian is whether the current CPU's native byte order is big
// endian.
const IsBigEndian = true
31 changes: 31 additions & 0 deletions native/endian_generic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//go:build !mips && !mips64 && !ppc64 && !s390x && !amd64 && !386 && !arm && !arm64 && !loong64 && !mipsle && !mips64le && !ppc64le && !riscv64 && !wasm
// +build !mips,!mips64,!ppc64,!s390x,!amd64,!386,!arm,!arm64,!loong64,!mipsle,!mips64le,!ppc64le,!riscv64,!wasm

// This file is a fallback, so that package native doesn't break
// the instant the Go project adds support for a new architecture.
//

package native

import (
"encoding/binary"
"log"
"runtime"
"unsafe"
)

var Endian binary.ByteOrder

var IsBigEndian bool

func init() {
b := uint16(0xff) // one byte
if *(*byte)(unsafe.Pointer(&b)) == 0 {
Endian = binary.BigEndian
IsBigEndian = true
} else {
Endian = binary.LittleEndian
IsBigEndian = false
}
log.Printf("unrecognized arch %v (%v), please file an issue", runtime.GOARCH, Endian)
}
14 changes: 14 additions & 0 deletions native/endian_little.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build amd64 || 386 || arm || arm64 || loong64 || mipsle || mips64le || ppc64le || riscv64 || wasm
// +build amd64 386 arm arm64 loong64 mipsle mips64le ppc64le riscv64 wasm

package native

import "encoding/binary"

// Endian is the encoding/binary.ByteOrder implementation for the
// current CPU's native byte order.
var Endian = binary.LittleEndian

// IsBigEndian is whether the current CPU's native byte order is big
// endian.
const IsBigEndian = false
20 changes: 20 additions & 0 deletions native/endian_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package native_test

import (
"encoding/binary"
"testing"

"golang.zx2c4.com/wireguard/native"
)

func TestPrintEndianness(t *testing.T) {
t.Logf("native endianness is %v", native.Endian)

var want binary.ByteOrder = binary.BigEndian
if !native.IsBigEndian {
want = binary.LittleEndian
}
if native.Endian != want {
t.Errorf("IsBigEndian = %v not consistent with native.Endian = %T", native.IsBigEndian, want)
}
}
7 changes: 7 additions & 0 deletions native/license
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2020 Josh Bleecher Snyder

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18 changes: 9 additions & 9 deletions tun/tcp_offload_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"golang.org/x/sys/unix"
"golang.zx2c4.com/wireguard/conn"
"golang.zx2c4.com/wireguard/endian"
"golang.zx2c4.com/wireguard/native"
)

// virtioNetHdrFlags are defined in the kernel in include/uapi/linux/virtio_net.h.
Expand Down Expand Up @@ -52,10 +52,10 @@ func (v *virtioNetHdr) decode(b []byte) error {
}
v.flags = b[0]
v.gsoType = b[1]
v.hdrLen = endian.Native.Uint16(b[2:])
v.gsoSize = endian.Native.Uint16(b[4:])
v.csumStart = endian.Native.Uint16(b[6:])
v.csumOffset = endian.Native.Uint16(b[8:])
v.hdrLen = native.Endian.Uint16(b[2:])
v.gsoSize = native.Endian.Uint16(b[4:])
v.csumStart = native.Endian.Uint16(b[6:])
v.csumOffset = native.Endian.Uint16(b[8:])
return nil
}

Expand All @@ -65,10 +65,10 @@ func (v *virtioNetHdr) encode(b []byte) error {
}
b[0] = v.flags
b[1] = v.gsoType
endian.Native.PutUint16(b[2:], v.hdrLen)
endian.Native.PutUint16(b[4:], v.gsoSize)
endian.Native.PutUint16(b[6:], v.csumStart)
endian.Native.PutUint16(b[8:], v.csumOffset)
native.Endian.PutUint16(b[2:], v.hdrLen)
native.Endian.PutUint16(b[4:], v.gsoSize)
native.Endian.PutUint16(b[6:], v.csumStart)
native.Endian.PutUint16(b[8:], v.csumOffset)
return nil
}

Expand Down