diff --git a/client_resolver.go b/client_resolver.go index ae041ac..20dc2bf 100644 --- a/client_resolver.go +++ b/client_resolver.go @@ -39,11 +39,11 @@ func (c *Client) AppendLookupNetIP(dst []netip.Addr, ctx context.Context, networ return nil, err } - cname := make([]byte, 0, 64) + var cname []byte for r := range resp.Records { switch r.Type { case TypeCNAME: - cname = resp.DecodeName(cname[:0], r.Data) + cname = r.Data case TypeA: dst = append(dst, netip.AddrFrom4(*(*[4]byte)(r.Data))) case TypeAAAA: @@ -51,8 +51,9 @@ func (c *Client) AppendLookupNetIP(dst []netip.Addr, ctx context.Context, networ } } - if len(cname) != 0 && len(dst) == 0 { - dst, err = c.AppendLookupNetIP(dst, ctx, network, b2s(cname)) + if cname != nil && len(dst) == 0 { + b := resp.DecodeName(make([]byte, 0, 64), cname) + dst, err = c.AppendLookupNetIP(dst, ctx, network, b2s(b)) } return dst, err diff --git a/client_test.go b/client_test.go index 6a45638..26771a3 100644 --- a/client_test.go +++ b/client_test.go @@ -157,6 +157,21 @@ func TestClientLookup(t *testing.T) { } } +func TestClientLookupNetIP(t *testing.T) { + host := "ip.phus.lu" + + client := &Client{ + Addr: "1.1.1.1:53", + Timeout: 1 * time.Second, + } + + ips, err := client.LookupNetIP(context.Background(), "ip", host) + if err != nil { + t.Errorf("dns_server=%+v LookupNetIP(%#v) error: %+v\n", client.Addr, host, err) + } + t.Logf("dns_server=%+v LookupNetIP(%#v) return %+v", client.Addr, host, ips) +} + func BenchmarkResolverPureGo(b *testing.B) { resolver := net.Resolver{PreferGo: true}