Skip to content

Commit 4be1253

Browse files
ianlancetaylorgopherbot
authored andcommitted
route: change from syscall to x/sys/unix
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]>
1 parent bc37675 commit 4be1253

33 files changed

+243
-489
lines changed

route/address.go

+23-22
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ package route
88

99
import (
1010
"runtime"
11-
"syscall"
11+
12+
"golang.org/x/sys/unix"
1213
)
1314

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

2728
// Family implements the Family method of Addr interface.
28-
func (a *LinkAddr) Family() int { return syscall.AF_LINK }
29+
func (a *LinkAddr) Family() int { return unix.AF_LINK }
2930

3031
func (a *LinkAddr) lenAndSpace() (int, int) {
3132
l := 8 + len(a.Name) + len(a.Addr)
@@ -42,7 +43,7 @@ func (a *LinkAddr) marshal(b []byte) (int, error) {
4243
return 0, errInvalidAddr
4344
}
4445
b[0] = byte(l)
45-
b[1] = syscall.AF_LINK
46+
b[1] = unix.AF_LINK
4647
if a.Index > 0 {
4748
nativeEndian.PutUint16(b[2:4], uint16(a.Index))
4849
}
@@ -64,7 +65,7 @@ func parseLinkAddr(b []byte) (Addr, error) {
6465
if len(b) < 8 {
6566
return nil, errInvalidAddr
6667
}
67-
_, a, err := parseKernelLinkAddr(syscall.AF_LINK, b[4:])
68+
_, a, err := parseKernelLinkAddr(unix.AF_LINK, b[4:])
6869
if err != nil {
6970
return nil, err
7071
}
@@ -124,10 +125,10 @@ type Inet4Addr struct {
124125
}
125126

126127
// Family implements the Family method of Addr interface.
127-
func (a *Inet4Addr) Family() int { return syscall.AF_INET }
128+
func (a *Inet4Addr) Family() int { return unix.AF_INET }
128129

129130
func (a *Inet4Addr) lenAndSpace() (int, int) {
130-
return sizeofSockaddrInet, roundup(sizeofSockaddrInet)
131+
return unix.SizeofSockaddrInet4, roundup(unix.SizeofSockaddrInet4)
131132
}
132133

133134
func (a *Inet4Addr) marshal(b []byte) (int, error) {
@@ -136,7 +137,7 @@ func (a *Inet4Addr) marshal(b []byte) (int, error) {
136137
return 0, errShortBuffer
137138
}
138139
b[0] = byte(l)
139-
b[1] = syscall.AF_INET
140+
b[1] = unix.AF_INET
140141
copy(b[4:8], a.IP[:])
141142
return ll, nil
142143
}
@@ -148,10 +149,10 @@ type Inet6Addr struct {
148149
}
149150

150151
// Family implements the Family method of Addr interface.
151-
func (a *Inet6Addr) Family() int { return syscall.AF_INET6 }
152+
func (a *Inet6Addr) Family() int { return unix.AF_INET6 }
152153

153154
func (a *Inet6Addr) lenAndSpace() (int, int) {
154-
return sizeofSockaddrInet6, roundup(sizeofSockaddrInet6)
155+
return unix.SizeofSockaddrInet6, roundup(unix.SizeofSockaddrInet6)
155156
}
156157

157158
func (a *Inet6Addr) marshal(b []byte) (int, error) {
@@ -160,7 +161,7 @@ func (a *Inet6Addr) marshal(b []byte) (int, error) {
160161
return 0, errShortBuffer
161162
}
162163
b[0] = byte(l)
163-
b[1] = syscall.AF_INET6
164+
b[1] = unix.AF_INET6
164165
copy(b[8:24], a.IP[:])
165166
if a.ZoneID > 0 {
166167
nativeEndian.PutUint32(b[24:28], uint32(a.ZoneID))
@@ -175,7 +176,7 @@ func parseInetAddr(af int, b []byte) (Addr, error) {
175176
off6 = 8 // offset of in6_addr
176177
)
177178
switch af {
178-
case syscall.AF_INET:
179+
case unix.AF_INET:
179180
if len(b) < (off4+1) || len(b) < int(b[0]) || b[0] == 0 {
180181
return nil, errInvalidAddr
181182
}
@@ -187,7 +188,7 @@ func parseInetAddr(af int, b []byte) (Addr, error) {
187188
}
188189
copy(a.IP[:], b[off4:n])
189190
return a, nil
190-
case syscall.AF_INET6:
191+
case unix.AF_INET6:
191192
if len(b) < (off6+1) || len(b) < int(b[0]) || b[0] == 0 {
192193
return nil, errInvalidAddr
193194
}
@@ -197,7 +198,7 @@ func parseInetAddr(af int, b []byte) (Addr, error) {
197198
n = sockAddrLen
198199
}
199200
a := &Inet6Addr{}
200-
if sockAddrLen == sizeofSockaddrInet6 {
201+
if sockAddrLen == unix.SizeofSockaddrInet6 {
201202
a.ZoneID = int(nativeEndian.Uint32(b[24:28]))
202203
}
203204
copy(a.IP[:], b[off6:n])
@@ -260,19 +261,19 @@ func parseKernelInetAddr(af int, b []byte) (int, Addr, error) {
260261
off6 = 8 // offset of in6_addr
261262
)
262263
switch {
263-
case b[0] == sizeofSockaddrInet6:
264+
case b[0] == unix.SizeofSockaddrInet6:
264265
a := &Inet6Addr{}
265266
copy(a.IP[:], b[off6:off6+16])
266267
return int(b[0]), a, nil
267-
case af == syscall.AF_INET6:
268+
case af == unix.AF_INET6:
268269
a := &Inet6Addr{}
269270
if l-1 < off6 {
270271
copy(a.IP[:], b[1:l])
271272
} else {
272273
copy(a.IP[:], b[l-off6:l])
273274
}
274275
return int(b[0]), a, nil
275-
case b[0] == sizeofSockaddrInet:
276+
case b[0] == unix.SizeofSockaddrInet4:
276277
a := &Inet4Addr{}
277278
copy(a.IP[:], b[off4:off4+4])
278279
return int(b[0]), a, nil
@@ -384,15 +385,15 @@ func marshalAddrs(b []byte, as []Addr) (uint, error) {
384385
}
385386

386387
func parseAddrs(attrs uint, fn func(int, []byte) (int, Addr, error), b []byte) ([]Addr, error) {
387-
var as [syscall.RTAX_MAX]Addr
388-
af := int(syscall.AF_UNSPEC)
389-
for i := uint(0); i < syscall.RTAX_MAX && len(b) >= roundup(0); i++ {
388+
var as [unix.RTAX_MAX]Addr
389+
af := int(unix.AF_UNSPEC)
390+
for i := uint(0); i < unix.RTAX_MAX && len(b) >= roundup(0); i++ {
390391
if attrs&(1<<i) == 0 {
391392
continue
392393
}
393-
if i <= syscall.RTAX_BRD {
394+
if i <= unix.RTAX_BRD {
394395
switch b[1] {
395-
case syscall.AF_LINK:
396+
case unix.AF_LINK:
396397
a, err := parseLinkAddr(b)
397398
if err != nil {
398399
return nil, err
@@ -403,7 +404,7 @@ func parseAddrs(attrs uint, fn func(int, []byte) (int, Addr, error), b []byte) (
403404
return nil, errMessageTooShort
404405
}
405406
b = b[l:]
406-
case syscall.AF_INET, syscall.AF_INET6:
407+
case unix.AF_INET, unix.AF_INET6:
407408
// #70528: if the sockaddrlen is 0, no address to parse inside,
408409
// skip over the record.
409410
if b[0] > 0 {

route/address_darwin_test.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ package route
66

77
import (
88
"reflect"
9-
"syscall"
109
"testing"
10+
11+
"golang.org/x/sys/unix"
1112
)
1213

1314
type parseAddrsOnDarwinTest struct {
@@ -19,7 +20,7 @@ type parseAddrsOnDarwinTest struct {
1920

2021
var parseAddrsOnDarwinLittleEndianTests = []parseAddrsOnDarwinTest{
2122
{
22-
syscall.RTA_DST | syscall.RTA_GATEWAY | syscall.RTA_NETMASK,
23+
unix.RTA_DST | unix.RTA_GATEWAY | unix.RTA_NETMASK,
2324
parseKernelInetAddr,
2425
[]byte{
2526
0x10, 0x2, 0x0, 0x0, 0xc0, 0xa8, 0x56, 0x0,
@@ -43,7 +44,7 @@ var parseAddrsOnDarwinLittleEndianTests = []parseAddrsOnDarwinTest{
4344
},
4445
},
4546
{
46-
syscall.RTA_DST | syscall.RTA_GATEWAY | syscall.RTA_NETMASK,
47+
unix.RTA_DST | unix.RTA_GATEWAY | unix.RTA_NETMASK,
4748
parseKernelInetAddr,
4849
[]byte{
4950
0x10, 0x02, 0x00, 0x00, 0x64, 0x71, 0x00, 0x00,
@@ -69,7 +70,7 @@ var parseAddrsOnDarwinLittleEndianTests = []parseAddrsOnDarwinTest{
6970
// route -n add -inet6 fd84:1b4e:6281:: -prefixlen 48 fe80::f22f:4bff:fe09:3bff%utun4319
7071
// gw fe80:0000:0000:0000:f22f:4bff:fe09:3bff
7172
{
72-
syscall.RTA_DST | syscall.RTA_GATEWAY | syscall.RTA_NETMASK,
73+
unix.RTA_DST | unix.RTA_GATEWAY | unix.RTA_NETMASK,
7374
parseKernelInetAddr,
7475
[]byte{
7576
0x1c, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -98,7 +99,7 @@ var parseAddrsOnDarwinLittleEndianTests = []parseAddrsOnDarwinTest{
9899
},
99100
// golang/go#70528, the kernel can produce addresses of length 0
100101
{
101-
syscall.RTA_DST | syscall.RTA_GATEWAY | syscall.RTA_NETMASK,
102+
unix.RTA_DST | unix.RTA_GATEWAY | unix.RTA_NETMASK,
102103
parseKernelInetAddr,
103104
[]byte{
104105
0x00, 0x1e, 0x00, 0x00,
@@ -124,7 +125,7 @@ var parseAddrsOnDarwinLittleEndianTests = []parseAddrsOnDarwinTest{
124125
},
125126
// Additional case: golang/go/issues/70528#issuecomment-2498692877
126127
{
127-
syscall.RTA_DST | syscall.RTA_GATEWAY | syscall.RTA_NETMASK,
128+
unix.RTA_DST | unix.RTA_GATEWAY | unix.RTA_NETMASK,
128129
parseKernelInetAddr,
129130
[]byte{
130131
0x84, 0x00, 0x05, 0x04, 0x01, 0x00, 0x00, 0x00, 0x03, 0x08, 0x00, 0x01, 0x15, 0x00, 0x00, 0x00,

route/address_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ package route
88

99
import (
1010
"reflect"
11-
"syscall"
1211
"testing"
12+
13+
"golang.org/x/sys/unix"
1314
)
1415

1516
type parseAddrsTest struct {
@@ -21,7 +22,7 @@ type parseAddrsTest struct {
2122

2223
var parseAddrsLittleEndianTests = []parseAddrsTest{
2324
{
24-
syscall.RTA_DST | syscall.RTA_GATEWAY | syscall.RTA_NETMASK | syscall.RTA_BRD,
25+
unix.RTA_DST | unix.RTA_GATEWAY | unix.RTA_NETMASK | unix.RTA_BRD,
2526
parseKernelInetAddr,
2627
[]byte{
2728
0x38, 0x12, 0x0, 0x0, 0xff, 0xff, 0xff, 0x0,
@@ -58,7 +59,7 @@ var parseAddrsLittleEndianTests = []parseAddrsTest{
5859
},
5960
},
6061
{
61-
syscall.RTA_NETMASK | syscall.RTA_IFP | syscall.RTA_IFA,
62+
unix.RTA_NETMASK | unix.RTA_IFP | unix.RTA_IFA,
6263
parseKernelInetAddr,
6364
[]byte{
6465
0x7, 0x0, 0x0, 0x0, 0xff, 0xff, 0xff, 0x0,

route/defs_darwin.go

+2-13
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,8 @@ package route
1919
import "C"
2020

2121
const (
22-
sizeofIfMsghdrDarwin15 = C.sizeof_struct_if_msghdr
23-
sizeofIfaMsghdrDarwin15 = C.sizeof_struct_ifa_msghdr
24-
sizeofIfmaMsghdrDarwin15 = C.sizeof_struct_ifma_msghdr
25-
sizeofIfMsghdr2Darwin15 = C.sizeof_struct_if_msghdr2
26-
sizeofIfmaMsghdr2Darwin15 = C.sizeof_struct_ifma_msghdr2
27-
sizeofIfDataDarwin15 = C.sizeof_struct_if_data
28-
sizeofIfData64Darwin15 = C.sizeof_struct_if_data64
22+
sizeofIfMsghdr2Darwin15 = C.sizeof_struct_if_msghdr2
23+
sizeofIfData64Darwin15 = C.sizeof_struct_if_data64
2924

30-
sizeofRtMsghdrDarwin15 = C.sizeof_struct_rt_msghdr
3125
sizeofRtMsghdr2Darwin15 = C.sizeof_struct_rt_msghdr2
32-
sizeofRtMetricsDarwin15 = C.sizeof_struct_rt_metrics
33-
34-
sizeofSockaddrStorage = C.sizeof_struct_sockaddr_storage
35-
sizeofSockaddrInet = C.sizeof_struct_sockaddr_in
36-
sizeofSockaddrInet6 = C.sizeof_struct_sockaddr_in6
3726
)

route/defs_dragonfly.go

-56
This file was deleted.

route/defs_freebsd.go

-12
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,6 @@ struct if_msghdr_freebsd11 {
218218
import "C"
219219

220220
const (
221-
sizeofIfMsghdrlFreeBSD10 = C.sizeof_struct_if_msghdrl
222-
sizeofIfaMsghdrFreeBSD10 = C.sizeof_struct_ifa_msghdr
223-
sizeofIfaMsghdrlFreeBSD10 = C.sizeof_struct_ifa_msghdrl
224-
sizeofIfmaMsghdrFreeBSD10 = C.sizeof_struct_ifma_msghdr
225-
sizeofIfAnnouncemsghdrFreeBSD10 = C.sizeof_struct_if_announcemsghdr
226-
227221
sizeofRtMsghdrFreeBSD10 = C.sizeof_struct_rt_msghdr
228222
sizeofRtMetricsFreeBSD10 = C.sizeof_struct_rt_metrics
229223

@@ -239,12 +233,6 @@ const (
239233
sizeofIfDataFreeBSD10 = C.sizeof_struct_if_data_freebsd10
240234
sizeofIfDataFreeBSD11 = C.sizeof_struct_if_data_freebsd11
241235

242-
sizeofIfMsghdrlFreeBSD10Emu = C.sizeof_struct_if_msghdrl
243-
sizeofIfaMsghdrFreeBSD10Emu = C.sizeof_struct_ifa_msghdr
244-
sizeofIfaMsghdrlFreeBSD10Emu = C.sizeof_struct_ifa_msghdrl
245-
sizeofIfmaMsghdrFreeBSD10Emu = C.sizeof_struct_ifma_msghdr
246-
sizeofIfAnnouncemsghdrFreeBSD10Emu = C.sizeof_struct_if_announcemsghdr
247-
248236
sizeofRtMsghdrFreeBSD10Emu = C.sizeof_struct_rt_msghdr
249237
sizeofRtMetricsFreeBSD10Emu = C.sizeof_struct_rt_metrics
250238

route/defs_netbsd.go

-32
This file was deleted.

0 commit comments

Comments
 (0)