forked from miekg/unbound
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lookup.go
164 lines (151 loc) · 4.48 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
package unbound
import (
"net"
"github.com/miekg/dns"
)
// These are function are a re-implementation of the net.Lookup* ones
// They are adapted to the package unbound and the package dns.
// LookupAddr performs a reverse lookup for the given address, returning a
// list of names mapping to that address.
func (u *Unbound) LookupAddr(addr string) (name []string, err error) {
reverse, err := dns.ReverseAddr(addr)
if err != nil {
return nil, err
}
r, err := u.Resolve(reverse, dns.TypePTR, dns.ClassINET)
if err != nil {
return nil, err
}
for _, rr := range r.Rr {
name = append(name, rr.(*dns.PTR).Ptr)
}
return
}
// LookupCNAME returns the canonical DNS host for the given name. Callers
// that do not care about the canonical name can call LookupHost or
// LookupIP directly; both take care of resolving the canonical name as
// part of the lookup.
func (u *Unbound) LookupCNAME(name string) (cname string, err error) {
r, err := u.Resolve(name, dns.TypeA, dns.ClassINET)
// TODO(mg): if nothing found try AAAA?
return r.CanonName, err
}
// LookupHost looks up the given host using Unbound. It returns
// an array of that host's addresses.
func (u *Unbound) LookupHost(host string) (addrs []string, err error) {
ipaddrs, err := u.LookupIP(host)
if err != nil {
return nil, err
}
for _, ip := range ipaddrs {
addrs = append(addrs, ip.String())
}
return addrs, nil
}
// LookupIP looks up host using Unbound. It returns an array of
// that host's IPv4 and IPv6 addresses.
// The A and AAAA lookups are performed in parallel.
func (u *Unbound) LookupIP(host string) (addrs []net.IP, err error) {
c := make(chan *ResultError)
u.ResolveAsync(host, dns.TypeA, dns.ClassINET, c)
u.ResolveAsync(host, dns.TypeAAAA, dns.ClassINET, c)
seen := 0
// TODO(miek): timeout?
Wait:
for {
select {
case r := <-c:
for _, rr := range r.Rr {
if x, ok := rr.(*dns.A); ok {
addrs = append(addrs, x.A)
}
if x, ok := rr.(*dns.AAAA); ok {
addrs = append(addrs, x.AAAA)
}
}
seen++
if seen == 2 {
break Wait
}
}
}
return
}
// LookupMX returns the DNS MX records for the given domain name sorted by
// preference.
func (u *Unbound) LookupMX(name string) (mx []*dns.MX, err error) {
r, err := u.Resolve(name, dns.TypeMX, dns.ClassINET)
if err != nil {
return nil, err
}
for _, rr := range r.Rr {
mx = append(mx, rr.(*dns.MX))
}
byPref(mx).sort()
return
}
// LookupNS returns the DNS NS records for the given domain name.
func (u *Unbound) LookupNS(name string) (ns []*dns.NS, err error) {
r, err := u.Resolve(name, dns.TypeNS, dns.ClassINET)
if err != nil {
return nil, err
}
for _, rr := range r.Rr {
ns = append(ns, rr.(*dns.NS))
}
return
}
// LookupSRV tries to resolve an SRV query of the given service, protocol,
// and domain name. The proto is "tcp" or "udp". The returned records are
// sorted by priority and randomized by weight within a priority.
//
// LookupSRV constructs the DNS name to look up following RFC 2782. That
// is, it looks up _service._proto.name. To accommodate services publishing
// SRV records under non-standard names, if both service and proto are
// empty strings, LookupSRV looks up name directly.
func (u *Unbound) LookupSRV(service, proto, name string) (cname string, srv []*dns.SRV, err error) {
r := new(Result)
if service == "" && proto == "" {
r, err = u.Resolve(name, dns.TypeSRV, dns.ClassINET)
} else {
r, err = u.Resolve("_"+service+"._"+proto+"."+name, dns.TypeSRV, dns.ClassINET)
}
if err != nil {
return "", nil, err
}
for _, rr := range r.Rr {
srv = append(srv, rr.(*dns.SRV))
}
byPriorityWeight(srv).sort()
return "", srv, err
}
// LookupTXT returns the DNS TXT records for the given domain name.
func (u *Unbound) LookupTXT(name string) (txt []string, err error) {
r, err := u.Resolve(name, dns.TypeTXT, dns.ClassINET)
if err != nil {
return nil, err
}
for _, rr := range r.Rr {
txt = append(txt, rr.(*dns.TXT).Txt...)
}
return
}
// LookupTLSA returns the DNS DANE records for the given domain service, protocol
// and domainname.
//
// LookupTLSA constructs the DNS name to look up following RFC 6698. That
// is, it looks up _port._proto.name.
func (u *Unbound) LookupTLSA(service, proto, name string) (tlsa []*dns.TLSA, err error) {
tlsaname, err := dns.TLSAName(name, service, proto)
if err != nil {
return nil, err
}
r, err := u.Resolve(tlsaname, dns.TypeTLSA, dns.ClassINET)
if err != nil {
return nil, err
}
for _, rr := range r.Rr {
tlsa = append(tlsa, rr.(*dns.TLSA))
}
return tlsa, nil
}