aboutsummaryrefslogtreecommitdiffstats
path: root/tun/tun_freebsd.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_freebsd.go
parenttun: use sysconn instead of .Fd with Go 1.12 (diff)
downloadwireguard-go-366cbd11a40a6a3fde5d0dc803ca6895859ed188.tar.xz
wireguard-go-366cbd11a40a6a3fde5d0dc803ca6895859ed188.zip
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_freebsd.go')
-rw-r--r--tun/tun_freebsd.go38
1 files changed, 6 insertions, 32 deletions
diff --git a/tun/tun_freebsd.go b/tun/tun_freebsd.go
index 1aec123..9e5c5ad 100644
--- a/tun/tun_freebsd.go
+++ b/tun/tun_freebsd.go
@@ -11,7 +11,6 @@ import (
"fmt"
"golang.org/x/net/ipv6"
"golang.org/x/sys/unix"
- "golang.zx2c4.com/wireguard/rwcancel"
"net"
"os"
"syscall"
@@ -52,7 +51,6 @@ type ifstat struct {
type nativeTun struct {
name string
tunFile *os.File
- rwcancel *rwcancel.RWCancel
events chan TUNEvent
errors chan error
routeSocket int
@@ -333,14 +331,6 @@ func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
return nil, err
}
- tun.operateOnFd(func(fd uintptr) {
- tun.rwcancel, err = rwcancel.NewRWCancel(int(fd))
- })
- if err != nil {
- tun.tunFile.Close()
- return nil, err
- }
-
tun.routeSocket, err = unix.Socket(unix.AF_ROUTE, unix.SOCK_RAW, unix.AF_UNSPEC)
if err != nil {
tun.tunFile.Close()
@@ -379,7 +369,7 @@ func (tun *nativeTun) Events() chan TUNEvent {
return tun.events
}
-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
@@ -393,18 +383,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.rwcancel.ReadyRead() {
- return 0, errors.New("tun device closed")
- }
- }
-}
-
func (tun *nativeTun) Write(buff []byte, offset int) (int, error) {
// reserve space for header
@@ -429,13 +407,12 @@ func (tun *nativeTun) Write(buff []byte, offset int) (int, error) {
}
func (tun *nativeTun) Close() error {
- var err4 error
- err1 := tun.rwcancel.Cancel()
- err2 := tun.tunFile.Close()
- err3 := tunDestroy(tun.name)
+ var err3 error
+ err1 := tun.tunFile.Close()
+ err2 := tunDestroy(tun.name)
if tun.routeSocket != -1 {
unix.Shutdown(tun.routeSocket, unix.SHUT_RDWR)
- err4 = unix.Close(tun.routeSocket)
+ err3 = unix.Close(tun.routeSocket)
tun.routeSocket = -1
} else if tun.events != nil {
close(tun.events)
@@ -446,10 +423,7 @@ func (tun *nativeTun) Close() error {
if err2 != nil {
return err2
}
- if err3 != nil {
- return err3
- }
- return err4
+ return err3
}
func (tun *nativeTun) setMTU(n int) error {