github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/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  }