Skip to content

Commit

Permalink
Fix miekgdns resolver to work with CNAME records too (#5271)
Browse files Browse the repository at this point in the history
* Fix miekgdns resolver to work with CNAME records too

Signed-off-by: Marco Pracucci <marco@pracucci.com>

* Remove unused context

Signed-off-by: Marco Pracucci <marco@pracucci.com>

* Update pkg/discovery/dns/miekgdns/resolver.go

Signed-off-by: Marco Pracucci <marco@pracucci.com>
Co-authored-by: Lucas Servén Marín <lserven@gmail.com>

Co-authored-by: Lucas Servén Marín <lserven@gmail.com>
  • Loading branch information
pracucci and squat committed Apr 11, 2022
1 parent e8ad95c commit 95aa03b
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions pkg/discovery/dns/miekgdns/resolver.go
Expand Up @@ -49,7 +49,16 @@ func (r *Resolver) LookupSRV(ctx context.Context, service, proto, name string) (
return "", addrs, nil
}

func (r *Resolver) LookupIPAddr(ctx context.Context, host string) ([]net.IPAddr, error) {
func (r *Resolver) LookupIPAddr(_ context.Context, host string) ([]net.IPAddr, error) {
return r.lookupIPAddr(host, 1, 8)
}

func (r *Resolver) lookupIPAddr(host string, currIteration, maxIterations int) ([]net.IPAddr, error) {
// We want to protect from infinite loops when resolving DNS records recursively.
if currIteration > maxIterations {
return nil, errors.Errorf("maximum number of recursive iterations reached (%d)", maxIterations)
}

response, err := r.lookupWithSearchPath(host, dns.Type(dns.TypeAAAA))
if err != nil || len(response.Answer) == 0 {
// Ugly fallback to A lookup.
Expand All @@ -66,8 +75,15 @@ func (r *Resolver) LookupIPAddr(ctx context.Context, host string) ([]net.IPAddr,
resp = append(resp, net.IPAddr{IP: addr.A})
case *dns.AAAA:
resp = append(resp, net.IPAddr{IP: addr.AAAA})
case *dns.CNAME:
// Recursively resolve it.
addrs, err := r.lookupIPAddr(addr.Target, currIteration+1, maxIterations)
if err != nil {
return nil, errors.Wrapf(err, "recursively resolve %s", addr.Target)
}
resp = append(resp, addrs...)
default:
return nil, errors.Errorf("invalid A or AAAA response record %s", record)
return nil, errors.Errorf("invalid A, AAAA or CNAME response record %s", record)
}
}
return resp, nil
Expand Down

0 comments on commit 95aa03b

Please sign in to comment.