Create ifacewatcher

This commit is contained in:
Aleksandar Pesic 2021-02-22 15:50:45 +01:00
parent 39f7a61e9c
commit f96cf1892a
3 changed files with 62 additions and 0 deletions

View File

@ -0,0 +1,25 @@
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//+build !windows
package wgengine
import (
"tailscale.com/types/logger"
)
// Dummy implementation that does nothing.
type ifaceWatcher struct {
}
func initWatcher(logf logger.Logf) (*ifaceWatcher, error) {
return &ifaceWatcher{}, nil
}
func (iw *ifaceWatcher) setTun(ifc interface{}) {
}
func (iw *ifaceWatcher) Destroy() {
}

View File

@ -0,0 +1,28 @@
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package wgengine
import (
"github.com/tailscale/wireguard-go/tun"
"tailscale.com/types/logger"
)
type ifaceWatcher struct {
logf logger.Logf
iface *tun.NativeTun
}
func initWatcher(logf logger.Logf) (*ifaceWatcher, error) {
return &ifaceWatcher{logf: logger.WithPrefix(logf, "ifaceWatcher: ")}, nil
}
func (iw *ifaceWatcher) setTun(ifc interface{}) {
iw.iface = ifc.(*tun.NativeTun)
iw.logf("setTun LUID=%v", iw.iface.LUID())
}
func (iw *ifaceWatcher) Destroy() {
iw.logf("Destroy")
}

View File

@ -179,14 +179,22 @@ func NewUserspaceEngine(logf logger.Logf, tunName string, listenPort uint16) (En
logf("Starting userspace wireguard engine with tun device %q", tunName)
iw, err := initWatcher(logf)
if err != nil {
return nil, err
}
tun, err := tun.CreateTUN(tunName, minimalMTU)
if err != nil {
iw.Destroy()
diagnoseTUNFailure(tunName, logf)
logf("CreateTUN: %v", err)
return nil, err
}
logf("CreateTUN ok.")
iw.setTun(tun)
conf := EngineConfig{
Logf: logf,
TUN: tun,
@ -196,6 +204,7 @@ func NewUserspaceEngine(logf logger.Logf, tunName string, listenPort uint16) (En
e, err := NewUserspaceEngineAdvanced(conf)
if err != nil {
iw.Destroy()
tun.Close()
return nil, err
}