summaryrefslogtreecommitdiffhomepage
path: root/tun/tun.go
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2019-02-27 01:48:58 +0100
committerJason A. Donenfeld <Jason@zx2c4.com>2019-02-27 01:52:55 +0100
commit366cbd11a40a6a3fde5d0dc803ca6895859ed188 (patch)
tree9521b573691cbf29610e5e9f488eb4330cf848e0 /tun/tun.go
parentab0f442dafba417c3a13d883a447e882d7983690 (diff)
tun: use netpoll instead of rwcancel
The new sysconn function of Go 1.12 makes this possible: package main import "log" import "os" import "unsafe" import "time" import "syscall" import "sync" import "golang.org/x/sys/unix" func main() { fd, err := os.OpenFile("/dev/net/tun", os.O_RDWR, 0) if err != nil { log.Fatal(err) } var ifr [unix.IFNAMSIZ + 64]byte copy(ifr[:], []byte("cheese")) *(*uint16)(unsafe.Pointer(&ifr[unix.IFNAMSIZ])) = unix.IFF_TUN var errno syscall.Errno s, _ := fd.SyscallConn() s.Control(func(fd uintptr) { _, _, errno = unix.Syscall( unix.SYS_IOCTL, fd, uintptr(unix.TUNSETIFF), uintptr(unsafe.Pointer(&ifr[0])), ) }) if errno != 0 { log.Fatal(errno) } b := [4]byte{} wait := sync.WaitGroup{} wait.Add(1) go func() { _, err := fd.Read(b[:]) log.Print("Read errored: ", err) wait.Done() }() time.Sleep(time.Second) log.Print("Closing") err = fd.Close() if err != nil { log.Print("Close errored: " , err) } wait.Wait() log.Print("Exiting") }
Diffstat (limited to 'tun/tun.go')
-rw-r--r--tun/tun.go2
1 files changed, 1 insertions, 1 deletions
diff --git a/tun/tun.go b/tun/tun.go
index d9c3c15..3493b48 100644
--- a/tun/tun.go
+++ b/tun/tun.go
@@ -38,4 +38,4 @@ func (tun *nativeTun) operateOnFd(fn func(fd uintptr)) {
if err != nil {
tun.errors <- fmt.Errorf("unable to control sysconn for tunfile: %s", err.Error())
}
-} \ No newline at end of file
+}