mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2024-11-23 10:05:17 +00:00
Refactor TUN setup (isolated config)
This commit is contained in:
parent
b1f61fb0a8
commit
a7d06e048a
@ -38,7 +38,6 @@ import (
|
|||||||
|
|
||||||
type node struct {
|
type node struct {
|
||||||
core *core.Core
|
core *core.Core
|
||||||
config *config.NodeConfig
|
|
||||||
tuntap *tuntap.TunAdapter
|
tuntap *tuntap.TunAdapter
|
||||||
multicast *multicast.Multicast
|
multicast *multicast.Multicast
|
||||||
admin *admin.AdminSocket
|
admin *admin.AdminSocket
|
||||||
@ -326,7 +325,7 @@ func run(args yggArgs, ctx context.Context, done chan struct{}) {
|
|||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
n := &node{config: cfg}
|
n := &node{}
|
||||||
|
|
||||||
// Setup the Yggdrasil node itself.
|
// Setup the Yggdrasil node itself.
|
||||||
{
|
{
|
||||||
@ -390,15 +389,22 @@ func run(args yggArgs, ctx context.Context, done chan struct{}) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start the TUN/TAP interface
|
// Setup the TUN module.
|
||||||
n.tuntap = &tuntap.TunAdapter{}
|
{
|
||||||
rwc := ipv6rwc.NewReadWriteCloser(n.core)
|
options := []tuntap.SetupOption{
|
||||||
if err := n.tuntap.Init(rwc, cfg, logger, nil); err != nil {
|
tuntap.InterfaceName(cfg.IfName),
|
||||||
logger.Errorln("An error occurred initialising TUN/TAP:", err)
|
tuntap.InterfaceMTU(cfg.IfMTU),
|
||||||
} else if err := n.tuntap.Start(); err != nil {
|
}
|
||||||
logger.Errorln("An error occurred starting TUN/TAP:", err)
|
rwc := ipv6rwc.NewReadWriteCloser(n.core)
|
||||||
|
n.tuntap, err = tuntap.New(rwc, logger, options...)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if n.admin != nil {
|
||||||
|
n.tuntap.SetupAdminHandlers(n.admin)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
n.tuntap.SetupAdminHandlers(n.admin)
|
|
||||||
// Make some nice output that tells us what our IPv6 address and subnet are.
|
// Make some nice output that tells us what our IPv6 address and subnet are.
|
||||||
// This is just logged to stdout for the user.
|
// This is just logged to stdout for the user.
|
||||||
address := n.core.Address()
|
address := n.core.Address()
|
||||||
|
@ -80,8 +80,8 @@ func (m *Multicast) _start() error {
|
|||||||
if len(m.config._interfaces) == 0 {
|
if len(m.config._interfaces) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
m.log.Infoln("Starting multicast module")
|
m.log.Debugln("Starting multicast module")
|
||||||
defer m.log.Infoln("Started multicast module")
|
defer m.log.Debugln("Started multicast module")
|
||||||
addr, err := net.ResolveUDPAddr("udp", string(m.config._groupAddr))
|
addr, err := net.ResolveUDPAddr("udp", string(m.config._groupAddr))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
20
src/tuntap/options.go
Normal file
20
src/tuntap/options.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package tuntap
|
||||||
|
|
||||||
|
func (m *TunAdapter) _applyOption(opt SetupOption) {
|
||||||
|
switch v := opt.(type) {
|
||||||
|
case InterfaceName:
|
||||||
|
m.config.name = v
|
||||||
|
case InterfaceMTU:
|
||||||
|
m.config.mtu = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type SetupOption interface {
|
||||||
|
isSetupOption()
|
||||||
|
}
|
||||||
|
|
||||||
|
type InterfaceName string
|
||||||
|
type InterfaceMTU uint64
|
||||||
|
|
||||||
|
func (a InterfaceName) isSetupOption() {}
|
||||||
|
func (a InterfaceMTU) isSetupOption() {}
|
@ -16,13 +16,12 @@ import (
|
|||||||
//"sync"
|
//"sync"
|
||||||
|
|
||||||
"github.com/Arceliar/phony"
|
"github.com/Arceliar/phony"
|
||||||
"github.com/gologme/log"
|
|
||||||
"golang.zx2c4.com/wireguard/tun"
|
"golang.zx2c4.com/wireguard/tun"
|
||||||
|
|
||||||
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
||||||
"github.com/yggdrasil-network/yggdrasil-go/src/config"
|
|
||||||
"github.com/yggdrasil-network/yggdrasil-go/src/defaults"
|
"github.com/yggdrasil-network/yggdrasil-go/src/defaults"
|
||||||
"github.com/yggdrasil-network/yggdrasil-go/src/ipv6rwc"
|
"github.com/yggdrasil-network/yggdrasil-go/src/ipv6rwc"
|
||||||
|
"github.com/yggdrasil-network/yggdrasil-go/src/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MTU uint16
|
type MTU uint16
|
||||||
@ -33,8 +32,7 @@ type MTU uint16
|
|||||||
// calling yggdrasil.Start().
|
// calling yggdrasil.Start().
|
||||||
type TunAdapter struct {
|
type TunAdapter struct {
|
||||||
rwc *ipv6rwc.ReadWriteCloser
|
rwc *ipv6rwc.ReadWriteCloser
|
||||||
config *config.NodeConfig
|
log util.Logger
|
||||||
log *log.Logger
|
|
||||||
addr address.Address
|
addr address.Address
|
||||||
subnet address.Subnet
|
subnet address.Subnet
|
||||||
mtu uint64
|
mtu uint64
|
||||||
@ -43,6 +41,10 @@ type TunAdapter struct {
|
|||||||
//mutex sync.RWMutex // Protects the below
|
//mutex sync.RWMutex // Protects the below
|
||||||
isOpen bool
|
isOpen bool
|
||||||
isEnabled bool // Used by the writer to drop sessionTraffic if not enabled
|
isEnabled bool // Used by the writer to drop sessionTraffic if not enabled
|
||||||
|
config struct {
|
||||||
|
name InterfaceName
|
||||||
|
mtu InterfaceMTU
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets the maximum supported MTU for the platform based on the defaults in
|
// Gets the maximum supported MTU for the platform based on the defaults in
|
||||||
@ -93,50 +95,39 @@ func MaximumMTU() uint64 {
|
|||||||
|
|
||||||
// Init initialises the TUN module. You must have acquired a Listener from
|
// Init initialises the TUN module. You must have acquired a Listener from
|
||||||
// the Yggdrasil core before this point and it must not be in use elsewhere.
|
// the Yggdrasil core before this point and it must not be in use elsewhere.
|
||||||
func (tun *TunAdapter) Init(rwc *ipv6rwc.ReadWriteCloser, config *config.NodeConfig, log *log.Logger, options interface{}) error {
|
func New(rwc *ipv6rwc.ReadWriteCloser, log util.Logger, opts ...SetupOption) (*TunAdapter, error) {
|
||||||
tun.rwc = rwc
|
tun := &TunAdapter{
|
||||||
tun.config = config
|
rwc: rwc,
|
||||||
tun.log = log
|
log: log,
|
||||||
return nil
|
}
|
||||||
}
|
for _, opt := range opts {
|
||||||
|
tun._applyOption(opt)
|
||||||
// Start the setup process for the TUN adapter. If successful, starts the
|
}
|
||||||
// reader actor to handle packets on that interface.
|
return tun, tun._start()
|
||||||
func (tun *TunAdapter) Start() error {
|
|
||||||
var err error
|
|
||||||
phony.Block(tun, func() {
|
|
||||||
err = tun._start()
|
|
||||||
})
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *TunAdapter) _start() error {
|
func (tun *TunAdapter) _start() error {
|
||||||
if tun.isOpen {
|
if tun.isOpen {
|
||||||
return errors.New("TUN module is already started")
|
return errors.New("TUN module is already started")
|
||||||
}
|
}
|
||||||
if tun.config == nil {
|
|
||||||
return errors.New("no configuration available to TUN")
|
|
||||||
}
|
|
||||||
tun.config.RLock()
|
|
||||||
defer tun.config.RUnlock()
|
|
||||||
tun.addr = tun.rwc.Address()
|
tun.addr = tun.rwc.Address()
|
||||||
tun.subnet = tun.rwc.Subnet()
|
tun.subnet = tun.rwc.Subnet()
|
||||||
addr := fmt.Sprintf("%s/%d", net.IP(tun.addr[:]).String(), 8*len(address.GetPrefix())-1)
|
addr := fmt.Sprintf("%s/%d", net.IP(tun.addr[:]).String(), 8*len(address.GetPrefix())-1)
|
||||||
if tun.config.IfName == "none" || tun.config.IfName == "dummy" {
|
if tun.config.name == "none" || tun.config.name == "dummy" {
|
||||||
tun.log.Debugln("Not starting TUN as ifname is none or dummy")
|
tun.log.Debugln("Not starting TUN as ifname is none or dummy")
|
||||||
tun.isEnabled = false
|
tun.isEnabled = false
|
||||||
go tun.write()
|
go tun.write()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
mtu := tun.config.IfMTU
|
mtu := uint64(tun.config.mtu)
|
||||||
if tun.rwc.MaxMTU() < mtu {
|
if tun.rwc.MaxMTU() < mtu {
|
||||||
mtu = tun.rwc.MaxMTU()
|
mtu = tun.rwc.MaxMTU()
|
||||||
}
|
}
|
||||||
if err := tun.setup(tun.config.IfName, addr, mtu); err != nil {
|
if err := tun.setup(string(tun.config.name), addr, mtu); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if tun.MTU() != mtu {
|
if tun.MTU() != mtu {
|
||||||
tun.log.Warnf("Warning: Interface MTU %d automatically adjusted to %d (supported range is 1280-%d)", tun.config.IfMTU, tun.MTU(), MaximumMTU())
|
tun.log.Warnf("Warning: Interface MTU %d automatically adjusted to %d (supported range is 1280-%d)", tun.config.mtu, tun.MTU(), MaximumMTU())
|
||||||
}
|
}
|
||||||
tun.rwc.SetMTU(tun.MTU())
|
tun.rwc.SetMTU(tun.MTU())
|
||||||
tun.isOpen = true
|
tun.isOpen = true
|
||||||
|
Loading…
Reference in New Issue
Block a user