Skip to content

Commit

Permalink
Revert "route: change from syscall to x/sys/unix"
Browse files Browse the repository at this point in the history
This reverts CL 632816.

Reason for revert: This CL causes x/net to depend on x/sys. We have a
policy that prevents us from vendoring x/sys into std, but x/net
needs to be vendored.

Change-Id: I0fe3bc9861d559d888db6fa7febd48a201f060b8
Reviewed-on: https://go-review.googlesource.com/c/net/+/634196
Auto-Submit: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
mknyszek authored and gopherbot committed Dec 6, 2024
1 parent 13a7c01 commit 552d8ac
Show file tree
Hide file tree
Showing 33 changed files with 489 additions and 243 deletions.
45 changes: 22 additions & 23 deletions route/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ package route

import (
"runtime"

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

// An Addr represents an address associated with packet routing.
Expand All @@ -26,7 +25,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 @@ -43,7 +42,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 @@ -65,7 +64,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 @@ -125,10 +124,10 @@ 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 unix.SizeofSockaddrInet4, roundup(unix.SizeofSockaddrInet4)
return sizeofSockaddrInet, roundup(sizeofSockaddrInet)
}

func (a *Inet4Addr) marshal(b []byte) (int, error) {
Expand All @@ -137,7 +136,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 @@ -149,10 +148,10 @@ 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 unix.SizeofSockaddrInet6, roundup(unix.SizeofSockaddrInet6)
return sizeofSockaddrInet6, roundup(sizeofSockaddrInet6)
}

