From 4b6b5196b00c110c2e12d4f0a40e65f5bc779351 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Mon, 18 Jun 2018 09:44:46 +0200 Subject: [PATCH] dns: correctly resolve PTR for hostname --- internal/dns/dns.go | 9 +++++++- internal/dns/dns_test.go | 47 ++++++++++++++++++++++++++++------------ 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/internal/dns/dns.go b/internal/dns/dns.go index 8c97170..b41bb6a 100644 --- a/internal/dns/dns.go +++ b/internal/dns/dns.go @@ -83,7 +83,9 @@ func (s *Server) initHostsLocked() { s.hostsByIP = make(map[string]string) if s.hostname != "" && s.ip != "" { s.hostsByName[s.hostname] = s.ip - s.hostsByIP[s.ip] = s.hostname + if rev, err := dns.ReverseAddr(s.ip); err == nil { + s.hostsByIP[rev] = s.hostname + } } } @@ -127,6 +129,11 @@ func mustParseCIDR(s string) *net.IPNet { var ( localNets = []*net.IPNet{ + // loopback: https://tools.ietf.org/html/rfc3330#section-2 + mustParseCIDR("127.0.0.0/8"), + // loopback: https://tools.ietf.org/html/rfc3513#section-2.4 + mustParseCIDR("::1/128"), + // reversed: https://tools.ietf.org/html/rfc1918#section-3 mustParseCIDR("10.0.0.0/8"), mustParseCIDR("172.16.0.0/12"), diff --git a/internal/dns/dns_test.go b/internal/dns/dns_test.go index 36546aa..cc7af00 100644 --- a/internal/dns/dns_test.go +++ b/internal/dns/dns_test.go @@ -83,19 +83,38 @@ func TestHostname(t *testing.T) { r := &recorder{} s := NewServer("127.0.0.2:0", "lan") - m := new(dns.Msg) - m.SetQuestion(hostname+".", dns.TypeA) - s.handleRequest(r, m) - if got, want := len(r.response.Answer), 1; got != want { - t.Fatalf("unexpected number of answers for %v: got %d, want %d", m.Question, got, want) - } - a := r.response.Answer[0] - if _, ok := a.(*dns.A); !ok { - t.Fatalf("unexpected response type: got %T, want dns.A", a) - } - if got, want := a.(*dns.A).A.To4(), (net.IP{127, 0, 0, 2}); !bytes.Equal(got, want) { - t.Fatalf("unexpected response IP: got %v, want %v", got, want) - } + + t.Run("A", func(t *testing.T) { + m := new(dns.Msg) + m.SetQuestion(hostname+".lan.", dns.TypeA) + s.handleRequest(r, m) + if got, want := len(r.response.Answer), 1; got != want { + t.Fatalf("unexpected number of answers for %v: got %d, want %d", m.Question, got, want) + } + a := r.response.Answer[0] + if _, ok := a.(*dns.A); !ok { + t.Fatalf("unexpected response type: got %T, want dns.A", a) + } + if got, want := a.(*dns.A).A.To4(), (net.IP{127, 0, 0, 2}); !bytes.Equal(got, want) { + t.Fatalf("unexpected response IP: got %v, want %v", got, want) + } + }) + + t.Run("PTR", func(t *testing.T) { + m := new(dns.Msg) + m.SetQuestion("2.0.0.127.in-addr.arpa.", dns.TypePTR) + s.handleRequest(r, m) + if got, want := len(r.response.Answer), 1; got != want { + t.Fatalf("unexpected number of answers: got %d, want %d", got, want) + } + a := r.response.Answer[0] + if _, ok := a.(*dns.PTR); !ok { + t.Fatalf("unexpected response type: got %T, want dns.PTR", a) + } + if got, want := a.(*dns.PTR).Ptr, hostname+".lan."; got != want { + t.Fatalf("unexpected response record: got %q, want %q", got, want) + } + }) } func TestDHCPReverse(t *testing.T) { @@ -140,7 +159,7 @@ func TestDHCPReverse(t *testing.T) { } a := r.response.Answer[0] if _, ok := a.(*dns.PTR); !ok { - t.Fatalf("unexpected response type: got %T, want dns.A", a) + t.Fatalf("unexpected response type: got %T, want dns.PTR", a) } if got, want := a.(*dns.PTR).Ptr, "xps.lan."; got != want { t.Fatalf("unexpected response record: got %q, want %q", got, want)