Skip to content

Commit

Permalink
route: import syscall rather than golang.org/x/sys/unix
Browse files Browse the repository at this point in the history
It happens that everything we need is already defined in syscall.
This avoids problems when this package is vendored into the
standard library.

For golang/go#54259

Change-Id: I86bfe44f20c9db2ecfdb8dbb2ef798391da2bfa6
Reviewed-on: https://go-review.googlesource.com/c/net/+/421425
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
  • Loading branch information
ianlancetaylor authored and gopherbot committed Aug 5, 2022
1 parent 7431dee commit a33c5aa
Show file tree
Hide file tree
Showing 18 changed files with 170 additions and 189 deletions.
35 changes: 17 additions & 18 deletions route/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ package route

import (
"runtime"

"golang.org/x/sys/unix"
"syscall"
)

// An Addr represents an address associated with packet routing.
Expand All @@ -27,7 +26,7 @@ type LinkAddr struct {
}

// Family implements the Family method of Addr interface.
func (a *LinkAddr) Family() int { return unix.AF_LINK }
func (a *LinkAddr) Family() int { return syscall.AF_LINK }

func (a *LinkAddr) lenAndSpace() (int, int) {
l := 8 + len(a.Name) + len(a.Addr)
Expand All @@ -44,7 +43,7 @@ func (a *LinkAddr) marshal(b []byte) (int, error) {
return 0, errInvalidAddr
}
b[0] = byte(l)
b[1] = unix.AF_LINK
b[1] = syscall.AF_LINK
if a.Index > 0 {
nativeEndian.PutUint16(b[2:4], uint16(a.Index))
}
Expand All @@ -66,7 +65,7 @@ func parseLinkAddr(b []byte) (Addr, error) {
if len(b) < 8 {
return nil, errInvalidAddr
}
_, a, err := parseKernelLinkAddr(unix.AF_LINK, b[4:])
_, a, err := parseKernelLinkAddr(syscall.AF_LINK, b[4:])
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -126,7 +125,7 @@ type Inet4Addr struct {
}

// Family implements the Family method of Addr interface.
func (a *Inet4Addr) Family() int { return unix.AF_INET }
func (a *Inet4Addr) Family() int { return syscall.AF_INET }

func (a *Inet4Addr) lenAndSpace() (int, int) {
return sizeofSockaddrInet, roundup(sizeofSockaddrInet)
Expand All @@ -138,7 +137,7 @@ func (a *Inet4Addr) marshal(b []byte) (int, error) {
return 0, errShortBuffer
}
b[0] = byte(l)
b[1] = unix.AF_INET
b[1] = syscall.AF_INET
copy(b[4:8], a.IP[:])
return ll, nil
}
Expand All @@ -150,7 +149,7 @@ type Inet6Addr struct {
}

// Family implements the Family method of Addr interface.
func (a *Inet6Addr) Family() int { return unix.AF_INET6 }
func (a *Inet6Addr) Family() int { return syscall.AF_INET6 }

func (a *Inet6Addr) lenAndSpace() (int, int) {
return sizeofSockaddrInet6, roundup(sizeofSockaddrInet6)
Expand All @@ -162,7 +161,7 @@ func (a *Inet6Addr) marshal(b []byte) (int, error) {
return 0, errShortBuffer
}
b[0] = byte(l)
b[1] = unix.AF_INET6
b[1] = syscall.AF_INET6
copy(b[8:24], a.IP[:])
if a.ZoneID > 0 {
nativeEndian.PutUint32(b[24:28], uint32(a.ZoneID))
Expand All @@ -173,14 +172,14 @@ func (a *Inet6Addr) marshal(b []byte) (int, error) {
// parseInetAddr parses b as an internet address for IPv4 or IPv6.
func parseInetAddr(af int, b []byte) (Addr, error) {
switch af {
case unix.AF_INET:
case syscall.AF_INET:
if len(b) < sizeofSockaddrInet {
return nil, errInvalidAddr
}
a := &Inet4Addr{}
copy(a.IP[:], b[4:8])
return a, nil
case unix.AF_INET6:
case syscall.AF_INET6:
if len(b) < sizeofSockaddrInet6 {
return nil, errInvalidAddr
}
Expand Down Expand Up @@ -249,7 +248,7 @@ func parseKernelInetAddr(af int, b []byte) (int, Addr, error) {
a := &Inet6Addr{}
copy(a.IP[:], b[off6:off6+16])
return int(b[0]), a, nil
case af == unix.AF_INET6:
case af == syscall.AF_INET6:
a := &Inet6Addr{}
if l-1 < off6 {
copy(a.IP[:], b[1:l])
Expand Down Expand Up @@ -369,15 +368,15 @@ func marshalAddrs(b []byte, as []Addr) (uint, error) {
}

func parseAddrs(attrs uint, fn func(int, []byte) (int, Addr, error), b []byte) ([]Addr, error) {
var as [unix.RTAX_MAX]Addr
af := int(unix.AF_UNSPEC)
for i := uint(0); i < unix.RTAX_MAX && len(b) >= roundup(0); i++ {
var as [syscall.RTAX_MAX]Addr
af := int(syscall.AF_UNSPEC)
for i := uint(0); i < syscall.RTAX_MAX && len(b) >= roundup(0); i++ {
if attrs&(1<<i) == 0 {
continue
}
if i <= unix.RTAX_BRD {
if i <= syscall.RTAX_BRD {
switch b[1] {
case unix.AF_LINK:
case syscall.AF_LINK:
a, err := parseLinkAddr(b)
if err != nil {
return nil, err
Expand All @@ -388,7 +387,7 @@ func parseAddrs(attrs uint, fn func(int, []byte) (int, Addr, error), b []byte) (
return nil, errMessageTooShort
}
b = b[l:]
case unix.AF_INET, unix.AF_INET6:
case syscall.AF_INET, syscall.AF_INET6:
af = int(b[1])
a, err := parseInetAddr(af, b)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions route/address_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ package route

import (
"reflect"
"syscall"
"testing"

"golang.org/x/sys/unix"
)

type parseAddrsOnDarwinTest struct {
Expand All @@ -20,7 +19,7 @@ type parseAddrsOnDarwinTest struct {

var parseAddrsOnDarwinLittleEndianTests = []parseAddrsOnDarwinTest{
{
unix.RTA_DST | unix.RTA_GATEWAY | unix.RTA_NETMASK,
syscall.RTA_DST | syscall.RTA_GATEWAY | syscall.RTA_NETMASK,
parseKernelInetAddr,
[]byte{
0x10, 0x2, 0x0, 0x0, 0xc0, 0xa8, 0x56, 0x0,
Expand Down
7 changes: 3 additions & 4 deletions route/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ package route

import (
"reflect"
"syscall"
"testing"

"golang.org/x/sys/unix"
)

type parseAddrsTest struct {
Expand All @@ -23,7 +22,7 @@ type parseAddrsTest struct {

var parseAddrsLittleEndianTests = []parseAddrsTest{
{
unix.RTA_DST | unix.RTA_GATEWAY | unix.RTA_NETMASK | unix.RTA_BRD,
syscall.RTA_DST | syscall.RTA_GATEWAY | syscall.RTA_NETMASK | syscall.RTA_BRD,
parseKernelInetAddr,
[]byte{
0x38, 0x12, 0x0, 0x0, 0xff, 0xff, 0xff, 0x0,
Expand Down Expand Up @@ -60,7 +59,7 @@ var parseAddrsLittleEndianTests = []parseAddrsTest{
},
},
{
unix.RTA_NETMASK | unix.RTA_IFP | unix.RTA_IFA,
syscall.RTA_NETMASK | syscall.RTA_IFP | syscall.RTA_IFA,
parseKernelInetAddr,
[]byte{
0x7, 0x0, 0x0, 0x0, 0xff, 0xff, 0xff, 0x0,
Expand Down
9 changes: 4 additions & 5 deletions route/interface_classic.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ package route

import (
"runtime"

"golang.org/x/sys/unix"
"syscall"
)

func (w *wireFormat) parseInterfaceMessage(_ RIBType, b []byte) (Message, error) {
Expand All @@ -22,13 +21,13 @@ func (w *wireFormat) parseInterfaceMessage(_ RIBType, b []byte) (Message, error)
return nil, errInvalidMessage
}
attrs := uint(nativeEndian.Uint32(b[4:8]))
if attrs&unix.RTA_IFP == 0 {
if attrs&syscall.RTA_IFP == 0 {
return nil, nil
}
m := &InterfaceMessage{
Version: int(b[2]),
Type: int(b[3]),
Addrs: make([]Addr, unix.RTAX_MAX),
Addrs: make([]Addr, syscall.RTAX_MAX),
Flags: int(nativeEndian.Uint32(b[8:12])),
Index: int(nativeEndian.Uint16(b[12:14])),
extOff: w.extOff,
Expand All @@ -38,7 +37,7 @@ func (w *wireFormat) parseInterfaceMessage(_ RIBType, b []byte) (Message, error)
if err != nil {
return nil, err
}
m.Addrs[unix.RTAX_IFP] = a
m.Addrs[syscall.RTAX_IFP] = a
m.Name = a.(*LinkAddr).Name
return m, nil
}
Expand Down
12 changes: 6 additions & 6 deletions route/interface_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

package route

import "golang.org/x/sys/unix"
import "syscall"

func (w *wireFormat) parseInterfaceMessage(typ RIBType, b []byte) (Message, error) {
var extOff, bodyOff int
if typ == unix.NET_RT_IFLISTL {
if typ == syscall.NET_RT_IFLISTL {
if len(b) < 20 {
return nil, errMessageTooShort
}
Expand All @@ -26,30 +26,30 @@ func (w *wireFormat) parseInterfaceMessage(typ RIBType, b []byte) (Message, erro
return nil, errInvalidMessage
}
attrs := uint(nativeEndian.Uint32(b[4:8]))
if attrs&unix.RTA_IFP == 0 {
if attrs&syscall.RTA_IFP == 0 {
return nil, nil
}
m := &InterfaceMessage{
Version: int(b[2]),
Type: int(b[3]),
Flags: int(nativeEndian.Uint32(b[8:12])),
Index: int(nativeEndian.Uint16(b[12:14])),
Addrs: make([]Addr, unix.RTAX_MAX),
Addrs: make([]Addr, syscall.RTAX_MAX),
extOff: extOff,
raw: b[:l],
}
a, err := parseLinkAddr(b[bodyOff:])
if err != nil {
return nil, err
}
m.Addrs[unix.RTAX_IFP] = a
m.Addrs[syscall.RTAX_IFP] = a
m.Name = a.(*LinkAddr).Name
return m, nil
}

func (w *wireFormat) parseInterfaceAddrMessage(typ RIBType, b []byte) (Message, error) {
var bodyOff int
if typ == unix.NET_RT_IFLISTL {
if typ == syscall.NET_RT_IFLISTL {
if len(b) < 24 {
return nil, errMessageTooShort
}
Expand Down
8 changes: 4 additions & 4 deletions route/interface_openbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

package route

import "golang.org/x/sys/unix"
import "syscall"

func (*wireFormat) parseInterfaceMessage(_ RIBType, b []byte) (Message, error) {
if len(b) < 32 {
Expand All @@ -15,15 +15,15 @@ func (*wireFormat) parseInterfaceMessage(_ RIBType, b []byte) (Message, error) {
return nil, errInvalidMessage
}
attrs := uint(nativeEndian.Uint32(b[12:16]))
if attrs&unix.RTA_IFP == 0 {
if attrs&syscall.RTA_IFP == 0 {
return nil, nil
}
m := &InterfaceMessage{
Version: int(b[2]),
Type: int(b[3]),
Flags: int(nativeEndian.Uint32(b[16:20])),
Index: int(nativeEndian.Uint16(b[6:8])),
Addrs: make([]Addr, unix.RTAX_MAX),
Addrs: make([]Addr, syscall.RTAX_MAX),
raw: b[:l],
}
ll := int(nativeEndian.Uint16(b[4:6]))
Expand All @@ -34,7 +34,7 @@ func (*wireFormat) parseInterfaceMessage(_ RIBType, b []byte) (Message, error) {
if err != nil {
return nil, err
}
m.Addrs[unix.RTAX_IFP] = a
m.Addrs[syscall.RTAX_IFP] = a
m.Name = a.(*LinkAddr).Name
return m, nil
}
Expand Down
7 changes: 3 additions & 4 deletions route/message_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
package route

import (
"syscall"
"testing"

"golang.org/x/sys/unix"
)

func TestFetchAndParseRIBOnDarwin(t *testing.T) {
for _, typ := range []RIBType{unix.NET_RT_FLAGS, unix.NET_RT_DUMP2, unix.NET_RT_IFLIST2} {
for _, typ := range []RIBType{syscall.NET_RT_FLAGS, syscall.NET_RT_DUMP2, syscall.NET_RT_IFLIST2} {
var lastErr error
var ms []Message
for _, af := range []int{unix.AF_UNSPEC, unix.AF_INET, unix.AF_INET6} {
for _, af := range []int{syscall.AF_UNSPEC, syscall.AF_INET, syscall.AF_INET6} {
rs, err := fetchAndParseRIB(af, typ)
if err != nil {
lastErr = err
Expand Down
15 changes: 7 additions & 8 deletions route/message_freebsd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
package route

import (
"syscall"
"testing"

"golang.org/x/sys/unix"
)

func TestFetchAndParseRIBOnFreeBSD(t *testing.T) {
for _, typ := range []RIBType{unix.NET_RT_IFMALIST} {
for _, typ := range []RIBType{syscall.NET_RT_IFMALIST} {
var lastErr error
var ms []Message
for _, af := range []int{unix.AF_UNSPEC, unix.AF_INET, unix.AF_INET6} {
for _, af := range []int{syscall.AF_UNSPEC, syscall.AF_INET, syscall.AF_INET6} {
rs, err := fetchAndParseRIB(af, typ)
if err != nil {
lastErr = err
Expand All @@ -38,7 +37,7 @@ func TestFetchAndParseRIBOnFreeBSD(t *testing.T) {
}

func TestFetchAndParseRIBOnFreeBSD10AndAbove(t *testing.T) {
if _, err := FetchRIB(unix.AF_UNSPEC, unix.NET_RT_IFLISTL, 0); err != nil {
if _, err := FetchRIB(syscall.AF_UNSPEC, syscall.NET_RT_IFLISTL, 0); err != nil {
t.Skip("NET_RT_IFLISTL not supported")
}
if compatFreeBSD32 {
Expand All @@ -51,12 +50,12 @@ func TestFetchAndParseRIBOnFreeBSD10AndAbove(t *testing.T) {
msgs []Message
ss []string
}{
{typ: unix.NET_RT_IFLIST},
{typ: unix.NET_RT_IFLISTL},
{typ: syscall.NET_RT_IFLIST},
{typ: syscall.NET_RT_IFLISTL},
}
for i := range tests {
var lastErr error
for _, af := range []int{unix.AF_UNSPEC, unix.AF_INET, unix.AF_INET6} {
for _, af := range []int{syscall.AF_UNSPEC, syscall.AF_INET, syscall.AF_INET6} {
rs, err := fetchAndParseRIB(af, tests[i].typ)
if err != nil {
lastErr = err
Expand Down
Loading

0 comments on commit a33c5aa

Please sign in to comment.