use unix getMTU

This commit is contained in:
xjasonlyu
2020-11-08 14:12:00 +08:00
parent b858c8758a
commit b435fbc51a
2 changed files with 11 additions and 24 deletions

2
go.mod
View File

@@ -16,7 +16,7 @@ require (
github.com/xjasonlyu/clash v0.15.1-0.20201105074459-aa45c8b56cf6 github.com/xjasonlyu/clash v0.15.1-0.20201105074459-aa45c8b56cf6
go.uber.org/atomic v1.7.0 go.uber.org/atomic v1.7.0
golang.org/x/net v0.0.0-20201031054903-ff519b6c9102 // indirect 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 golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
gvisor.dev/gvisor v0.0.0-20201107072535-9e848922ed33 gvisor.dev/gvisor v0.0.0-20201107072535-9e848922ed33

View File

@@ -3,10 +3,9 @@ package tun
import ( import (
"fmt" "fmt"
"io" "io"
"syscall"
"unsafe"
"github.com/songgao/water" "github.com/songgao/water"
"golang.org/x/sys/unix"
"gvisor.dev/gvisor/pkg/tcpip/stack" "gvisor.dev/gvisor/pkg/tcpip/stack"
"github.com/xjasonlyu/tun2socks/pkg/link/rwc" "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) { func getMTU(name string) (uint32, error) {
// open datagram socket fd, err := unix.Socket(
fd, err := syscall.Socket( unix.AF_INET,
syscall.AF_INET, unix.SOCK_DGRAM,
syscall.SOCK_DGRAM,
0, 0,
) )
if err != nil { if err != nil {
return 0, err return 0, err
} }
defer syscall.Close(fd) defer unix.Close(fd)
// do ioctl call ifr, err := unix.IoctlGetIfreqMTU(fd, name)
var ifr struct { if err != nil {
name [16]byte return 0, fmt.Errorf("get MTU on %s: %w", name, err)
mtu uint32
}
copy(ifr.name[:], name)
_, _, 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())
} }
return ifr.mtu, nil return uint32(ifr.MTU), nil
} }