From a371e34a187cc3642a2bdaa7ff150f443b5e55dc Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Thu, 10 Jan 2019 10:44:44 +0000 Subject: [PATCH 1/7] Add Android support, add addStaticPeers --- src/yggdrasil/mobile.go | 24 ++++++++++++++++++++++++ src/yggdrasil/mobile_android.go | 12 ++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/yggdrasil/mobile_android.go diff --git a/src/yggdrasil/mobile.go b/src/yggdrasil/mobile.go index 300d132..5cd15ec 100644 --- a/src/yggdrasil/mobile.go +++ b/src/yggdrasil/mobile.go @@ -9,6 +9,7 @@ import ( "log" "os" "regexp" + "time" hjson "github.com/hjson/hjson-go" "github.com/mitchellh/mapstructure" @@ -23,6 +24,25 @@ import ( // that in the case of iOS we handle reading/writing to/from TUN in Swift // therefore we use the "dummy" TUN interface instead. +func (c *Core) addStaticPeers(cfg *config.NodeConfig) { + if len(cfg.Peers) == 0 && len(cfg.InterfacePeers) == 0 { + return + } + for { + for _, peer := range cfg.Peers { + c.AddPeer(peer, "") + time.Sleep(time.Second) + } + for intf, intfpeers := range cfg.InterfacePeers { + for _, peer := range intfpeers { + c.AddPeer(peer, intf) + time.Sleep(time.Second) + } + } + time.Sleep(time.Minute) + } +} + func (c *Core) StartAutoconfigure() error { mobilelog := MobileLogger{} logger := log.New(mobilelog, "", 0) @@ -40,6 +60,7 @@ func (c *Core) StartAutoconfigure() error { if err := c.Start(nc, logger); err != nil { return err } + go c.addStaticPeers(nc) return nil } @@ -55,7 +76,9 @@ func (c *Core) StartJSON(configjson []byte) error { return err } nc.IfName = "dummy" + //c.log.Println(nc.MulticastInterfaces) for _, ll := range nc.MulticastInterfaces { + //c.log.Println("Processing MC", ll) ifceExpr, err := regexp.Compile(ll) if err != nil { panic(err) @@ -65,6 +88,7 @@ func (c *Core) StartJSON(configjson []byte) error { if err := c.Start(nc, logger); err != nil { return err } + go c.addStaticPeers(nc) return nil } diff --git a/src/yggdrasil/mobile_android.go b/src/yggdrasil/mobile_android.go new file mode 100644 index 0000000..2476484 --- /dev/null +++ b/src/yggdrasil/mobile_android.go @@ -0,0 +1,12 @@ +// +build android + +package yggdrasil + +import "log" + +type MobileLogger struct{} + +func (nsl MobileLogger) Write(p []byte) (n int, err error) { + log.Println(string(p)) + return len(p), nil +} From e24e8592541a4862db1cd14ba137a46024c402ad Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Thu, 10 Jan 2019 11:31:04 +0000 Subject: [PATCH 2/7] Add some comments, move AWDL functions to iOS-specific build tags --- src/yggdrasil/mobile.go | 85 ++++++------------------------------- src/yggdrasil/mobile_ios.go | 78 +++++++++++++++++++++++++++++++++- 2 files changed, 90 insertions(+), 73 deletions(-) diff --git a/src/yggdrasil/mobile.go b/src/yggdrasil/mobile.go index 5cd15ec..b751061 100644 --- a/src/yggdrasil/mobile.go +++ b/src/yggdrasil/mobile.go @@ -3,9 +3,7 @@ package yggdrasil import ( - "encoding/hex" "encoding/json" - "errors" "log" "os" "regexp" @@ -14,7 +12,6 @@ import ( hjson "github.com/hjson/hjson-go" "github.com/mitchellh/mapstructure" "github.com/yggdrasil-network/yggdrasil-go/src/config" - "github.com/yggdrasil-network/yggdrasil-go/src/crypto" "github.com/yggdrasil-network/yggdrasil-go/src/util" ) @@ -43,6 +40,7 @@ func (c *Core) addStaticPeers(cfg *config.NodeConfig) { } } +// Starts a node with a randomly generated config. func (c *Core) StartAutoconfigure() error { mobilelog := MobileLogger{} logger := log.New(mobilelog, "", 0) @@ -64,6 +62,8 @@ func (c *Core) StartAutoconfigure() error { return nil } +// Starts a node with the given JSON config. You can get JSON config (rather +// than HJSON) by using the GenerateConfigJSON() function. func (c *Core) StartJSON(configjson []byte) error { mobilelog := MobileLogger{} logger := log.New(mobilelog, "", 0) @@ -92,6 +92,7 @@ func (c *Core) StartJSON(configjson []byte) error { return nil } +// Generates mobile-friendly configuration in JSON format. func GenerateConfigJSON() []byte { nc := config.GenerateConfig(false) nc.IfName = "dummy" @@ -102,90 +103,30 @@ func GenerateConfigJSON() []byte { } } +// Gets the node's IPv6 address. func (c *Core) GetAddressString() string { return c.GetAddress().String() } +// Gets the node's IPv6 subnet in CIDR notation. func (c *Core) GetSubnetString() string { return c.GetSubnet().String() } +// 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. func (c *Core) RouterRecvPacket() ([]byte, error) { packet := <-c.router.tun.recv return packet, nil } +// Send a packet to the router. You will use this when implementing a +// dummy adapter in place of real TUN - when the operating system tells you +// that a new packet is available from TUN, call this function to give it to +// Yggdrasil. func (c *Core) RouterSendPacket(buf []byte) error { packet := append(util.GetBytes(), buf[:]...) c.router.tun.send <- packet return nil } - -func (c *Core) AWDLCreateInterface(boxPubKey string, sigPubKey string, name string) error { - fromAWDL := make(chan []byte, 32) - toAWDL := make(chan []byte, 32) - - var boxPub crypto.BoxPubKey - var sigPub crypto.SigPubKey - boxPubHex, err := hex.DecodeString(boxPubKey) - if err != nil { - c.log.Println(err) - return err - } - sigPubHex, err := hex.DecodeString(sigPubKey) - if err != nil { - c.log.Println(err) - return err - } - copy(boxPub[:], boxPubHex) - copy(sigPub[:], sigPubHex) - - if intf, err := c.awdl.create(fromAWDL, toAWDL, &boxPub, &sigPub, name); err == nil { - if intf != nil { - c.log.Println(err) - return err - } else { - c.log.Println("c.awdl.create didn't return an interface") - return errors.New("c.awdl.create didn't return an interface") - } - } else { - c.log.Println(err) - return err - } -} - -func (c *Core) AWDLCreateInterfaceFromContext(context []byte, name string) error { - if len(context) < crypto.BoxPubKeyLen+crypto.SigPubKeyLen { - return errors.New("Not enough bytes in context") - } - boxPubKey := hex.EncodeToString(context[:crypto.BoxPubKeyLen]) - sigPubKey := hex.EncodeToString(context[crypto.BoxPubKeyLen:]) - return c.AWDLCreateInterface(boxPubKey, sigPubKey, name) -} - -func (c *Core) AWDLShutdownInterface(name string) error { - return c.awdl.shutdown(name) -} - -func (c *Core) AWDLRecvPacket(identity string) ([]byte, error) { - if intf := c.awdl.getInterface(identity); intf != nil { - return <-intf.toAWDL, nil - } - return nil, errors.New("AWDLRecvPacket identity not known: " + identity) -} - -func (c *Core) AWDLSendPacket(identity string, buf []byte) error { - packet := append(util.GetBytes(), buf[:]...) - if intf := c.awdl.getInterface(identity); intf != nil { - intf.fromAWDL <- packet - return nil - } - return errors.New("AWDLSendPacket identity not known: " + identity) -} - -func (c *Core) AWDLConnectionContext() []byte { - var context []byte - context = append(context, c.boxPub[:]...) - context = append(context, c.sigPub[:]...) - return context -} diff --git a/src/yggdrasil/mobile_ios.go b/src/yggdrasil/mobile_ios.go index 4abc6e9..6d2570d 100644 --- a/src/yggdrasil/mobile_ios.go +++ b/src/yggdrasil/mobile_ios.go @@ -12,7 +12,14 @@ void Log(const char *text) { } */ import "C" -import "unsafe" +import ( + "encoding/hex" + "errors" + "unsafe" + + "github.com/yggdrasil-network/yggdrasil-go/src/crypto" + "github.com/yggdrasil-network/yggdrasil-go/src/util" +) type MobileLogger struct { } @@ -23,3 +30,72 @@ func (nsl MobileLogger) Write(p []byte) (n int, err error) { C.Log(cstr) return len(p), nil } + +func (c *Core) AWDLCreateInterface(boxPubKey string, sigPubKey string, name string) error { + fromAWDL := make(chan []byte, 32) + toAWDL := make(chan []byte, 32) + + var boxPub crypto.BoxPubKey + var sigPub crypto.SigPubKey + boxPubHex, err := hex.DecodeString(boxPubKey) + if err != nil { + c.log.Println(err) + return err + } + sigPubHex, err := hex.DecodeString(sigPubKey) + if err != nil { + c.log.Println(err) + return err + } + copy(boxPub[:], boxPubHex) + copy(sigPub[:], sigPubHex) + + if intf, err := c.awdl.create(fromAWDL, toAWDL, &boxPub, &sigPub, name); err == nil { + if intf != nil { + c.log.Println(err) + return err + } else { + c.log.Println("c.awdl.create didn't return an interface") + return errors.New("c.awdl.create didn't return an interface") + } + } else { + c.log.Println(err) + return err + } +} + +func (c *Core) AWDLCreateInterfaceFromContext(context []byte, name string) error { + if len(context) < crypto.BoxPubKeyLen+crypto.SigPubKeyLen { + return errors.New("Not enough bytes in context") + } + boxPubKey := hex.EncodeToString(context[:crypto.BoxPubKeyLen]) + sigPubKey := hex.EncodeToString(context[crypto.BoxPubKeyLen:]) + return c.AWDLCreateInterface(boxPubKey, sigPubKey, name) +} + +func (c *Core) AWDLShutdownInterface(name string) error { + return c.awdl.shutdown(name) +} + +func (c *Core) AWDLRecvPacket(identity string) ([]byte, error) { + if intf := c.awdl.getInterface(identity); intf != nil { + return <-intf.toAWDL, nil + } + return nil, errors.New("AWDLRecvPacket identity not known: " + identity) +} + +func (c *Core) AWDLSendPacket(identity string, buf []byte) error { + packet := append(util.GetBytes(), buf[:]...) + if intf := c.awdl.getInterface(identity); intf != nil { + intf.fromAWDL <- packet + return nil + } + return errors.New("AWDLSendPacket identity not known: " + identity) +} + +func (c *Core) AWDLConnectionContext() []byte { + var context []byte + context = append(context, c.boxPub[:]...) + context = append(context, c.sigPub[:]...) + return context +} From 4f7e8856b853c4b6e5bc456078c3abb915f30fae Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Thu, 10 Jan 2019 17:44:52 +0000 Subject: [PATCH 3/7] Update build script for iOS/Android --- build | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/build b/build index a3a679f..7ceb4c9 100755 --- a/build +++ b/build @@ -6,13 +6,14 @@ PKGVER=${PKGVER:-$(sh contrib/semver/version.sh --bare)} LDFLAGS="-X $PKGSRC.buildName=$PKGNAME -X $PKGSRC.buildVersion=$PKGVER" -while getopts "udmtc:l:" option +while getopts "udaitc:l:" option do case "${option}" in u) UPX=true;; d) DEBUG=true;; - m) MOBILE=true;; + i) IOS=true;; + a) ANDROID=true;; t) TABLES=true;; c) GCFLAGS="$GCFLAGS $OPTARG";; l) LDFLAGS="$LDFLAGS $OPTARG";; @@ -23,17 +24,23 @@ if [ -z $TABLES ]; then STRIP="-s -w" fi -for CMD in `ls cmd/` ; do - echo "Building: $CMD" +if [ $IOS ]; then + echo "Building framework for iOS" + gomobile bind -target ios -tags mobile -ldflags="$LDFLAGS $STRIP" -gcflags="$GCFLAGS" github.com/yggdrasil-network/yggdrasil-go/src/yggdrasil +elif [ $ANDROID ]; then + echo "Building aar for Android" + gomobile bind -target android -tags mobile -ldflags="$LDFLAGS $STRIP" -gcflags="$GCFLAGS" github.com/yggdrasil-network/yggdrasil-go/src/yggdrasil +else + for CMD in `ls cmd/` ; do + echo "Building: $CMD" - if [ $MOBILE ]; then - go build -ldflags="$LDFLAGS" -gcflags="$GCFLAGS" -tags mobile -v ./cmd/$CMD - elif [ $DEBUG ]; then - go build -ldflags="$LDFLAGS" -gcflags="$GCFLAGS" -tags debug -v ./cmd/$CMD - else - go build -ldflags="$LDFLAGS $STRIP" -gcflags="$GCFLAGS" -v ./cmd/$CMD - fi - if [ $UPX ]; then - upx --brute $CMD - fi -done + if [ $DEBUG ]; then + go build -ldflags="$LDFLAGS" -gcflags="$GCFLAGS" -tags debug -v ./cmd/$CMD + else + go build -ldflags="$LDFLAGS $STRIP" -gcflags="$GCFLAGS" -v ./cmd/$CMD + fi + if [ $UPX ]; then + upx --brute $CMD + fi + done +fi From 4622a85c34e2c548b415db69720e9605c9acffcd Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Sun, 13 Jan 2019 18:08:41 +0000 Subject: [PATCH 4/7] 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 b751061..2ffeffb 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 0000000..71eecce --- /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 9c6d1f1..54bbc64 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 6d92344..8d8fee3 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 0000000..6483ef8 --- /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 0000000..5d62b53 --- /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 +} From c8db66b17d6a5a7f88a6ffc9ce75492bd135b8d6 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Sun, 13 Jan 2019 18:11:36 +0000 Subject: [PATCH 5/7] Remove unneeded AWDL context functions --- src/yggdrasil/mobile_ios.go | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/yggdrasil/mobile_ios.go b/src/yggdrasil/mobile_ios.go index 6d2570d..789df57 100644 --- a/src/yggdrasil/mobile_ios.go +++ b/src/yggdrasil/mobile_ios.go @@ -64,15 +64,6 @@ func (c *Core) AWDLCreateInterface(boxPubKey string, sigPubKey string, name stri } } -func (c *Core) AWDLCreateInterfaceFromContext(context []byte, name string) error { - if len(context) < crypto.BoxPubKeyLen+crypto.SigPubKeyLen { - return errors.New("Not enough bytes in context") - } - boxPubKey := hex.EncodeToString(context[:crypto.BoxPubKeyLen]) - sigPubKey := hex.EncodeToString(context[crypto.BoxPubKeyLen:]) - return c.AWDLCreateInterface(boxPubKey, sigPubKey, name) -} - func (c *Core) AWDLShutdownInterface(name string) error { return c.awdl.shutdown(name) } @@ -92,10 +83,3 @@ func (c *Core) AWDLSendPacket(identity string, buf []byte) error { } return errors.New("AWDLSendPacket identity not known: " + identity) } - -func (c *Core) AWDLConnectionContext() []byte { - var context []byte - context = append(context, c.boxPub[:]...) - context = append(context, c.sigPub[:]...) - return context -} From 704e4a062fbf953b5fc3065f5d32be1d82494515 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Sun, 13 Jan 2019 22:51:34 +0000 Subject: [PATCH 6/7] Specify source interface when responding to multicast beacon --- src/yggdrasil/multicast.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/yggdrasil/multicast.go b/src/yggdrasil/multicast.go index 749dfcd..c0a676a 100644 --- a/src/yggdrasil/multicast.go +++ b/src/yggdrasil/multicast.go @@ -157,6 +157,6 @@ func (m *multicast) listen() { } addr.Zone = from.Zone saddr := addr.String() - m.core.tcp.connect(saddr, "") + m.core.tcp.connect(saddr, addr.Zone) } } From f556f3e2a8ffd261f2a8cbcd809bb61f9812d1af Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Sun, 13 Jan 2019 22:57:37 +0000 Subject: [PATCH 7/7] Try to perform TCP-like key exchange --- src/yggdrasil/awdl.go | 59 +++++++++++++++++++------------------ src/yggdrasil/mobile_ios.go | 21 ++----------- 2 files changed, 32 insertions(+), 48 deletions(-) diff --git a/src/yggdrasil/awdl.go b/src/yggdrasil/awdl.go index 65e84cb..633d5f9 100644 --- a/src/yggdrasil/awdl.go +++ b/src/yggdrasil/awdl.go @@ -34,42 +34,42 @@ func (l *awdl) init(c *Core) error { return nil } -func (l *awdl) create(fromAWDL chan []byte, toAWDL chan []byte, boxPubKey *crypto.BoxPubKey, sigPubKey *crypto.SigPubKey, name string) (*awdlInterface, error) { - /* - myLinkPub, myLinkPriv := crypto.NewBoxKeys() - meta := version_getBaseMetadata() - meta.box = l.core.boxPub - meta.sig = l.core.sigPub - meta.link = *myLinkPub - metaBytes := meta.encode() - l.core.log.Println("toAWDL <- metaBytes") - toAWDL <- metaBytes - l.core.log.Println("metaBytes = <-fromAWDL") - metaBytes = <-fromAWDL - l.core.log.Println("version_metadata{}") - meta = version_metadata{} - if !meta.decode(metaBytes) || !meta.check() { - return nil, errors.New("Metadata decode failure") - } - base := version_getBaseMetadata() - if meta.ver > base.ver || meta.ver == base.ver && meta.minorVer > base.minorVer { - return nil, errors.New("Failed to connect to node: " + name + " version: " + fmt.Sprintf("%d.%d", meta.ver, meta.minorVer)) - } - shared := crypto.GetSharedKey(myLinkPriv, &meta.link) - */ - shared := crypto.GetSharedKey(&l.core.boxPriv, boxPubKey) +func (l *awdl) create(fromAWDL chan []byte, toAWDL chan []byte /*boxPubKey *crypto.BoxPubKey, sigPubKey *crypto.SigPubKey*/, name string) (*awdlInterface, error) { intf := awdlInterface{ awdl: l, fromAWDL: fromAWDL, toAWDL: toAWDL, shutdown: make(chan bool), - peer: l.core.peers.newPeer(boxPubKey, sigPubKey, shared, name), - //peer: l.core.peers.newPeer(&meta.box, &meta.sig, shared, name), } + l.mutex.Lock() + l.interfaces[name] = &intf + l.mutex.Unlock() + myLinkPub, myLinkPriv := crypto.NewBoxKeys() + meta := version_getBaseMetadata() + meta.box = l.core.boxPub + meta.sig = l.core.sigPub + meta.link = *myLinkPub + metaBytes := meta.encode() + l.core.log.Println("toAWDL <- metaBytes") + toAWDL <- metaBytes + l.core.log.Println("metaBytes = <-fromAWDL") + metaBytes = <-fromAWDL + l.core.log.Println("version_metadata{}") + meta = version_metadata{} + if !meta.decode(metaBytes) || !meta.check() { + return nil, errors.New("Metadata decode failure") + } + l.core.log.Println("version_getBaseMetadata{}") + base := version_getBaseMetadata() + if meta.ver > base.ver || meta.ver == base.ver && meta.minorVer > base.minorVer { + return nil, errors.New("Failed to connect to node: " + name + " version: " + fmt.Sprintf("%d.%d", meta.ver, meta.minorVer)) + } + l.core.log.Println("crypto.GetSharedKey") + shared := crypto.GetSharedKey(myLinkPriv, &meta.link) + //shared := crypto.GetSharedKey(&l.core.boxPriv, boxPubKey) + l.core.log.Println("l.core.peers.newPeer") + intf.peer = l.core.peers.newPeer(&meta.box, &meta.sig, shared, name) if intf.peer != nil { - l.mutex.Lock() - l.interfaces[name] = &intf - l.mutex.Unlock() intf.peer.linkOut = make(chan []byte, 1) // protocol traffic intf.peer.out = func(msg []byte) { defer func() { recover() }() @@ -84,6 +84,7 @@ func (l *awdl) create(fromAWDL chan []byte, toAWDL chan []byte, boxPubKey *crypt go intf.peer.linkLoop() return &intf, nil } + delete(l.interfaces, name) return nil, errors.New("l.core.peers.newPeer failed") } diff --git a/src/yggdrasil/mobile_ios.go b/src/yggdrasil/mobile_ios.go index 789df57..72920fe 100644 --- a/src/yggdrasil/mobile_ios.go +++ b/src/yggdrasil/mobile_ios.go @@ -13,11 +13,9 @@ void Log(const char *text) { */ import "C" import ( - "encoding/hex" "errors" "unsafe" - "github.com/yggdrasil-network/yggdrasil-go/src/crypto" "github.com/yggdrasil-network/yggdrasil-go/src/util" ) @@ -31,26 +29,11 @@ func (nsl MobileLogger) Write(p []byte) (n int, err error) { return len(p), nil } -func (c *Core) AWDLCreateInterface(boxPubKey string, sigPubKey string, name string) error { +func (c *Core) AWDLCreateInterface(name string) error { fromAWDL := make(chan []byte, 32) toAWDL := make(chan []byte, 32) - var boxPub crypto.BoxPubKey - var sigPub crypto.SigPubKey - boxPubHex, err := hex.DecodeString(boxPubKey) - if err != nil { - c.log.Println(err) - return err - } - sigPubHex, err := hex.DecodeString(sigPubKey) - if err != nil { - c.log.Println(err) - return err - } - copy(boxPub[:], boxPubHex) - copy(sigPub[:], sigPubHex) - - if intf, err := c.awdl.create(fromAWDL, toAWDL, &boxPub, &sigPub, name); err == nil { + if intf, err := c.awdl.create(fromAWDL, toAWDL, name); err == nil { if intf != nil { c.log.Println(err) return err