-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.go
284 lines (240 loc) · 6.03 KB
/
iterator.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package resolv
import (
"fmt"
"log"
"net"
"strings"
"github.com/miekg/dns"
"golang.org/x/net/context"
)
// Iterator represents an iterative DNS query.
type Iterator struct {
Debug bool
rs *Resolver
}
// NewIterator initializes an Iterator structure.
func NewIterator(r *Resolver) *Iterator {
return &Iterator{rs: r}
}
// LookupIP looks up the IPv4 and IPv6 addresses for the host.
func (it *Iterator) LookupIP(ctx context.Context, host string) ([]net.IP, error) {
a4, err := it.LookupIPv4(ctx, host)
if err != nil {
return nil, err
}
a6, err := it.LookupIPv6(ctx, host)
if err != nil {
return nil, err
}
return append(a4, a6...), nil
}
// LookupIPv4 looks up the IPv4 addresses for the host.
func (it *Iterator) LookupIPv4(ctx context.Context, host string) ([]net.IP, error) {
if ip, ok := toIPv4(host); ok {
return []net.IP{ip}, nil
}
var last *Response
for r := range it.Resolve(ctx, host, dns.TypeA) {
if r.Err != nil {
return nil, r.Err
}
last = r
}
var ips []net.IP
for _, i := range last.Msg.Answer {
switch i.(type) {
case (*dns.A):
rr := i.(*dns.A)
ips = append(ips, rr.A)
default:
return nil, fmt.Errorf("iterator: unexpected record: %v", i)
}
}
return ips, nil
}
// LookupIPv6 looks up the IPv6 addresses for the host.
func (it *Iterator) LookupIPv6(ctx context.Context, host string) ([]net.IP, error) {
if ip, ok := toIPv6(host); ok {
return []net.IP{ip}, nil
}
var last *Response
for r := range it.Resolve(ctx, host, dns.TypeAAAA) {
if r.Err != nil {
return nil, r.Err
}
last = r
}
var ips []net.IP
for _, i := range last.Msg.Answer {
switch i.(type) {
case (*dns.AAAA):
rr := i.(*dns.AAAA)
ips = append(ips, rr.AAAA)
default:
return nil, fmt.Errorf("iterator: unexpected record: %v", i)
}
}
return ips, nil
}
// LookupNS looks up the DNS NS records for the given name.
func (it *Iterator) LookupNS(ctx context.Context, host string) ([]*net.NS, error) {
var last *Response
for r := range it.Resolve(ctx, host, dns.TypeNS) {
if r.Err != nil {
return nil, r.Err
}
last = r
}
var nss []*net.NS
for _, i := range last.Msg.Answer {
switch i.(type) {
case (*dns.NS):
rr := i.(*dns.NS)
hn := strings.ToLower(rr.Ns)
nss = append(nss, &net.NS{Host: hn})
default:
return nil, fmt.Errorf("iterator: unexpected record: %v", i)
}
}
return nss, nil
}
// Delegation looks up the delegation returning the parent name and the DNS NS records for a given name.
func (it *Iterator) Delegation(ctx context.Context, name string) (string, []*net.NS, error) {
// We'll look in the Authority section unless the name
// is ".", for this case we'll check the Answer section.
// We have at least one response (the root servers).
var prev, last *Response
for r := range it.Resolve(ctx, name, dns.TypeNS) {
if r.Err != nil {
return "", nil, r.Err
}
if last == nil {
prev = r
last = r
}
prev = last
last = r
}
s := prev.Msg.Ns
if last == prev {
// Root domain (name = ".").
s = prev.Msg.Answer
}
var nss []*net.NS
for _, i := range s {
switch i.(type) {
case (*dns.NS):
rr := i.(*dns.NS)
hn := strings.ToLower(rr.Ns)
nss = append(nss, &net.NS{Host: hn})
default:
return "", nil, fmt.Errorf("iterator: unexpected record: %v", i)
}
}
return prev.Host(), nss, nil
}
// Resolve looks up the name starting from root servers following referals.
func (it *Iterator) Resolve(ctx context.Context, name string, type_ uint16) <-chan *Response {
return it.run(ctx, name, type_, 0, 0, map[string]bool{}, RootServers...)
}
func (it *Iterator) run(ctx context.Context, name string, type_ uint16, depth, i int, skip map[string]bool, nss ...string) <-chan *Response {
out := make(chan *Response, MaxDepth)
defer close(out)
if depth > MaxDepth {
out <- &Response{Err: fmt.Errorf("iterator: max depth reached")}
return out
}
for ; len(nss) > 0; i++ {
var ns string
if i > MaxIterations {
out <- &Response{Err: fmt.Errorf("iterator: max iterations reached")}
return out
}
// Peek random name server and mark it as used.
ns, nss = peekRandom(nss)
if _, ok := skip[ns]; ok {
continue
}
skip[ns] = true
// Issue DNS query.
fqdn := dns.Fqdn(strings.ToLower(name))
c := it.rs.Resolve(
NewRequest(ns, fqdn, type_),
)
select {
case resp := <-c:
if it.Debug {
log.Println("iterator: servers=", nss)
log.Println("iterator: ===>", resp.Addr, resp)
}
if resp.Err != nil {
// Ignore DNS errors, stop if error is NXDOMAIN.
if err, ok := resp.Err.(*DNSError); ok {
switch {
case err.NameError():
out <- &Response{Err: err}
return out
default:
continue
}
}
out <- resp
return out
}
out <- resp
if referals, cname, ok := it.lookup(resp.Msg, fqdn); ok {
if cname != "" {
// Follow CNAME.
for sr := range it.run(ctx, cname, type_, depth+1, i+1, map[string]bool{}, RootServers...) {
out <- sr
}
}
} else {
// Follow referals.
for sr := range it.run(ctx, name, type_, depth+1, i+1, skip, referals...) {
out <- sr
}
}
return out
case <-ctx.Done():
out <- &Response{Err: ctx.Err()}
return out
}
}
out <- &Response{Err: fmt.Errorf("iterator: no more servers to try")}
return out
}
func (it *Iterator) lookup(msg *dns.Msg, fqdn string) ([]string, string, bool) {
cname := ""
found := false
// Look for fqdn in the Answer section.
for _, i := range msg.Answer {
nm := strings.ToLower(i.Header().Name)
if nm == fqdn {
found = true
}
switch i.(type) {
case (*dns.CNAME):
rr := i.(*dns.CNAME)
cname = dns.Fqdn(strings.ToLower(rr.Target))
}
}
if found {
return nil, cname, found
}
// If fqdn is not found then collect and return referals.
var nss []string
for _, i := range msg.Ns {
switch i.(type) {
case *dns.NS:
rr := i.(*dns.NS)
nm := strings.ToLower(rr.Header().Name)
if strings.HasSuffix(fqdn, nm) {
nss = append(nss, strings.ToLower(rr.Ns))
}
default:
log.Println("iterator: bad RR type", i, "for", fqdn)
}
}
return nss, cname, found
}