5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-12-22 18:45:40 +00:00

Add setTunTap

This commit is contained in:
Neil Alexander 2018-02-28 15:15:57 +00:00
parent 2b48fd1fce
commit fdd32b9571
2 changed files with 55 additions and 2 deletions

View File

@ -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{"<peer>"}, 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{"<ifname|auto|none>", "[<tun|tap>]", "[<mtu>]"}, 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

View File

@ -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() {