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

     1  package libbox
     2  
     3  import (
     4  	"fmt"
     5  	"syscall"
     6  	"unsafe"
     7  
     8  	"golang.org/x/sys/unix"
     9  )
    10  
    11  const ifReqSize = unix.IFNAMSIZ + 64
    12  
    13  func getTunnelName(fd int32) (string, error) {
    14  	var ifr [ifReqSize]byte
    15  	var errno syscall.Errno
    16  	_, _, errno = unix.Syscall(
    17  		unix.SYS_IOCTL,
    18  		uintptr(fd),
    19  		uintptr(unix.TUNGETIFF),
    20  		uintptr(unsafe.Pointer(&ifr[0])),
    21  	)
    22  	if errno != 0 {
    23  		return "", fmt.Errorf("failed to get name of TUN device: %w", errno)
    24  	}
    25  	return unix.ByteSliceToString(ifr[:]), nil
    26  }