net/dns/resolver: remove the Config struct.

In preparation for reintroducing a runtime reconfig Config struct.

Signed-off-by: David Anderson <danderson@tailscale.com>
This commit is contained in:
David Anderson 2021-03-31 22:13:23 -07:00
parent bc81dd4690
commit 075fb93e69
3 changed files with 23 additions and 54 deletions

View File

@ -82,34 +82,18 @@ type Resolver struct {
dnsMap *Map dnsMap *Map
} }
// Config is the set of configuration options for a Resolver. // New returns a new resolver.
type Config struct { // linkMon optionally specifies a link monitor to use for socket rebinding.
// Logf is the logger to use throughout the Resolver. func New(logf logger.Logf, linkMon *monitor.Mon) *Resolver {
Logf logger.Logf
// Forward determines whether the resolver will forward packets to
// nameservers set with SetUpstreams if the domain name is not of a Tailscale node.
Forward bool
// LinkMonitor optionally provides a link monitor to use to rebind
// connections on link changes.
// If nil, rebinds are not performend.
LinkMonitor *monitor.Mon
}
// New constructs a resolver associated with the given root domain.
// The root domain must be in canonical form (with a trailing period).
func New(config Config) *Resolver {
r := &Resolver{ r := &Resolver{
logf: logger.WithPrefix(config.Logf, "dns: "), logf: logger.WithPrefix(logf, "dns: "),
linkMon: config.LinkMonitor, linkMon: linkMon,
queue: make(chan Packet, queueSize), queue: make(chan Packet, queueSize),
responses: make(chan Packet), responses: make(chan Packet),
errors: make(chan error), errors: make(chan error),
closed: make(chan struct{}), closed: make(chan struct{}),
} }
r.forwarder = newForwarder(r.logf, r.responses)
if config.Forward {
r.forwarder = newForwarder(r.logf, r.responses)
}
if r.linkMon != nil { if r.linkMon != nil {
r.unregLinkMon = r.linkMon.RegisterChangeCallback(r.onLinkMonitorChange) r.unregLinkMon = r.linkMon.RegisterChangeCallback(r.onLinkMonitorChange)
} }
@ -145,10 +129,7 @@ func (r *Resolver) Close() {
r.unregLinkMon() r.unregLinkMon()
} }
if r.forwarder != nil { r.forwarder.Close()
r.forwarder.Close()
}
r.wg.Wait() r.wg.Wait()
} }
@ -156,9 +137,7 @@ func (r *Resolver) onLinkMonitorChange(changed bool, state *interfaces.State) {
if !changed { if !changed {
return return
} }
if r.forwarder != nil { r.forwarder.rebindFromNetworkChange()
r.forwarder.rebindFromNetworkChange()
}
} }
// SetMap sets the resolver's DNS map, taking ownership of it. // SetMap sets the resolver's DNS map, taking ownership of it.
@ -173,9 +152,7 @@ func (r *Resolver) SetMap(m *Map) {
// SetUpstreams sets the addresses of the resolver's // SetUpstreams sets the addresses of the resolver's
// upstream nameservers, taking ownership of the argument. // upstream nameservers, taking ownership of the argument.
func (r *Resolver) SetUpstreams(upstreams []net.Addr) { func (r *Resolver) SetUpstreams(upstreams []net.Addr) {
if r.forwarder != nil { r.forwarder.setUpstreams(upstreams)
r.forwarder.setUpstreams(upstreams)
}
r.logf("set upstreams: %v", upstreams) r.logf("set upstreams: %v", upstreams)
} }
@ -307,14 +284,10 @@ func (r *Resolver) poll() {
out, err := r.respond(packet.Payload) out, err := r.respond(packet.Payload)
if err == errNotOurName { if err == errNotOurName {
if r.forwarder != nil { err = r.forwarder.forward(packet)
err = r.forwarder.forward(packet) if err == nil {
if err == nil { // forward will send response into r.responses, nothing to do.
// forward will send response into r.responses, nothing to do. continue
continue
}
} else {
err = errNotForwarding
} }
} }

View File

@ -194,7 +194,7 @@ func TestRDNSNameToIPv6(t *testing.T) {
} }
func TestResolve(t *testing.T) { func TestResolve(t *testing.T) {
r := New(Config{Logf: t.Logf, Forward: false}) r := New(t.Logf, nil)
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 := New(Config{Logf: t.Logf, Forward: false}) r := New(t.Logf, nil)
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 := New(Config{Logf: t.Logf, Forward: true}) r := New(t.Logf, nil)
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 := New(Config{Logf: t.Logf, Forward: true}) r := New(t.Logf, nil)
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 := New(Config{Logf: t.Logf, Forward: false}) r := New(t.Logf, nil)
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 := New(Config{Logf: t.Logf, Forward: true}) r := New(t.Logf, nil)
r.SetMap(dnsMap) r.SetMap(dnsMap)
if err := r.Start(); err != nil { if err := r.Start(); err != nil {
@ -670,7 +670,7 @@ func TestConcurrentSetUpstreams(t *testing.T) {
} }
func TestFull(t *testing.T) { func TestFull(t *testing.T) {
r := New(Config{Logf: t.Logf, Forward: false}) r := New(t.Logf, nil)
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 := New(Config{Logf: t.Logf, Forward: false}) r := New(t.Logf, nil)
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 := New(Config{Logf: b.Logf, Forward: true}) r := New(b.Logf, nil)
r.SetMap(dnsMap) r.SetMap(dnsMap)
r.SetUpstreams([]net.Addr{server.PacketConn.LocalAddr()}) r.SetUpstreams([]net.Addr{server.PacketConn.LocalAddr()})

View File

@ -219,11 +219,7 @@ func NewUserspaceEngine(logf logger.Logf, conf Config) (_ Engine, reterr error)
e.linkMonOwned = true e.linkMonOwned = true
} }
e.resolver = resolver.New(resolver.Config{ e.resolver = resolver.New(logf, e.linkMon)
Logf: logf,
Forward: true,
LinkMonitor: e.linkMon,
})
logf("link state: %+v", e.linkMon.InterfaceState()) logf("link state: %+v", e.linkMon.InterfaceState())