From 5a5a748b9f2e98770cc5bff298cb07e7d39a41b4 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Sun, 5 Aug 2018 11:30:58 +0200 Subject: [PATCH] =?UTF-8?q?dnsd:=20don=E2=80=99t=20serve=20expired=20lease?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fixes #6 --- internal/dns/dns.go | 4 ++++ internal/dns/dns_test.go | 20 +++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/internal/dns/dns.go b/internal/dns/dns.go index 329d859..93b73e9 100644 --- a/internal/dns/dns.go +++ b/internal/dns/dns.go @@ -129,7 +129,11 @@ func (s *Server) SetLeases(leases []dhcp4d.Lease) { s.mu.Lock() defer s.mu.Unlock() s.initHostsLocked() + now := time.Now() for _, l := range leases { + if l.Expired(now) { + continue + } s.hostsByName[l.Hostname] = l.Addr.String() if rev, err := dns.ReverseAddr(l.Addr.String()); err == nil { s.hostsByIP[rev] = l.Hostname diff --git a/internal/dns/dns_test.go b/internal/dns/dns_test.go index d6ffd48..be27fc9 100644 --- a/internal/dns/dns_test.go +++ b/internal/dns/dns_test.go @@ -19,6 +19,7 @@ import ( "net" "os" "testing" + "time" "github.com/rtr7/router7/internal/dhcp4d" @@ -76,7 +77,7 @@ func TestDHCP(t *testing.T) { }, }) - t.Run("xps.lan.", func(t *testing.T) { + resolveXps := func(t *testing.T) { m := new(dns.Msg) m.SetQuestion("xps.lan.", dns.TypeA) s.Mux.ServeDNS(r, m) @@ -90,8 +91,25 @@ func TestDHCP(t *testing.T) { if got, want := a.(*dns.A).A, net.ParseIP("192.168.42.23"); !got.Equal(want) { t.Fatalf("unexpected response IP: got %v, want %v", got, want) } + } + t.Run("xps.lan.", resolveXps) + + expired := time.Now().Add(-1 * time.Second) + s.SetLeases([]dhcp4d.Lease{ + { + Hostname: "xps", + Addr: net.IP{192, 168, 42, 23}, + Expiry: time.Now().Add(1 * time.Minute), + }, + { + Hostname: "xps", + Addr: net.IP{192, 168, 42, 150}, + Expiry: expired, + }, }) + t.Run("xps.lan. (expired)", resolveXps) + t.Run("notfound.lan.", func(t *testing.T) { m := new(dns.Msg) m.SetQuestion("notfound.lan.", dns.TypeA)