From 4ff3db2309c61aff8c3aa3c263517ac9bf92c51f Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Wed, 2 Jan 2019 18:05:54 +0000 Subject: [PATCH] Add dummy tun, helper functions --- src/yggdrasil/ios.go | 51 --------------------------------- src/yggdrasil/mobile.go | 56 +++++++++++++++++++++++++++++++++++++ src/yggdrasil/tun.go | 10 ++++--- src/yggdrasil/tun_darwin.go | 2 ++ src/yggdrasil/tun_dummy.go | 19 +++++++++++++ src/yggdrasil/tun_linux.go | 2 ++ src/yggdrasil/tun_other.go | 2 +- 7 files changed, 86 insertions(+), 56 deletions(-) delete mode 100644 src/yggdrasil/ios.go create mode 100644 src/yggdrasil/mobile.go create mode 100644 src/yggdrasil/tun_dummy.go diff --git a/src/yggdrasil/ios.go b/src/yggdrasil/ios.go deleted file mode 100644 index 5f32a85c..00000000 --- a/src/yggdrasil/ios.go +++ /dev/null @@ -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() -} diff --git a/src/yggdrasil/mobile.go b/src/yggdrasil/mobile.go new file mode 100644 index 00000000..9142c36c --- /dev/null +++ b/src/yggdrasil/mobile.go @@ -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 +} diff --git a/src/yggdrasil/tun.go b/src/yggdrasil/tun.go index 8ed53332..8c0f91d5 100644 --- a/src/yggdrasil/tun.go +++ b/src/yggdrasil/tun.go @@ -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 // the read/write goroutines to handle packets on that interface. func (tun *tunAdapter) start(ifname string, iftapmode bool, addr string, mtu int) error { - if ifname == "none" { - return nil + if ifname != "none" { + if err := tun.setup(ifname, iftapmode, addr, mtu); err != nil { + return err + } } - if err := tun.setup(ifname, iftapmode, addr, mtu); err != nil { - return err + if ifname == "none" || ifname == "dummy" { + return nil } tun.mutex.Lock() tun.isOpen = true diff --git a/src/yggdrasil/tun_darwin.go b/src/yggdrasil/tun_darwin.go index 943468e6..828c01ea 100644 --- a/src/yggdrasil/tun_darwin.go +++ b/src/yggdrasil/tun_darwin.go @@ -1,3 +1,5 @@ +// +build !mobile + package yggdrasil // The darwin platform specific tun parts diff --git a/src/yggdrasil/tun_dummy.go b/src/yggdrasil/tun_dummy.go new file mode 100644 index 00000000..234ab1de --- /dev/null +++ b/src/yggdrasil/tun_dummy.go @@ -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 +} diff --git a/src/yggdrasil/tun_linux.go b/src/yggdrasil/tun_linux.go index 7a7c9cb7..8ccdd30b 100644 --- a/src/yggdrasil/tun_linux.go +++ b/src/yggdrasil/tun_linux.go @@ -1,3 +1,5 @@ +// +build !mobile + package yggdrasil // The linux platform specific tun parts diff --git a/src/yggdrasil/tun_other.go b/src/yggdrasil/tun_other.go index 625f9cd5..22058c11 100644 --- a/src/yggdrasil/tun_other.go +++ b/src/yggdrasil/tun_other.go @@ -1,4 +1,4 @@ -// +build !linux,!darwin,!windows,!openbsd,!freebsd,!netbsd +// +build !linux,!darwin,!windows,!openbsd,!freebsd,!netbsd,!mobile package yggdrasil