Give nodeconfig to tun

This commit is contained in:
Neil Alexander 2019-01-14 14:25:52 +00:00
parent 738a9da796
commit aed3c7e784
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944
3 changed files with 39 additions and 13 deletions

View File

@ -3,9 +3,10 @@ package yggdrasil
// Defines the minimum required struct members for an adapter type (this is // Defines the minimum required struct members for an adapter type (this is
// now the base type for tunAdapter in tun.go) // now the base type for tunAdapter in tun.go)
type Adapter struct { type Adapter struct {
core *Core core *Core
send chan<- []byte send chan<- []byte
recv <-chan []byte recv <-chan []byte
reconfigure chan chan error
} }
// Initialises the adapter. // Initialises the adapter.
@ -13,4 +14,5 @@ func (adapter *Adapter) init(core *Core, send chan<- []byte, recv <-chan []byte)
adapter.core = core adapter.core = core
adapter.send = send adapter.send = send
adapter.recv = recv adapter.recv = recv
adapter.reconfigure = make(chan chan error, 1)
} }

View File

@ -2,7 +2,6 @@ package yggdrasil
import ( import (
"encoding/hex" "encoding/hex"
"fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"net" "net"
@ -110,12 +109,13 @@ func (c *Core) UpdateConfig(config *config.NodeConfig) {
components := []chan chan error{ components := []chan chan error{
c.admin.reconfigure, c.admin.reconfigure,
//c.searches.reconfigure, c.searches.reconfigure,
//c.dht.reconfigure, c.dht.reconfigure,
//c.sessions.reconfigure, c.sessions.reconfigure,
//c.peers.reconfigure, c.peers.reconfigure,
//c.router.reconfigure, c.router.reconfigure,
//c.switchTable.reconfigure, c.router.tun.reconfigure,
c.switchTable.reconfigure,
c.tcp.reconfigure, c.tcp.reconfigure,
c.multicast.reconfigure, c.multicast.reconfigure,
} }
@ -240,8 +240,7 @@ func (c *Core) Start(nc *config.NodeConfig, log *log.Logger) error {
return err return err
} }
ip := net.IP(c.router.addr[:]).String() if err := c.router.tun.start(); err != nil {
if err := c.router.tun.start(nc.IfName, nc.IfTAPMode, fmt.Sprintf("%s/%d", ip, 8*len(address.GetPrefix())-1), nc.IfMTU); err != nil {
c.log.Println("Failed to start TUN/TAP") c.log.Println("Failed to start TUN/TAP")
return err return err
} }

View File

@ -5,6 +5,8 @@ package yggdrasil
import ( import (
"bytes" "bytes"
"errors" "errors"
"fmt"
"net"
"sync" "sync"
"time" "time"
@ -42,11 +44,34 @@ func getSupportedMTU(mtu int) int {
func (tun *tunAdapter) init(core *Core, send chan<- []byte, recv <-chan []byte) { func (tun *tunAdapter) init(core *Core, send chan<- []byte, recv <-chan []byte) {
tun.Adapter.init(core, send, recv) tun.Adapter.init(core, send, recv)
tun.icmpv6.init(tun) tun.icmpv6.init(tun)
go func() {
for {
select {
case e := <-tun.reconfigure:
tun.core.configMutex.RLock()
updated := tun.core.config.IfName != tun.core.configOld.IfName ||
tun.core.config.IfTAPMode != tun.core.configOld.IfTAPMode ||
tun.core.config.IfMTU != tun.core.configOld.IfMTU
tun.core.configMutex.RUnlock()
if updated {
e <- nil
} else {
e <- nil
}
}
}
}()
} }
// 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() error {
tun.core.configMutex.RLock()
ifname := tun.core.config.IfName
iftapmode := tun.core.config.IfTAPMode
addr := fmt.Sprintf("%s/%d", net.IP(tun.core.router.addr[:]).String(), 8*len(address.GetPrefix())-1)
mtu := tun.core.config.IfMTU
tun.core.configMutex.RUnlock()
if ifname != "none" { if ifname != "none" {
if err := tun.setup(ifname, iftapmode, addr, mtu); err != nil { if err := tun.setup(ifname, iftapmode, addr, mtu); err != nil {
return err return err