2008-12-18 16:42:39 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// DNS client.
|
|
|
|
// Has to be linked into package net for Dial.
|
|
|
|
|
|
|
|
// TODO(rsc):
|
|
|
|
// Check periodically whether /etc/resolv.conf has changed.
|
|
|
|
// Could potentially handle many outstanding lookups faster.
|
|
|
|
// Could have a small cache.
|
|
|
|
// Random UDP source port (net.Dial should do that for us).
|
|
|
|
// Random request IDs.
|
|
|
|
// More substantial error reporting.
|
|
|
|
// Remove use of fmt?
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt";
|
|
|
|
"io";
|
|
|
|
"net";
|
|
|
|
"once";
|
|
|
|
"os";
|
|
|
|
"strings";
|
|
|
|
)
|
|
|
|
|
2009-03-05 16:48:12 -07:00
|
|
|
// DNS errors returned by LookupHost.
|
2009-05-08 15:40:20 -06:00
|
|
|
type DNSError struct {
|
|
|
|
os.ErrorString
|
|
|
|
}
|
2009-01-20 15:40:40 -07:00
|
|
|
var (
|
2009-05-08 15:40:20 -06:00
|
|
|
DNS_InternalError os.Error = &DNSError{"internal dns error"};
|
|
|
|
DNS_MissingConfig os.Error = &DNSError{"no dns configuration"};
|
|
|
|
DNS_No_Answer os.Error = &DNSError{"dns got no answer"};
|
|
|
|
DNS_BadRequest os.Error = &DNSError{"malformed dns request"};
|
|
|
|
DNS_BadReply os.Error = &DNSError{"malformed dns reply"};
|
|
|
|
DNS_ServerFailure os.Error = &DNSError{"dns server failure"};
|
|
|
|
DNS_NoServers os.Error = &DNSError{"no dns servers"};
|
|
|
|
DNS_NameTooLong os.Error = &DNSError{"dns name too long"};
|
|
|
|
DNS_RedirectLoop os.Error = &DNSError{"dns redirect loop"};
|
|
|
|
DNS_NameNotFound os.Error = &DNSError{"dns name not found"};
|
2009-03-05 16:48:12 -07:00
|
|
|
)
|
2008-12-18 16:42:39 -07:00
|
|
|
|
|
|
|
// Send a request on the connection and hope for a reply.
|
|
|
|
// Up to cfg.attempts attempts.
|
2009-04-17 01:08:24 -06:00
|
|
|
func _Exchange(cfg *_DNS_Config, c Conn, name string) (m *_DNS_Msg, err os.Error) {
|
2008-12-18 16:42:39 -07:00
|
|
|
if len(name) >= 256 {
|
|
|
|
return nil, DNS_NameTooLong
|
|
|
|
}
|
2009-03-05 16:48:12 -07:00
|
|
|
out := new(_DNS_Msg);
|
2008-12-18 16:42:39 -07:00
|
|
|
out.id = 0x1234;
|
2009-03-05 16:48:12 -07:00
|
|
|
out.question = []_DNS_Question{
|
|
|
|
_DNS_Question{ name, _DNS_TypeA, _DNS_ClassINET }
|
2009-03-03 09:39:12 -07:00
|
|
|
};
|
2008-12-18 16:42:39 -07:00
|
|
|
out.recursion_desired = true;
|
|
|
|
msg, ok := out.Pack();
|
|
|
|
if !ok {
|
|
|
|
return nil, DNS_InternalError
|
|
|
|
}
|
|
|
|
|
|
|
|
for attempt := 0; attempt < cfg.attempts; attempt++ {
|
|
|
|
n, err := c.Write(msg);
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2009-03-06 18:51:31 -07:00
|
|
|
c.SetReadTimeout(1e9); // nanoseconds
|
2008-12-18 16:42:39 -07:00
|
|
|
|
2009-01-06 16:19:02 -07:00
|
|
|
buf := make([]byte, 2000); // More than enough.
|
2008-12-18 16:42:39 -07:00
|
|
|
n, err = c.Read(buf);
|
2009-03-06 18:51:31 -07:00
|
|
|
if err == os.EAGAIN {
|
|
|
|
continue;
|
|
|
|
}
|
2008-12-18 16:42:39 -07:00
|
|
|
if err != nil {
|
2009-03-06 18:51:31 -07:00
|
|
|
return nil, err;
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
buf = buf[0:n];
|
2009-03-05 16:48:12 -07:00
|
|
|
in := new(_DNS_Msg);
|
2008-12-18 16:42:39 -07:00
|
|
|
if !in.Unpack(buf) || in.id != out.id {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return in, nil
|
|
|
|
}
|
2009-01-16 12:04:44 -07:00
|
|
|
return nil, DNS_No_Answer
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
|
2008-12-18 23:37:22 -07:00
|
|
|
|
2008-12-18 16:42:39 -07:00
|
|
|
// Find answer for name in dns message.
|
|
|
|
// On return, if err == nil, addrs != nil.
|
2009-03-06 18:51:31 -07:00
|
|
|
// TODO(rsc): Maybe return []IP instead?
|
2009-04-17 01:08:24 -06:00
|
|
|
func answer(name string, dns *_DNS_Msg) (addrs []string, err os.Error) {
|
2009-01-06 16:19:02 -07:00
|
|
|
addrs = make([]string, 0, len(dns.answer));
|
2008-12-18 16:42:39 -07:00
|
|
|
|
2009-03-05 16:48:12 -07:00
|
|
|
if dns.rcode == _DNS_RcodeNameError && dns.authoritative {
|
2008-12-19 04:05:37 -07:00
|
|
|
return nil, DNS_NameNotFound // authoritative "no such host"
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
2009-03-05 16:48:12 -07:00
|
|
|
if dns.rcode != _DNS_RcodeSuccess {
|
2008-12-18 16:42:39 -07:00
|
|
|
// None of the error codes make sense
|
|
|
|
// for the query we sent. If we didn't get
|
|
|
|
// a name error and we didn't get success,
|
|
|
|
// the server is behaving incorrectly.
|
2008-12-19 04:05:37 -07:00
|
|
|
return nil, DNS_ServerFailure
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Look for the name.
|
|
|
|
// Presotto says it's okay to assume that servers listed in
|
|
|
|
// /etc/resolv.conf are recursive resolvers.
|
|
|
|
// We asked for recursion, so it should have included
|
|
|
|
// all the answers we need in this one packet.
|
|
|
|
Cname:
|
|
|
|
for cnameloop := 0; cnameloop < 10; cnameloop++ {
|
|
|
|
addrs = addrs[0:0];
|
|
|
|
for i := 0; i < len(dns.answer); i++ {
|
|
|
|
rr := dns.answer[i];
|
|
|
|
h := rr.Header();
|
2009-03-05 16:48:12 -07:00
|
|
|
if h.class == _DNS_ClassINET && h.name == name {
|
2008-12-18 16:42:39 -07:00
|
|
|
switch h.rrtype {
|
2009-03-05 16:48:12 -07:00
|
|
|
case _DNS_TypeA:
|
2008-12-18 16:42:39 -07:00
|
|
|
n := len(addrs);
|
2009-03-05 16:48:12 -07:00
|
|
|
a := rr.(*_DNS_RR_A).a;
|
2008-12-18 16:42:39 -07:00
|
|
|
addrs = addrs[0:n+1];
|
2009-01-15 14:48:11 -07:00
|
|
|
addrs[n] = fmt.Sprintf("%d.%d.%d.%d", (a>>24), (a>>16)&0xFF, (a>>8)&0xFF, a&0xFF);
|
2009-03-05 16:48:12 -07:00
|
|
|
case _DNS_TypeCNAME:
|
2008-12-18 16:42:39 -07:00
|
|
|
// redirect to cname
|
2009-03-05 16:48:12 -07:00
|
|
|
name = rr.(*_DNS_RR_CNAME).cname;
|
2008-12-18 16:42:39 -07:00
|
|
|
continue Cname
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(addrs) == 0 {
|
2008-12-19 04:05:37 -07:00
|
|
|
return nil, DNS_NameNotFound
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
return addrs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Too many redirects
|
2008-12-19 04:05:37 -07:00
|
|
|
return nil, DNS_RedirectLoop
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do a lookup for a single name, which must be rooted
|
2009-02-15 15:18:39 -07:00
|
|
|
// (otherwise answer will not find the answers).
|
2009-04-17 01:08:24 -06:00
|
|
|
func tryOneName(cfg *_DNS_Config, name string) (addrs []string, err os.Error) {
|
2008-12-18 16:42:39 -07:00
|
|
|
err = DNS_NoServers;
|
|
|
|
for i := 0; i < len(cfg.servers); i++ {
|
|
|
|
// Calling Dial here is scary -- we have to be sure
|
|
|
|
// not to dial a name that will require a DNS lookup,
|
|
|
|
// or Dial will call back here to translate it.
|
|
|
|
// The DNS config parser has already checked that
|
|
|
|
// all the cfg.servers[i] are IP addresses, which
|
|
|
|
// Dial will use without a DNS lookup.
|
|
|
|
c, cerr := Dial("udp", "", cfg.servers[i] + ":53");
|
|
|
|
if cerr != nil {
|
|
|
|
err = cerr;
|
|
|
|
continue;
|
|
|
|
}
|
2009-01-16 12:04:44 -07:00
|
|
|
msg, merr := _Exchange(cfg, c, name);
|
2008-12-18 16:42:39 -07:00
|
|
|
c.Close();
|
|
|
|
if merr != nil {
|
|
|
|
err = merr;
|
|
|
|
continue;
|
|
|
|
}
|
2009-02-15 15:18:39 -07:00
|
|
|
addrs, aerr := answer(name, msg);
|
2008-12-18 16:42:39 -07:00
|
|
|
if aerr != nil && aerr != DNS_NameNotFound {
|
|
|
|
err = aerr;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return addrs, aerr;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-03-05 16:48:12 -07:00
|
|
|
var cfg *_DNS_Config
|
2009-04-17 01:08:24 -06:00
|
|
|
var dnserr os.Error
|
2008-12-18 16:42:39 -07:00
|
|
|
|
2009-02-15 15:18:39 -07:00
|
|
|
func loadConfig() {
|
2009-03-05 16:48:12 -07:00
|
|
|
cfg, dnserr = _DNS_ReadConfig();
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
|
2009-03-05 16:48:12 -07:00
|
|
|
// LookupHost looks up the host name using the local DNS resolver.
|
|
|
|
// It returns the canonical name for the host and an array of that
|
|
|
|
// host's addresses.
|
2009-04-17 01:08:24 -06:00
|
|
|
func LookupHost(name string) (cname string, addrs []string, err os.Error)
|
2009-03-05 16:48:12 -07:00
|
|
|
{
|
2008-12-18 16:42:39 -07:00
|
|
|
// TODO(rsc): Pick out obvious non-DNS names to avoid
|
|
|
|
// sending stupid requests to the server?
|
|
|
|
|
2009-02-15 15:18:39 -07:00
|
|
|
once.Do(loadConfig);
|
2009-03-05 16:48:12 -07:00
|
|
|
if dnserr != nil || cfg == nil {
|
|
|
|
// better error than file not found.
|
2008-12-18 16:42:39 -07:00
|
|
|
err = DNS_MissingConfig;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If name is rooted (trailing dot) or has enough dots,
|
|
|
|
// try it by itself first.
|
|
|
|
rooted := len(name) > 0 && name[len(name)-1] == '.';
|
2009-01-16 13:47:24 -07:00
|
|
|
if rooted || strings.Count(name, ".") >= cfg.ndots {
|
2008-12-18 16:42:39 -07:00
|
|
|
rname := name;
|
|
|
|
if !rooted {
|
|
|
|
rname += ".";
|
|
|
|
}
|
|
|
|
// Can try as ordinary name.
|
2009-02-15 15:18:39 -07:00
|
|
|
addrs, aerr := tryOneName(cfg, rname);
|
2008-12-18 16:42:39 -07:00
|
|
|
if aerr == nil {
|
|
|
|
return rname, addrs, nil;
|
|
|
|
}
|
|
|
|
err = aerr;
|
|
|
|
}
|
|
|
|
if rooted {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, try suffixes.
|
|
|
|
for i := 0; i < len(cfg.search); i++ {
|
|
|
|
newname := name+"."+cfg.search[i];
|
|
|
|
if newname[len(newname)-1] != '.' {
|
|
|
|
newname += "."
|
|
|
|
}
|
2009-02-15 15:18:39 -07:00
|
|
|
addrs, aerr := tryOneName(cfg, newname);
|
2008-12-18 16:42:39 -07:00
|
|
|
if aerr == nil {
|
|
|
|
return newname, addrs, nil;
|
|
|
|
}
|
|
|
|
err = aerr;
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|