summaryrefslogtreecommitdiffhomepage
path: root/main.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 /main.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 'main.go')
-rw-r--r--main.go5
1 files changed, 5 insertions, 0 deletions
diff --git a/main.go b/main.go
index 23a1d2d..08f8cc6 100644
--- a/main.go
+++ b/main.go
@@ -145,6 +145,11 @@ func main() {
return nil, err
}
+ err = syscall.SetNonblock(int(fd), true)
+ if err != nil {
+ return nil, err
+ }
+
file := os.NewFile(uintptr(fd), "")
return tun.CreateTUNFromFile(file, DefaultMTU)
}()