func (a *Inet6Addr) marshal(b []byte) (int, error) {
Expand All @@ -161,7 +160,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 @@ -176,7 +175,7 @@ func parseInetAddr(af int, b []byte) (Addr, error) {
off6 = 8 // offset of in6_addr
)
switch af {
case unix.AF_INET:
case syscall.AF_INET:
if len(b) < (off4+1) || len(b) < int(b[0]) || b[0] == 0 {
return nil, errInvalidAddr
}
Expand All @@ -188,7 +187,7 @@ func parseInetAddr(af int, b []byte) (Addr, error) {
}
copy(a.IP[:], b[off4:n])
return a, nil
case unix.AF_INET6:
case syscall.AF_INET6:
if len(b) < (off6+1) || len(b) < int(b[0]) || b[0] == 0 {
return nil, errInvalidAddr
}
Expand All @@ -198,7 +197,7 @@ func parseInetAddr(af int, b []byte) (Addr, error) {
n = sockAddrLen
}
a := &Inet6Addr{}
if sockAddrLen == unix.SizeofSockaddrInet6 {
if sockAddrLen == sizeofSockaddrInet6 {
a.ZoneID = int(nativeEndian.Uint32(b[24:28]))
}
copy(a.IP[:], b[off6:n])
Expand Down Expand Up @@ -261,19 +260,19 @@ func parseKernelInetAddr(af int, b []byte) (int, Addr, error) {
off6 = 8 // offset of in6_addr
)
switch {
case b[0] == unix.SizeofSockaddrInet6:
case b[0] == sizeofSockaddrInet6:
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])
} else {
copy(a.IP[:], b[l-off6:l])
}
return int(b[0]), a, nil
case b[0] == unix.SizeofSockaddrInet4:
case b[0] == sizeofSockaddrInet:
a := &Inet4Addr{}
copy(a.IP[:], b[off4:off4+4])
return int(b[0]), a, nil
Expand Down Expand Up @@ -385,15 +384,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 @@ -404,7 +403,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:
// #70528: if the sockaddrlen is 0, no address to parse inside,
// skip over the record.
if b[0] > 0 {
Expand Down
13 changes: 6 additions & 7 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 All @@ -44,7 +43,7 @@ var parseAddrsOnDarwinLittleEndianTests = []parseAddrsOnDarwinTest{
},
},
{
unix.RTA_DST | unix.RTA_GATEWAY | unix.RTA_NETMASK,
syscall.RTA_DST | syscall.RTA_GATEWAY | syscall.RTA_NETMASK,
parseKernelInetAddr,
[]byte{
0x10, 0x02, 0x00, 0x00, 0x64, 0x71, 0x00, 0x00,
Expand All @@ -70,7 +69,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
{
unix.RTA_DST | unix.RTA_GATEWAY | unix.RTA_NETMASK,
syscall.RTA_DST | syscall.RTA_GATEWAY | syscall.RTA_NETMASK,
parseKernelInetAddr,
[]byte{
0x1c, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Expand Down Expand Up @@ -99,7 +98,7 @@ var parseAddrsOnDarwinLittleEndianTests = []parseAddrsOnDarwinTest{
},
// golang/go#70528, the kernel can produce addresses of length 0
{
unix.RTA_DST | unix.RTA_GATEWAY | unix.RTA_NETMASK,
syscall.RTA_DST | syscall.RTA_GATEWAY | syscall.RTA_NETMASK,
parseKernelInetAddr,
[]byte{
0x00, 0x1e, 0x00, 0x00,
Expand All @@ -125,7 +124,7 @@ var parseAddrsOnDarwinLittleEndianTests = []parseAddrsOnDarwinTest{
},
// Additional case: golang/go/issues/70528#issuecomment-2498692877
{
unix.RTA_DST | unix.RTA_GATEWAY | unix.RTA_NETMASK,
syscall.RTA_DST | syscall.RTA_GATEWAY | syscall.RTA_NETMASK,
parseKernelInetAddr,
[]byte{
0x84, 0x00, 0x05, 0x04, 0x01, 0x00, 0x00, 0x00, 0x03, 0x08, 0x00, 0x01, 0x15, 0x00, 0x00, 0x00,
Expand Down
7 changes: 3 additions & 4 deletions route/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ package route

import (
"reflect"
"syscall"
"testing"

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

type parseAddrsTest struct {
Expand All @@ -22,7 +21,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 @@ -59,7 +58,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
15 changes: 13 additions & 2 deletions route/defs_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,19 @@ package route
import "C"

const (
sizeofIfMsghdr2Darwin15 = C.sizeof_struct_if_msghdr2
sizeofIfData64Darwin15 = C.sizeof_struct_if_data64
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

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: 56 additions & 0 deletions route/defs_dragonfly.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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.

//go:build ignore

package route

/*
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/route.h>
#include <netinet/in.h>
struct ifa_msghdr_dfly4 {
u_short ifam_msglen;
u_char ifam_version;
u_char ifam_type;
int ifam_addrs;
int ifam_flags;
u_short ifam_index;
int ifam_metric;
};
struct ifa_msghdr_dfly58 {
u_short ifam_msglen;
u_char ifam_version;
u_char ifam_type;
u_short ifam_index;
int ifam_flags;
int ifam_addrs;
int ifam_addrflags;
int ifam_metric;
};
*/
import "C"

const (
sizeofIfMsghdrDragonFlyBSD4 = C.sizeof_struct_if_msghdr
sizeofIfaMsghdrDragonFlyBSD4 = C.sizeof_struct_ifa_msghdr_dfly4
sizeofIfmaMsghdrDragonFlyBSD4 = C.sizeof_struct_ifma_msghdr
sizeofIfAnnouncemsghdrDragonFlyBSD4 = C.sizeof_struct_if_announcemsghdr

sizeofIfaMsghdrDragonFlyBSD58 = C.sizeof_struct_ifa_msghdr_dfly58

sizeofRtMsghdrDragonFlyBSD4 = C.sizeof_struct_rt_msghdr
sizeofRtMetricsDragonFlyBSD4 = C.sizeof_struct_rt_metrics

sizeofSockaddrStorage = C.sizeof_struct_sockaddr_storage
sizeofSockaddrInet = C.sizeof_struct_sockaddr_in
sizeofSockaddrInet6 = C.sizeof_struct_sockaddr_in6
)
12 changes: 12 additions & 0 deletions route/defs_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@ 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 @@ -233,6 +239,12 @@ 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: 32 additions & 0 deletions route/defs_netbsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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.

//go:build ignore

package route

/*
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/route.h>
#include <netinet/in.h>
*/
import "C"

const (
sizeofIfMsghdrNetBSD7 = C.sizeof_struct_if_msghdr
sizeofIfaMsghdrNetBSD7 = C.sizeof_struct_ifa_msghdr
sizeofIfAnnouncemsghdrNetBSD7 = C.sizeof_struct_if_announcemsghdr

sizeofRtMsghdrNetBSD7 = C.sizeof_struct_rt_msghdr
sizeofRtMetricsNetBSD7 = C.sizeof_struct_rt_metrics

sizeofSockaddrStorage = C.sizeof_struct_sockaddr_storage
sizeofSockaddrInet = C.sizeof_struct_sockaddr_in
sizeofSockaddrInet6 = C.sizeof_struct_sockaddr_in6
)
Loading

0 comments on commit 552d8ac

Please sign in to comment.