5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-22 13:00:47 +00:00

Fix session bug, fix dummy adapter, fix mobile framework builds

This commit is contained in:
Neil Alexander 2019-04-01 19:59:50 +01:00
parent 047717abf2
commit 58f5cc88d0
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944
5 changed files with 58 additions and 45 deletions

10
build
View File

@ -28,10 +28,16 @@ fi
if [ $IOS ]; then if [ $IOS ]; then
echo "Building framework for iOS" 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 elif [ $ANDROID ]; then
echo "Building aar for Android" 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 else
for CMD in `ls cmd/` ; do for CMD in `ls cmd/` ; do
echo "Building: $CMD" echo "Building: $CMD"

View File

@ -2,56 +2,60 @@ package dummy
import ( import (
"github.com/gologme/log" "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/config"
"github.com/yggdrasil-network/yggdrasil-go/src/defaults"
"github.com/yggdrasil-network/yggdrasil-go/src/util" "github.com/yggdrasil-network/yggdrasil-go/src/util"
"github.com/yggdrasil-network/yggdrasil-go/src/yggdrasil" "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 { type DummyAdapter struct {
yggdrasil.Adapter 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) { 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) m.Adapter.Init(config, log, send, recv, reject)
} }
// Name returns the name of the adapter, e.g. "tun0". On Windows, this may // Name returns the name of the adapter. This is always "dummy" for dummy
// return a canonical adapter name instead. // adapters.
func (m *DummyAdapter) Name() string { func (m *DummyAdapter) Name() string {
return "dummy" return "dummy"
} }
// MTU gets the adapter's MTU. This can range between 1280 and 65535, although // MTU gets the adapter's MTU. This returns your platform's maximum MTU for
// the maximum value is determined by your platform. The returned value will // dummy adapters.
// never exceed that of MaximumMTU().
func (m *DummyAdapter) MTU() int { 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 // IsTAP always returns false for dummy adapters.
// is a TUN adapter (Layer 3).
func (m *DummyAdapter) IsTAP() bool { func (m *DummyAdapter) IsTAP() bool {
return false return false
} }
// Wait for a packet from the router. You will use this when implementing a // Recv waits for and returns for a packet from the router.
// 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 (m *DummyAdapter) Recv() ([]byte, error) { func (m *DummyAdapter) Recv() ([]byte, error) {
packet := <-m.recv packet := <-m.Adapter.Recv
return packet, nil return packet, nil
} }
// Send a packet to the router. You will use this when implementing a // Send a packet to the router.
// 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 (m *DummyAdapter) Send(buf []byte) error { func (m *DummyAdapter) Send(buf []byte) error {
packet := append(util.GetBytes(), buf[:]...) 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 return nil
} }

View File

