aboutsummaryrefslogtreecommitdiffstats
path: root/tun/tun_linux.go
diff options
context:
space:
mode:
Diffstat (limited to 'tun/tun_linux.go')
-rw-r--r--tun/tun_linux.go54
1 files changed, 13 insertions, 41 deletions
diff --git a/tun/tun_linux.go b/tun/tun_linux.go
index abdcce2..8dcad6b 100644
--- a/tun/tun_linux.go
+++ b/tun/tun_linux.go
@@ -31,7 +31,6 @@ const (
type nativeTun struct {
tunFile *os.File
- fdCancel *rwcancel.RWCancel
index int32 // if index
name string // name of interface
errors chan error // async error handling
@@ -307,7 +306,7 @@ func (tun *nativeTun) Write(buff []byte, offset int) (int, error) {
return tun.tunFile.Write(buff)
}
-func (tun *nativeTun) doRead(buff []byte, offset int) (int, error) {
+func (tun *nativeTun) Read(buff []byte, offset int) (int, error) {
select {
case err := <-tun.errors:
return 0, err
@@ -325,18 +324,6 @@ func (tun *nativeTun) doRead(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) {
- return n, err
- }
- if !tun.fdCancel.ReadyRead() {
- return 0, errors.New("tun device closed")
- }
- }
-}
-
func (tun *nativeTun) Events() chan TUNEvent {
return tun.events
}
@@ -352,30 +339,20 @@ func (tun *nativeTun) Close() error {
close(tun.events)
}
err2 := tun.tunFile.Close()
- err3 := tun.fdCancel.Cancel()
if err1 != nil {
return err1
}
- if err2 != nil {
- return err2
- }
- return err3
+ return err2
}
func CreateTUN(name string, mtu int) (TUNDevice, error) {
- nfd, err := unix.Open(cloneDevicePath, os.O_RDWR, 0)
- if err != nil {
- return nil, err
- }
-
- fd := os.NewFile(uintptr(nfd), cloneDevicePath)
+ tunFile, err := os.OpenFile(cloneDevicePath, os.O_RDWR, 0)
if err != nil {
return nil, err
}
// create new device
-
var ifr [ifReqSize]byte
var flags uint16 = unix.IFF_TUN // | unix.IFF_NO_PI (disabled for TUN status hack)
nameBytes := []byte(name)
@@ -385,17 +362,20 @@ func CreateTUN(name string, mtu int) (TUNDevice, error) {
copy(ifr[:], nameBytes)
*(*uint16)(unsafe.Pointer(&ifr[unix.IFNAMSIZ])) = flags
- _, _, errno := unix.Syscall(
- unix.SYS_IOCTL,
- nfd,
- uintptr(unix.TUNSETIFF),
- uintptr(unsafe.Pointer(&ifr[0])),
- )
+ var errno syscall.Errno
+ (&nativeTun{tunFile: tunFile}).operateOnFd(func(fd uintptr) {
+ _, _, errno = unix.Syscall(
+ unix.SYS_IOCTL,
+ fd,
+ uintptr(unix.TUNSETIFF),
+ uintptr(unsafe.Pointer(&ifr[0])),
+ )
+ })
if errno != 0 {
return nil, errno
}
- return CreateTUNFromFile(fd, mtu)
+ return CreateTUNFromFile(tunFile, mtu)
}
func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
@@ -408,14 +388,6 @@ func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
}
var err error
- tun.operateOnFd(func(fd uintptr) {
- tun.fdCancel, err = rwcancel.NewRWCancel(int(fd))
- })
- if err != nil {
- tun.tunFile.Close()
- return nil, err
- }
-
_, err = tun.Name()
if err != nil {
tun.tunFile.Close()