dnsd: don’t serve expired leases

fixes #6
This commit is contained in:
Michael Stapelberg 2018-08-05 11:30:58 +02:00
parent daa14845ab
commit 5a5a748b9f
2 changed files with 23 additions and 1 deletions

View File

@ -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

View File

@ -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)