github.com/Cloud-Foundations/Dominator@v0.3.4/lib/net/createTapDevice_linux.go (about)

     1  package net
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  	"syscall"
     7  	"unsafe"
     8  
     9  	"github.com/Cloud-Foundations/Dominator/lib/wsyscall"
    10  )
    11  
    12  const (
    13  	cIFF_TUN         = 0x0001
    14  	cIFF_TAP         = 0x0002
    15  	cIFF_NO_PI       = 0x1000
    16  	cIFF_MULTI_QUEUE = 0x0100
    17  )
    18  
    19  type ifReq struct {
    20  	Name  [0x10]byte
    21  	Flags uint16
    22  	pad   [0x28 - 0x10 - 2]byte
    23  }
    24  
    25  func createTapDevice() (*os.File, string, error) {
    26  	file, err := os.OpenFile("/dev/net/tun", os.O_RDWR, 0)
    27  	if err != nil {
    28  		return nil, "", err
    29  	}
    30  	req := ifReq{Flags: cIFF_TAP | cIFF_NO_PI}
    31  	err = wsyscall.Ioctl(int(file.Fd()), syscall.TUNSETIFF,
    32  		uintptr(unsafe.Pointer(&req)))
    33  	if err != nil {
    34  		file.Close()
    35  		return nil, "", err
    36  	}
    37  	return file, strings.Trim(string(req.Name[:]), "\x00"), nil
    38  }