-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlookup.go
165 lines (130 loc) · 3.2 KB
/
lookup.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
package goresolver
import (
"log"
"net"
"github.com/miekg/dns"
)
const MaxReturnedIPAddressesCount = 64
func (resolver *Resolver) LookupIP(qname string) (ips []net.IP, err error) {
if len(qname) < 1 {
return nil, nil
}
qtypes := []uint16{dns.TypeA, dns.TypeAAAA}
answers := make([]*RRSet, 0, len(qtypes))
for _, qtype := range qtypes {
answer, err := resolver.queryRRset(qname, qtype)
if answer == nil {
continue
}
if err != nil {
continue
}
if answer.IsEmpty() {
continue
}
if !answer.IsSigned() {
continue
}
answers = append(answers, answer)
}
if len(answers) < 1 {
log.Printf("no results")
return nil, ErrNoResult
}
signerName := answers[0].SignerName()
authChain := NewAuthenticationChain()
err = authChain.Populate(signerName)
if err != nil {
log.Printf("Cannot populate authentication chain: %s\n", err)
return nil, err
}
resultIPs := make([]net.IP, MaxReturnedIPAddressesCount)
for _, answer := range answers {
err = authChain.Verify(answer)
if err != nil {
log.Printf("DNSSEC validation failed: %s\n", err)
continue
}
ips := formatResultRRs(answer)
resultIPs = append(resultIPs, ips...)
}
return resultIPs, nil
}
func (resolver *Resolver) LookupIPv4(qname string) (ips []net.IP, err error) {
return resolver.LookupIPType(qname, dns.TypeA)
}
func (resolver *Resolver) LookupIPv6(qname string) (ips []net.IP, err error) {
return resolver.LookupIPType(qname, dns.TypeAAAA)
}
// Queries an A or AAAA RR
func (resolver *Resolver) LookupIPType(qname string, qtype uint16) (ips []net.IP, err error) {
if len(qname) < 1 {
return nil, nil
}
answer, err := resolver.queryRRset(qname, qtype)
if answer == nil {
return nil, ErrNoResult
}
if err != nil {
return nil, err
}
if !answer.IsSigned() {
return formatResultRRs(answer), ErrResourceNotSigned
}
signerName := answer.SignerName()
authChain := NewAuthenticationChain()
err = authChain.Populate(signerName)
if err != nil {
log.Printf("Cannot populate authentication chain: %s\n", err)
return nil, err
}
err = authChain.Verify(answer)
if err != nil {
log.Printf("DNSSEC validation failed: %s\n", err)
return nil, err
}
return formatResultRRs(answer), nil
}
func (resolver *Resolver) StrictNSQuery(qname string, qtype uint16) (rrSet []dns.RR, err error) {
if len(qname) < 1 {
return nil, ErrInvalidQuery
}
answer, err := resolver.queryRRset(qname, qtype)
if err != nil {
return nil, err
}
if answer.IsEmpty() {
return nil, ErrNoResult
}
if !answer.IsSigned() {
return nil, ErrResourceNotSigned
}
err = answer.CheckHeaderIntegrity(qname)
if err != nil {
return nil, err
}
signerName := answer.SignerName()
authChain := NewAuthenticationChain()
err = authChain.Populate(signerName)
if err == ErrNoResult {
return nil, err
}
err = authChain.Verify(answer)
if err != nil {
log.Printf("DNSSEC validation failed: %s\n", err)
return nil, err
}
return answer.rrSet, nil
}
func formatResultRRs(signedRrset *RRSet) []net.IP {
ips := make([]net.IP, 0, len(signedRrset.rrSet))
for _, rr := range signedRrset.rrSet {
switch t := rr.(type) {
case *dns.A:
ips = append(ips, t.A)
case *dns.AAAA:
ips = append(ips, t.AAAA)
}
}
return ips
}