From 70edcab16b7d7a6900d1a2499ff12e07f039a057 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Sat, 7 Sep 2019 19:27:12 +0200 Subject: [PATCH] dns: return NXDOMAIN for DHCP leases once they expire --- internal/dns/dns.go | 9 +++++++-- internal/dns/dns_test.go | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/internal/dns/dns.go b/internal/dns/dns.go index 12eac48..b48fbac 100644 --- a/internal/dns/dns.go +++ b/internal/dns/dns.go @@ -448,7 +448,12 @@ func (s *Server) resolveSubname(hostname string, q dns.Question) (dns.RR, error) if lower := strings.ToLower(q.Name); lower == hostname+"." || lower == hostname+"."+s.domain+"." { - host, _ := s.hostByName(hostname) + host, ok := s.hostByName(hostname) + if !ok { + // The corresponding DHCP lease might have expired, but this + // handler is still installed on the mux. + return nil, nil // NXDOMAIN + } if q.Qtype == dns.TypeA { return dns.NewRR(q.Name + " 3600 IN A " + host) } @@ -482,7 +487,7 @@ func (s *Server) subnameHandler(hostname string) func(w dns.ResponseWriter, r *d w.WriteMsg(m) return } - log.Fatal(err) + log.Fatalf("question %#v: %v", r.Question[0], err) } if rr != nil { m := new(dns.Msg) diff --git a/internal/dns/dns_test.go b/internal/dns/dns_test.go index 4153b19..c278cab 100644 --- a/internal/dns/dns_test.go +++ b/internal/dns/dns_test.go @@ -187,6 +187,11 @@ func TestDHCP(t *testing.T) { Addr: net.IP{192, 168, 42, 150}, Expiry: expired, }, + { + Hostname: "aged", + Addr: net.IP{192, 168, 42, 42}, + Expiry: time.Now().Add(1 * time.Minute), + }, }) t.Run("testtarget.lan. (expired)", func(t *testing.T) { @@ -203,6 +208,23 @@ func TestDHCP(t *testing.T) { t.Fatalf("unexpected rcode: got %v, want %v", got, want) } }) + + s.SetLeases([]dhcp4d.Lease{ + { + Hostname: "aged", + Addr: net.IP{192, 168, 42, 42}, + Expiry: expired, + }, + }) + + t.Run("aged.lan. (expired)", func(t *testing.T) { + m := new(dns.Msg) + m.SetQuestion("aged.lan.", dns.TypeA) + s.Mux.ServeDNS(r, m) + if got, want := r.response.Rcode, dns.RcodeNameError; got != want { + t.Fatalf("unexpected rcode: got %v, want %v", got, want) + } + }) } func TestHostname(t *testing.T) {