net/dns/resolver: factor the resolver out into a sub-package.

Signed-off-by: David Anderson <danderson@tailscale.com>
pull/1635/head
David Anderson 4 years ago
parent 53cfff109b
commit d99f5b1596

@ -91,6 +91,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de
tailscale.com/logtail/filch from tailscale.com/logpolicy tailscale.com/logtail/filch from tailscale.com/logpolicy
tailscale.com/metrics from tailscale.com/derp tailscale.com/metrics from tailscale.com/derp
tailscale.com/net/dns from tailscale.com/ipn/ipnlocal+ tailscale.com/net/dns from tailscale.com/ipn/ipnlocal+
tailscale.com/net/dns/resolver from tailscale.com/ipn/ipnlocal+
tailscale.com/net/dnscache from tailscale.com/control/controlclient+ tailscale.com/net/dnscache from tailscale.com/control/controlclient+
tailscale.com/net/dnsfallback from tailscale.com/control/controlclient tailscale.com/net/dnsfallback from tailscale.com/control/controlclient
tailscale.com/net/flowtrack from tailscale.com/wgengine/filter+ tailscale.com/net/flowtrack from tailscale.com/wgengine/filter+

@ -28,6 +28,7 @@ import (
"tailscale.com/ipn/ipnstate" "tailscale.com/ipn/ipnstate"
"tailscale.com/ipn/policy" "tailscale.com/ipn/policy"
"tailscale.com/net/dns" "tailscale.com/net/dns"
"tailscale.com/net/dns/resolver"
"tailscale.com/net/interfaces" "tailscale.com/net/interfaces"
"tailscale.com/net/tsaddr" "tailscale.com/net/tsaddr"
"tailscale.com/paths" "tailscale.com/paths"
@ -871,7 +872,7 @@ func (b *LocalBackend) updateDNSMap(netMap *netmap.NetworkMap) {
} }
set(netMap.Name, netMap.Addresses) set(netMap.Name, netMap.Addresses)
dnsMap := dns.NewMap(nameToIP, magicDNSRootDomains(netMap)) dnsMap := resolver.NewMap(nameToIP, magicDNSRootDomains(netMap))
// map diff will be logged in dns.Resolver.SetMap. // map diff will be logged in dns.Resolver.SetMap.
b.e.SetDNSMap(dnsMap) b.e.SetDNSMap(dnsMap)
} }

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package dns package resolver
import ( import (
"bytes" "bytes"

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package dns package resolver
import ( import (
"sort" "sort"

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package dns package resolver
import ( import (
"fmt" "fmt"

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package dns package resolver
import ( import (
"errors" "errors"

@ -4,7 +4,7 @@
// +build !darwin,!windows // +build !darwin,!windows
package dns package resolver
func networkIsDown(err error) bool { return false } func networkIsDown(err error) bool { return false }
func networkIsUnreachable(err error) bool { return false } func networkIsUnreachable(err error) bool { return false }

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package dns package resolver
import ( import (
"net" "net"

@ -2,9 +2,8 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// Package dns provides a Resolver capable of resolving // Package resolver a DNS resolver.
// domains on a Tailscale network. package resolver
package dns
import ( import (
"encoding/hex" "encoding/hex"
@ -96,9 +95,9 @@ type ResolverConfig struct {
LinkMonitor *monitor.Mon LinkMonitor *monitor.Mon
} }
// NewResolver constructs a resolver associated with the given root domain. // New constructs a resolver associated with the given root domain.
// The root domain must be in canonical form (with a trailing period). // The root domain must be in canonical form (with a trailing period).
func NewResolver(config ResolverConfig) *Resolver { func New(config ResolverConfig) *Resolver {
r := &Resolver{ r := &Resolver{
logf: logger.WithPrefix(config.Logf, "dns: "), logf: logger.WithPrefix(config.Logf, "dns: "),
linkMon: config.LinkMonitor, linkMon: config.LinkMonitor,

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package dns package resolver
import ( import (
"log" "log"

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package dns package resolver
import ( import (
"bytes" "bytes"
@ -194,7 +194,7 @@ func TestRDNSNameToIPv6(t *testing.T) {
} }
func TestResolve(t *testing.T) { func TestResolve(t *testing.T) {
r := NewResolver(ResolverConfig{Logf: t.Logf, Forward: false}) r := New(ResolverConfig{Logf: t.Logf, Forward: false})
r.SetMap(dnsMap) r.SetMap(dnsMap)
if err := r.Start(); err != nil { if err := r.Start(); err != nil {
@ -240,7 +240,7 @@ func TestResolve(t *testing.T) {
} }
func TestResolveReverse(t *testing.T) { func TestResolveReverse(t *testing.T) {
r := NewResolver(ResolverConfig{Logf: t.Logf, Forward: false}) r := New(ResolverConfig{Logf: t.Logf, Forward: false})
r.SetMap(dnsMap) r.SetMap(dnsMap)
if err := r.Start(); err != nil { if err := r.Start(); err != nil {
@ -318,7 +318,7 @@ func TestDelegate(t *testing.T) {
return return
} }
r := NewResolver(ResolverConfig{Logf: t.Logf, Forward: true}) r := New(ResolverConfig{Logf: t.Logf, Forward: true})
r.SetMap(dnsMap) r.SetMap(dnsMap)
r.SetUpstreams([]net.Addr{ r.SetUpstreams([]net.Addr{
v4server.PacketConn.LocalAddr(), v4server.PacketConn.LocalAddr(),
@ -397,7 +397,7 @@ func TestDelegateCollision(t *testing.T) {
} }
defer server.Shutdown() defer server.Shutdown()
r := NewResolver(ResolverConfig{Logf: t.Logf, Forward: true}) r := New(ResolverConfig{Logf: t.Logf, Forward: true})
r.SetMap(dnsMap) r.SetMap(dnsMap)
r.SetUpstreams([]net.Addr{server.PacketConn.LocalAddr()}) r.SetUpstreams([]net.Addr{server.PacketConn.LocalAddr()})
@ -463,7 +463,7 @@ func TestDelegateCollision(t *testing.T) {
} }
func TestConcurrentSetMap(t *testing.T) { func TestConcurrentSetMap(t *testing.T) {
r := NewResolver(ResolverConfig{Logf: t.Logf, Forward: false}) r := New(ResolverConfig{Logf: t.Logf, Forward: false})
if err := r.Start(); err != nil { if err := r.Start(); err != nil {
t.Fatalf("start: %v", err) t.Fatalf("start: %v", err)
@ -499,7 +499,7 @@ func TestConcurrentSetUpstreams(t *testing.T) {
} }
defer server.Shutdown() defer server.Shutdown()
r := NewResolver(ResolverConfig{Logf: t.Logf, Forward: true}) r := New(ResolverConfig{Logf: t.Logf, Forward: true})
r.SetMap(dnsMap) r.SetMap(dnsMap)
if err := r.Start(); err != nil { if err := r.Start(); err != nil {
@ -670,7 +670,7 @@ var emptyResponse = []byte{
} }
func TestFull(t *testing.T) { func TestFull(t *testing.T) {
r := NewResolver(ResolverConfig{Logf: t.Logf, Forward: false}) r := New(ResolverConfig{Logf: t.Logf, Forward: false})
r.SetMap(dnsMap) r.SetMap(dnsMap)
if err := r.Start(); err != nil { if err := r.Start(); err != nil {
@ -709,7 +709,7 @@ func TestFull(t *testing.T) {
} }
func TestAllocs(t *testing.T) { func TestAllocs(t *testing.T) {
r := NewResolver(ResolverConfig{Logf: t.Logf, Forward: false}) r := New(ResolverConfig{Logf: t.Logf, Forward: false})
r.SetMap(dnsMap) r.SetMap(dnsMap)
if err := r.Start(); err != nil { if err := r.Start(); err != nil {
@ -778,7 +778,7 @@ func BenchmarkFull(b *testing.B) {
} }
defer server.Shutdown() defer server.Shutdown()
r := NewResolver(ResolverConfig{Logf: b.Logf, Forward: true}) r := New(ResolverConfig{Logf: b.Logf, Forward: true})
r.SetMap(dnsMap) r.SetMap(dnsMap)
r.SetUpstreams([]net.Addr{server.PacketConn.LocalAddr()}) r.SetUpstreams([]net.Addr{server.PacketConn.LocalAddr()})

@ -29,7 +29,7 @@ import (
"tailscale.com/health" "tailscale.com/health"
"tailscale.com/internal/deepprint" "tailscale.com/internal/deepprint"
"tailscale.com/ipn/ipnstate" "tailscale.com/ipn/ipnstate"
"tailscale.com/net/dns" "tailscale.com/net/dns/resolver"
"tailscale.com/net/flowtrack" "tailscale.com/net/flowtrack"
"tailscale.com/net/interfaces" "tailscale.com/net/interfaces"
"tailscale.com/net/packet" "tailscale.com/net/packet"
@ -83,7 +83,7 @@ type userspaceEngine struct {
tundev *tstun.Wrapper tundev *tstun.Wrapper
wgdev *device.Device wgdev *device.Device
router router.Router router router.Router
resolver *dns.Resolver resolver *resolver.Resolver
magicConn *magicsock.Conn magicConn *magicsock.Conn
linkMon *monitor.Mon linkMon *monitor.Mon
linkMonOwned bool // whether we created linkMon (and thus need to close it) linkMonOwned bool // whether we created linkMon (and thus need to close it)
@ -219,7 +219,7 @@ func NewUserspaceEngine(logf logger.Logf, conf Config) (_ Engine, reterr error)
e.linkMonOwned = true e.linkMonOwned = true
} }
e.resolver = dns.NewResolver(dns.ResolverConfig{ e.resolver = resolver.New(resolver.ResolverConfig{
Logf: logf, Logf: logf,
Forward: true, Forward: true,
LinkMonitor: e.linkMon, LinkMonitor: e.linkMon,
@ -435,7 +435,7 @@ func (e *userspaceEngine) handleLocalPackets(p *packet.Parsed, t *tstun.Wrapper)
// handleDNS is an outbound pre-filter resolving Tailscale domains. // handleDNS is an outbound pre-filter resolving Tailscale domains.
func (e *userspaceEngine) handleDNS(p *packet.Parsed, t *tstun.Wrapper) filter.Response { func (e *userspaceEngine) handleDNS(p *packet.Parsed, t *tstun.Wrapper) filter.Response {
if p.Dst.IP == magicDNSIP && p.Dst.Port == magicDNSPort && p.IPProto == ipproto.UDP { if p.Dst.IP == magicDNSIP && p.Dst.Port == magicDNSPort && p.IPProto == ipproto.UDP {
request := dns.Packet{ request := resolver.Packet{
Payload: append([]byte(nil), p.Payload()...), Payload: append([]byte(nil), p.Payload()...),
Addr: netaddr.IPPort{IP: p.Src.IP, Port: p.Src.Port}, Addr: netaddr.IPPort{IP: p.Src.IP, Port: p.Src.Port},
} }
@ -452,7 +452,7 @@ func (e *userspaceEngine) handleDNS(p *packet.Parsed, t *tstun.Wrapper) filter.R
func (e *userspaceEngine) pollResolver() { func (e *userspaceEngine) pollResolver() {
for { for {
resp, err := e.resolver.NextResponse() resp, err := e.resolver.NextResponse()
if err == dns.ErrClosed { if err == resolver.ErrClosed {
return return
} }
if err != nil { if err != nil {
@ -1024,7 +1024,7 @@ func (e *userspaceEngine) SetFilter(filt *filter.Filter) {
e.tundev.SetFilter(filt) e.tundev.SetFilter(filt)
} }
func (e *userspaceEngine) SetDNSMap(dm *dns.Map) { func (e *userspaceEngine) SetDNSMap(dm *resolver.Map) {
e.resolver.SetMap(dm) e.resolver.SetMap(dm)
} }

@ -14,7 +14,7 @@ import (
"inet.af/netaddr" "inet.af/netaddr"
"tailscale.com/ipn/ipnstate" "tailscale.com/ipn/ipnstate"
"tailscale.com/net/dns" "tailscale.com/net/dns/resolver"
"tailscale.com/net/tstun" "tailscale.com/net/tstun"
"tailscale.com/tailcfg" "tailscale.com/tailcfg"
"tailscale.com/types/netmap" "tailscale.com/types/netmap"
@ -86,7 +86,7 @@ func (e *watchdogEngine) GetFilter() *filter.Filter {
func (e *watchdogEngine) SetFilter(filt *filter.Filter) { func (e *watchdogEngine) SetFilter(filt *filter.Filter) {
e.watchdog("SetFilter", func() { e.wrap.SetFilter(filt) }) e.watchdog("SetFilter", func() { e.wrap.SetFilter(filt) })
} }
func (e *watchdogEngine) SetDNSMap(dm *dns.Map) { func (e *watchdogEngine) SetDNSMap(dm *resolver.Map) {
e.watchdog("SetDNSMap", func() { e.wrap.SetDNSMap(dm) }) e.watchdog("SetDNSMap", func() { e.wrap.SetDNSMap(dm) })
} }
func (e *watchdogEngine) SetStatusCallback(cb StatusCallback) { func (e *watchdogEngine) SetStatusCallback(cb StatusCallback) {

@ -9,7 +9,7 @@ import (
"inet.af/netaddr" "inet.af/netaddr"
"tailscale.com/ipn/ipnstate" "tailscale.com/ipn/ipnstate"
"tailscale.com/net/dns" "tailscale.com/net/dns/resolver"
"tailscale.com/tailcfg" "tailscale.com/tailcfg"
"tailscale.com/types/netmap" "tailscale.com/types/netmap"
"tailscale.com/wgengine/filter" "tailscale.com/wgengine/filter"
@ -66,7 +66,7 @@ type Engine interface {
SetFilter(*filter.Filter) SetFilter(*filter.Filter)
// SetDNSMap updates the DNS map. // SetDNSMap updates the DNS map.
SetDNSMap(*dns.Map) SetDNSMap(*resolver.Map)
// SetStatusCallback sets the function to call when the // SetStatusCallback sets the function to call when the
// WireGuard status changes. // WireGuard status changes.

Loading…
Cancel
Save