mirror of
https://github.com/cwinfo/yggdrasil-go.git
synced 2024-11-23 00:51:35 +00:00
TUN/TAP now uses config, log, etc from adapter.go
This commit is contained in:
parent
58f5cc88d0
commit
350b51cabb
@ -35,8 +35,6 @@ type TunAdapter struct {
|
||||
yggdrasil.Adapter
|
||||
addr address.Address
|
||||
subnet address.Subnet
|
||||
log *log.Logger
|
||||
config *config.NodeState
|
||||
icmpv6 ICMPv6
|
||||
mtu int
|
||||
iface *water.Interface
|
||||
@ -98,20 +96,18 @@ func MaximumMTU() int {
|
||||
|
||||
// Init initialises the TUN/TAP adapter.
|
||||
func (tun *TunAdapter) Init(config *config.NodeState, log *log.Logger, send chan<- []byte, recv <-chan []byte, reject <-chan yggdrasil.RejectedPacket) {
|
||||
tun.config = config
|
||||
tun.log = log
|
||||
tun.Adapter.Init(config, log, send, recv, reject)
|
||||
tun.icmpv6.Init(tun)
|
||||
go func() {
|
||||
for {
|
||||
e := <-tun.Reconfigure
|
||||
tun.config.Mutex.RLock()
|
||||
updated := tun.config.Current.IfName != tun.config.Previous.IfName ||
|
||||
tun.config.Current.IfTAPMode != tun.config.Previous.IfTAPMode ||
|
||||
tun.config.Current.IfMTU != tun.config.Previous.IfMTU
|
||||
tun.config.Mutex.RUnlock()
|
||||
tun.Config.Mutex.RLock()
|
||||
updated := tun.Config.Current.IfName != tun.Config.Previous.IfName ||
|
||||
tun.Config.Current.IfTAPMode != tun.Config.Previous.IfTAPMode ||
|
||||
tun.Config.Current.IfMTU != tun.Config.Previous.IfMTU
|
||||
tun.Config.Mutex.RUnlock()
|
||||
if updated {
|
||||
tun.log.Warnln("Reconfiguring TUN/TAP is not supported yet")
|
||||
tun.Log.Warnln("Reconfiguring TUN/TAP is not supported yet")
|
||||
e <- nil
|
||||
} else {
|
||||
e <- nil
|
||||
@ -125,34 +121,34 @@ func (tun *TunAdapter) Init(config *config.NodeState, log *log.Logger, send chan
|
||||
func (tun *TunAdapter) Start(a address.Address, s address.Subnet) error {
|
||||
tun.addr = a
|
||||
tun.subnet = s
|
||||
if tun.config == nil {
|
||||
if tun.Config == nil {
|
||||
return errors.New("No configuration available to TUN/TAP")
|
||||
}
|
||||
tun.config.Mutex.RLock()
|
||||
ifname := tun.config.Current.IfName
|
||||
iftapmode := tun.config.Current.IfTAPMode
|
||||
tun.Config.Mutex.RLock()
|
||||
ifname := tun.Config.Current.IfName
|
||||
iftapmode := tun.Config.Current.IfTAPMode
|
||||
addr := fmt.Sprintf("%s/%d", net.IP(tun.addr[:]).String(), 8*len(address.GetPrefix())-1)
|
||||
mtu := tun.config.Current.IfMTU
|
||||
tun.config.Mutex.RUnlock()
|
||||
mtu := tun.Config.Current.IfMTU
|
||||
tun.Config.Mutex.RUnlock()
|
||||
if ifname != "none" {
|
||||
if err := tun.setup(ifname, iftapmode, addr, mtu); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if ifname == "none" || ifname == "dummy" {
|
||||
tun.log.Debugln("Not starting TUN/TAP as ifname is none or dummy")
|
||||
tun.Log.Debugln("Not starting TUN/TAP as ifname is none or dummy")
|
||||
return nil
|
||||
}
|
||||
tun.mutex.Lock()
|
||||
tun.isOpen = true
|
||||
tun.mutex.Unlock()
|
||||
go func() {
|
||||
tun.log.Debugln("Starting TUN/TAP reader goroutine")
|
||||
tun.log.Errorln("WARNING: tun.read() exited with error:", tun.read())
|
||||
tun.Log.Debugln("Starting TUN/TAP reader goroutine")
|
||||
tun.Log.Errorln("WARNING: tun.read() exited with error:", tun.read())
|
||||
}()
|
||||
go func() {
|
||||
tun.log.Debugln("Starting TUN/TAP writer goroutine")
|
||||
tun.log.Errorln("WARNING: tun.write() exited with error:", tun.write())
|
||||
tun.Log.Debugln("Starting TUN/TAP writer goroutine")
|
||||
tun.Log.Errorln("WARNING: tun.write() exited with error:", tun.write())
|
||||
}()
|
||||
if iftapmode {
|
||||
go func() {
|
||||
|
@ -109,14 +109,14 @@ func (tun *TunAdapter) setupAddress(addr string) error {
|
||||
|
||||
// Create system socket
|
||||
if sfd, err = unix.Socket(unix.AF_INET, unix.SOCK_DGRAM, 0); err != nil {
|
||||
tun.log.Printf("Create AF_INET socket failed: %v.", err)
|
||||
tun.Log.Printf("Create AF_INET socket failed: %v.", err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Friendly output
|
||||
tun.log.Infof("Interface name: %s", tun.iface.Name())
|
||||
tun.log.Infof("Interface IPv6: %s", addr)
|
||||
tun.log.Infof("Interface MTU: %d", tun.mtu)
|
||||
tun.Log.Infof("Interface name: %s", tun.iface.Name())
|
||||
tun.Log.Infof("Interface IPv6: %s", addr)
|
||||
tun.Log.Infof("Interface MTU: %d", tun.mtu)
|
||||
|
||||
// Create the MTU request
|
||||
var ir in6_ifreq_mtu
|
||||
@ -126,15 +126,15 @@ func (tun *TunAdapter) setupAddress(addr string) error {
|
||||
// Set the MTU
|
||||
if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(sfd), uintptr(syscall.SIOCSIFMTU), uintptr(unsafe.Pointer(&ir))); errno != 0 {
|
||||
err = errno
|
||||
tun.log.Errorf("Error in SIOCSIFMTU: %v", errno)
|
||||
tun.Log.Errorf("Error in SIOCSIFMTU: %v", errno)
|
||||
|
||||
// Fall back to ifconfig to set the MTU
|
||||
cmd := exec.Command("ifconfig", tun.iface.Name(), "mtu", string(tun.mtu))
|
||||
tun.log.Warnf("Using ifconfig as fallback: %v", strings.Join(cmd.Args, " "))
|
||||
tun.Log.Warnf("Using ifconfig as fallback: %v", strings.Join(cmd.Args, " "))
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
tun.log.Errorf("SIOCSIFMTU fallback failed: %v.", err)
|
||||
tun.log.Traceln(string(output))
|
||||
tun.Log.Errorf("SIOCSIFMTU fallback failed: %v.", err)
|
||||
tun.Log.Traceln(string(output))
|
||||
}
|
||||
}
|
||||
|
||||
@ -155,15 +155,15 @@ func (tun *TunAdapter) setupAddress(addr string) error {
|
||||
// Set the interface address
|
||||
if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(sfd), uintptr(SIOCSIFADDR_IN6), uintptr(unsafe.Pointer(&ar))); errno != 0 {
|
||||
err = errno
|
||||
tun.log.Errorf("Error in SIOCSIFADDR_IN6: %v", errno)
|
||||
tun.Log.Errorf("Error in SIOCSIFADDR_IN6: %v", errno)
|
||||
|
||||
// Fall back to ifconfig to set the address
|
||||
cmd := exec.Command("ifconfig", tun.iface.Name(), "inet6", addr)
|
||||
tun.log.Warnf("Using ifconfig as fallback: %v", strings.Join(cmd.Args, " "))
|
||||
tun.Log.Warnf("Using ifconfig as fallback: %v", strings.Join(cmd.Args, " "))
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
tun.log.Errorf("SIOCSIFADDR_IN6 fallback failed: %v.", err)
|
||||
tun.log.Traceln(string(output))
|
||||
tun.Log.Errorf("SIOCSIFADDR_IN6 fallback failed: %v.", err)
|
||||
tun.Log.Traceln(string(output))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -18,7 +18,7 @@ import (
|
||||
// Configures the "utun" adapter with the correct IPv6 address and MTU.
|
||||
func (tun *TunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int) error {
|
||||
if iftapmode {
|
||||
tun.log.Warnln("TAP mode is not supported on this platform, defaulting to TUN")
|
||||
tun.Log.Warnln("TAP mode is not supported on this platform, defaulting to TUN")
|
||||
}
|
||||
config := water.Config{DeviceType: water.TUN}
|
||||
iface, err := water.New(config)
|
||||
@ -69,7 +69,7 @@ func (tun *TunAdapter) setupAddress(addr string) error {
|
||||
var err error
|
||||
|
||||
if fd, err = unix.Socket(unix.AF_INET6, unix.SOCK_DGRAM, 0); err != nil {
|
||||
tun.log.Printf("Create AF_SYSTEM socket failed: %v.", err)
|
||||
tun.Log.Printf("Create AF_SYSTEM socket failed: %v.", err)
|
||||
return err
|
||||
}
|
||||
|
||||
@ -98,19 +98,19 @@ func (tun *TunAdapter) setupAddress(addr string) error {
|
||||
copy(ir.ifr_name[:], tun.iface.Name())
|
||||
ir.ifru_mtu = uint32(tun.mtu)
|
||||
|
||||
tun.log.Infof("Interface name: %s", ar.ifra_name)
|
||||
tun.log.Infof("Interface IPv6: %s", addr)
|
||||
tun.log.Infof("Interface MTU: %d", ir.ifru_mtu)
|
||||
tun.Log.Infof("Interface name: %s", ar.ifra_name)
|
||||
tun.Log.Infof("Interface IPv6: %s", addr)
|
||||
tun.Log.Infof("Interface MTU: %d", ir.ifru_mtu)
|
||||
|
||||
if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(darwin_SIOCAIFADDR_IN6), uintptr(unsafe.Pointer(&ar))); errno != 0 {
|
||||
err = errno
|
||||
tun.log.Errorf("Error in darwin_SIOCAIFADDR_IN6: %v", errno)
|
||||
tun.Log.Errorf("Error in darwin_SIOCAIFADDR_IN6: %v", errno)
|
||||
return err
|
||||
}
|
||||
|
||||
if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(unix.SIOCSIFMTU), uintptr(unsafe.Pointer(&ir))); errno != 0 {
|
||||
err = errno
|
||||
tun.log.Errorf("Error in SIOCSIFMTU: %v", errno)
|
||||
tun.Log.Errorf("Error in SIOCSIFMTU: %v", errno)
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -40,9 +40,9 @@ func (tun *TunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int
|
||||
}
|
||||
}
|
||||
// Friendly output
|
||||
tun.log.Infof("Interface name: %s", tun.iface.Name())
|
||||
tun.log.Infof("Interface IPv6: %s", addr)
|
||||
tun.log.Infof("Interface MTU: %d", tun.mtu)
|
||||
tun.Log.Infof("Interface name: %s", tun.iface.Name())
|
||||
tun.Log.Infof("Interface IPv6: %s", addr)
|
||||
tun.Log.Infof("Interface MTU: %d", tun.mtu)
|
||||
return tun.setupAddress(addr)
|
||||
}
|
||||
|
||||
|
@ -28,6 +28,6 @@ func (tun *TunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int
|
||||
// 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 {
|
||||
tun.log.Warnln("Platform not supported, you must set the address of", tun.iface.Name(), "to", addr)
|
||||
tun.Log.Warnln("Platform not supported, you must set the address of", tun.iface.Name(), "to", addr)
|
||||
return nil
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ import (
|
||||
// delegate the hard work to "netsh".
|
||||
func (tun *TunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int) error {
|
||||
if !iftapmode {
|
||||
tun.log.Warnln("TUN mode is not supported on this platform, defaulting to TAP")
|
||||
tun.Log.Warnln("TUN mode is not supported on this platform, defaulting to TAP")
|
||||
}
|
||||
config := water.Config{DeviceType: water.TAP}
|
||||
config.PlatformSpecificParams.ComponentID = "tap0901"
|
||||
@ -31,19 +31,19 @@ func (tun *TunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int
|
||||
}
|
||||
// Disable/enable the interface to resets its configuration (invalidating iface)
|
||||
cmd := exec.Command("netsh", "interface", "set", "interface", iface.Name(), "admin=DISABLED")
|
||||
tun.log.Printf("netsh command: %v", strings.Join(cmd.Args, " "))
|
||||
tun.Log.Printf("netsh command: %v", strings.Join(cmd.Args, " "))
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
tun.log.Errorf("Windows netsh failed: %v.", err)
|
||||
tun.log.Traceln(string(output))
|
||||
tun.Log.Errorf("Windows netsh failed: %v.", err)
|
||||
tun.Log.Traceln(string(output))
|
||||
return err
|
||||
}
|
||||
cmd = exec.Command("netsh", "interface", "set", "interface", iface.Name(), "admin=ENABLED")
|
||||
tun.log.Printf("netsh command: %v", strings.Join(cmd.Args, " "))
|
||||
tun.Log.Printf("netsh command: %v", strings.Join(cmd.Args, " "))
|
||||
output, err = cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
tun.log.Errorf("Windows netsh failed: %v.", err)
|
||||
tun.log.Traceln(string(output))
|
||||
tun.Log.Errorf("Windows netsh failed: %v.", err)
|
||||
tun.Log.Traceln(string(output))
|
||||
return err
|
||||
}
|
||||
// Get a new iface
|
||||
@ -58,9 +58,9 @@ func (tun *TunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int
|
||||
panic(err)
|
||||
}
|
||||
// Friendly output
|
||||
tun.log.Infof("Interface name: %s", tun.iface.Name())
|
||||
tun.log.Infof("Interface IPv6: %s", addr)
|
||||
tun.log.Infof("Interface MTU: %d", tun.mtu)
|
||||
tun.Log.Infof("Interface name: %s", tun.iface.Name())
|
||||
tun.Log.Infof("Interface IPv6: %s", addr)
|
||||
tun.Log.Infof("Interface MTU: %d", tun.mtu)
|
||||
return tun.setupAddress(addr)
|
||||
}
|
||||
|
||||
@ -71,11 +71,11 @@ func (tun *TunAdapter) setupMTU(mtu int) error {
|
||||
fmt.Sprintf("interface=%s", tun.iface.Name()),
|
||||
fmt.Sprintf("mtu=%d", mtu),
|
||||
"store=active")
|
||||
tun.log.Debugln("netsh command: %v", strings.Join(cmd.Args, " "))
|
||||
tun.Log.Debugln("netsh command: %v", strings.Join(cmd.Args, " "))
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
tun.log.Errorf("Windows netsh failed: %v.", err)
|
||||
tun.log.Traceln(string(output))
|
||||
tun.Log.Errorf("Windows netsh failed: %v.", err)
|
||||
tun.Log.Traceln(string(output))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@ -88,11 +88,11 @@ func (tun *TunAdapter) setupAddress(addr string) error {
|
||||
fmt.Sprintf("interface=%s", tun.iface.Name()),
|
||||
fmt.Sprintf("addr=%s", addr),
|
||||
"store=active")
|
||||
tun.log.Debugln("netsh command: %v", strings.Join(cmd.Args, " "))
|
||||
tun.Log.Debugln("netsh command: %v", strings.Join(cmd.Args, " "))
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
tun.log.Errorf("Windows netsh failed: %v.", err)
|
||||
tun.log.Traceln(string(output))
|
||||
tun.Log.Errorf("Windows netsh failed: %v.", err)
|
||||
tun.Log.Traceln(string(output))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
@ -6,13 +6,15 @@ import (
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/config"
|
||||
)
|
||||
|
||||
// Defines the minimum required struct members for an adapter type. This is now
|
||||
// the base type for adapters like tun.go. When implementing a new adapter type,
|
||||
// you should extend the adapter struct with this one and should call the
|
||||
// Adapter.Init() function when initialising.
|
||||
// Adapter defines the minimum required struct members for an adapter type. This
|
||||
// is now the base type for adapters like tun.go. When implementing a new
|
||||
// adapter type, you should extend the adapter struct with this one and should
|
||||
// call the Adapter.Init() function when initialising.
|
||||
type Adapter struct {
|
||||
adapterImplementation
|
||||
Core *Core
|
||||
Config *config.NodeState
|
||||
Log *log.Logger
|
||||
Send chan<- []byte
|
||||
Recv <-chan []byte
|
||||
Reject <-chan RejectedPacket
|
||||
@ -31,11 +33,13 @@ type adapterImplementation interface {
|
||||
Close() error
|
||||
}
|
||||
|
||||
// Initialises the adapter with the necessary channels to operate from the
|
||||
// Init initialises the adapter with the necessary channels to operate from the
|
||||
// router. When defining a new Adapter type, the Adapter should call this
|
||||
// function from within it's own Init function to set up the channels. It is
|
||||
// otherwise not expected for you to call this function directly.
|
||||
func (adapter *Adapter) Init(config *config.NodeState, log *log.Logger, send chan<- []byte, recv <-chan []byte, reject <-chan RejectedPacket) {
|
||||
adapter.Config = config
|
||||
adapter.Log = log
|
||||
adapter.Send = send
|
||||
adapter.Recv = recv
|
||||
adapter.Reject = reject
|
||||
|
Loading…
Reference in New Issue
Block a user