Skip to content

Commit

Permalink
lif: 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: I999eba5d089a1dfb341e27ebf3651ace0de26947
Reviewed-on: https://go-review.googlesource.com/c/net/+/421419
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
  • Loading branch information
ianlancetaylor authored and gopherbot committed Aug 5, 2022
1 parent 0bcc04d commit 7431dee
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 30 deletions.
13 changes: 6 additions & 7 deletions lif/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ package lif

import (
"errors"
"syscall"
"unsafe"

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

// An Addr represents an address associated with packet routing.
Expand All @@ -27,7 +26,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 }

// An Inet6Addr represents an internet address for IPv6.
type Inet6Addr struct {
Expand All @@ -37,7 +36,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 }

// Addrs returns a list of interface addresses.
//
Expand All @@ -64,7 +63,7 @@ func Addrs(af int, name string) ([]Addr, error) {
lifr.Name[i] = int8(ll.Name[i])
}
for _, ep := range eps {
ioc := int64(unix.SIOCGLIFADDR)
ioc := int64(syscall.SIOCGLIFADDR)
err := ioctl(ep.s, uintptr(ioc), unsafe.Pointer(&lifr))
if err != nil {
continue
Expand All @@ -75,11 +74,11 @@ func Addrs(af int, name string) ([]Addr, error) {
continue
}
switch sa.Family {
case unix.AF_INET:
case syscall.AF_INET:
a := &Inet4Addr{PrefixLen: l}
copy(a.IP[:], lifr.Lifru[4:8])
as = append(as, a)
case unix.AF_INET6:
case syscall.AF_INET6:
a := &Inet6Addr{PrefixLen: l, ZoneID: int(nativeEndian.Uint32(lifr.Lifru[24:28]))}
copy(a.IP[:], lifr.Lifru[8:24])
as = append(as, a)
Expand Down
11 changes: 5 additions & 6 deletions lif/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,19 @@ package lif

import (
"fmt"
"syscall"
"testing"

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

type addrFamily int

func (af addrFamily) String() string {
switch af {
case unix.AF_UNSPEC:
case syscall.AF_UNSPEC:
return "unspec"
case unix.AF_INET:
case syscall.AF_INET:
return "inet4"
case unix.AF_INET6:
case syscall.AF_INET6:
return "inet6"
default:
return fmt.Sprintf("%d", af)
Expand Down Expand Up @@ -83,7 +82,7 @@ type addrPack struct {
func addrPacks() ([]addrPack, error) {
var lastErr error
var aps []addrPack
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} {
as, err := Addrs(af, "")
if err != nil {
lastErr = err
Expand Down
8 changes: 3 additions & 5 deletions lif/lif.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ package lif

import (
"syscall"

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

type endpoint struct {
Expand All @@ -29,12 +27,12 @@ func (ep *endpoint) close() error {
func newEndpoints(af int) ([]endpoint, error) {
var lastErr error
var eps []endpoint
afs := []int{unix.AF_INET, unix.AF_INET6}
if af != unix.AF_UNSPEC {
afs := []int{syscall.AF_INET, syscall.AF_INET6}
if af != syscall.AF_UNSPEC {
afs = []int{af}
}
for _, af := range afs {
s, err := syscall.Socket(af, unix.SOCK_DGRAM, 0)
s, err := syscall.Socket(af, syscall.SOCK_DGRAM, 0)
if err != nil {
lastErr = err
continue
Expand Down
17 changes: 8 additions & 9 deletions lif/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
package lif

import (
"syscall"
"unsafe"

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

// A Link represents logical data link information.
Expand All @@ -34,22 +33,22 @@ func (ll *Link) fetch(s uintptr) {
for i := 0; i < len(ll.Name); i++ {
lifr.Name[i] = int8(ll.Name[i])
}
ioc := int64(unix.SIOCGLIFINDEX)
ioc := int64(syscall.SIOCGLIFINDEX)
if err := ioctl(s, uintptr(ioc), unsafe.Pointer(&lifr)); err == nil {
ll.Index = int(nativeEndian.Uint32(lifr.Lifru[:4]))
}
ioc = int64(unix.SIOCGLIFFLAGS)
ioc = int64(syscall.SIOCGLIFFLAGS)
if err := ioctl(s, uintptr(ioc), unsafe.Pointer(&lifr)); err == nil {
ll.Flags = int(nativeEndian.Uint64(lifr.Lifru[:8]))
}
ioc = int64(unix.SIOCGLIFMTU)
ioc = int64(syscall.SIOCGLIFMTU)
if err := ioctl(s, uintptr(ioc), unsafe.Pointer(&lifr)); err == nil {
ll.MTU = int(nativeEndian.Uint32(lifr.Lifru[:4]))
}
switch ll.Type {
case unix.IFT_IPV4, unix.IFT_IPV6, unix.IFT_6TO4:
case syscall.IFT_IPV4, syscall.IFT_IPV6, syscall.IFT_6TO4:
default:
ioc = int64(unix.SIOCGLIFHWADDR)
ioc = int64(syscall.SIOCGLIFHWADDR)
if err := ioctl(s, uintptr(ioc), unsafe.Pointer(&lifr)); err == nil {
ll.Addr, _ = parseLinkAddr(lifr.Lifru[4:])
}
Expand Down Expand Up @@ -79,7 +78,7 @@ func links(eps []endpoint, name string) ([]Link, error) {
lifc := lifconf{Flags: sysLIFC_NOXMIT | sysLIFC_TEMPORARY | sysLIFC_ALLZONES | sysLIFC_UNDER_IPMP}
for _, ep := range eps {
lifn.Family = uint16(ep.af)
ioc := int64(unix.SIOCGLIFNUM)
ioc := int64(syscall.SIOCGLIFNUM)
if err := ioctl(ep.s, uintptr(ioc), unsafe.Pointer(&lifn)); err != nil {
continue
}
Expand All @@ -94,7 +93,7 @@ func links(eps []endpoint, name string) ([]Link, error) {
} else {
nativeEndian.PutUint32(lifc.Lifcu[:], uint32(uintptr(unsafe.Pointer(&b[0]))))
}
ioc = int64(unix.SIOCGLIFCONF)
ioc = int64(syscall.SIOCGLIFCONF)
if err := ioctl(ep.s, uintptr(ioc), unsafe.Pointer(&lifc)); err != nil {
continue
}
Expand Down
5 changes: 2 additions & 3 deletions lif/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ package lif

import (
"fmt"
"syscall"
"testing"

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

func (ll *Link) String() string {
Expand All @@ -26,7 +25,7 @@ type linkPack struct {
func linkPacks() ([]linkPack, error) {
var lastErr error
var lps []linkPack
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} {
lls, err := Links(af, "")
if err != nil {
lastErr = err
Expand Down

0 comments on commit 7431dee

Please sign in to comment.