blob: 387a9bbf9cfceafa765ff5f2f679f318354829b4 [file] [log] [blame]
Russ Cox83348f92008-12-18 15:42:39 -08001// 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.
4
Russ Coxa0bcaf42009-06-25 20:24:55 -07005// DNS client: see RFC 1035.
Russ Cox83348f92008-12-18 15:42:39 -08006// Has to be linked into package net for Dial.
7
8// TODO(rsc):
9// Check periodically whether /etc/resolv.conf has changed.
10// Could potentially handle many outstanding lookups faster.
11// Could have a small cache.
12// Random UDP source port (net.Dial should do that for us).
13// Random request IDs.
Russ Cox83348f92008-12-18 15:42:39 -080014
15package net
16
17import (
Robert Griesemera3d10452009-12-15 15:35:38 -080018 "once"
19 "os"
Russ Coxc312d0e2010-02-10 16:35:35 -080020 "rand"
21 "time"
Russ Cox83348f92008-12-18 15:42:39 -080022)
23
Russ Coxa0bcaf42009-06-25 20:24:55 -070024// DNSError represents a DNS lookup error.
Russ Cox1b301ba2009-05-08 14:40:20 -070025type DNSError struct {
Robert Griesemera3d10452009-12-15 15:35:38 -080026 Error string // description of the error
27 Name string // name looked for
28 Server string // server used
Russ Cox1b301ba2009-05-08 14:40:20 -070029}
Russ Coxa0bcaf42009-06-25 20:24:55 -070030
31func (e *DNSError) String() string {
Robert Griesemera3d10452009-12-15 15:35:38 -080032 s := "lookup " + e.Name
Russ Coxa0bcaf42009-06-25 20:24:55 -070033 if e.Server != "" {
Robert Griesemer40621d52009-11-09 12:07:39 -080034 s += " on " + e.Server
Russ Coxa0bcaf42009-06-25 20:24:55 -070035 }
Robert Griesemera3d10452009-12-15 15:35:38 -080036 s += ": " + e.Error
37 return s
Russ Coxa0bcaf42009-06-25 20:24:55 -070038}
39
40const noSuchHost = "no such host"
Russ Cox83348f92008-12-18 15:42:39 -080041
42// Send a request on the connection and hope for a reply.
43// Up to cfg.attempts attempts.
Rob Pikeaaf63f82009-04-17 00:08:24 -070044func _Exchange(cfg *_DNS_Config, c Conn, name string) (m *_DNS_Msg, err os.Error) {
Russ Cox83348f92008-12-18 15:42:39 -080045 if len(name) >= 256 {
Robert Griesemer40621d52009-11-09 12:07:39 -080046 return nil, &DNSError{"name too long", name, ""}
Russ Cox83348f92008-12-18 15:42:39 -080047 }
Robert Griesemera3d10452009-12-15 15:35:38 -080048 out := new(_DNS_Msg)
Russ Coxc312d0e2010-02-10 16:35:35 -080049 out.id = uint16(rand.Int()) ^ uint16(time.Nanoseconds())
Russ Coxc93da7c72009-03-05 15:48:12 -080050 out.question = []_DNS_Question{
Robert Griesemer5d377052009-11-04 23:16:46 -080051 _DNS_Question{name, _DNS_TypeA, _DNS_ClassINET},
Robert Griesemera3d10452009-12-15 15:35:38 -080052 }
53 out.recursion_desired = true
54 msg, ok := out.Pack()
Russ Cox83348f92008-12-18 15:42:39 -080055 if !ok {
Robert Griesemer40621d52009-11-09 12:07:39 -080056 return nil, &DNSError{"internal error - cannot pack message", name, ""}
Russ Cox83348f92008-12-18 15:42:39 -080057 }
58
59 for attempt := 0; attempt < cfg.attempts; attempt++ {
Robert Griesemera3d10452009-12-15 15:35:38 -080060 n, err := c.Write(msg)
Russ Cox83348f92008-12-18 15:42:39 -080061 if err != nil {
Robert Griesemer40621d52009-11-09 12:07:39 -080062 return nil, err
Russ Cox83348f92008-12-18 15:42:39 -080063 }
64
Stephen Mab73e5922010-03-03 15:25:26 +110065 c.SetReadTimeout(int64(cfg.timeout) * 1e9) // nanoseconds
Russ Cox83348f92008-12-18 15:42:39 -080066
Robert Griesemera3d10452009-12-15 15:35:38 -080067 buf := make([]byte, 2000) // More than enough.
68 n, err = c.Read(buf)
Robert Griesemer5d377052009-11-04 23:16:46 -080069 if isEAGAIN(err) {
Robert Griesemera3d10452009-12-15 15:35:38 -080070 err = nil
71 continue
Russ Cox1e37e8a2009-03-06 17:51:31 -080072 }
Russ Cox83348f92008-12-18 15:42:39 -080073 if err != nil {
Robert Griesemer40621d52009-11-09 12:07:39 -080074 return nil, err
Russ Cox83348f92008-12-18 15:42:39 -080075 }
Robert Griesemera3d10452009-12-15 15:35:38 -080076 buf = buf[0:n]
77 in := new(_DNS_Msg)
Russ Cox83348f92008-12-18 15:42:39 -080078 if !in.Unpack(buf) || in.id != out.id {
Robert Griesemer40621d52009-11-09 12:07:39 -080079 continue
Russ Cox83348f92008-12-18 15:42:39 -080080 }
Robert Griesemera3d10452009-12-15 15:35:38 -080081 return in, nil
Russ Cox83348f92008-12-18 15:42:39 -080082 }
Robert Griesemera3d10452009-12-15 15:35:38 -080083 var server string
Russ Coxc83b8382009-11-02 18:37:30 -080084 if a := c.RemoteAddr(); a != nil {
Robert Griesemer40621d52009-11-09 12:07:39 -080085 server = a.String()
Russ Coxc83b8382009-11-02 18:37:30 -080086 }
Robert Griesemera3d10452009-12-15 15:35:38 -080087 return nil, &DNSError{"no answer from server", name, server}
Russ Cox83348f92008-12-18 15:42:39 -080088}
89
Russ Coxd47d8882008-12-18 22:37:22 -080090
Russ Cox83348f92008-12-18 15:42:39 -080091// Find answer for name in dns message.
92// On return, if err == nil, addrs != nil.
Russ Coxa0bcaf42009-06-25 20:24:55 -070093func answer(name, server string, dns *_DNS_Msg) (addrs []string, err *DNSError) {
Robert Griesemera3d10452009-12-15 15:35:38 -080094 addrs = make([]string, 0, len(dns.answer))
Russ Cox83348f92008-12-18 15:42:39 -080095
Russ Coxa0bcaf42009-06-25 20:24:55 -070096 if dns.rcode == _DNS_RcodeNameError && dns.recursion_available {
Robert Griesemer40621d52009-11-09 12:07:39 -080097 return nil, &DNSError{noSuchHost, name, ""}
Russ Cox83348f92008-12-18 15:42:39 -080098 }
Russ Coxc93da7c72009-03-05 15:48:12 -080099 if dns.rcode != _DNS_RcodeSuccess {
Russ Cox83348f92008-12-18 15:42:39 -0800100 // None of the error codes make sense
101 // for the query we sent. If we didn't get
102 // a name error and we didn't get success,
103 // the server is behaving incorrectly.
Robert Griesemer40621d52009-11-09 12:07:39 -0800104 return nil, &DNSError{"server misbehaving", name, server}
Russ Cox83348f92008-12-18 15:42:39 -0800105 }
106
107 // Look for the name.
108 // Presotto says it's okay to assume that servers listed in
109 // /etc/resolv.conf are recursive resolvers.
110 // We asked for recursion, so it should have included
111 // all the answers we need in this one packet.
112Cname:
113 for cnameloop := 0; cnameloop < 10; cnameloop++ {
Robert Griesemera3d10452009-12-15 15:35:38 -0800114 addrs = addrs[0:0]
Russ Cox83348f92008-12-18 15:42:39 -0800115 for i := 0; i < len(dns.answer); i++ {
Robert Griesemera3d10452009-12-15 15:35:38 -0800116 rr := dns.answer[i]
117 h := rr.Header()
Russ Coxf1bc7122009-07-07 11:04:26 -0700118 if h.Class == _DNS_ClassINET && h.Name == name {
119 switch h.Rrtype {
Russ Coxc93da7c72009-03-05 15:48:12 -0800120 case _DNS_TypeA:
Robert Griesemera3d10452009-12-15 15:35:38 -0800121 n := len(addrs)
122 a := rr.(*_DNS_RR_A).A
123 addrs = addrs[0 : n+1]
124 addrs[n] = IPv4(byte(a>>24), byte(a>>16), byte(a>>8), byte(a)).String()
Russ Coxc93da7c72009-03-05 15:48:12 -0800125 case _DNS_TypeCNAME:
Russ Cox83348f92008-12-18 15:42:39 -0800126 // redirect to cname
Robert Griesemera3d10452009-12-15 15:35:38 -0800127 name = rr.(*_DNS_RR_CNAME).Cname
128 continue Cname
Russ Cox83348f92008-12-18 15:42:39 -0800129 }
130 }
131 }
132 if len(addrs) == 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800133 return nil, &DNSError{noSuchHost, name, server}
Russ Cox83348f92008-12-18 15:42:39 -0800134 }
Robert Griesemera3d10452009-12-15 15:35:38 -0800135 return addrs, nil
Russ Cox83348f92008-12-18 15:42:39 -0800136 }
137
Robert Griesemera3d10452009-12-15 15:35:38 -0800138 return nil, &DNSError{"too many redirects", name, server}
Russ Cox83348f92008-12-18 15:42:39 -0800139}
140
141// Do a lookup for a single name, which must be rooted
Russ Coxd8921c52009-02-15 14:18:39 -0800142// (otherwise answer will not find the answers).
Rob Pikeaaf63f82009-04-17 00:08:24 -0700143func tryOneName(cfg *_DNS_Config, name string) (addrs []string, err os.Error) {
Russ Coxa0bcaf42009-06-25 20:24:55 -0700144 if len(cfg.servers) == 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800145 return nil, &DNSError{"no DNS servers", name, ""}
Russ Coxa0bcaf42009-06-25 20:24:55 -0700146 }
Russ Cox83348f92008-12-18 15:42:39 -0800147 for i := 0; i < len(cfg.servers); i++ {
148 // Calling Dial here is scary -- we have to be sure
149 // not to dial a name that will require a DNS lookup,
150 // or Dial will call back here to translate it.
151 // The DNS config parser has already checked that
152 // all the cfg.servers[i] are IP addresses, which
153 // Dial will use without a DNS lookup.
Robert Griesemera3d10452009-12-15 15:35:38 -0800154 server := cfg.servers[i] + ":53"
155 c, cerr := Dial("udp", "", server)
Russ Cox83348f92008-12-18 15:42:39 -0800156 if cerr != nil {
Robert Griesemera3d10452009-12-15 15:35:38 -0800157 err = cerr
158 continue
Russ Cox83348f92008-12-18 15:42:39 -0800159 }
Robert Griesemera3d10452009-12-15 15:35:38 -0800160 msg, merr := _Exchange(cfg, c, name)
161 c.Close()
Russ Cox83348f92008-12-18 15:42:39 -0800162 if merr != nil {
Robert Griesemera3d10452009-12-15 15:35:38 -0800163 err = merr
164 continue
Russ Cox83348f92008-12-18 15:42:39 -0800165 }
Robert Griesemera3d10452009-12-15 15:35:38 -0800166 var dnserr *DNSError
167 addrs, dnserr = answer(name, server, msg)
Russ Coxa0bcaf42009-06-25 20:24:55 -0700168 if dnserr != nil {
Robert Griesemer40621d52009-11-09 12:07:39 -0800169 err = dnserr
Russ Coxa0bcaf42009-06-25 20:24:55 -0700170 } else {
Robert Griesemera3d10452009-12-15 15:35:38 -0800171 err = nil // nil os.Error, not nil *DNSError
Russ Cox83348f92008-12-18 15:42:39 -0800172 }
Russ Coxa0bcaf42009-06-25 20:24:55 -0700173 if dnserr == nil || dnserr.Error == noSuchHost {
Robert Griesemer40621d52009-11-09 12:07:39 -0800174 break
Russ Coxa0bcaf42009-06-25 20:24:55 -0700175 }
Russ Cox83348f92008-12-18 15:42:39 -0800176 }
Robert Griesemera3d10452009-12-15 15:35:38 -0800177 return
Russ Cox83348f92008-12-18 15:42:39 -0800178}
179
Russ Coxc93da7c72009-03-05 15:48:12 -0800180var cfg *_DNS_Config
Rob Pikeaaf63f82009-04-17 00:08:24 -0700181var dnserr os.Error
Russ Cox83348f92008-12-18 15:42:39 -0800182
Robert Griesemera3d10452009-12-15 15:35:38 -0800183func loadConfig() { cfg, dnserr = _DNS_ReadConfig() }
Russ Cox83348f92008-12-18 15:42:39 -0800184
Russ Coxa0bcaf42009-06-25 20:24:55 -0700185func isDomainName(s string) bool {
186 // Requirements on DNS name:
187 // * must not be empty.
188 // * must be alphanumeric plus - and .
189 // * each of the dot-separated elements must begin
190 // and end with a letter or digit.
191 // RFC 1035 required the element to begin with a letter,
192 // but RFC 3696 says this has been relaxed to allow digits too.
193 // still, there must be a letter somewhere in the entire name.
194 if len(s) == 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800195 return false
Russ Coxa0bcaf42009-06-25 20:24:55 -0700196 }
Robert Griesemera3d10452009-12-15 15:35:38 -0800197 if s[len(s)-1] != '.' { // simplify checking loop: make name end in dot
Robert Griesemer40621d52009-11-09 12:07:39 -0800198 s += "."
Russ Coxa0bcaf42009-06-25 20:24:55 -0700199 }
200
Robert Griesemera3d10452009-12-15 15:35:38 -0800201 last := byte('.')
202 ok := false // ok once we've seen a letter
Russ Coxa0bcaf42009-06-25 20:24:55 -0700203 for i := 0; i < len(s); i++ {
Robert Griesemera3d10452009-12-15 15:35:38 -0800204 c := s[i]
Russ Coxa0bcaf42009-06-25 20:24:55 -0700205 switch {
206 default:
Robert Griesemer40621d52009-11-09 12:07:39 -0800207 return false
Russ Coxa0bcaf42009-06-25 20:24:55 -0700208 case 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z':
Robert Griesemer40621d52009-11-09 12:07:39 -0800209 ok = true
Russ Coxa0bcaf42009-06-25 20:24:55 -0700210 case '0' <= c && c <= '9':
211 // fine
212 case c == '-':
213 // byte before dash cannot be dot
214 if last == '.' {
Robert Griesemer40621d52009-11-09 12:07:39 -0800215 return false
Russ Coxa0bcaf42009-06-25 20:24:55 -0700216 }
217 case c == '.':
218 // byte before dot cannot be dot, dash
219 if last == '.' || last == '-' {
Robert Griesemer40621d52009-11-09 12:07:39 -0800220 return false
Russ Coxa0bcaf42009-06-25 20:24:55 -0700221 }
222 }
Robert Griesemera3d10452009-12-15 15:35:38 -0800223 last = c
Russ Coxa0bcaf42009-06-25 20:24:55 -0700224 }
225
Robert Griesemera3d10452009-12-15 15:35:38 -0800226 return ok
Russ Coxa0bcaf42009-06-25 20:24:55 -0700227}
228
Yves Junqueirad6054fc2010-01-15 13:43:14 -0800229// LookupHost looks for name using the local hosts file and DNS resolver.
Russ Coxc93da7c72009-03-05 15:48:12 -0800230// It returns the canonical name for the host and an array of that
231// host's addresses.
Russ Coxa0bcaf42009-06-25 20:24:55 -0700232func LookupHost(name string) (cname string, addrs []string, err os.Error) {
233 if !isDomainName(name) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800234 return name, nil, &DNSError{"invalid domain name", name, ""}
Russ Coxa0bcaf42009-06-25 20:24:55 -0700235 }
Robert Griesemera3d10452009-12-15 15:35:38 -0800236 once.Do(loadConfig)
Russ Coxc93da7c72009-03-05 15:48:12 -0800237 if dnserr != nil || cfg == nil {
Robert Griesemera3d10452009-12-15 15:35:38 -0800238 err = dnserr
239 return
Russ Cox83348f92008-12-18 15:42:39 -0800240 }
Yves Junqueirad6054fc2010-01-15 13:43:14 -0800241 // Use entries from /etc/hosts if they match.
242 addrs = lookupStaticHost(name)
243 if len(addrs) > 0 {
244 cname = name
245 return
246 }
Russ Cox83348f92008-12-18 15:42:39 -0800247 // If name is rooted (trailing dot) or has enough dots,
248 // try it by itself first.
Robert Griesemera3d10452009-12-15 15:35:38 -0800249 rooted := len(name) > 0 && name[len(name)-1] == '.'
Russ Cox35ace1d2009-11-01 11:15:34 -0800250 if rooted || count(name, '.') >= cfg.ndots {
Robert Griesemera3d10452009-12-15 15:35:38 -0800251 rname := name
Russ Cox83348f92008-12-18 15:42:39 -0800252 if !rooted {
Robert Griesemer40621d52009-11-09 12:07:39 -0800253 rname += "."
Russ Cox83348f92008-12-18 15:42:39 -0800254 }
255 // Can try as ordinary name.
Robert Griesemera3d10452009-12-15 15:35:38 -0800256 addrs, err = tryOneName(cfg, rname)
Russ Coxa0bcaf42009-06-25 20:24:55 -0700257 if err == nil {
Robert Griesemera3d10452009-12-15 15:35:38 -0800258 cname = rname
259 return
Russ Cox83348f92008-12-18 15:42:39 -0800260 }
Russ Cox83348f92008-12-18 15:42:39 -0800261 }
262 if rooted {
Robert Griesemer40621d52009-11-09 12:07:39 -0800263 return
Russ Cox83348f92008-12-18 15:42:39 -0800264 }
265
266 // Otherwise, try suffixes.
267 for i := 0; i < len(cfg.search); i++ {
Robert Griesemera3d10452009-12-15 15:35:38 -0800268 rname := name + "." + cfg.search[i]
Russ Coxa0bcaf42009-06-25 20:24:55 -0700269 if rname[len(rname)-1] != '.' {
Robert Griesemer40621d52009-11-09 12:07:39 -0800270 rname += "."
Russ Cox83348f92008-12-18 15:42:39 -0800271 }
Robert Griesemera3d10452009-12-15 15:35:38 -0800272 addrs, err = tryOneName(cfg, rname)
Russ Coxa0bcaf42009-06-25 20:24:55 -0700273 if err == nil {
Robert Griesemera3d10452009-12-15 15:35:38 -0800274 cname = rname
275 return
Russ Cox83348f92008-12-18 15:42:39 -0800276 }
Russ Cox83348f92008-12-18 15:42:39 -0800277 }
Russ Cox484f46d2009-11-10 17:09:33 -0800278
279 // Last ditch effort: try unsuffixed.
Robert Griesemera3d10452009-12-15 15:35:38 -0800280 rname := name
Russ Cox484f46d2009-11-10 17:09:33 -0800281 if !rooted {
282 rname += "."
283 }
Robert Griesemera3d10452009-12-15 15:35:38 -0800284 addrs, err = tryOneName(cfg, rname)
Russ Cox484f46d2009-11-10 17:09:33 -0800285 if err == nil {
Robert Griesemera3d10452009-12-15 15:35:38 -0800286 cname = rname
287 return
Russ Cox484f46d2009-11-10 17:09:33 -0800288 }
Robert Griesemera3d10452009-12-15 15:35:38 -0800289 return
Russ Cox83348f92008-12-18 15:42:39 -0800290}