wgengine: add IsNetstack func and test

So we have a documented & tested way to check whether we're in
netstack mode. To be used by future commits.

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/1618/head
Brad Fitzpatrick 4 years ago
parent 91bc723817
commit 4c83bbf850

@ -164,6 +164,20 @@ func NewFakeUserspaceEngine(logf logger.Logf, listenPort uint16) (Engine, error)
}) })
} }
// IsNetstack reports whether e is a netstack-based TUN-free engine.
func IsNetstack(e Engine) bool {
ig, ok := e.(InternalsGetter)
if !ok {
return false
}
tw, _, ok := ig.GetInternals()
if !ok {
return false
}
name, err := tw.Name()
return err == nil && name == "FakeTUN"
}
// NewUserspaceEngine creates the named tun device and returns a // NewUserspaceEngine creates the named tun device and returns a
// Tailscale Engine running on it. // Tailscale Engine running on it.
func NewUserspaceEngine(logf logger.Logf, conf Config) (_ Engine, reterr error) { func NewUserspaceEngine(logf logger.Logf, conf Config) (_ Engine, reterr error) {

@ -186,3 +186,14 @@ func BenchmarkGenLocalAddrFunc(b *testing.B) {
}) })
b.Logf("x = %v", x) b.Logf("x = %v", x)
} }
func TestIsNetstack(t *testing.T) {
e, err := NewUserspaceEngine(t.Logf, Config{})
if err != nil {
t.Fatal(err)
}
defer e.Close()
if !IsNetstack(e) {
t.Errorf("IsNetstack = false; want true")
}
}

Loading…
Cancel
Save