Close() error // stops the device and closes the event channel
}
-func (tun *nativeTun) operateOnFd(fn func(fd uintptr)) {
+func (tun *NativeTun) operateOnFd(fn func(fd uintptr)) {
sysconn, err := tun.tunFile.SyscallConn()
if err != nil {
tun.errors <- fmt.Errorf("unable to find sysconn for tunfile: %s", err.Error())
scReserved [5]uint32
}
-type nativeTun struct {
+type NativeTun struct {
name string
tunFile *os.File
events chan TUNEvent
var sockaddrCtlSize uintptr = 32
-func (tun *nativeTun) routineRouteListener(tunIfindex int) {
+func (tun *NativeTun) routineRouteListener(tunIfindex int) {
var (
statusUp bool
statusMTU int
if err == nil && name == "utun" {
fname := os.Getenv("WG_TUN_NAME_FILE")
if fname != "" {
- ioutil.WriteFile(fname, []byte(tun.(*nativeTun).name+"\n"), 0400)
+ ioutil.WriteFile(fname, []byte(tun.(*NativeTun).name+"\n"), 0400)
}
}
}
func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
- tun := &nativeTun{
+ tun := &NativeTun{
tunFile: file,
events: make(chan TUNEvent, 10),
errors: make(chan error, 1),
return tun, nil
}
-func (tun *nativeTun) Name() (string, error) {
+func (tun *NativeTun) Name() (string, error) {
var ifName struct {
name [16]byte
}
return tun.name, nil
}
-func (tun *nativeTun) File() *os.File {
+func (tun *NativeTun) File() *os.File {
return tun.tunFile
}
-func (tun *nativeTun) Events() chan TUNEvent {
+func (tun *NativeTun) Events() chan TUNEvent {
return tun.events
}
-func (tun *nativeTun) Read(buff []byte, offset int) (int, error) {
+func (tun *NativeTun) Read(buff []byte, offset int) (int, error) {
select {
case err := <-tun.errors:
return 0, err
}
}
-func (tun *nativeTun) Write(buff []byte, offset int) (int, error) {
+func (tun *NativeTun) Write(buff []byte, offset int) (int, error) {
// reserve space for header
return tun.tunFile.Write(buff)
}
-func (tun *nativeTun) Close() error {
+func (tun *NativeTun) Close() error {
var err2 error
err1 := tun.tunFile.Close()
if tun.routeSocket != -1 {
return err2
}
-func (tun *nativeTun) setMTU(n int) error {
+func (tun *NativeTun) setMTU(n int) error {
// open datagram socket
return nil
}
-func (tun *nativeTun) MTU() (int, error) {
+func (tun *NativeTun) MTU() (int, error) {
// open datagram socket
Ascii [_IFSTATMAX]byte
}
-type nativeTun struct {
+type NativeTun struct {
name string
tunFile *os.File
events chan TUNEvent
routeSocket int
}
-func (tun *nativeTun) routineRouteListener(tunIfindex int) {
+func (tun *NativeTun) routineRouteListener(tunIfindex int) {
var (
statusUp bool
statusMTU int
return nil, err
}
- tun := nativeTun{tunFile: tunFile}
+ tun := NativeTun{tunFile: tunFile}
var assignedName string
tun.operateOnFd(func(fd uintptr) {
assignedName, err = tunName(fd)
func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
- tun := &nativeTun{
+ tun := &NativeTun{
tunFile: file,
events: make(chan TUNEvent, 10),
errors: make(chan error, 1),
return tun, nil
}
-func (tun *nativeTun) Name() (string, error) {
+func (tun *NativeTun) Name() (string, error) {
var name string
var err error
tun.operateOnFd(func(fd uintptr) {
return name, nil
}
-func (tun *nativeTun) File() *os.File {
+func (tun *NativeTun) File() *os.File {
return tun.tunFile
}
-func (tun *nativeTun) Events() chan TUNEvent {
+func (tun *NativeTun) Events() chan TUNEvent {
return tun.events
}
-func (tun *nativeTun) Read(buff []byte, offset int) (int, error) {
+func (tun *NativeTun) Read(buff []byte, offset int) (int, error) {
select {
case err := <-tun.errors:
return 0, err
}
}
-func (tun *nativeTun) Write(buff []byte, offset int) (int, error) {
+func (tun *NativeTun) Write(buff []byte, offset int) (int, error) {
// reserve space for header
return tun.tunFile.Write(buff)
}
-func (tun *nativeTun) Close() error {
+func (tun *NativeTun) Close() error {
var err3 error
err1 := tun.tunFile.Close()
err2 := tunDestroy(tun.name)
return err3
}
-func (tun *nativeTun) setMTU(n int) error {
+func (tun *NativeTun) setMTU(n int) error {
// open datagram socket
var fd int
return nil
}
-func (tun *nativeTun) MTU() (int, error) {
+func (tun *NativeTun) MTU() (int, error) {
// open datagram socket
fd, err := unix.Socket(
ifReqSize = unix.IFNAMSIZ + 64
)
-type nativeTun struct {
+type NativeTun struct {
tunFile *os.File
fd uintptr
fdCancel *rwcancel.RWCancel
statusListenersShutdown chan struct{}
}
-func (tun *nativeTun) File() *os.File {
+func (tun *NativeTun) File() *os.File {
return tun.tunFile
}
-func (tun *nativeTun) routineHackListener() {
+func (tun *NativeTun) routineHackListener() {
defer tun.hackListenerClosed.Unlock()
/* This is needed for the detection to work across network namespaces
* If you are reading this and know a better method, please get in touch.
return sock, nil
}
-func (tun *nativeTun) routineNetlinkListener() {
+func (tun *NativeTun) routineNetlinkListener() {
defer func() {
unix.Close(tun.netlinkSock)
tun.hackListenerClosed.Lock()
}
}
-func (tun *nativeTun) isUp() (bool, error) {
+func (tun *NativeTun) isUp() (bool, error) {
inter, err := net.InterfaceByName(tun.name)
return inter.Flags&net.FlagUp != 0, err
}
return *(*int32)(unsafe.Pointer(&ifr[unix.IFNAMSIZ])), nil
}
-func (tun *nativeTun) setMTU(n int) error {
+func (tun *NativeTun) setMTU(n int) error {
// open datagram socket
fd, err := unix.Socket(
unix.AF_INET,
return nil
}
-func (tun *nativeTun) MTU() (int, error) {
+func (tun *NativeTun) MTU() (int, error) {
// open datagram socket
fd, err := unix.Socket(
unix.AF_INET,
return int(*(*int32)(unsafe.Pointer(&ifr[unix.IFNAMSIZ]))), nil
}
-func (tun *nativeTun) Name() (string, error) {
+func (tun *NativeTun) Name() (string, error) {
var ifr [ifReqSize]byte
_, _, errno := unix.Syscall(
unix.SYS_IOCTL,
return tun.name, nil
}
-func (tun *nativeTun) Write(buff []byte, offset int) (int, error) {
+func (tun *NativeTun) Write(buff []byte, offset int) (int, error) {
if tun.nopi {
buff = buff[offset:]
return tun.tunFile.Write(buff)
}
-func (tun *nativeTun) doRead(buff []byte, offset int) (int, error) {
+func (tun *NativeTun) doRead(buff []byte, offset int) (int, error) {
select {
case err := <-tun.errors:
return 0, err
}
}
-func (tun *nativeTun) Read(buff []byte, offset int) (int, error) {
+func (tun *NativeTun) Read(buff []byte, offset int) (int, error) {
for {
n, err := tun.doRead(buff, offset)
if err == nil || !rwcancel.RetryAfterError(err) {
}
}
-func (tun *nativeTun) Events() chan TUNEvent {
+func (tun *NativeTun) Events() chan TUNEvent {
return tun.events
}
-func (tun *nativeTun) Close() error {
+func (tun *NativeTun) Close() error {
var err1 error
if tun.statusListenersShutdown != nil {
close(tun.statusListenersShutdown)
}
func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
- tun := &nativeTun{
+ tun := &NativeTun{
tunFile: file,
fd: file.Fd(),
events: make(chan TUNEvent, 5),
const _TUNSIFMODE = 0x8004745d
-type nativeTun struct {
+type NativeTun struct {
name string
tunFile *os.File
events chan TUNEvent
routeSocket int
}
-func (tun *nativeTun) routineRouteListener(tunIfindex int) {
+func (tun *NativeTun) routineRouteListener(tunIfindex int) {
var (
statusUp bool
statusMTU int
if err == nil && name == "tun" {
fname := os.Getenv("WG_TUN_NAME_FILE")
if fname != "" {
- ioutil.WriteFile(fname, []byte(tun.(*nativeTun).name+"\n"), 0400)
+ ioutil.WriteFile(fname, []byte(tun.(*NativeTun).name+"\n"), 0400)
}
}
func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
- tun := &nativeTun{
+ tun := &NativeTun{
tunFile: file,
events: make(chan TUNEvent, 10),
errors: make(chan error, 1),
return tun, nil
}
-func (tun *nativeTun) Name() (string, error) {
+func (tun *NativeTun) Name() (string, error) {
gostat, err := tun.tunFile.Stat()
if err != nil {
tun.name = ""
return tun.name, nil
}
-func (tun *nativeTun) File() *os.File {
+func (tun *NativeTun) File() *os.File {
return tun.tunFile
}
-func (tun *nativeTun) Events() chan TUNEvent {
+func (tun *NativeTun) Events() chan TUNEvent {
return tun.events
}
-func (tun *nativeTun) Read(buff []byte, offset int) (int, error) {
+func (tun *NativeTun) Read(buff []byte, offset int) (int, error) {
select {
case err := <-tun.errors:
return 0, err
}
}
-func (tun *nativeTun) Write(buff []byte, offset int) (int, error) {
+func (tun *NativeTun) Write(buff []byte, offset int) (int, error) {
// reserve space for header
return tun.tunFile.Write(buff)
}
-func (tun *nativeTun) Close() error {
+func (tun *NativeTun) Close() error {
var err2 error
err1 := tun.tunFile.Close()
if tun.routeSocket != -1 {
return err2
}
-func (tun *nativeTun) setMTU(n int) error {
+func (tun *NativeTun) setMTU(n int) error {
// open datagram socket
var fd int
return nil
}
-func (tun *nativeTun) MTU() (int, error) {
+func (tun *NativeTun) MTU() (int, error) {
// open datagram socket
fd, err := unix.Socket(
packetNum uint32
}
-type nativeTun struct {
+type NativeTun struct {
wt *wintun.Wintun
tunName string
signalName *uint16
}
// Create instance.
- tun := &nativeTun{
+ tun := &NativeTun{
wt: wt,
tunName: wt.DataFileName(),
signalName: signalNameUTF16,
return tun, nil
}
-func (tun *nativeTun) openTUN() error {
+func (tun *NativeTun) openTUN() error {
for {
// Open interface data pipe.
// Data pipe must be opened first, as the interface data available event is created when somebody actually connects to the data pipe.
}
}
-func (tun *nativeTun) closeTUN() (err error) {
+func (tun *NativeTun) closeTUN() (err error) {
tun.tunLock.Lock()
defer tun.tunLock.Unlock()
return
}
-func (tun *nativeTun) getTUN() (*os.File, windows.Handle, error) {
+func (tun *NativeTun) getTUN() (*os.File, windows.Handle, error) {
tun.tunLock.Lock()
defer tun.tunLock.Unlock()
return tun.tunFile, tun.tunDataAvail, nil
}
-func (tun *nativeTun) Name() (string, error) {
+func (tun *NativeTun) Name() (string, error) {
return tun.wt.GetInterfaceName()
}
-func (tun *nativeTun) File() *os.File {
+func (tun *NativeTun) File() *os.File {
return nil
}
-func (tun *nativeTun) Events() chan TUNEvent {
+func (tun *NativeTun) Events() chan TUNEvent {
return tun.events
}
-func (tun *nativeTun) Close() error {
+func (tun *NativeTun) Close() error {
windows.SetEvent(tun.userClose)
err := windows.CloseHandle(tun.userClose)
return err
}
-func (tun *nativeTun) MTU() (int, error) {
+func (tun *NativeTun) MTU() (int, error) {
return 1500, nil
}
-func (tun *nativeTun) Read(buff []byte, offset int) (int, error) {
+func (tun *NativeTun) Read(buff []byte, offset int) (int, error) {
select {
case err := <-tun.errors:
return 0, err
// Note: flush() and putTunPacket() assume the caller comes only from a single thread; there's no locking.
-func (tun *nativeTun) flush() error {
+func (tun *NativeTun) flush() error {
// Get TUN data pipe.
file, _, err := tun.getTUN()
if err != nil {
return nil
}
-func (tun *nativeTun) putTunPacket(buff []byte) error {
+func (tun *NativeTun) putTunPacket(buff []byte) error {
size := uint32(len(buff))
if size == 0 {
return errors.New("Empty packet")
return nil
}
-func (tun *nativeTun) Write(buff []byte, offset int) (int, error) {
+func (tun *NativeTun) Write(buff []byte, offset int) (int, error) {
err := tun.putTunPacket(buff[offset:])
if err != nil {
return 0, err