mirror of
				https://github.com/yggdrasil-network/yggdrasil-go.git
				synced 2025-11-04 11:15:07 +03:00 
			
		
		
		
	Fix session bug, fix dummy adapter, fix mobile framework builds
This commit is contained in:
		
							parent
							
								
									047717abf2
								
							
						
					
					
						commit
						58f5cc88d0
					
				
					 5 changed files with 58 additions and 45 deletions
				
			
		| 
						 | 
				
			
			@ -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
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue