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 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.Nanoseconds())
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)
48 c.SetReadTimeout(int64(cfg.timeout) * 1e9) // nanoseconds
50 buf := make([]byte, 2000) // More than enough.
53 if e, ok := err.(Error); ok && e.Timeout() {
60 if !in.Unpack(buf) || in.id != out.id {
66 if a := c.RemoteAddr(); a != nil {
69 return nil, &DNSError{Err: "no answer from server", Name: name, Server: server, IsTimeout: true}
72 // Do a lookup for a single name, which must be rooted
73 // (otherwise answer will not find the answers).
74 func tryOneName(cfg *dnsConfig, name string, qtype uint16) (cname string, addrs []dnsRR, err error) {
75 if len(cfg.servers) == 0 {
76 return "", nil, &DNSError{Err: "no DNS servers", Name: name}
78 for i := 0; i < len(cfg.servers); i++ {
79 // Calling Dial here is scary -- we have to be sure
80 // not to dial a name that will require a DNS lookup,
81 // or Dial will call back here to translate it.
82 // The DNS config parser has already checked that
83 // all the cfg.servers[i] are IP addresses, which
84 // Dial will use without a DNS lookup.
85 server := cfg.servers[i] + ":53"
86 c, cerr := Dial("udp", server)
91 msg, merr := exchange(cfg, c, name, qtype)
97 cname, addrs, err = answer(name, server, msg, qtype)
98 if err == nil || err.(*DNSError).Err == noSuchHost {
105 func convertRR_A(records []dnsRR) []IP {
106 addrs := make([]IP, len(records))
107 for i, rr := range records {
109 addrs[i] = IPv4(byte(a>>24), byte(a>>16), byte(a>>8), byte(a))
114 func convertRR_AAAA(records []dnsRR) []IP {
115 addrs := make([]IP, len(records))
116 for i, rr := range records {
117 a := make(IP, IPv6len)
118 copy(a, rr.(*dnsRR_AAAA).AAAA[:])
127 func loadConfig() { cfg, dnserr = dnsReadConfig() }
129 var onceLoadConfig sync.Once
131 func lookup(name string, qtype uint16) (cname string, addrs []dnsRR, err error) {
132 if !isDomainName(name) {
133 return name, nil, &DNSError{Err: "invalid domain name", Name: name}
135 onceLoadConfig.Do(loadConfig)
136 if dnserr != nil || cfg == nil {
140 // If name is rooted (trailing dot) or has enough dots,
141 // try it by itself first.
142 rooted := len(name) > 0 && name[len(name)-1] == '.'
143 if rooted || count(name, '.') >= cfg.ndots {
148 // Can try as ordinary name.
149 cname, addrs, err = tryOneName(cfg, rname, qtype)
158 // Otherwise, try suffixes.
159 for i := 0; i < len(cfg.search); i++ {
160 rname := name + "." + cfg.search[i]
161 if rname[len(rname)-1] != '.' {
164 cname, addrs, err = tryOneName(cfg, rname, qtype)
170 // Last ditch effort: try unsuffixed.
175 cname, addrs, err = tryOneName(cfg, rname, qtype)
182 // goLookupHost is the native Go implementation of LookupHost.
183 // Used only if cgoLookupHost refuses to handle the request
184 // (that is, only if cgoLookupHost is the stub in cgo_stub.go).
185 // Normally we let cgo use the C library resolver instead of
186 // depending on our lookup code, so that Go and C get the same
188 func goLookupHost(name string) (addrs []string, err error) {
189 // Use entries from /etc/hosts if they match.
190 addrs = lookupStaticHost(name)
194 onceLoadConfig.Do(loadConfig)
195 if dnserr != nil || cfg == nil {
199 ips, err := goLookupIP(name)
203 addrs = make([]string, 0, len(ips))
204 for _, ip := range ips {
205 addrs = append(addrs, ip.String())
210 // goLookupIP is the native Go implementation of LookupIP.
211 // Used only if cgoLookupIP refuses to handle the request
212 // (that is, only if cgoLookupIP is the stub in cgo_stub.go).
213 // Normally we let cgo use the C library resolver instead of
214 // depending on our lookup code, so that Go and C get the same
216 func goLookupIP(name string) (addrs []IP, err error) {
217 // Use entries from /etc/hosts if possible.
218 haddrs := lookupStaticHost(name)
220 for _, haddr := range haddrs {
221 if ip := ParseIP(haddr); ip != nil {
222 addrs = append(addrs, ip)
229 onceLoadConfig.Do(loadConfig)
230 if dnserr != nil || cfg == nil {
236 cname, records, err = lookup(name, dnsTypeA)
240 addrs = convertRR_A(records)
244 _, records, err = lookup(name, dnsTypeAAAA)
245 if err != nil && len(addrs) > 0 {
246 // Ignore error because A lookup succeeded.
252 addrs = append(addrs, convertRR_AAAA(records)...)
256 // goLookupCNAME is the native Go implementation of LookupCNAME.
257 // Used only if cgoLookupCNAME refuses to handle the request
258 // (that is, only if cgoLookupCNAME is the stub in cgo_stub.go).
259 // Normally we let cgo use the C library resolver instead of
260 // depending on our lookup code, so that Go and C get the same
262 func goLookupCNAME(name string) (cname string, err error) {
263 onceLoadConfig.Do(loadConfig)
264 if dnserr != nil || cfg == nil {
268 _, rr, err := lookup(name, dnsTypeCNAME)
272 cname = rr[0].(*dnsRR_CNAME).Cname