From 58f5cc88d03abee0afc1f3555c8aad0b1f274a1f Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Mon, 1 Apr 2019 19:59:50 +0100 Subject: [PATCH] Fix session bug, fix dummy adapter, fix mobile framework builds --- build | 10 +++++++-- src/dummy/dummy.go | 46 ++++++++++++++++++++++------------------ src/mobile/mobile.go | 38 ++++++++++++++++----------------- src/yggdrasil/core.go | 5 ++++- src/yggdrasil/session.go | 4 +++- 5 files changed, 58 insertions(+), 45 deletions(-) diff --git a/build b/build index 127af75..f76ee7b 100755 --- a/build +++ b/build @@ -28,10 +28,16 @@ fi 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/mobile + gomobile bind -target ios -tags mobile -ldflags="$LDFLAGS $STRIP" -gcflags="$GCFLAGS" \ + github.com/yggdrasil-network/yggdrasil-go/src/yggdrasil \ + github.com/yggdrasil-network/yggdrasil-go/src/mobile \ + github.com/yggdrasil-network/yggdrasil-go/src/config 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/mobile + gomobile bind -target android -tags mobile -ldflags="$LDFLAGS $STRIP" -gcflags="$GCFLAGS" \ + github.com/yggdrasil-network/yggdrasil-go/src/yggdrasil \ + github.com/yggdrasil-network/yggdrasil-go/src/mobile \ + github.com/yggdrasil-network/yggdrasil-go/src/config else for CMD in `ls cmd/` ; do echo "Building: $CMD" diff --git a/src/dummy/dummy.go b/src/dummy/dummy.go index a199eee..ca6bb7b 100644 --- a/src/dummy/dummy.go +++ b/src/dummy/dummy.go @@ -2,56 +2,60 @@ package dummy import ( "github.com/gologme/log" + "github.com/yggdrasil-network/yggdrasil-go/src/address" "github.com/yggdrasil-network/yggdrasil-go/src/config" + "github.com/yggdrasil-network/yggdrasil-go/src/defaults" "github.com/yggdrasil-network/yggdrasil-go/src/util" "github.com/yggdrasil-network/yggdrasil-go/src/yggdrasil" ) +// DummyAdapter is a non-specific adapter that is used by the mobile APIs. +// You can also use it to send or receive custom traffic over Yggdrasil. type DummyAdapter struct { yggdrasil.Adapter - send chan<- []byte - recv <-chan []byte - reject <-chan yggdrasil.RejectedPacket } -// Init initialises the TUN/TAP adapter. +// Init initialises the dummy adapter. func (m *DummyAdapter) Init(config *config.NodeState, log *log.Logger, send chan<- []byte, recv <-chan []byte, reject <-chan yggdrasil.RejectedPacket) { m.Adapter.Init(config, log, send, recv, reject) } -// Name returns the name of the adapter, e.g. "tun0". On Windows, this may -// return a canonical adapter name instead. +// Name returns the name of the adapter. This is always "dummy" for dummy +// adapters. func (m *DummyAdapter) Name() string { return "dummy" } -// MTU gets the adapter's MTU. This can range between 1280 and 65535, although -// the maximum value is determined by your platform. The returned value will -// never exceed that of MaximumMTU(). +// MTU gets the adapter's MTU. This returns your platform's maximum MTU for +// dummy adapters. func (m *DummyAdapter) MTU() int { - return 65535 + return defaults.GetDefaults().MaximumIfMTU } -// IsTAP returns true if the adapter is a TAP adapter (Layer 2) or false if it -// is a TUN adapter (Layer 3). +// IsTAP always returns false for dummy adapters. func (m *DummyAdapter) IsTAP() bool { return false } -// 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. +// Recv waits for and returns for a packet from the router. func (m *DummyAdapter) Recv() ([]byte, error) { - packet := <-m.recv + packet := <-m.Adapter.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. +// Send a packet to the router. func (m *DummyAdapter) Send(buf []byte) error { packet := append(util.GetBytes(), buf[:]...) - m.send <- packet + m.Adapter.Send <- packet + return nil +} + +// Start is not implemented for dummy adapters. +func (m *DummyAdapter) Start(address.Address, address.Subnet) error { + return nil +} + +// Close is not implemented for dummy adapters. +func (m *DummyAdapter) Close() error { return nil } diff --git a/src/mobile/mobile.go b/src/mobile/mobile.go index ecc4d65..5eec96e 100644 --- a/src/mobile/mobile.go +++ b/src/mobile/mobile.go @@ -15,14 +15,15 @@ import ( "github.com/yggdrasil-network/yggdrasil-go/src/yggdrasil" ) -// Yggdrasil's mobile package is meant to "plug the gap" for mobile support, as +// Yggdrasil mobile package is meant to "plug the gap" for mobile support, as // Gomobile will not create headers for Swift/Obj-C etc if they have complex // (non-native) types. Therefore for iOS we will expose some nice simple // functions. Note that in the case of iOS we handle reading/writing to/from TUN // in Swift therefore we use the "dummy" TUN interface instead. type Yggdrasil struct { - core *yggdrasil.Core - multicast *multicast.Multicast + core yggdrasil.Core + multicast multicast.Multicast + log MobileLogger dummy.DummyAdapter } @@ -47,10 +48,7 @@ func (m *Yggdrasil) addStaticPeers(cfg *config.NodeConfig) { // StartAutoconfigure starts a node with a randomly generated config func (m *Yggdrasil) StartAutoconfigure() error { - m.core = &yggdrasil.Core{} - //m.Adapter = dummy.DummyAdapter{} - mobilelog := MobileLogger{} - logger := log.New(mobilelog, "", 0) + logger := log.New(m.log, "", 0) nc := config.GenerateConfig() nc.IfName = "dummy" nc.AdminListen = "tcp://localhost:9001" @@ -58,13 +56,15 @@ func (m *Yggdrasil) StartAutoconfigure() error { if hostname, err := os.Hostname(); err == nil { nc.NodeInfo = map[string]interface{}{"name": hostname} } - m.core.SetRouterAdapter(&m) + if err := m.core.SetRouterAdapter(m); err != nil { + logger.Errorln("An error occured setting router adapter:", err) + return err + } state, err := m.core.Start(nc, logger) if err != nil { return err } - // Start the multicast interface - m.multicast.Init(m.core, state, logger, nil) + m.multicast.Init(&m.core, state, logger, nil) if err := m.multicast.Start(); err != nil { logger.Errorln("An error occurred starting multicast:", err) } @@ -75,10 +75,7 @@ func (m *Yggdrasil) StartAutoconfigure() error { // StartJSON starts a node with the given JSON config. You can get JSON config // (rather than HJSON) by using the GenerateConfigJSON() function func (m *Yggdrasil) StartJSON(configjson []byte) error { - m.core = &yggdrasil.Core{} - //m.Adapter = dummy.DummyAdapter{} - mobilelog := MobileLogger{} - logger := log.New(mobilelog, "", 0) + logger := log.New(m.log, "", 0) nc := config.GenerateConfig() var dat map[string]interface{} if err := hjson.Unmarshal(configjson, &dat); err != nil { @@ -88,13 +85,15 @@ func (m *Yggdrasil) StartJSON(configjson []byte) error { return err } nc.IfName = "dummy" - m.core.SetRouterAdapter(&m) + if err := m.core.SetRouterAdapter(m); err != nil { + logger.Errorln("An error occured setting router adapter:", err) + return err + } state, err := m.core.Start(nc, logger) if err != nil { return err } - // Start the multicast interface - m.multicast.Init(m.core, state, logger, nil) + m.multicast.Init(&m.core, state, logger, nil) if err := m.multicast.Start(); err != nil { logger.Errorln("An error occurred starting multicast:", err) } @@ -102,7 +101,7 @@ func (m *Yggdrasil) StartJSON(configjson []byte) error { return nil } -// Stops the mobile Yggdrasil instance +// Stop the mobile Yggdrasil instance func (m *Yggdrasil) Stop() error { m.core.Stop() if err := m.Stop(); err != nil { @@ -117,9 +116,8 @@ func GenerateConfigJSON() []byte { nc.IfName = "dummy" if json, err := json.Marshal(nc); err == nil { return json - } else { - return nil } + return nil } // GetAddressString gets the node's IPv6 address diff --git a/src/yggdrasil/core.go b/src/yggdrasil/core.go index fcee124..037ef09 100644 --- a/src/yggdrasil/core.go +++ b/src/yggdrasil/core.go @@ -2,6 +2,7 @@ package yggdrasil import ( "encoding/hex" + "errors" "io/ioutil" "net" "time" @@ -170,13 +171,15 @@ func BuildVersion() string { // the router. The adapter must implement the standard // adapter.adapterImplementation interface and should extend the adapter.Adapter // struct. -func (c *Core) SetRouterAdapter(adapter interface{}) { +func (c *Core) SetRouterAdapter(adapter interface{}) error { // We do this because adapterImplementation is not a valid type for the // gomobile bindings so we just ask for a generic interface and try to cast it // to adapterImplementation instead if a, ok := adapter.(adapterImplementation); ok { c.router.adapter = a + return nil } + return errors.New("unsuitable adapter") } // Start starts up Yggdrasil using the provided config.NodeConfig, and outputs diff --git a/src/yggdrasil/session.go b/src/yggdrasil/session.go index 8deff95..74255c0 100644 --- a/src/yggdrasil/session.go +++ b/src/yggdrasil/session.go @@ -277,7 +277,9 @@ func (ss *sessions) createSession(theirPermKey *crypto.BoxPubKey) *sessionInfo { sinfo.mySesPriv = *priv sinfo.myNonce = *crypto.NewBoxNonce() sinfo.theirMTU = 1280 - sinfo.myMTU = uint16(ss.core.router.adapter.MTU()) + if ss.core.router.adapter != nil { + sinfo.myMTU = uint16(ss.core.router.adapter.MTU()) + } now := time.Now() sinfo.time = now sinfo.mtuTime = now