libgo: Merge from revision 18783:00cce3a34d7e of master library.
[official-gcc.git] / libgo / go / net / dnsclient_unix.go
bloba30c9a73d7e0e920f7a8a7ee33e79ff44b39b0a4
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.
10 // TODO(rsc):
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.
17 package net
19 import (
20 "io"
21 "math/rand"
22 "sync"
23 "time"
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)
30 if len(name) >= 256 {
31 return nil, &DNSError{Err: "name too long", Name: name}
33 out := new(dnsMsg)
34 out.id = uint16(rand.Int()) ^ uint16(time.Now().UnixNano())
35 out.question = []dnsQuestion{
36 {name, qtype, dnsClassINET},
38 out.recursion_desired = true
39 msg, ok := out.Pack()
40 if !ok {
41 return nil, &DNSError{Err: "internal error - cannot pack message", Name: name}
43 if useTCP {
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)
49 if err != nil {
50 return nil, err
53 if cfg.timeout == 0 {
54 c.SetReadDeadline(noDeadline)
55 } else {
56 c.SetReadDeadline(time.Now().Add(time.Duration(cfg.timeout) * time.Second))
58 buf := make([]byte, 2000)
59 if useTCP {
60 n, err = io.ReadFull(c, buf[:2])
61 if err != nil {
62 if e, ok := err.(Error); ok && e.Timeout() {
63 continue
66 mlen := int(buf[0])<<8 | int(buf[1])
67 if mlen > len(buf) {
68 buf = make([]byte, mlen)
70 n, err = io.ReadFull(c, buf[:mlen])
71 } else {
72 n, err = c.Read(buf)
74 if err != nil {
75 if e, ok := err.(Error); ok && e.Timeout() {
76 continue
78 return nil, err
80 buf = buf[:n]
81 in := new(dnsMsg)
82 if !in.Unpack(buf) || in.id != out.id {
83 continue
85 return in, nil
87 var server string
88 if a := c.RemoteAddr(); a != nil {
89 server = a.String()
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)
109 if cerr != nil {
110 err = cerr
111 continue
113 msg, merr := exchange(cfg, c, name, qtype)
114 c.Close()
115 if merr != nil {
116 err = merr
117 continue
119 if msg.truncated { // see RFC 5966
120 c, cerr = Dial("tcp", server)
121 if cerr != nil {
122 err = cerr
123 continue
125 msg, merr = exchange(cfg, c, name, qtype)
126 c.Close()
127 if merr != nil {
128 err = merr
129 continue
132 cname, addrs, err = answer(name, server, msg, qtype)
133 if err == nil || err.(*DNSError).Err == noSuchHost {
134 break
137 return
140 func convertRR_A(records []dnsRR) []IP {
141 addrs := make([]IP, len(records))
142 for i, rr := range records {
143 a := rr.(*dnsRR_A).A
144 addrs[i] = IPv4(byte(a>>24), byte(a>>16), byte(a>>8), byte(a))
146 return addrs
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[:])
154 addrs[i] = a
156 return addrs
159 var cfg *dnsConfig
160 var dnserr error
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 {
173 err = dnserr
174 return
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 {
180 rname := name
181 if !rooted {
182 rname += "."
184 // Can try as ordinary name.
185 cname, addrs, err = tryOneName(cfg, rname, qtype)
186 if err == nil {
187 return
190 if rooted {
191 return
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] != '.' {
198 rname += "."
200 cname, addrs, err = tryOneName(cfg, rname, qtype)
201 if err == nil {
202 return
206 // Last ditch effort: try unsuffixed.
207 rname := name
208 if !rooted {
209 rname += "."
211 cname, addrs, err = tryOneName(cfg, rname, qtype)
212 if err == nil {
213 return
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.
219 e.Name = name
221 return
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
229 // answers.
230 func goLookupHost(name string) (addrs []string, err error) {
231 // Use entries from /etc/hosts if they match.
232 addrs = lookupStaticHost(name)
233 if len(addrs) > 0 {
234 return
236 onceLoadConfig.Do(loadConfig)
237 if dnserr != nil || cfg == nil {
238 err = dnserr
239 return
241 ips, err := goLookupIP(name)
242 if err != nil {
243 return
245 addrs = make([]string, 0, len(ips))
246 for _, ip := range ips {
247 addrs = append(addrs, ip.String())
249 return
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
257 // answers.
258 func goLookupIP(name string) (addrs []IP, err error) {
259 // Use entries from /etc/hosts if possible.
260 haddrs := lookupStaticHost(name)
261 if len(haddrs) > 0 {
262 for _, haddr := range haddrs {
263 if ip := ParseIP(haddr); ip != nil {
264 addrs = append(addrs, ip)
267 if len(addrs) > 0 {
268 return
271 onceLoadConfig.Do(loadConfig)
272 if dnserr != nil || cfg == nil {
273 err = dnserr
274 return
276 var records []dnsRR
277 var cname string
278 var err4, err6 error
279 cname, records, err4 = lookup(name, dnsTypeA)
280 addrs = convertRR_A(records)
281 if cname != "" {
282 name = cname
284 _, records, err6 = lookup(name, dnsTypeAAAA)
285 if err4 != nil && err6 == nil {
286 // Ignore A error because AAAA lookup succeeded.
287 err4 = nil
289 if err6 != nil && len(addrs) > 0 {
290 // Ignore AAAA error because A lookup succeeded.
291 err6 = nil
293 if err4 != nil {
294 return nil, err4
296 if err6 != nil {
297 return nil, err6
300 addrs = append(addrs, convertRR_AAAA(records)...)
301 return addrs, nil
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
309 // answers.
310 func goLookupCNAME(name string) (cname string, err error) {
311 onceLoadConfig.Do(loadConfig)
312 if dnserr != nil || cfg == nil {
313 err = dnserr
314 return
316 _, rr, err := lookup(name, dnsTypeCNAME)
317 if err != nil {
318 return
320 cname = rr[0].(*dnsRR_CNAME).Cname
321 return