Skip to content

Commit db9b57c

Browse files
committed
Update contrib/mobile for the latest iOS build
1 parent 1420ea5 commit db9b57c

File tree

6 files changed

+73
-13
lines changed

6 files changed

+73
-13
lines changed

contrib/mobile/build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ if [ $IOS ]; then
3737
echo "Building framework for iOS"
3838
go get golang.org/x/mobile/bind
3939
gomobile bind \
40-
-target ios -tags mobile -o Yggdrasil.xcframework \
40+
-target ios,macos -tags mobile -o Yggdrasil.xcframework \
4141
-ldflags="$LDFLAGS $STRIP" -gcflags="$GCFLAGS" \
4242
./contrib/mobile ./src/config;
4343
fi

contrib/mobile/mobile.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"net"
88
"regexp"
9+
"runtime/debug"
910

1011
"github.com/gologme/log"
1112

@@ -15,6 +16,7 @@ import (
1516
"github.com/yggdrasil-network/yggdrasil-go/src/defaults"
1617
"github.com/yggdrasil-network/yggdrasil-go/src/ipv6rwc"
1718
"github.com/yggdrasil-network/yggdrasil-go/src/multicast"
19+
"github.com/yggdrasil-network/yggdrasil-go/src/tun"
1820
"github.com/yggdrasil-network/yggdrasil-go/src/version"
1921

2022
_ "golang.org/x/mobile/bind"
@@ -30,7 +32,9 @@ type Yggdrasil struct {
3032
iprwc *ipv6rwc.ReadWriteCloser
3133
config *config.NodeConfig
3234
multicast *multicast.Multicast
35+
tun *tun.TunAdapter // optional
3336
log MobileLogger
37+
logger *log.Logger
3438
}
3539

3640
// StartAutoconfigure starts a node with a randomly generated config
@@ -41,10 +45,12 @@ func (m *Yggdrasil) StartAutoconfigure() error {
4145
// StartJSON starts a node with the given JSON config. You can get JSON config
4246
// (rather than HJSON) by using the GenerateConfigJSON() function
4347
func (m *Yggdrasil) StartJSON(configjson []byte) error {
44-
logger := log.New(m.log, "", 0)
45-
logger.EnableLevel("error")
46-
logger.EnableLevel("warn")
47-
logger.EnableLevel("info")
48+
debug.SetMemoryLimit(1024 * 1024 * 40)
49+
50+
m.logger = log.New(m.log, "", 0)
51+
m.logger.EnableLevel("error")
52+
m.logger.EnableLevel("warn")
53+
m.logger.EnableLevel("info")
4854
m.config = defaults.GenerateConfig()
4955
if err := json.Unmarshal(configjson, &m.config); err != nil {
5056
return err
@@ -71,7 +77,7 @@ func (m *Yggdrasil) StartJSON(configjson []byte) error {
7177
}
7278
options = append(options, core.AllowedPublicKey(k[:]))
7379
}
74-
m.core, err = core.New(sk[:], logger, options...)
80+
m.core, err = core.New(sk[:], m.logger, options...)
7581
if err != nil {
7682
panic(err)
7783
}
@@ -90,9 +96,9 @@ func (m *Yggdrasil) StartJSON(configjson []byte) error {
9096
Priority: uint8(intf.Priority),
9197
})
9298
}
93-
m.multicast, err = multicast.New(m.core, logger, options...)
99+
m.multicast, err = multicast.New(m.core, m.logger, options...)
94100
if err != nil {
95-
logger.Errorln("An error occurred starting multicast:", err)
101+
m.logger.Errorln("An error occurred starting multicast:", err)
96102
}
97103
}
98104

@@ -155,6 +161,11 @@ func (m *Yggdrasil) Stop() error {
155161
if err := m.multicast.Stop(); err != nil {
156162
return err
157163
}
164+
if m.tun != nil {
165+
if err := m.tun.Stop(); err != nil {
166+
return err
167+
}
168+
}
158169
m.core.Stop()
159170
return nil
160171
}

