github.com/sagernet/sing@v0.4.0-beta.19.0.20240518125136-f67a0988a636/common/control/bind_darwin.go (about)

     1  package control
     2  
     3  import (
     4  	"os"
     5  	"syscall"
     6  
     7  	"golang.org/x/sys/unix"
     8  )
     9  
    10  func bindToInterface(conn syscall.RawConn, network string, address string, finder InterfaceFinder, interfaceName string, interfaceIndex int, preferInterfaceName bool) error {
    11  	return Raw(conn, func(fd uintptr) error {
    12  		var err error
    13  		if interfaceIndex == -1 {
    14  			if finder == nil {
    15  				return os.ErrInvalid
    16  			}
    17  			interfaceIndex, err = finder.InterfaceIndexByName(interfaceName)
    18  			if err != nil {
    19  				return err
    20  			}
    21  		}
    22  		switch network {
    23  		case "tcp6", "udp6":
    24  			return unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_BOUND_IF, interfaceIndex)
    25  		default:
    26  			return unix.SetsockoptInt(int(fd), unix.IPPROTO_IP, unix.IP_BOUND_IF, interfaceIndex)
    27  		}
    28  	})
    29  }