| index b05641a30..e43fbde3a 100644 |
| --- a/internal/domains/freshdns.go |
| +++ b/internal/domains/freshdns.go |
| @@ -64,6 +64,11 @@ type FreshResolver struct { |
| // errNoAuthority is a fresh lookup that got no usable authoritative answer. |
| var errNoAuthority = errors.New("fresh dns: no authoritative answer") |
| |
| +// ErrBackingOff is the answer for a name whose last lookup failed less than |
| +// Backoff ago: the failure again, without asking anyone. A caller that |
| +// counts the lookups it takes can tell this one cost nothing. |
| +var ErrBackingOff = errors.New("fresh dns: backing off after a failure") |
| + |
| const ( |
| // freshMaxServers bounds the nameserver addresses tried per zone, so a |
| // zone of silent servers leaves time for the next zone up. |
| @@ -100,6 +105,16 @@ func freshPublicName(name string) bool { |
| // An authoritative "no such name" is a *net.DNSError with IsNotFound, the |
| // same shape the cached resolver gives, so callers classify both alike. |
| func (r *FreshResolver) Resolve(host string) ([]string, error) { |
| + return r.lookup(host, false) |
| +} |
| + |
| +// ResolveAll is Resolve with the name's AAAA records too (see askAll). It |
| +// shares Resolve's timeout, backoff and zone verification. |
| +func (r *FreshResolver) ResolveAll(host string) ([]string, error) { |
| + return r.lookup(host, true) |
| +} |
| + |
| +func (r *FreshResolver) lookup(host string, all bool) ([]string, error) { |
| name := strings.ToLower(strings.TrimSuffix(host, ".")) |
| if !freshPublicName(name) { |
| return nil, fmt.Errorf("%w: %s has no public zone", errNoAuthority, name) |
| @@ -121,7 +136,7 @@ func (r *FreshResolver) Resolve(host string) ([]string, error) { |
| } |
| if at, ok := r.failedAt[name]; ok { |
| r.mu.Unlock() |
| - return nil, fmt.Errorf("%w for %s (backing off since %s)", errNoAuthority, name, at.UTC().Format(time.RFC3339)) |
| + return nil, fmt.Errorf("%w: %w for %s (since %s)", errNoAuthority, ErrBackingOff, name, at.UTC().Format(time.RFC3339)) |
| } |
| r.mu.Unlock() |
| |
| @@ -131,7 +146,7 @@ func (r *FreshResolver) Resolve(host string) ([]string, error) { |
| } |
| ctx, cancel := context.WithTimeout(context.Background(), timeout) |
| defer cancel() |
| - ips, err := r.resolve(ctx, name) |
| + ips, err := r.resolve(ctx, name, all) |
| |
| if err != nil && !isNotFound(err) { |
| r.mu.Lock() |
| @@ -174,7 +189,7 @@ func isNotFound(err error) bool { |
| // target, tied to the zone that really holds the name. A level the cached |
| // resolver already knows to be an alias is skipped without asking anyone, |
| // since a CNAME cannot sit at a zone apex. |
| -func (r *FreshResolver) resolve(ctx context.Context, name string) ([]string, error) { |
| +func (r *FreshResolver) resolve(ctx context.Context, name string, all bool) ([]string, error) { |
| last := fmt.Errorf("%w: no zone found for %s", errNoAuthority, name) |
| zones := 0 |
| for zone := name; strings.Contains(zone, "."); zone = zone[strings.Index(zone, ".")+1:] { |
| @@ -204,7 +219,11 @@ func (r *FreshResolver) resolve(ctx context.Context, name string) ([]string, err |
| } |
| continue |
| } |
| - ips, err := r.ask(ctx, s, name, zone) |
| + ask := r.ask |
| + if all { |
| + ask = r.askAll |
| + } |
| + ips, err := ask(ctx, s, name, zone) |
| if err == nil || isNotFound(err) { |
| return ips, err |
| } |
| @@ -383,24 +402,34 @@ func (r *FreshResolver) verifyZone(ctx context.Context, server netip.AddrPort, z |
| return fmt.Errorf("%w: %s does not hold zone %s", errNoAuthority, server, zone) |
| } |
| |
| -// ask sends one non-recursive IN A query for name to server, which |
| -// verifyZone has shown holds zone, and reads the answer. A CNAME chain in |
| -// the reply is followed; if it ends outside zone with no address in the |
| -// reply, the target goes to the cached LookupHost. A chain that loops, runs |
| -// past freshMaxCNAMEs, or ends inside zone with no address is not an |
| -// answer. |
| -func (r *FreshResolver) ask(ctx context.Context, server netip.AddrPort, name, zone string) ([]string, error) { |
| - reply, err := r.exchange(ctx, server, name, dnsmessage.TypeA) |
| +var ( |
| + // errNoData: the server answered with authority that name (or the end |
| + // of its in-reply CNAME chain, if that is name itself) has no records |
| + // of the asked type. |
| + errNoData = errors.New("fresh dns: no records of that type") |
| + // errInZoneNoData: the chain ends at another name inside the zone and |
| + // the reply carries no records of the asked type for it. |
| + errInZoneNoData = errors.New("fresh dns: in-zone CNAME target with no records of that type") |
| +) |
| + |
| +// askType sends one non-recursive IN query of qtype (A or AAAA) for name to |
| +// server, which verifyZone has shown holds zone, and reads the answer. A |
| +// CNAME chain in the reply is followed; if it ends outside zone with no |
| +// address in the reply, the target goes to the cached LookupHost, which |
| +// answers for both families, and cached reports that. A chain that loops or |
| +// runs past freshMaxCNAMEs is not an answer. |
| +func (r *FreshResolver) askType(ctx context.Context, server netip.AddrPort, name, zone string, qtype dnsmessage.Type) (ips []string, cached bool, err error) { |
| + reply, err := r.exchange(ctx, server, name, qtype) |
| if err != nil { |
| - return nil, err |
| + return nil, false, err |
| } |
| fqdn := name + "." |
| switch reply.RCode { |
| case dnsmessage.RCodeSuccess: |
| case dnsmessage.RCodeNameError: |
| - return nil, &net.DNSError{Err: "no such host", Name: name, Server: server.String(), IsNotFound: true} |
| + return nil, false, &net.DNSError{Err: "no such host", Name: name, Server: server.String(), IsNotFound: true} |
| default: |
| - return nil, fmt.Errorf("%w: %s answered %s for %s", errNoAuthority, server, reply.RCode, name) |
| + return nil, false, fmt.Errorf("%w: %s answered %s for %s", errNoAuthority, server, reply.RCode, name) |
| } |
| |
| current := fqdn |
| @@ -418,30 +447,88 @@ func (r *FreshResolver) ask(ctx context.Context, server netip.AddrPort, name, zo |
| break |
| } |
| if seen[next] || hops >= freshMaxCNAMEs { |
| - return nil, fmt.Errorf("%w: CNAME chain from %s loops or runs too long", errNoAuthority, name) |
| + return nil, false, fmt.Errorf("%w: CNAME chain from %s loops or runs too long", errNoAuthority, name) |
| } |
| seen[next] = true |
| current = next |
| } |
| - var ips []string |
| for _, rr := range reply.Answers { |
| - if a, ok := rr.Body.(*dnsmessage.AResource); ok && rr.Header.Class == dnsmessage.ClassINET && |
| - strings.EqualFold(rr.Header.Name.String(), current) { |
| - ips = append(ips, netip.AddrFrom4(a.A).String()) |
| + if rr.Header.Class != dnsmessage.ClassINET || !strings.EqualFold(rr.Header.Name.String(), current) { |
| + continue |
| + } |
| + switch b := rr.Body.(type) { |
| + case *dnsmessage.AResource: |
| + if qtype == dnsmessage.TypeA { |
| + ips = append(ips, netip.AddrFrom4(b.A).String()) |
| + } |
| + case *dnsmessage.AAAAResource: |
| + if qtype == dnsmessage.TypeAAAA { |
| + ips = append(ips, netip.AddrFrom16(b.AAAA).String()) |
| + } |
| } |
| } |
| if len(ips) > 0 { |
| - return ips, nil |
| + return ips, false, nil |
| } |
| target := strings.TrimSuffix(current, ".") |
| if target == name { |
| - // Authoritative, the name exists, and it has no A record. |
| - return nil, &net.DNSError{Err: "no A record", Name: name, Server: server.String(), IsNotFound: true} |
| + return nil, false, errNoData |
| } |
| if target == zone || strings.HasSuffix(target, "."+zone) { |
| // In-zone target this server gave no address for: no fallback to |
| // a cache that may hold an old one. |
| - return nil, fmt.Errorf("%w: %s ends at in-zone %s with no address", errNoAuthority, name, target) |
| + return nil, false, errInZoneNoData |
| + } |
| + ips, err = r.LookupHost(ctx, target) |
| + return ips, true, err |
| +} |
| + |
| +// ask is askType for A, with Resolve's long-standing errors: no A record at |
| +// the name is "not found", and an in-zone target with no address is not an |
| +// answer at all. |
| +func (r *FreshResolver) ask(ctx context.Context, server netip.AddrPort, name, zone string) ([]string, error) { |
| + ips, _, err := r.askType(ctx, server, name, zone, dnsmessage.TypeA) |
| + switch { |
| + case errors.Is(err, errNoData): |
| + return nil, &net.DNSError{Err: "no A record", Name: name, Server: server.String(), IsNotFound: true} |
| + case errors.Is(err, errInZoneNoData): |
| + return nil, fmt.Errorf("%w: %s ends at in-zone target with no address", errNoAuthority, name) |
| + } |
| + return ips, err |
| +} |
| + |
| +// askAll is ask plus the name's AAAA records from the same server, for a |
| +// caller that must see every address a client could be sent to (the cert |
| +// warm's gate: a CA validating over IPv6 against a foreign AAAA fails even |
| +// when every A record is ours). The AAAA half may be authoritatively empty |
| +// only at the name itself; a CNAME target the reply gives no AAAA for may |
| +// sit in a delegated child this server does not answer for, so that, and |
| +// any other AAAA failure, fails the whole answer. When the A half ended at an |
| +// out-of-zone target, the cached LookupHost already answered for both |
| +// families and nothing more is asked. |
| +func (r *FreshResolver) askAll(ctx context.Context, server netip.AddrPort, name, zone string) ([]string, error) { |
| + v4, cached, err := r.askType(ctx, server, name, zone, dnsmessage.TypeA) |
| + switch { |
| + case errors.Is(err, errNoData): |
| + return nil, &net.DNSError{Err: "no A record", Name: name, Server: server.String(), IsNotFound: true} |
| + case errors.Is(err, errInZoneNoData): |
| + return nil, fmt.Errorf("%w: %s ends at in-zone target with no address", errNoAuthority, name) |
| + case err != nil: |
| + return nil, err |
| } |
| - return r.LookupHost(ctx, target) |
| + if cached { |
| + return v4, nil |
| + } |
| + v6, _, err := r.askType(ctx, server, name, zone, dnsmessage.TypeAAAA) |
| + switch { |
| + case err == nil: |
| + return append(v4, v6...), nil |
| + case errors.Is(err, errNoData): |
| + // The name itself, with no CNAME, has no AAAA: complete evidence. |
| + return v4, nil |
| + case isNotFound(err): |
| + // NXDOMAIN for AAAA right after addresses for A: inconsistent. |
| + return nil, fmt.Errorf("%w: %s gave A records but NXDOMAIN for AAAA", errNoAuthority, name) |
| + } |
| + return nil, err |
| } |