Skip to content

Commit

Permalink
route: change from syscall to x/sys/unix
Browse files Browse the repository at this point in the history
This lets us drop some of the defs files and cgo usage.

Change-Id: I5a00e77610da36c752d28ea07e40b8a9c7c59ae4
Reviewed-on: https://go-review.googlesource.com/c/net/+/632816
Auto-Submit: Ian Lance Taylor <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
Run-TryBot: Ian Lance Taylor <[email protected]>
Reviewed-by: Damien Neil <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
  • Loading branch information
ianlancetaylor authored and gopherbot committed Dec 2, 2024
1 parent bc37675 commit 4be1253
Show file tree
Hide file tree
Showing 33 changed files with 243 additions and 489 deletions.
45 changes: 23 additions & 22 deletions route/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ package route

import (
"runtime"
"syscall"

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

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

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

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

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

func (a *Inet4Addr) lenAndSpace() (int, int) {
return sizeofSockaddrInet, roundup(sizeofSockaddrInet)
return unix.SizeofSockaddrInet4, roundup(unix.SizeofSockaddrInet4)
}

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

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

func (a *Inet6Addr) lenAndSpace() (int, int) {
return sizeofSockaddrInet6, roundup(sizeofSockaddrInet6)
return unix.SizeofSockaddrInet6, roundup(unix.SizeofSockaddrInet6)
}

func (a *Inet6Addr) marshal(b []byte) (int, error) {
Expand All @@ -160,7 +161,7 @@ func (a *Inet6Addr) marshal(b []byte) (int, error) {
return 0, errShortBuffer
}
b[0] = byte(l)
b[1] = syscall.AF_INET6
b[1] = unix.AF_INET6
copy(b[8:24], a.IP[:])
if a.ZoneID > 0 {
nativeEndian.PutUint32(b[24:28], uint32(a.ZoneID))
Expand All @@ -175,7 +176,7 @@ func parseInetAddr(af int, b []byte) (Addr, error) {
off6 = 8 // offset of in6_addr
)
switch af {
case syscall.AF_INET:
case unix.AF_INET:
if len(b) < (off4+1) || len(b) < int(b[0]) || b[0] == 0 {
return nil, errInvalidAddr
}
Expand All @@ -187,7 +188,7 @@ func parseInetAddr(af int, b []byte) (Addr, error) {
}
copy(a.IP[:], b[off4:n])
return a, nil
case syscall.AF_INET6:
case unix.AF_INET6:
if len(b) < (off6+1) || len(b) < int(b[0]) || b[0] == 0 {
return nil, errInvalidAddr
}
Expand All @@ -197,7 +198,7 @@ func parseInetAddr(af int, b []byte) (Addr, error) {
n = sockAddrLen
}
a := &Inet6Addr{}
if sockAddrLen == sizeofSockaddrInet6 {
if sockAddrLen == unix.SizeofSockaddrInet6 {
a.ZoneID = int(nativeEndian.Uint32(b[24:28]))
}
copy(a.IP[:], b[off6:n])
Expand Down Expand Up @@ -260,19 +261,19 @@ func parseKernelInetAddr(af int, b []byte) (int, Addr, error) {
off6 = 8 // offset of in6_addr
)
switch {
case b[0] == sizeofSockaddrInet6:
case b[0] == unix.SizeofSockaddrInet6:
a := &Inet6Addr{}
copy(a.IP[:], b[off6:off6+16])
return int(b[0]), a, nil
case af == syscall.AF_INET6:
case af == unix.AF_INET6:
a := &Inet6Addr{}
if l-1 < off6 {
copy(a.IP[:], b[1:l])
} else {
copy(a.IP[:], b[l-off6:l])
}
return int(b[0]), a, nil
case b[0] == sizeofSockaddrInet:
case b[0] == unix.SizeofSockaddrInet4:
a := &Inet4Addr{}
copy(a.IP[:], b[off4:off4+4])
return int(b[0]), a, nil
Expand Down Expand Up @@ -384,15 +385,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 [syscall.RTAX_MAX]Addr
af := int(syscall.AF_UNSPEC)
for i := uint(0); i < syscall.RTAX_MAX && len(b) >= roundup(0); i++ {
var as [unix.RTAX_MAX]Addr
af := int(unix.AF_UNSPEC)
for i := uint(0); i < unix.RTAX_MAX && len(b) >= roundup(0); i++ {
if attrs&(1<<i) == 0 {
continue
}
if i <= syscall.RTAX_BRD {
if i <= unix.RTAX_BRD {
switch b[1] {
case syscall.AF_LINK:
case unix.AF_LINK:
a, err := parseLinkAddr(b)
if err != nil {
return nil, err
Expand All @@ -403,7 +404,7 @@ func parseAddrs(attrs uint, fn func(int, []byte) (int, Addr, error), b []byte) (
return nil, errMessageTooShort
}
b = b[l:]
case syscall.AF_INET, syscall.AF_INET6:
case unix.AF_INET, unix.AF_INET6:
// #70528: if the sockaddrlen is 0, no address to parse inside,
// skip over the record.
if b[0] > 0 {
Expand Down
13 changes: 7 additions & 6 deletions route/address_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ package route

import (
"reflect"
"syscall"
"testing"

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

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

var parseAddrsOnDarwinLittleEndianTests = []parseAddrsOnDarwinTest{
{
syscall.RTA_DST | syscall.RTA_GATEWAY | syscall.RTA_NETMASK,
unix.RTA_DST | unix.RTA_GATEWAY | unix.RTA_NETMASK,
parseKernelInetAddr,
[]byte{
0x10, 0x2, 0x0, 0x0, 0xc0, 0xa8, 0x56, 0x0,
Expand All @@ -43,7 +44,7 @@ var parseAddrsOnDarwinLittleEndianTests = []parseAddrsOnDarwinTest{
},
},
{
syscall.RTA_DST | syscall.RTA_GATEWAY | syscall.RTA_NETMASK,
unix.RTA_DST | unix.RTA_GATEWAY | unix.RTA_NETMASK,
parseKernelInetAddr,
[]byte{
0x10, 0x02, 0x00, 0x00, 0x64, 0x71, 0x00, 0x00,
Expand All @@ -69,7 +70,7 @@ var parseAddrsOnDarwinLittleEndianTests = []parseAddrsOnDarwinTest{
// route -n add -inet6 fd84:1b4e:6281:: -prefixlen 48 fe80::f22f:4bff:fe09:3bff%utun4319
// gw fe80:0000:0000:0000:f22f:4bff:fe09:3bff
{
syscall.RTA_DST | syscall.RTA_GATEWAY | syscall.RTA_NETMASK,
unix.RTA_DST | unix.RTA_GATEWAY | unix.RTA_NETMASK,
parseKernelInetAddr,
[]byte{
0x1c, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Expand Down Expand Up @@ -98,7 +99,7 @@ var parseAddrsOnDarwinLittleEndianTests = []parseAddrsOnDarwinTest{
},
// golang/go#70528, the kernel can produce addresses of length 0
{
syscall.RTA_DST | syscall.RTA_GATEWAY | syscall.RTA_NETMASK,
unix.RTA_DST | unix.RTA_GATEWAY | unix.RTA_NETMASK,
parseKernelInetAddr,
[]byte{
0x00, 0x1e, 0x00, 0x00,
Expand All @@ -124,7 +125,7 @@ var parseAddrsOnDarwinLittleEndianTests = []parseAddrsOnDarwinTest{
},
// Additional case: golang/go/issues/70528#issuecomment-2498692877
{
syscall.RTA_DST | syscall.RTA_GATEWAY | syscall.RTA_NETMASK,
unix.RTA_DST | unix.RTA_GATEWAY | unix.RTA_NETMASK,
parseKernelInetAddr,
[]byte{
0x84, 0x00, 0x05, 0x04, 0x01, 0x00, 0x00, 0x00, 0x03, 0x08, 0x00, 0x01, 0x15, 0x00, 0x00, 0x00,
Expand Down
7 changes: 4 additions & 3 deletions route/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ package route

import (
"reflect"
"syscall"
"testing"

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

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

var parseAddrsLittleEndianTests = []parseAddrsTest{
{
syscall.RTA_DST | syscall.RTA_GATEWAY | syscall.RTA_NETMASK | syscall.RTA_BRD,
unix.RTA_DST | unix.RTA_GATEWAY | unix.RTA_NETMASK | unix.RTA_BRD,
parseKernelInetAddr,
[]byte{
0x38, 0x12, 0x0, 0x0, 0xff, 0xff, 0xff, 0x0,
Expand Down Expand Up @@ -58,7 +59,7 @@ var parseAddrsLittleEndianTests = []parseAddrsTest{
},
},
{
syscall.RTA_NETMASK | syscall.RTA_IFP | syscall.RTA_IFA,
unix.RTA_NETMASK | unix.RTA_IFP | unix.RTA_IFA,
parseKernelInetAddr,
[]byte{
0x7, 0x0, 0x0, 0x0, 0xff, 0xff, 0xff, 0x0,
Expand Down
15 changes: 2 additions & 13 deletions route/defs_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,8 @@ package route
import "C"

const (
sizeofIfMsghdrDarwin15 = C.sizeof_struct_if_msghdr
sizeofIfaMsghdrDarwin15 = C.sizeof_struct_ifa_msghdr
sizeofIfmaMsghdrDarwin15 = C.sizeof_struct_ifma_msghdr
sizeofIfMsghdr2Darwin15 = C.sizeof_struct_if_msghdr2
sizeofIfmaMsghdr2Darwin15 = C.sizeof_struct_ifma_msghdr2
sizeofIfDataDarwin15 = C.sizeof_struct_if_data
sizeofIfData64Darwin15 = C.sizeof_struct_if_data64
sizeofIfMsghdr2Darwin15 = C.sizeof_struct_if_msghdr2
sizeofIfData64Darwin15 = C.sizeof_struct_if_data64

sizeofRtMsghdrDarwin15 = C.sizeof_struct_rt_msghdr
sizeofRtMsghdr2Darwin15 = C.sizeof_struct_rt_msghdr2
sizeofRtMetricsDarwin15 = C.sizeof_struct_rt_metrics

sizeofSockaddrStorage = C.sizeof_struct_sockaddr_storage
sizeofSockaddrInet = C.sizeof_struct_sockaddr_in
sizeofSockaddrInet6 = C.sizeof_struct_sockaddr_in6
)
56 changes: 0 additions & 56 deletions route/defs_dragonfly.go

This file was deleted.

12 changes: 0 additions & 12 deletions route/defs_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,6 @@ struct if_msghdr_freebsd11 {
import "C"

const (
sizeofIfMsghdrlFreeBSD10 = C.sizeof_struct_if_msghdrl
sizeofIfaMsghdrFreeBSD10 = C.sizeof_struct_ifa_msghdr
sizeofIfaMsghdrlFreeBSD10 = C.sizeof_struct_ifa_msghdrl
sizeofIfmaMsghdrFreeBSD10 = C.sizeof_struct_ifma_msghdr
sizeofIfAnnouncemsghdrFreeBSD10 = C.sizeof_struct_if_announcemsghdr

sizeofRtMsghdrFreeBSD10 = C.sizeof_struct_rt_msghdr
sizeofRtMetricsFreeBSD10 = C.sizeof_struct_rt_metrics

Expand All @@ -239,12 +233,6 @@ const (
sizeofIfDataFreeBSD10 = C.sizeof_struct_if_data_freebsd10
sizeofIfDataFreeBSD11 = C.sizeof_struct_if_data_freebsd11

sizeofIfMsghdrlFreeBSD10Emu = C.sizeof_struct_if_msghdrl
sizeofIfaMsghdrFreeBSD10Emu = C.sizeof_struct_ifa_msghdr
sizeofIfaMsghdrlFreeBSD10Emu = C.sizeof_struct_ifa_msghdrl
sizeofIfmaMsghdrFreeBSD10Emu = C.sizeof_struct_ifma_msghdr
sizeofIfAnnouncemsghdrFreeBSD10Emu = C.sizeof_struct_if_announcemsghdr

sizeofRtMsghdrFreeBSD10Emu = C.sizeof_struct_rt_msghdr
sizeofRtMetricsFreeBSD10Emu = C.sizeof_struct_rt_metrics

Expand Down
32 changes: 0 additions & 32 deletions route/defs_netbsd.go

This file was deleted.

Loading

0 comments on commit 4be1253

Please sign in to comment.