dns: correctly resolve PTR for hostname

This commit is contained in:
Michael Stapelberg 2018-06-18 09:44:46 +02:00
parent c743091929
commit 4b6b5196b0
2 changed files with 41 additions and 15 deletions

View File

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

View File

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