]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
tun: import mobile particularities
authorJason A. Donenfeld <Jason@zx2c4.com>
Sun, 3 Mar 2019 04:20:13 +0000 (05:20 +0100)
committerJason A. Donenfeld <Jason@zx2c4.com>
Mon, 4 Mar 2019 15:37:11 +0000 (16:37 +0100)
device/queueconstants_android.go [new file with mode: 0644]
device/queueconstants_default.go [moved from device/queueconstants.go with 90% similarity]
device/queueconstants_ios.go [new file with mode: 0644]
tun/tun_darwin.go
tun/tun_linux.go

diff --git a/device/queueconstants_android.go b/device/queueconstants_android.go
new file mode 100644 (file)
index 0000000..8d051ad
--- /dev/null
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: MIT
+ *
+ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
+ */
+
+package device
+
+/* Reduce memory consumption for Android */
+
+const (
+       QueueOutboundSize          = 1024
+       QueueInboundSize           = 1024
+       QueueHandshakeSize         = 1024
+       MaxSegmentSize             = 2200
+       PreallocatedBuffersPerPool = 4096
+)
\ No newline at end of file
similarity index 90%
rename from device/queueconstants.go
rename to device/queueconstants_default.go
index 3e94b7fc2d47be07cd55a88270443ef2ef0f774e..cf86ba15c082ce0937f8e7c6fac0c64a1e980d3a 100644 (file)
@@ -1,3 +1,5 @@
+// +build !android,!ios
+
 /* SPDX-License-Identifier: MIT
  *
  * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
@@ -5,8 +7,6 @@
 
 package device
 
-/* Implementation specific constants */
-
 const (
        QueueOutboundSize          = 1024
        QueueInboundSize           = 1024
diff --git a/device/queueconstants_ios.go b/device/queueconstants_ios.go
new file mode 100644 (file)
index 0000000..589b0aa
--- /dev/null
@@ -0,0 +1,18 @@
+// +build ios
+
+/* SPDX-License-Identifier: MIT
+ *
+ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
+ */
+
+package device
+
+/* Fit within memory limits for iOS's Network Extension API, which has stricter requirements */
+
+const (
+       QueueOutboundSize          = 1024
+       QueueInboundSize           = 1024
+       QueueHandshakeSize         = 1024
+       MaxSegmentSize             = 1700
+       PreallocatedBuffersPerPool = 1024
+)
index 600b1569b07ab74ccfdc152a0b1ea910f5181d9f..3b3998288097701ab12e90cdefdc1e8d32d62676 100644 (file)
@@ -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
index f80807917c488bcdcb86c1b7d2ba9560616058b7..c352c1a1e61e0be157d03580ec1250c8848516ca 100644 (file)
@@ -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
+}