-
Notifications
You must be signed in to change notification settings - Fork 7
/
dns.go
265 lines (219 loc) · 5.71 KB
/
dns.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
package dane
import (
"errors"
"fmt"
"net"
"github.com/miekg/dns"
)
//
// Query contains parameters of a DNS query: name, type, and class.
//
type Query struct {
Name string
Type uint16
Class uint16
}
//
// NewQuery returns an initialized Query structure from the given query
// parameters.
//
func NewQuery(qname string, qtype uint16, qclass uint16) *Query {
q := new(Query)
q.Name = dns.Fqdn(qname)
q.Type = qtype
q.Class = qclass
return q
}
//
// MakeQuery constructs a DNS query message (*dns.Msg) from the given
// query and resolver parameters.
//
func makeQueryMessage(query *Query, resolver *Resolver) *dns.Msg {
m := new(dns.Msg)
m.Id = dns.Id()
m.RecursionDesired = resolver.Rdflag
m.AuthenticatedData = resolver.Adflag
m.CheckingDisabled = resolver.Cdflag
m.SetEdns0(resolver.Payload, true)
m.Question = make([]dns.Question, 1)
m.Question[0] = dns.Question{Name: query.Name, Qtype: query.Type,
Qclass: query.Class}
return m
}
//
// SendQueryUDP sends a DNS query via UDP with timeout and retries if
// necessary.
//
func sendQueryUDP(query *Query, resolver *Resolver) (*dns.Msg, error) {
var response *dns.Msg
var err error
m := makeQueryMessage(query, resolver)
c := new(dns.Client)
c.Net = "udp"
c.Timeout = resolver.Timeout
retries := resolver.Retries
for retries > 0 {
for _, server := range resolver.Servers {
response, _, err = c.Exchange(m, server.Address())
if err == nil {
return response, err
}
if nerr, ok := err.(net.Error); ok && !nerr.Timeout() {
continue
}
}
retries--
}
return nil, err
}
//
// SendQueryTCP sends a DNS query via TCP.
//
func sendQueryTCP(query *Query, resolver *Resolver) (*dns.Msg, error) {
var response *dns.Msg
var err error
m := makeQueryMessage(query, resolver)
c := new(dns.Client)
c.Net = "tcp"
c.Timeout = resolver.Timeout
for _, server := range resolver.Servers {
response, _, err = c.Exchange(m, server.Address())
if err == nil {
return response, err
}
}
return response, err
}
//
// SendQuery sends a DNS query via UDP with fallback to TCP upon truncation.
//
func sendQuery(query *Query, resolver *Resolver) (*dns.Msg, error) {
var response *dns.Msg
var err error
response, err = sendQueryUDP(query, resolver)
if err == nil && response.MsgHdr.Truncated {
response, err = sendQueryTCP(query, resolver)
}
if err != nil {
return nil, err
}
if response == nil {
return nil, errors.New("null response to DNS query")
}
return response, err
}
//
// responseOK determines whether we have an authoritative response in
// the given DNS message (NOERROR or NXDOMAIN).
//
func responseOK(response *dns.Msg) bool {
switch response.MsgHdr.Rcode {
case dns.RcodeSuccess, dns.RcodeNameError:
return true
default:
return false
}
}
//
// GetAddresses obtains a list of IPv4 and IPv6 addresses for given hostname.
//
func GetAddresses(resolver *Resolver, hostname string, secure bool) ([]net.IP, error) {
var ipList []net.IP
var q *Query
var rrTypes []uint16
if resolver.IPv6 {
rrTypes = append(rrTypes, dns.TypeAAAA)
}
if resolver.IPv4 {
rrTypes = append(rrTypes, dns.TypeA)
}
for _, rrtype := range rrTypes {
q = NewQuery(hostname, rrtype, dns.ClassINET)
response, err := sendQuery(q, resolver)
if err != nil {
return nil, err
}
if !responseOK(response) {
return nil, fmt.Errorf("address lookup for %s failed, rcode %d",
hostname, response.MsgHdr.Rcode)
}
if response.MsgHdr.Rcode == dns.RcodeNameError {
return nil, fmt.Errorf("%s: non-existent domain name", hostname)
}
if secure && !response.MsgHdr.AuthenticatedData {
return nil, fmt.Errorf("%s address response was not authenticated", hostname)
}
for _, rr := range response.Answer {
if rr.Header().Rrtype == rrtype {
if rrtype == dns.TypeAAAA {
ipList = append(ipList, rr.(*dns.AAAA).AAAA)
} else if rrtype == dns.TypeA {
ipList = append(ipList, rr.(*dns.A).A)
}
}
}
}
return ipList, nil
}
//
// Message2TSLAinfo returns a populated TLSAinfo structure from the
// contents of a given dns message that contains a response to a
// TLSA query. The qname parameter provides the expected TLSA query
// name string.
//
func Message2TSLAinfo(qname string, message *dns.Msg) *TLSAinfo {
var tr *TLSArdata
tlsa := new(TLSAinfo)
tlsa.Qname = dns.Fqdn(qname)
for _, rr := range message.Answer {
if tlsarr, ok := rr.(*dns.TLSA); ok {
if tlsarr.Hdr.Name != tlsa.Qname {
tlsa.Alias = append(tlsa.Alias, tlsarr.Hdr.Name)
}
tr = new(TLSArdata)
tr.Usage = tlsarr.Usage
tr.Selector = tlsarr.Selector
tr.Mtype = tlsarr.MatchingType
tr.Data = tlsarr.Certificate
tlsa.Rdata = append(tlsa.Rdata, tr)
}
}
return tlsa
}
//
// GetTLSA returns the DNS TLSA RRset information for the given hostname,
// port and resolver parameters.
//
func GetTLSA(resolver *Resolver, hostname string, port int) (*TLSAinfo, error) {
var q *Query
qname := fmt.Sprintf("_%d._tcp.%s", port, hostname)
q = NewQuery(qname, dns.TypeTLSA, dns.ClassINET)
response, err := sendQuery(q, resolver)
if err != nil {
return nil, err
}
if !responseOK(response) {
return nil, fmt.Errorf("bad response code to TLSA query %s: %s", qname,
dns.RcodeToString[response.MsgHdr.Rcode])
}
if !response.MsgHdr.AuthenticatedData {
if resolver.Pkixfallback {
return nil, nil
}
return nil, fmt.Errorf("response unauthenticated: %s/TLSA", qname)
}
if response.MsgHdr.Rcode == dns.RcodeNameError {
if resolver.Pkixfallback {
return nil, nil
}
return nil, fmt.Errorf("%s: non-existent domain name", hostname)
}
tlsa := Message2TSLAinfo(q.Name, response)
if len(tlsa.Rdata) == 0 {
if resolver.Pkixfallback {
return nil, nil
}
return nil, fmt.Errorf("no TLSA records found: %s", qname)
}
return tlsa, err
}