dns: correctly resolve PTR for hostname
This commit is contained in:
parent
c743091929
commit
4b6b5196b0
@ -83,7 +83,9 @@ func (s *Server) initHostsLocked() {
|
|||||||
s.hostsByIP = make(map[string]string)
|
s.hostsByIP = make(map[string]string)
|
||||||
if s.hostname != "" && s.ip != "" {
|
if s.hostname != "" && s.ip != "" {
|
||||||
s.hostsByName[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 (
|
var (
|
||||||
localNets = []*net.IPNet{
|
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
|
// reversed: https://tools.ietf.org/html/rfc1918#section-3
|
||||||
mustParseCIDR("10.0.0.0/8"),
|
mustParseCIDR("10.0.0.0/8"),
|
||||||
mustParseCIDR("172.16.0.0/12"),
|
mustParseCIDR("172.16.0.0/12"),
|
||||||
|
@ -83,8 +83,10 @@ func TestHostname(t *testing.T) {
|
|||||||
|
|
||||||
r := &recorder{}
|
r := &recorder{}
|
||||||
s := NewServer("127.0.0.2:0", "lan")
|
s := NewServer("127.0.0.2:0", "lan")
|
||||||
|
|
||||||
|
t.Run("A", func(t *testing.T) {
|
||||||
m := new(dns.Msg)
|
m := new(dns.Msg)
|
||||||
m.SetQuestion(hostname+".", dns.TypeA)
|
m.SetQuestion(hostname+".lan.", dns.TypeA)
|
||||||
s.handleRequest(r, m)
|
s.handleRequest(r, m)
|
||||||
if got, want := len(r.response.Answer), 1; got != want {
|
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)
|
t.Fatalf("unexpected number of answers for %v: got %d, want %d", m.Question, got, want)
|
||||||
@ -96,6 +98,23 @@ func TestHostname(t *testing.T) {
|
|||||||
if got, want := a.(*dns.A).A.To4(), (net.IP{127, 0, 0, 2}); !bytes.Equal(got, want) {
|
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.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) {
|
func TestDHCPReverse(t *testing.T) {
|
||||||
@ -140,7 +159,7 @@ func TestDHCPReverse(t *testing.T) {
|
|||||||
}
|
}
|
||||||
a := r.response.Answer[0]
|
a := r.response.Answer[0]
|
||||||
if _, ok := a.(*dns.PTR); !ok {
|
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 {
|
if got, want := a.(*dns.PTR).Ptr, "xps.lan."; got != want {
|
||||||
t.Fatalf("unexpected response record: got %q, want %q", got, want)
|
t.Fatalf("unexpected response record: got %q, want %q", got, want)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user