Commit f9d2e385 authored by Mikio Hara's avatar Mikio Hara

icmp: use socket.NativeEndian

Change-Id: I991e893a0f0b0aea680ef18e09008b30e8a972c6
Reviewed-on: https://go-review.googlesource.com/46231
Run-TryBot: Mikio Hara <mikioh.mikioh@gmail.com>
Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 454122b1
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package icmp
import (
"encoding/binary"
"unsafe"
)
var (
// See http://www.freebsd.org/doc/en/books/porters-handbook/freebsd-versions.html.
freebsdVersion uint32
nativeEndian binary.ByteOrder
)
func init() {
i := uint32(1)
b := (*[4]byte)(unsafe.Pointer(&i))
if b[0] == 1 {
nativeEndian = binary.LittleEndian
} else {
nativeEndian = binary.BigEndian
}
}
...@@ -9,9 +9,14 @@ import ( ...@@ -9,9 +9,14 @@ import (
"net" "net"
"runtime" "runtime"
"golang.org/x/net/internal/socket"
"golang.org/x/net/ipv4" "golang.org/x/net/ipv4"
) )
// freebsdVersion is set in sys_freebsd.go.
// See http://www.freebsd.org/doc/en/books/porters-handbook/freebsd-versions.html.
var freebsdVersion uint32
// ParseIPv4Header parses b as an IPv4 header of ICMP error message // ParseIPv4Header parses b as an IPv4 header of ICMP error message
// invoking packet, which is contained in ICMP error message. // invoking packet, which is contained in ICMP error message.
func ParseIPv4Header(b []byte) (*ipv4.Header, error) { func ParseIPv4Header(b []byte) (*ipv4.Header, error) {
...@@ -36,12 +41,12 @@ func ParseIPv4Header(b []byte) (*ipv4.Header, error) { ...@@ -36,12 +41,12 @@ func ParseIPv4Header(b []byte) (*ipv4.Header, error) {
} }
switch runtime.GOOS { switch runtime.GOOS {
case "darwin": case "darwin":
h.TotalLen = int(nativeEndian.Uint16(b[2:4])) h.TotalLen = int(socket.NativeEndian.Uint16(b[2:4]))
case "freebsd": case "freebsd":
if freebsdVersion >= 1000000 { if freebsdVersion >= 1000000 {
h.TotalLen = int(binary.BigEndian.Uint16(b[2:4])) h.TotalLen = int(binary.BigEndian.Uint16(b[2:4]))
} else { } else {
h.TotalLen = int(nativeEndian.Uint16(b[2:4])) h.TotalLen = int(socket.NativeEndian.Uint16(b[2:4]))
} }
default: default:
h.TotalLen = int(binary.BigEndian.Uint16(b[2:4])) h.TotalLen = int(binary.BigEndian.Uint16(b[2:4]))
......
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
"runtime" "runtime"
"testing" "testing"
"golang.org/x/net/internal/socket"
"golang.org/x/net/ipv4" "golang.org/x/net/ipv4"
) )
...@@ -55,7 +56,7 @@ var ipv4HeaderLittleEndianTest = ipv4HeaderTest{ ...@@ -55,7 +56,7 @@ var ipv4HeaderLittleEndianTest = ipv4HeaderTest{
func TestParseIPv4Header(t *testing.T) { func TestParseIPv4Header(t *testing.T) {
tt := &ipv4HeaderLittleEndianTest tt := &ipv4HeaderLittleEndianTest
if nativeEndian != binary.LittleEndian { if socket.NativeEndian != binary.LittleEndian {
t.Skip("no test for non-little endian machine yet") t.Skip("no test for non-little endian machine yet")
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment