diff --git a/go.mod b/go.mod index 191fa7b..c00adfb 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,7 @@ require ( github.com/xjasonlyu/clash v0.15.1-0.20201105074459-aa45c8b56cf6 go.uber.org/atomic v1.7.0 golang.org/x/net v0.0.0-20201031054903-ff519b6c9102 // indirect - golang.org/x/sys v0.0.0-20201107080550-4d91cf3a1aaf // indirect + golang.org/x/sys v0.0.0-20201107080550-4d91cf3a1aaf golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect gvisor.dev/gvisor v0.0.0-20201107072535-9e848922ed33 diff --git a/internal/dev/tun/tun_darwin.go b/internal/dev/tun/tun_darwin.go index a902ed3..1ec47da 100644 --- a/internal/dev/tun/tun_darwin.go +++ b/internal/dev/tun/tun_darwin.go @@ -3,10 +3,9 @@ package tun import ( "fmt" "io" - "syscall" - "unsafe" "github.com/songgao/water" + "golang.org/x/sys/unix" "gvisor.dev/gvisor/pkg/tcpip/stack" "github.com/xjasonlyu/tun2socks/pkg/link/rwc" @@ -37,34 +36,22 @@ func Open(name string) (ep stack.LinkEndpoint, c io.Closer, err error) { } func getMTU(name string) (uint32, error) { - // open datagram socket - fd, err := syscall.Socket( - syscall.AF_INET, - syscall.SOCK_DGRAM, + fd, err := unix.Socket( + unix.AF_INET, + unix.SOCK_DGRAM, 0, ) + if err != nil { return 0, err } - defer syscall.Close(fd) - - // do ioctl call - var ifr struct { - name [16]byte - mtu uint32 - } - copy(ifr.name[:], name) + defer unix.Close(fd) - _, _, errno := syscall.Syscall( - syscall.SYS_IOCTL, - uintptr(fd), - uintptr(syscall.SIOCGIFMTU), - uintptr(unsafe.Pointer(&ifr)), - ) - if errno != 0 { - return 0, fmt.Errorf("get MTU on %s: %s", name, errno.Error()) + ifr, err := unix.IoctlGetIfreqMTU(fd, name) + if err != nil { + return 0, fmt.Errorf("get MTU on %s: %w", name, err) } - return ifr.mtu, nil + return uint32(ifr.MTU), nil }