resolver/dns: support ipv6 addrs

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-08-29 16:53:53 +03:00
parent 9c695ac343
commit fb233374a0

View File

@ -42,23 +42,29 @@ func (r *Resolver) Resolve(name string) ([]*resolver.Record, error) {
return records, nil return records, nil
} }
for _, q := range []uint16{dns.TypeA, dns.TypeAAAA} {
m := new(dns.Msg) m := new(dns.Msg)
m.SetQuestion(dns.Fqdn(host), dns.TypeA) m.SetQuestion(dns.Fqdn(host), q)
rec, err := dns.ExchangeContext(context.Background(), m, r.Address) rec, err := dns.ExchangeContext(context.Background(), m, r.Address)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var addr string
for _, answer := range rec.Answer { for _, answer := range rec.Answer {
h := answer.Header() h := answer.Header()
// check record type matches // check record type matches
if h.Rrtype != dns.TypeA { switch h.Rrtype {
case dns.TypeA:
arec, _ := answer.(*dns.A)
addr = arec.A.String()
case dns.TypeAAAA:
arec, _ := answer.(*dns.AAAA)
addr = arec.AAAA.String()
default:
continue continue
} }
arec, _ := answer.(*dns.A)
addr := arec.A.String()
// join resolved record with port // join resolved record with port
address := net.JoinHostPort(addr, port) address := net.JoinHostPort(addr, port)
// append to record set // append to record set
@ -66,6 +72,7 @@ func (r *Resolver) Resolve(name string) ([]*resolver.Record, error) {
Address: address, Address: address,
}) })
} }
}
// no records returned so just best effort it // no records returned so just best effort it
if len(records) == 0 { if len(records) == 0 {