1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // +build darwin freebsd linux netbsd openbsd
7 // DNS client: see RFC 1035.
8 // Has to be linked into package net for Dial.
11 // Check periodically whether /etc/resolv.conf has changed.
12 // Could potentially handle many outstanding lookups faster.
13 // Could have a small cache.
14 // Random UDP source port (net.Dial should do that for us).
15 // Random request IDs.
25 // Send a request on the connection and hope for a reply.
26 // Up to cfg.attempts attempts.
27 func exchange(cfg
*dnsConfig
, c Conn
, name
string, qtype
uint16) (*dnsMsg
, error
) {
29 return nil, &DNSError
{Err
: "name too long", Name
: name
}
32 out
.id
= uint16(rand
.Int()) ^ uint16(time
.Now().UnixNano())
33 out
.question
= []dnsQuestion
{
34 {name
, qtype
, dnsClassINET
},
36 out
.recursion_desired
= true
39 return nil, &DNSError
{Err
: "internal error - cannot pack message", Name
: name
}
42 for attempt
:= 0; attempt
< cfg
.attempts
; attempt
++ {
43 n
, err
:= c
.Write(msg
)
49 c
.SetReadDeadline(time
.Time
{})
51 c
.SetReadDeadline(time
.Now().Add(time
.Duration(cfg
.timeout
) * time
.Second
))
54 buf
:= make([]byte, 2000) // More than enough.
57 if e
, ok
:= err
.(Error
); ok
&& e
.Timeout() {
64 if !in
.Unpack(buf
) || in
.id
!= out
.id
{
70 if a
:= c
.RemoteAddr(); a
!= nil {
73 return nil, &DNSError
{Err
: "no answer from server", Name
: name
, Server
: server
, IsTimeout
: true}
76 // Do a lookup for a single name, which must be rooted
77 // (otherwise answer will not find the answers).
78 func tryOneName(cfg
*dnsConfig
, name
string, qtype
uint16) (cname
string, addrs
[]dnsRR
, err error
) {
79 if len(cfg
.servers
) == 0 {
80 return "", nil, &DNSError
{Err
: "no DNS servers", Name
: name
}
82 for i
:= 0; i
< len(cfg
.servers
); i
++ {
83 // Calling Dial here is scary -- we have to be sure
84 // not to dial a name that will require a DNS lookup,
85 // or Dial will call back here to translate it.
86 // The DNS config parser has already checked that
87 // all the cfg.servers[i] are IP addresses, which
88 // Dial will use without a DNS lookup.
89 server
:= cfg
.servers
[i
] + ":53"
90 c
, cerr
:= Dial("udp", server
)
95 msg
, merr
:= exchange(cfg
, c
, name
, qtype
)
101 cname
, addrs
, err
= answer(name
, server
, msg
, qtype
)
102 if err
== nil || err
.(*DNSError
).Err
== noSuchHost
{
109 func convertRR_A(records
[]dnsRR
) []IP
{
110 addrs
:= make([]IP
, len(records
))
111 for i
, rr
:= range records
{
113 addrs
[i
] = IPv4(byte(a
>>24), byte(a
>>16), byte(a
>>8), byte(a
))
118 func convertRR_AAAA(records
[]dnsRR
) []IP
{
119 addrs
:= make([]IP
, len(records
))
120 for i
, rr
:= range records
{
121 a
:= make(IP
, IPv6len
)
122 copy(a
, rr
.(*dnsRR_AAAA
).AAAA
[:])
131 func loadConfig() { cfg
, dnserr
= dnsReadConfig() }
133 var onceLoadConfig sync
.Once
135 func lookup(name
string, qtype
uint16) (cname
string, addrs
[]dnsRR
, err error
) {
136 if !isDomainName(name
) {
137 return name
, nil, &DNSError
{Err
: "invalid domain name", Name
: name
}
139 onceLoadConfig
.Do(loadConfig
)
140 if dnserr
!= nil || cfg
== nil {
144 // If name is rooted (trailing dot) or has enough dots,
145 // try it by itself first.
146 rooted
:= len(name
) > 0 && name
[len(name
)-1] == '.'
147 if rooted ||
count(name
, '.') >= cfg
.ndots
{
152 // Can try as ordinary name.
153 cname
, addrs
, err
= tryOneName(cfg
, rname
, qtype
)
162 // Otherwise, try suffixes.
163 for i
:= 0; i
< len(cfg
.search
); i
++ {
164 rname
:= name
+ "." + cfg
.search
[i
]
165 if rname
[len(rname
)-1] != '.' {
168 cname
, addrs
, err
= tryOneName(cfg
, rname
, qtype
)
174 // Last ditch effort: try unsuffixed.
179 cname
, addrs
, err
= tryOneName(cfg
, rname
, qtype
)
186 // goLookupHost is the native Go implementation of LookupHost.
187 // Used only if cgoLookupHost refuses to handle the request
188 // (that is, only if cgoLookupHost is the stub in cgo_stub.go).
189 // Normally we let cgo use the C library resolver instead of
190 // depending on our lookup code, so that Go and C get the same
192 func goLookupHost(name
string) (addrs
[]string, err error
) {
193 // Use entries from /etc/hosts if they match.
194 addrs
= lookupStaticHost(name
)
198 onceLoadConfig
.Do(loadConfig
)
199 if dnserr
!= nil || cfg
== nil {
203 ips
, err
:= goLookupIP(name
)
207 addrs
= make([]string, 0, len(ips
))
208 for _
, ip
:= range ips
{
209 addrs
= append(addrs
, ip
.String())
214 // goLookupIP is the native Go implementation of LookupIP.
215 // Used only if cgoLookupIP refuses to handle the request
216 // (that is, only if cgoLookupIP is the stub in cgo_stub.go).
217 // Normally we let cgo use the C library resolver instead of
218 // depending on our lookup code, so that Go and C get the same
220 func goLookupIP(name
string) (addrs
[]IP
, err error
) {
221 // Use entries from /etc/hosts if possible.
222 haddrs
:= lookupStaticHost(name
)
224 for _
, haddr
:= range haddrs
{
225 if ip
:= ParseIP(haddr
); ip
!= nil {
226 addrs
= append(addrs
, ip
)
233 onceLoadConfig
.Do(loadConfig
)
234 if dnserr
!= nil || cfg
== nil {
241 cname
, records
, err4
= lookup(name
, dnsTypeA
)
242 addrs
= convertRR_A(records
)
246 _
, records
, err6
= lookup(name
, dnsTypeAAAA
)
247 if err4
!= nil && err6
== nil {
248 // Ignore A error because AAAA lookup succeeded.
251 if err6
!= nil && len(addrs
) > 0 {
252 // Ignore AAAA error because A lookup succeeded.
262 addrs
= append(addrs
, convertRR_AAAA(records
)...)
266 // goLookupCNAME is the native Go implementation of LookupCNAME.
267 // Used only if cgoLookupCNAME refuses to handle the request
268 // (that is, only if cgoLookupCNAME is the stub in cgo_stub.go).
269 // Normally we let cgo use the C library resolver instead of
270 // depending on our lookup code, so that Go and C get the same
272 func goLookupCNAME(name
string) (cname
string, err error
) {
273 onceLoadConfig
.Do(loadConfig
)
274 if dnserr
!= nil || cfg
== nil {
278 _
, rr
, err
:= lookup(name
, dnsTypeCNAME
)
282 cname
= rr
[0].(*dnsRR_CNAME
).Cname