From 4622a85c34e2c548b415db69720e9605c9acffcd Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Sun, 13 Jan 2019 18:08:41 +0000 Subject: [PATCH] AWDL support for macOS/iOS --- src/yggdrasil/mobile.go | 11 +++++++++++ src/yggdrasil/multicast_darwin.go | 28 ++++++++++++++++++++++++++++ src/yggdrasil/multicast_unix.go | 2 +- src/yggdrasil/tcp.go | 11 +++++++++-- src/yggdrasil/tcp_darwin.go | 28 ++++++++++++++++++++++++++++ src/yggdrasil/tcp_other.go | 13 +++++++++++++ 6 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 src/yggdrasil/multicast_darwin.go create mode 100644 src/yggdrasil/tcp_darwin.go create mode 100644 src/yggdrasil/tcp_other.go diff --git a/src/yggdrasil/mobile.go b/src/yggdrasil/mobile.go index b7510613..2ffeffb9 100644 --- a/src/yggdrasil/mobile.go +++ b/src/yggdrasil/mobile.go @@ -3,6 +3,7 @@ package yggdrasil import ( + "encoding/hex" "encoding/json" "log" "os" @@ -113,6 +114,16 @@ func (c *Core) GetSubnetString() string { return c.GetSubnet().String() } +// Gets the node's public encryption key. +func (c *Core) GetBoxPubKeyString() string { + return hex.EncodeToString(c.boxPub[:]) +} + +// Gets the node's public signing key. +func (c *Core) GetSigPubKeyString() string { + return hex.EncodeToString(c.sigPub[:]) +} + // Wait for a packet from the router. You will use this when implementing a // dummy adapter in place of real TUN - when this call returns a packet, you // will probably want to give it to the OS to write to TUN. diff --git a/src/yggdrasil/multicast_darwin.go b/src/yggdrasil/multicast_darwin.go new file mode 100644 index 00000000..71eecce4 --- /dev/null +++ b/src/yggdrasil/multicast_darwin.go @@ -0,0 +1,28 @@ +// +build darwin + +package yggdrasil + +import "syscall" +import "golang.org/x/sys/unix" + +func multicastReuse(network string, address string, c syscall.RawConn) error { + var control error + var reuseport error + var recvanyif error + + control = c.Control(func(fd uintptr) { + reuseport = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1) + + // sys/socket.h: #define SO_RECV_ANYIF 0x1104 + recvanyif = unix.SetsockoptInt(int(fd), syscall.SOL_SOCKET, 0x1104, 1) + }) + + switch { + case reuseport != nil: + return reuseport + case recvanyif != nil: + return recvanyif + default: + return control + } +} diff --git a/src/yggdrasil/multicast_unix.go b/src/yggdrasil/multicast_unix.go index 9c6d1f11..54bbc645 100644 --- a/src/yggdrasil/multicast_unix.go +++ b/src/yggdrasil/multicast_unix.go @@ -1,4 +1,4 @@ -// +build linux darwin netbsd freebsd openbsd dragonflybsd +// +build linux netbsd freebsd openbsd dragonflybsd package yggdrasil diff --git a/src/yggdrasil/tcp.go b/src/yggdrasil/tcp.go index 6d923440..8d8fee34 100644 --- a/src/yggdrasil/tcp.go +++ b/src/yggdrasil/tcp.go @@ -15,6 +15,7 @@ package yggdrasil // See version.go for version metadata format import ( + "context" "errors" "fmt" "io" @@ -88,7 +89,11 @@ func (iface *tcpInterface) init(core *Core, addr string, readTimeout int32) (err iface.tcp_timeout = default_tcp_timeout } - iface.serv, err = net.Listen("tcp", addr) + ctx := context.Background() + lc := net.ListenConfig{ + Control: iface.tcpContext, + } + iface.serv, err = lc.Listen(ctx, "tcp", addr) if err == nil { iface.calls = make(map[string]struct{}) iface.conns = make(map[tcpInfo](chan struct{})) @@ -164,7 +169,9 @@ func (iface *tcpInterface) call(saddr string, socksaddr *string, sintf string) { }, } } else { - dialer := net.Dialer{} + dialer := net.Dialer{ + Control: iface.tcpContext, + } if sintf != "" { ief, err := net.InterfaceByName(sintf) if err != nil { diff --git a/src/yggdrasil/tcp_darwin.go b/src/yggdrasil/tcp_darwin.go new file mode 100644 index 00000000..6483ef86 --- /dev/null +++ b/src/yggdrasil/tcp_darwin.go @@ -0,0 +1,28 @@ +// +build darwin + +package yggdrasil + +import ( + "syscall" + + "golang.org/x/sys/unix" +) + +// WARNING: This context is used both by net.Dialer and net.Listen in tcp.go + +func (iface *tcpInterface) tcpContext(network, address string, c syscall.RawConn) error { + var control error + var recvanyif error + + control = c.Control(func(fd uintptr) { + // sys/socket.h: #define SO_RECV_ANYIF 0x1104 + recvanyif = unix.SetsockoptInt(int(fd), syscall.SOL_SOCKET, 0x1104, 1) + }) + + switch { + case recvanyif != nil: + return recvanyif + default: + return control + } +} diff --git a/src/yggdrasil/tcp_other.go b/src/yggdrasil/tcp_other.go new file mode 100644 index 00000000..5d62b530 --- /dev/null +++ b/src/yggdrasil/tcp_other.go @@ -0,0 +1,13 @@ +// +build !darwin + +package yggdrasil + +import ( + "syscall" +) + +// WARNING: This context is used both by net.Dialer and net.Listen in tcp.go + +func (iface *tcpInterface) tcpContext(network, address string, c syscall.RawConn) error { + return nil +}