@ -15,14 +15,15 @@ import (
"github.com/yggdrasil-network/yggdrasil-go/src/yggdrasil" "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 // 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 // (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 // 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. // in Swift therefore we use the "dummy" TUN interface instead.
type Yggdrasil struct { type Yggdrasil struct {
core *yggdrasil.Core core yggdrasil.Core
multicast *multicast.Multicast multicast multicast.Multicast
log MobileLogger
dummy.DummyAdapter dummy.DummyAdapter
} }
@ -47,10 +48,7 @@ func (m *Yggdrasil) addStaticPeers(cfg *config.NodeConfig) {
// StartAutoconfigure starts a node with a randomly generated config // StartAutoconfigure starts a node with a randomly generated config
func (m *Yggdrasil) StartAutoconfigure() error { func (m *Yggdrasil) StartAutoconfigure() error {
m.core = &yggdrasil.Core{} logger := log.New(m.log, "", 0)
//m.Adapter = dummy.DummyAdapter{}
mobilelog := MobileLogger{}
logger := log.New(mobilelog, "", 0)
nc := config.GenerateConfig() nc := config.GenerateConfig()
nc.IfName = "dummy" nc.IfName = "dummy"
nc.AdminListen = "tcp://localhost:9001" nc.AdminListen = "tcp://localhost:9001"
@ -58,13 +56,15 @@ func (m *Yggdrasil) StartAutoconfigure() error {
if hostname, err := os.Hostname(); err == nil { if hostname, err := os.Hostname(); err == nil {
nc.NodeInfo = map[string]interface{}{"name": hostname} 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) state, err := m.core.Start(nc, logger)
if err != nil { if err != nil {
return err 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 { if err := m.multicast.Start(); err != nil {
logger.Errorln("An error occurred starting multicast:", err) 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 // StartJSON starts a node with the given JSON config. You can get JSON config
// (rather than HJSON) by using the GenerateConfigJSON() function // (rather than HJSON) by using the GenerateConfigJSON() function
func (m *Yggdrasil) StartJSON(configjson []byte) error { func (m *Yggdrasil) StartJSON(configjson []byte) error {
m.core = &yggdrasil.Core{} logger := log.New(m.log, "", 0)
//m.Adapter = dummy.DummyAdapter{}
mobilelog := MobileLogger{}
logger := log.New(mobilelog, "", 0)
nc := config.GenerateConfig() nc := config.GenerateConfig()
var dat map[string]interface{} var dat map[string]interface{}
if err := hjson.Unmarshal(configjson, &dat); err != nil { if err := hjson.Unmarshal(configjson, &dat); err != nil {
@ -88,13 +85,15 @@ func (m *Yggdrasil) StartJSON(configjson []byte) error {
return err return err
} }
nc.IfName = "dummy" 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) state, err := m.core.Start(nc, logger)
if err != nil { if err != nil {
return err 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 { if err := m.multicast.Start(); err != nil {
logger.Errorln("An error occurred starting multicast:", err) logger.Errorln("An error occurred starting multicast:", err)
} }
@ -102,7 +101,7 @@ func (m *Yggdrasil) StartJSON(configjson []byte) error {
return nil return nil
} }
// Stops the mobile Yggdrasil instance // Stop the mobile Yggdrasil instance
func (m *Yggdrasil) Stop() error { func (m *Yggdrasil) Stop() error {
m.core.Stop() m.core.Stop()
if err := m.Stop(); err != nil { if err := m.Stop(); err != nil {
@ -117,9 +116,8 @@ func GenerateConfigJSON() []byte {
nc.IfName = "dummy" nc.IfName = "dummy"
if json, err := json.Marshal(nc); err == nil { if json, err := json.Marshal(nc); err == nil {
return json return json
} else {
return nil
} }
return nil
} }
// GetAddressString gets the node's IPv6 address // GetAddressString gets the node's IPv6 address

View File

@ -2,6 +2,7 @@ package yggdrasil
import ( import (
"encoding/hex" "encoding/hex"
"errors"
"io/ioutil" "io/ioutil"
"net" "net"
"time" "time"
@ -170,13 +171,15 @@ func BuildVersion() string {
// the router. The adapter must implement the standard // the router. The adapter must implement the standard
// adapter.adapterImplementation interface and should extend the adapter.Adapter // adapter.adapterImplementation interface and should extend the adapter.Adapter
// struct. // 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 // 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 // gomobile bindings so we just ask for a generic interface and try to cast it
// to adapterImplementation instead // to adapterImplementation instead
if a, ok := adapter.(adapterImplementation); ok { if a, ok := adapter.(adapterImplementation); ok {
c.router.adapter = a c.router.adapter = a
return nil
} }
return errors.New("unsuitable adapter")
} }
// Start starts up Yggdrasil using the provided config.NodeConfig, and outputs // Start starts up Yggdrasil using the provided config.NodeConfig, and outputs

View File

@ -277,7 +277,9 @@ func (ss *sessions) createSession(theirPermKey *crypto.BoxPubKey) *sessionInfo {
sinfo.mySesPriv = *priv sinfo.mySesPriv = *priv
sinfo.myNonce = *crypto.NewBoxNonce() sinfo.myNonce = *crypto.NewBoxNonce()
sinfo.theirMTU = 1280 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() now := time.Now()
sinfo.time = now sinfo.time = now
sinfo.mtuTime = now sinfo.mtuTime = now