From 2b48fd1fcee7099398f5f22ab1ef7c7f43a4e814 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Wed, 28 Feb 2018 13:43:06 +0000 Subject: [PATCH 1/3] Add addPeer to admin socket --- src/yggdrasil/admin.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/yggdrasil/admin.go b/src/yggdrasil/admin.go index ab0e33e..f24da17 100644 --- a/src/yggdrasil/admin.go +++ b/src/yggdrasil/admin.go @@ -55,6 +55,13 @@ func (a *admin) init(c *Core, listenaddr string) { a.addHandler("getSessions", nil, func(out *[]byte, _ ...string) { *out = []byte(a.printInfos(a.getData_getSessions())) }) + a.addHandler("addPeer", nil, func(out *[]byte, saddr ...string) { + if a.addPeer(saddr[0]) == nil { + *out = []byte("Adding peer: " + saddr[0] + "\n") + } else { + *out = []byte("Failed to add peer: " + saddr[0] + "\n") + } + }) go a.listen() } @@ -143,6 +150,39 @@ func (a *admin) printInfos(infos []admin_nodeInfo) string { return strings.Join(out, "\n") } +func (a *admin) addPeer(p string) error { + pAddr := p + if p[:4] == "tcp:" || p[:4] == "udp:" { + pAddr = p[4:] + } + switch { + case len(p) >= 4 && p[:4] == "udp:": + // Connect to peer over UDP + udpAddr, err := net.ResolveUDPAddr("udp", pAddr) + if err != nil { + return err + } + var addr connAddr + addr.fromUDPAddr(udpAddr) + a.core.udp.mutex.RLock() + _, isIn := a.core.udp.conns[addr] + a.core.udp.mutex.RUnlock() + if !isIn { + a.core.udp.sendKeys(addr) + } + return nil + case len(p) >= 4 && p[:4] == "tcp:": + default: + // Connect to peer over TCP + _, err := net.ResolveTCPAddr("tcp", pAddr) + if err != nil { + return err + } + a.core.tcp.call(p) + } + return nil +} + func (a *admin) getData_getSelf() *admin_nodeInfo { table := a.core.switchTable.table.Load().(lookupTable) addr := a.core.router.addr From fdd32b957109a5fe26b0f4f5530e4a47acd12d66 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Wed, 28 Feb 2018 15:15:57 +0000 Subject: [PATCH 2/3] Add setTunTap --- src/yggdrasil/admin.go | 54 +++++++++++++++++++++++++++++++++++++++++- src/yggdrasil/tun.go | 3 ++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/yggdrasil/admin.go b/src/yggdrasil/admin.go index f24da17..ebbe8b5 100644 --- a/src/yggdrasil/admin.go +++ b/src/yggdrasil/admin.go @@ -6,6 +6,7 @@ import "bytes" import "fmt" import "sort" import "strings" +import "strconv" // TODO? Make all of this JSON // TODO: Add authentication @@ -55,13 +56,47 @@ func (a *admin) init(c *Core, listenaddr string) { a.addHandler("getSessions", nil, func(out *[]byte, _ ...string) { *out = []byte(a.printInfos(a.getData_getSessions())) }) - a.addHandler("addPeer", nil, func(out *[]byte, saddr ...string) { + a.addHandler("addPeer", []string{""}, func(out *[]byte, saddr ...string) { if a.addPeer(saddr[0]) == nil { *out = []byte("Adding peer: " + saddr[0] + "\n") } else { *out = []byte("Failed to add peer: " + saddr[0] + "\n") } }) + a.addHandler("setTunTap", []string{"", "[]", "[]"}, func(out *[]byte, ifparams ...string) { + // Check parameters + if (ifparams[0] != "none" && len(ifparams) != 3) || + (ifparams[0] == "none" && len(ifparams) != 1) { + *out = []byte("Invalid number of parameters given\n") + return + } + // Set sane defaults + iftapmode := false + ifmtu := 1280 + var err error + if len(ifparams) > 1 { + // Is it a TAP adapter? + if ifparams[1] == "tap" { + iftapmode = true + } + // Make sure the MTU is sane + ifmtu, err = strconv.Atoi(ifparams[2]) + if err != nil || ifmtu < 1280 || ifmtu > 65535 { + ifmtu = 1280 + } + } + // Start the TUN adapter + if err := a.startTunWithMTU(ifparams[0], iftapmode, ifmtu); err != nil { + *out = []byte(fmt.Sprintf("Failed to set TUN: %v\n", err)) + } else { + info := admin_nodeInfo{ + {"Interface name", ifparams[0]}, + {"TAP mode", strconv.FormatBool(iftapmode)}, + {"MTU", strconv.Itoa(ifmtu)}, + } + *out = []byte(a.printInfos([]admin_nodeInfo{info})) + } + }) go a.listen() } @@ -183,6 +218,23 @@ func (a *admin) addPeer(p string) error { return nil } +func (a *admin) startTunWithMTU(ifname string, iftapmode bool, ifmtu int) error { + // Close the TUN first if open + _ = a.core.tun.close() + // Then reconfigure and start it + addr := a.core.router.addr + straddr := fmt.Sprintf("%s/%v", net.IP(addr[:]).String(), 8*len(address_prefix)) + if ifname != "none" { + err := a.core.tun.setup(ifname, iftapmode, straddr, ifmtu) + if err != nil { + return err + } + go a.core.tun.read() + } + go a.core.tun.write() + return nil +} + func (a *admin) getData_getSelf() *admin_nodeInfo { table := a.core.switchTable.table.Load().(lookupTable) addr := a.core.router.addr diff --git a/src/yggdrasil/tun.go b/src/yggdrasil/tun.go index bbe98d2..49acff6 100644 --- a/src/yggdrasil/tun.go +++ b/src/yggdrasil/tun.go @@ -66,7 +66,8 @@ func (tun *tunDevice) read() error { for { n, err := tun.iface.Read(buf) if err != nil { - panic(err) + // panic(err) + return err } o := 0 if tun.iface.IsTAP() { From ebc4eacee4bf1c7ceec9a43d0f65e48a57b39fed Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Wed, 28 Feb 2018 15:27:58 +0000 Subject: [PATCH 3/3] Allow sane defaults on setTunTap --- src/yggdrasil/admin.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/yggdrasil/admin.go b/src/yggdrasil/admin.go index ebbe8b5..9051d3f 100644 --- a/src/yggdrasil/admin.go +++ b/src/yggdrasil/admin.go @@ -64,21 +64,19 @@ func (a *admin) init(c *Core, listenaddr string) { } }) a.addHandler("setTunTap", []string{"", "[]", "[]"}, func(out *[]byte, ifparams ...string) { - // Check parameters - if (ifparams[0] != "none" && len(ifparams) != 3) || - (ifparams[0] == "none" && len(ifparams) != 1) { - *out = []byte("Invalid number of parameters given\n") - return - } // Set sane defaults iftapmode := false ifmtu := 1280 var err error + // Check we have enough params for TAP mode if len(ifparams) > 1 { // Is it a TAP adapter? if ifparams[1] == "tap" { iftapmode = true } + } + // Check we have enough params for MTU + if len(ifparams) > 2 { // Make sure the MTU is sane ifmtu, err = strconv.Atoi(ifparams[2]) if err != nil || ifmtu < 1280 || ifmtu > 65535 {