mirror of
				https://github.com/yggdrasil-network/yggdrasil-go.git
				synced 2025-11-04 11:15:07 +03:00 
			
		
		
		
	Add dummy tun, helper functions
This commit is contained in:
		
							parent
							
								
									53aeca8fa2
								
							
						
					
					
						commit
						4ff3db2309
					
				
					 7 changed files with 86 additions and 56 deletions
				
			
		| 
						 | 
					@ -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
									
								
							
							
						
						
									
										56
									
								
								src/yggdrasil/mobile.go
									
										
									
									
									
										Normal 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
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -47,12 +47,14 @@ 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 {
 | 
							if err := tun.setup(ifname, iftapmode, addr, mtu); err != nil {
 | 
				
			||||||
			return err
 | 
								return err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if ifname == "none" || ifname == "dummy" {
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	tun.mutex.Lock()
 | 
						tun.mutex.Lock()
 | 
				
			||||||
	tun.isOpen = true
 | 
						tun.isOpen = true
 | 
				
			||||||
	tun.mutex.Unlock()
 | 
						tun.mutex.Unlock()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,3 +1,5 @@
 | 
				
			||||||
 | 
					// +build !mobile
 | 
				
			||||||
 | 
					
 | 
				
			||||||
package yggdrasil
 | 
					package yggdrasil
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// The darwin platform specific tun parts
 | 
					// The darwin platform specific tun parts
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										19
									
								
								src/yggdrasil/tun_dummy.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								src/yggdrasil/tun_dummy.go
									
										
									
									
									
										Normal 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
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -1,3 +1,5 @@
 | 
				
			||||||
 | 
					// +build !mobile
 | 
				
			||||||
 | 
					
 | 
				
			||||||
package yggdrasil
 | 
					package yggdrasil
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// The linux platform specific tun parts
 | 
					// The linux platform specific tun parts
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,4 +1,4 @@
 | 
				
			||||||
// +build !linux,!darwin,!windows,!openbsd,!freebsd,!netbsd
 | 
					// +build !linux,!darwin,!windows,!openbsd,!freebsd,!netbsd,!mobile
 | 
				
			||||||
 | 
					
 | 
				
			||||||
package yggdrasil
 | 
					package yggdrasil
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue