Files
tun2socks/component/dialer/bind_darwin.go
xjasonlyu 90d7d2dfe6 Refactor
2021-02-05 20:02:29 +08:00

25 lines
578 B
Go
Executable File

package dialer
import (
"net"
"syscall"
"golang.org/x/sys/unix"
)
func bindToInterface(network, address string, c syscall.RawConn) error {
ipStr, _, _ := net.SplitHostPort(address)
if ip := net.ParseIP(ipStr); ip != nil && !ip.IsGlobalUnicast() {
return nil
}
return c.Control(func(fd uintptr) {
switch network {
case "tcp4", "udp4":
unix.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_BOUND_IF, _boundInterface.Index)
case "tcp6", "udp6":
unix.SetsockoptInt(int(fd), syscall.IPPROTO_IPV6, syscall.IPV6_BOUND_IF, _boundInterface.Index)
}
})
}