github.com/clmul/water@v0.0.3-0.20221017135504-100d910a03ab/if.go (about)

     1  //go:build darwin || linux
     2  
     3  package water
     4  
     5  import (
     6  	"os"
     7  	_ "unsafe"
     8  )
     9  
    10  // Interface is a TUN/TAP interface.
    11  //
    12  // MultiQueue(Linux kernel > 3.8): With MultiQueue enabled, user should hold multiple
    13  // interfaces to send/receive packet in parallel.
    14  // Kernel document about MultiQueue: https://www.kernel.org/doc/Documentation/networking/tuntap.txt
    15  type Interface struct {
    16  	*os.File
    17  	fd   int
    18  	name string
    19  }
    20  
    21  // Name returns the interface name of ifce, e.g. tun0, tap1, tun0, etc..
    22  func (ifce *Interface) Name() string {
    23  	return ifce.name
    24  }
    25  
    26  func (ifce *Interface) Fd() int {
    27  	return ifce.fd
    28  }
    29  
    30  //go:linkname newFile os.newFile
    31  func newFile(fd uintptr, name string, kind int) *os.File
    32  
    33  func file(fd uintptr, name string) *os.File {
    34  	return newFile(fd, name, 1)
    35  }
    36  
    37  func New(c Config) (*Interface, error) {
    38  	fd, name, err := open(c)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  	ifce := &Interface{
    43  		File: file(uintptr(fd), name),
    44  		name: name,
    45  	}
    46  	return ifce, nil
    47  }