forked from folbricht/routedns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
truncate-retry.go
50 lines (42 loc) · 1.38 KB
/
truncate-retry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package rdns
import (
"github.com/miekg/dns"
)
// TruncateRetry retries truncated responses with an alternative resolver. This
// is typically used when using UDP/DTLS transports, to fail over to a stream-
// based protocol.
type TruncateRetry struct {
id string
TruncateRetryOptions
resolver Resolver
retryResolver Resolver
}
var _ Resolver = &TruncateRetry{}
type TruncateRetryOptions struct {
}
// NewTruncateRetry returns a new instance of a truncate-retry router.
func NewTruncateRetry(id string, resolver, retryResolver Resolver, opt TruncateRetryOptions) *TruncateRetry {
return &TruncateRetry{
id: id,
TruncateRetryOptions: opt,
retryResolver: retryResolver,
resolver: resolver,
}
}
// Resolve a DNS query by first resoling it upstream, if the response is truncated, the
// retry resolver is used to resolve the same query again.
func (r *TruncateRetry) Resolve(q *dns.Msg, ci ClientInfo) (*dns.Msg, error) {
a, err := r.resolver.Resolve(q, ci)
if err != nil || a == nil {
return a, err
}
// Retry the same query on the other resolver if the first one returned a truncated response.
if a.Truncated {
logger(r.id, q, ci).WithField("resolver", r.retryResolver).Debug("truncated response, forwarding to retry-resolver")
a, err = r.retryResolver.Resolve(q, ci)
}
return a, err
}
func (r *TruncateRetry) String() string {
return r.id
}