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 dragonfly 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.
26 // Send a request on the connection and hope for a reply.
27 // Up to cfg.attempts attempts.
28 func exchange(cfg
*dnsConfig
, c Conn
, name
string, qtype
uint16) (*dnsMsg
, error
) {
29 _
, useTCP
:= c
.(*TCPConn
)
31 return nil, &DNSError
{Err
: "name too long", Name
: name
}
34 out
.id
= uint16(rand
.Int()) ^ uint16(time
.Now().UnixNano())
35 out
.question
= []dnsQuestion
{
36 {name
, qtype
, dnsClassINET
},
38 out
.recursion_desired
= true
41 return nil, &DNSError
{Err
: "internal error - cannot pack message", Name
: name
}
44 mlen
:= uint16(len(msg
))
45 msg
= append([]byte{byte(mlen
>> 8), byte(mlen
)}, msg
...)
47 for attempt
:= 0; attempt
< cfg
.attempts
; attempt
++ {
48 n
, err
:= c
.Write(msg
)
54 c
.SetReadDeadline(noDeadline
)
56 c
.SetReadDeadline(time
.Now().Add(time
.Duration(cfg
.timeout
) * time
.Second
))
58 buf
:= make([]byte, 2000)
60 n
, err
= io
.ReadFull(c
, buf
[:2])
62 if e
, ok
:= err
.(Error
); ok
&& e
.Timeout() {
66 mlen
:= int(buf
[0])<<8 |
int(buf
[1])
68 buf
= make([]byte, mlen
)
70 n
, err
= io
.ReadFull(c
, buf
[:mlen
])
75 if e
, ok
:= err
.(Error
); ok
&& e
.Timeout() {
82 if !in
.Unpack(buf
) || in
.id
!= out
.id
{
88 if a
:= c
.RemoteAddr(); a
!= nil {
91 return nil, &DNSError
{Err
: "no answer from server", Name
: name
, Server
: server
, IsTimeout
: true}
94 // Do a lookup for a single name, which must be rooted
95 // (otherwise answer will not find the answers).
96 func tryOneName(cfg
*dnsConfig
, name
string, qtype
uint16) (cname
string, addrs
[]dnsRR
, err error
) {
97 if len(cfg
.servers
) == 0 {
98 return "", nil, &DNSError
{Err
: "no DNS servers", Name
: name
}
100 for i
:= 0; i
< len(cfg
.servers
); i
++ {
101 // Calling Dial here is scary -- we have to be sure
102 // not to dial a name that will require a DNS lookup,
103 // or Dial will call back here to translate it.
104 // The DNS config parser has already checked that
105 // all the cfg.servers[i] are IP addresses, which
106 // Dial will use without a DNS lookup.
107 server
:= cfg
.servers
[i
] + ":53"
108 c
, cerr
:= Dial("udp", server
)
113 msg
, merr
:= exchange(cfg
, c
, name
, qtype
)
119 if msg
.truncated
{ // see RFC 5966
120 c
, cerr
= Dial("tcp", server
)
125 msg
, merr
= exchange(cfg
, c
, name
, qtype
)
132 cname
, addrs
, err
= answer(name
, server
, msg
, qtype
)
133 if err
== nil || err
.(*DNSError
).Err
== noSuchHost
{
140 func convertRR_A(records
[]dnsRR
) []IP
{
141 addrs
:= make([]IP
, len(records
))
142 for i
, rr
:= range records
{
144 addrs
[i
] = IPv4(byte(a
>>24), byte(a
>>16), byte(a
>>8), byte(a
))
149 func convertRR_AAAA(records
[]dnsRR
) []IP
{
150 addrs
:= make([]IP
, len(records
))
151 for i
, rr
:= range records
{
152 a
:= make(IP
, IPv6len
)
153 copy(a
, rr
.(*dnsRR_AAAA
).AAAA
[:])
162 // Assume dns config file is /etc/resolv.conf here
163 func loadConfig() { cfg
, dnserr
= dnsReadConfig("/etc/resolv.conf") }
165 var onceLoadConfig sync
.Once
167 func lookup(name
string, qtype
uint16) (cname
string, addrs
[]dnsRR
, err error
) {
168 if !isDomainName(name
) {
169 return name
, nil, &DNSError
{Err
: "invalid domain name", Name
: name
}
171 onceLoadConfig
.Do(loadConfig
)
172 if dnserr
!= nil || cfg
== nil {
176 // If name is rooted (trailing dot) or has enough dots,
177 // try it by itself first.
178 rooted
:= len(name
) > 0 && name
[len(name
)-1] == '.'
179 if rooted ||
count(name
, '.') >= cfg
.ndots
{
184 // Can try as ordinary name.
185 cname
, addrs
, err
= tryOneName(cfg
, rname
, qtype
)
194 // Otherwise, try suffixes.
195 for i
:= 0; i
< len(cfg
.search
); i
++ {
196 rname
:= name
+ "." + cfg
.search
[i
]
197 if rname
[len(rname
)-1] != '.' {
200 cname
, addrs
, err
= tryOneName(cfg
, rname
, qtype
)
206 // Last ditch effort: try unsuffixed.
211 cname
, addrs
, err
= tryOneName(cfg
, rname
, qtype
)
215 if e
, ok
:= err
.(*DNSError
); ok
{
216 // Show original name passed to lookup, not suffixed one.
217 // In general we might have tried many suffixes; showing
218 // just one is misleading. See also golang.org/issue/6324.
224 // goLookupHost is the native Go implementation of LookupHost.
225 // Used only if cgoLookupHost refuses to handle the request
226 // (that is, only if cgoLookupHost is the stub in cgo_stub.go).
227 // Normally we let cgo use the C library resolver instead of
228 // depending on our lookup code, so that Go and C get the same
230 func goLookupHost(name
string) (addrs
[]string, err error
) {
231 // Use entries from /etc/hosts if they match.
232 addrs
= lookupStaticHost(name
)
236 onceLoadConfig
.Do(loadConfig
)
237 if dnserr
!= nil || cfg
== nil {
241 ips
, err
:= goLookupIP(name
)
245 addrs
= make([]string, 0, len(ips
))
246 for _
, ip
:= range ips
{
247 addrs
= append(addrs
, ip
.String())
252 // goLookupIP is the native Go implementation of LookupIP.
253 // Used only if cgoLookupIP refuses to handle the request
254 // (that is, only if cgoLookupIP is the stub in cgo_stub.go).
255 // Normally we let cgo use the C library resolver instead of
256 // depending on our lookup code, so that Go and C get the same
258 func goLookupIP(name
string) (addrs
[]IP
, err error
) {
259 // Use entries from /etc/hosts if possible.
260 haddrs
:= lookupStaticHost(name
)
262 for _
, haddr
:= range haddrs
{
263 if ip
:= ParseIP(haddr
); ip
!= nil {
264 addrs
= append(addrs
, ip
)
271 onceLoadConfig
.Do(loadConfig
)
272 if dnserr
!= nil || cfg
== nil {
279 cname
, records
, err4
= lookup(name
, dnsTypeA
)
280 addrs
= convertRR_A(records
)
284 _
, records
, err6
= lookup(name
, dnsTypeAAAA
)
285 if err4
!= nil && err6
== nil {
286 // Ignore A error because AAAA lookup succeeded.
289 if err6
!= nil && len(addrs
) > 0 {
290 // Ignore AAAA error because A lookup succeeded.
300 addrs
= append(addrs
, convertRR_AAAA(records
)...)
304 // goLookupCNAME is the native Go implementation of LookupCNAME.
305 // Used only if cgoLookupCNAME refuses to handle the request
306 // (that is, only if cgoLookupCNAME is the stub in cgo_stub.go).
307 // Normally we let cgo use the C library resolver instead of
308 // depending on our lookup code, so that Go and C get the same
310 func goLookupCNAME(name
string) (cname
string, err error
) {
311 onceLoadConfig
.Do(loadConfig
)
312 if dnserr
!= nil || cfg
== nil {
316 _
, rr
, err
:= lookup(name
, dnsTypeCNAME
)
320 cname
= rr
[0].(*dnsRR_CNAME
).Cname