Add dummy tun, helper functions

This commit is contained in:
Neil Alexander 2019-01-02 18:05:54 +00:00
parent 53aeca8fa2
commit 4ff3db2309
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944
7 changed files with 86 additions and 56 deletions

View File

@ -1,51 +0,0 @@
// +build mobile
package yggdrasil
import (
"log"
"os"
"regexp"
"github.com/yggdrasil-network/yggdrasil-go/src/config"
)
// This file is meant to "plug the gap" for Gomobile support, as Gomobile
// will not create headers for Swift/Obj-C if they have complex (read: non-
// native) types. Therefore for iOS we will expose some nice simple functions
// to do what we need to do.
func (c *Core) StartAutoconfigure() error {
logger := log.New(os.Stdout, "", 0)
//logger.Println("Created logger")
//c := Core{}
//logger.Println("Created Core")
nc := config.GenerateConfig(true)
//logger.Println("Generated config")
nc.IfName = "none"
nc.AdminListen = "tcp://[::]:9001"
nc.Peers = []string{}
//logger.Println("Set some config options")
ifceExpr, err := regexp.Compile(".*")
if err == nil {
c.ifceExpr = append(c.ifceExpr, ifceExpr)
}
//logger.Println("Added multicast interface")
if err := c.Start(nc, logger); err != nil {
return err
}
//logger.Println("Started")
address := c.GetAddress()
subnet := c.GetSubnet()
logger.Printf("Your IPv6 address is %s", address.String())
logger.Printf("Your IPv6 subnet is %s", subnet.String())
return nil
}
func (c *Core) GetAddressString() string {
return c.GetAddress().String()
}
func (c *Core) GetSubetString() string {
return c.GetSubnet().String()
}

56
src/yggdrasil/mobile.go Normal file
View File

@ -0,0 +1,56 @@
// +build mobile
package yggdrasil
import (
"log"
"os"
"regexp"
"github.com/yggdrasil-network/yggdrasil-go/src/config"
"github.com/yggdrasil-network/yggdrasil-go/src/util"
)
// This file 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.
func (c *Core) StartAutoconfigure() error {
logger := log.New(os.Stdout, "", 0)
nc := config.GenerateConfig(true)
nc.IfName = "dummy"
nc.AdminListen = "tcp://[::]:9001"
nc.Peers = []string{}
if hostname, err := os.Hostname(); err == nil {
nc.NodeInfo = map[string]interface{}{"name": hostname}
}
ifceExpr, err := regexp.Compile(".*")
if err == nil {
c.ifceExpr = append(c.ifceExpr, ifceExpr)
}
if err := c.Start(nc, logger); err != nil {
return err
}
return nil
}
func (c *Core) GetAddressString() string {
return c.GetAddress().String()
}
func (c *Core) GetSubnetString() string {
return c.GetSubnet().String()
}
func (c *Core) RouterRecvPacket() ([]byte, error) {
packet := <-c.router.tun.recv
return packet, nil
}
func (c *Core) RouterSendPacket(buf []byte) error {
packet := append(util.GetBytes(), buf[:]...)
c.router.tun.send <- packet
return nil
}

View File

@ -47,11 +47,13 @@ func (tun *tunAdapter) init(core *Core, send chan<- []byte, recv <-chan []byte)
// Starts the setup process for the TUN/TAP adapter, and if successful, starts // Starts the setup process for the TUN/TAP adapter, and if successful, starts
// the read/write goroutines to handle packets on that interface. // the read/write goroutines to handle packets on that interface.
func (tun *tunAdapter) start(ifname string, iftapmode bool, addr string, mtu int) error { func (tun *tunAdapter) start(ifname string, iftapmode bool, addr string, mtu int) error {
if ifname == "none" { if ifname != "none" {
return nil if err := tun.setup(ifname, iftapmode, addr, mtu); err != nil {
return err
}
} }
if err := tun.setup(ifname, iftapmode, addr, mtu); err != nil { if ifname == "none" || ifname == "dummy" {
return err return nil
} }
tun.mutex.Lock() tun.mutex.Lock()
tun.isOpen = true tun.isOpen = true

View File

@ -1,3 +1,5 @@
// +build !mobile
package yggdrasil package yggdrasil
// The darwin platform specific tun parts // The darwin platform specific tun parts

View File

@ -0,0 +1,19 @@
// +build mobile
package yggdrasil
// This is to catch unsupported platforms
// If your platform supports tun devices, you could try configuring it manually
// Creates the TUN/TAP adapter, if supported by the Water library. Note that
// no guarantees are made at this point on an unsupported platform.
func (tun *tunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int) error {
tun.mtu = getSupportedMTU(mtu)
return tun.setupAddress(addr)
}
// We don't know how to set the IPv6 address on an unknown platform, therefore
// write about it to stdout and don't try to do anything further.
func (tun *tunAdapter) setupAddress(addr string) error {
return nil
}

View File

@ -1,3 +1,5 @@
// +build !mobile
package yggdrasil package yggdrasil
// The linux platform specific tun parts // The linux platform specific tun parts

View File

@ -1,4 +1,4 @@
// +build !linux,!darwin,!windows,!openbsd,!freebsd,!netbsd // +build !linux,!darwin,!windows,!openbsd,!freebsd,!netbsd,!mobile
package yggdrasil package yggdrasil