contrib/mobile/mobile_ios.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ void Log(const char *text) {
1515
import "C"
1616
import (
1717
"unsafe"
18+
19+
"github.com/yggdrasil-network/yggdrasil-go/src/tun"
1820
)
1921

2022
type MobileLogger struct {
@@ -26,3 +28,13 @@ func (nsl MobileLogger) Write(p []byte) (n int, err error) {
2628
C.Log(cstr)
2729
return len(p), nil
2830
}
31+
32+
func (m *Yggdrasil) TakeOverTUN(fd int32) error {
33+
options := []tun.SetupOption{
34+
tun.FileDescriptor(fd),
35+
tun.InterfaceMTU(m.iprwc.MTU()),
36+
}
37+
var err error
38+
m.tun, err = tun.New(m.iprwc, m.logger, options...)
39+
return err
40+
}

src/tun/options.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ func (m *TunAdapter) _applyOption(opt SetupOption) {
66
m.config.name = v
77
case InterfaceMTU:
88
m.config.mtu = v
9+
case FileDescriptor:
10+
m.config.fd = int32(v)
911
}
1012
}
1113

@@ -15,6 +17,8 @@ type SetupOption interface {
1517

1618
type InterfaceName string
1719
type InterfaceMTU uint64
20+
type FileDescriptor int32
1821

19-
func (a InterfaceName) isSetupOption() {}
20-
func (a InterfaceMTU) isSetupOption() {}
22+
func (a InterfaceName) isSetupOption() {}
23+
func (a InterfaceMTU) isSetupOption() {}
24+
func (a FileDescriptor) isSetupOption() {}

src/tun/tun.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type TunAdapter struct {
3737
isOpen bool
3838
isEnabled bool // Used by the writer to drop sessionTraffic if not enabled
3939
config struct {
40+
fd int32
4041
name InterfaceName
4142
mtu InterfaceMTU
4243
}
@@ -119,7 +120,13 @@ func (tun *TunAdapter) _start() error {
119120
if tun.rwc.MaxMTU() < mtu {
120121
mtu = tun.rwc.MaxMTU()
121122
}
122-
if err := tun.setup(string(tun.config.name), addr, mtu); err != nil {
123+
var err error
124+
if tun.config.fd > 0 {
125+
err = tun.setupFD(tun.config.fd, addr, mtu)
126+
} else {
127+
err = tun.setup(string(tun.config.name), addr, mtu)
128+
}
129+
if err != nil {
123130
return err
124131
}
125132
if tun.MTU() != mtu {

src/tun/tun_darwin.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
//go:build !mobile
2-
// +build !mobile
1+
//go:build darwin || ios
2+
// +build darwin ios
33

44
package tun
55

66
// The darwin platform specific tun parts
77

88
import (
99
"encoding/binary"
10+
"os"
1011
"strconv"
1112
"strings"
1213
"unsafe"
@@ -34,6 +35,31 @@ func (tun *TunAdapter) setup(ifname string, addr string, mtu uint64) error {
3435
return tun.setupAddress(addr)
3536
}
3637

38+
// Configures the "utun" adapter from an existing file descriptor.
39+
func (tun *TunAdapter) setupFD(fd int32, addr string, mtu uint64) error {
40+
dfd, err := unix.Dup(int(fd))
41+
if err != nil {
42+
return err
43+
}
44+
err = unix.SetNonblock(dfd, true)
45+
if err != nil {
46+
unix.Close(dfd)
47+
return err
48+
}
49+
iface, err := wgtun.CreateTUNFromFile(os.NewFile(uintptr(dfd), "/dev/tun"), 0)
50+
if err != nil {
51+
unix.Close(dfd)
52+
return err
53+
}
54+
tun.iface = iface
55+
if m, err := iface.MTU(); err == nil {
56+
tun.mtu = getSupportedMTU(uint64(m))
57+
} else {
58+
tun.mtu = 0
59+
}
60+
return nil // tun.setupAddress(addr)
61+
}
62+
3763
const (
3864
darwin_SIOCAIFADDR_IN6 = 2155899162 // netinet6/in6_var.h
3965
darwin_IN6_IFF_NODAD = 0x0020 // netinet6/in6_var.h

0 commit comments

Comments
 (0)