| 182 | } |
| 183 | |
| 184 | func (r *Resolver) query(ctx context.Context, conf *dns.ClientConfig, name string, qType dns.Type) (*dns.Msg, error) { |
| 185 | // We don't support search domains, all names are assumed to be fully qualified already. |
| 186 | msg := new(dns.Msg).SetQuestion(dns.Fqdn(name), uint16(qType)) |
| 187 | |
| 188 | merr := multierror.New() |
| 189 | // `man 5 resolv.conf` says that we should try each server, continuing to the next if |
| 190 | // there is a timeout. We should repeat this process up to "attempt" times trying to get |
| 191 | // a viable response. |
| 192 | // |
| 193 | // > (The algorithm used is to try a name server, and if the query times out, try the next, |
| 194 | // > until out of name servers, then repeat trying all the name servers until a maximum number |
| 195 | // > of retries are made.) |
| 196 | for i := 0; i < conf.Attempts; i++ { |
| 197 | for _, ip := range conf.Servers { |
| 198 | server := net.JoinHostPort(ip, conf.Port) |
| 199 | response, _, err := r.client.Exchange(ctx, msg, server) |
| 200 | if err != nil { |
| 201 | merr.Add(fmt.Errorf("resolution against server %s: %w", server, err)) |
| 202 | continue |
| 203 | } |
| 204 | |
| 205 | if response.Truncated { |
| 206 | merr.Add(fmt.Errorf("resolution against server %s: response truncated", server)) |
| 207 | continue |
| 208 | } |
| 209 | |
| 210 | if response.Rcode == dns.RcodeSuccess || response.Rcode == dns.RcodeNameError { |
| 211 | return response, nil |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | return nil, fmt.Errorf("could not resolve %s: no servers returned a viable answer. Errs %s", name, merr.Err()) |
| 217 | } |
| 218 | |
| 219 | // loop periodically reloads configuration from resolv.conf until the resolver is stopped. |
| 220 | func (r *Resolver) loop() { |