MCPcopy
hub / github.com/WireGuard/wireguard-go / CreateTUN

Function CreateTUN

tun/tun_linux.go:551–582  ·  view source on GitHub ↗

CreateTUN creates a Device with the provided name and MTU.

(name string, mtu int)

Source from the content-addressed store, hash-verified

549
550// CreateTUN creates a Device with the provided name and MTU.
551func CreateTUN(name string, mtu int) (Device, error) {
552 nfd, err := unix.Open(cloneDevicePath, unix.O_RDWR|unix.O_CLOEXEC, 0)
553 if err != nil {
554 if os.IsNotExist(err) {
555 return nil, fmt.Errorf("CreateTUN(%q) failed; %s does not exist", name, cloneDevicePath)
556 }
557 return nil, err
558 }
559
560 ifr, err := unix.NewIfreq(name)
561 if err != nil {
562 return nil, err
563 }
564 // IFF_VNET_HDR enables the "tun status hack" via routineHackListener()
565 // where a null write will return EINVAL indicating the TUN is up.
566 ifr.SetUint16(unix.IFF_TUN | unix.IFF_NO_PI | unix.IFF_VNET_HDR)
567 err = unix.IoctlIfreq(nfd, unix.TUNSETIFF, ifr)
568 if err != nil {
569 return nil, err
570 }
571
572 err = unix.SetNonblock(nfd, true)
573 if err != nil {
574 unix.Close(nfd)
575 return nil, err
576 }
577
578 // Note that the above -- open,ioctl,nonblock -- must happen prior to handing it to netpoll as below this line.
579
580 fd := os.NewFile(uintptr(nfd), cloneDevicePath)
581 return CreateTUNFromFile(fd, mtu)
582}
583
584// CreateTUNFromFile creates a Device from an os.File with the provided MTU.
585func CreateTUNFromFile(file *os.File, mtu int) (Device, error) {

Callers 2

mainFunction · 0.92
mainFunction · 0.92

Calls 3

CreateTUNFromFileFunction · 0.70
OpenMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected