aboutsummaryrefslogtreecommitdiffstats
path: root/tun
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2019-03-03 05:20:13 +0100
committerJason A. Donenfeld <Jason@zx2c4.com>2019-03-04 16:37:11 +0100
commit68f0721c6a1aea6bb97320ec0cf6b8f7f723603d (patch)
tree615ff2a882d73f9f8febe94aaaf22cf54c229621 /tun
parentboundif: introduce API for socket binding (diff)
downloadwireguard-go-68f0721c6a1aea6bb97320ec0cf6b8f7f723603d.tar.xz
wireguard-go-68f0721c6a1aea6bb97320ec0cf6b8f7f723603d.zip
tun: import mobile particularities
Diffstat (limited to 'tun')
-rw-r--r--tun/tun_darwin.go12
-rw-r--r--tun/tun_linux.go22
2 files changed, 29 insertions, 5 deletions
diff --git a/tun/tun_darwin.go b/tun/tun_darwin.go
index 600b156..3b39982 100644
--- a/tun/tun_darwin.go
+++ b/tun/tun_darwin.go
@@ -171,7 +171,7 @@ func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
tun := &NativeTun{
tunFile: file,
events: make(chan TUNEvent, 10),
- errors: make(chan error, 1),
+ errors: make(chan error, 5),
}
name, err := tun.Name()
@@ -200,10 +200,12 @@ func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
go tun.routineRouteListener(tunIfindex)
- err = tun.setMTU(mtu)
- if err != nil {
- tun.Close()
- return nil, err
+ if mtu > 0 {
+ err = tun.setMTU(mtu)
+ if err != nil {
+ tun.Close()
+ return nil, err
+ }
}
return tun, nil
diff --git a/tun/tun_linux.go b/tun/tun_linux.go
index f808079..c352c1a 100644
--- a/tun/tun_linux.go
+++ b/tun/tun_linux.go
@@ -443,3 +443,25 @@ func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
return tun, nil
}
+
+func CreateUnmonitoredTUNFromFD(tunFd int) (TUNDevice, string, error) {
+ file := os.NewFile(uintptr(tunFd), "/dev/tun")
+ tun := &NativeTun{
+ tunFile: file,
+ fd: file.Fd(),
+ events: make(chan TUNEvent, 5),
+ errors: make(chan error, 5),
+ nopi: true,
+ }
+ var err error
+ tun.fdCancel, err = rwcancel.NewRWCancel(int(tun.fd))
+ if err != nil {
+ return nil, "", err
+ }
+ name, err := tun.Name()
+ if err != nil {
+ tun.fdCancel.Cancel()
+ return nil, "", err
+ }
+ return tun, name, nil
+}