github.com/sagernet/sing-box@v1.9.0-rc.20/experimental/libbox/tun_darwin.go (about)

     1  package libbox
     2  
     3  import (
     4  	"golang.org/x/sys/unix"
     5  )
     6  
     7  // kanged from wireauard-apple
     8  
     9  const utunControlName = "com.apple.net.utun_control"
    10  
    11  func GetTunnelFileDescriptor() int32 {
    12  	ctlInfo := &unix.CtlInfo{}
    13  	copy(ctlInfo.Name[:], utunControlName)
    14  	for fd := 0; fd < 1024; fd++ {
    15  		addr, err := unix.Getpeername(fd)
    16  		if err != nil {
    17  			continue
    18  		}
    19  		addrCTL, loaded := addr.(*unix.SockaddrCtl)
    20  		if !loaded {
    21  			continue
    22  		}
    23  		if ctlInfo.Id == 0 {
    24  			err = unix.IoctlCtlInfo(fd, ctlInfo)
    25  			if err != nil {
    26  				continue
    27  			}
    28  		}
    29  		if addrCTL.ID == ctlInfo.Id {
    30  			return int32(fd)
    31  		}
    32  	}
    33  	return -1
    34  }