2011-06-12 18:22:31 -06: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.
|
|
|
|
|
2014-09-18 04:17:55 -06:00
|
|
|
// +build darwin dragonfly freebsd linux netbsd openbsd solaris
|
build: add build comments to core packages
The go/build package already recognizes
system-specific file names like
mycode_darwin.go
mycode_darwin_386.go
mycode_386.s
However, it is also common to write files that
apply to multiple architectures, so a recent CL added
to go/build the ability to process comments
listing a set of conditions for building. For example:
// +build darwin freebsd openbsd/386
says that this file should be compiled only on
OS X, FreeBSD, or 32-bit x86 OpenBSD systems.
These conventions are not yet documented
(hence this long CL description).
This CL adds build comments to the multi-system
files in the core library, a step toward making it
possible to use go/build to build them.
With this change go/build can handle crypto/rand,
exec, net, path/filepath, os/user, and time.
os and syscall need additional adjustments.
R=golang-dev, r, gri, r, gustavo
CC=golang-dev
https://golang.org/cl/5011046
2011-09-15 14:48:57 -06:00
|
|
|
|
2011-06-12 18:22:31 -06:00
|
|
|
// DNS client: see RFC 1035.
|
|
|
|
// Has to be linked into package net for Dial.
|
|
|
|
|
|
|
|
// TODO(rsc):
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2016-04-14 18:47:25 -06:00
|
|
|
"context"
|
2014-08-05 18:58:47 -06:00
|
|
|
"errors"
|
2013-08-13 10:44:12 -06:00
|
|
|
"io"
|
2011-11-08 16:41:54 -07:00
|
|
|
"math/rand"
|
2014-05-14 18:11:00 -06:00
|
|
|
"os"
|
2011-06-12 18:22:31 -06:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2014-08-05 18:58:47 -06:00
|
|
|
// A dnsConn represents a DNS transport endpoint.
|
|
|
|
type dnsConn interface {
|
2015-11-19 12:24:42 -07:00
|
|
|
io.Closer
|
|
|
|
|
|
|
|
SetDeadline(time.Time) error
|
2014-08-05 18:58:47 -06:00
|
|
|
|
2016-04-15 20:19:58 -06:00
|
|
|
// dnsRoundTrip executes a single DNS transaction, returning a
|
|
|
|
// DNS response message for the provided DNS query message.
|
|
|
|
dnsRoundTrip(query *dnsMsg) (*dnsMsg, error)
|
2014-08-05 18:58:47 -06:00
|
|
|
}
|
|
|
|
|
2017-06-08 14:19:28 -06:00
|
|
|
// dnsPacketConn implements the dnsConn interface for RFC 1035's
|
|
|
|
// "UDP usage" transport mechanism. Conn is a packet-oriented connection,
|
|
|
|
// such as a *UDPConn.
|
|
|
|
type dnsPacketConn struct {
|
|
|
|
Conn
|
2016-04-15 20:19:58 -06:00
|
|
|
}
|
|
|
|
|
2017-06-08 14:19:28 -06:00
|
|
|
func (c *dnsPacketConn) dnsRoundTrip(query *dnsMsg) (*dnsMsg, error) {
|
2016-04-15 20:19:58 -06:00
|
|
|
b, ok := query.Pack()
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("cannot marshal DNS message")
|
|
|
|
}
|
|
|
|
if _, err := c.Write(b); err != nil {
|
2014-08-05 18:58:47 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-04-15 20:19:58 -06:00
|
|
|
|
|
|
|
b = make([]byte, 512) // see RFC 1035
|
|
|
|
for {
|
|
|
|
n, err := c.Read(b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resp := &dnsMsg{}
|
|
|
|
if !resp.Unpack(b[:n]) || !resp.IsResponseTo(query) {
|
|
|
|
// Ignore invalid responses as they may be malicious
|
|
|
|
// forgery attempts. Instead continue waiting until
|
|
|
|
// timeout. See golang.org/issue/13281.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return resp, nil
|
2014-08-05 18:58:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-08 14:19:28 -06:00
|
|
|
// dnsStreamConn implements the dnsConn interface for RFC 1035's
|
|
|
|
// "TCP usage" transport mechanism. Conn is a stream-oriented connection,
|
|
|
|
// such as a *TCPConn.
|
|
|
|
type dnsStreamConn struct {
|
|
|
|
Conn
|
2016-04-15 20:19:58 -06:00
|
|
|
}
|
|
|
|
|
2017-06-08 14:19:28 -06:00
|
|
|
func (c *dnsStreamConn) dnsRoundTrip(query *dnsMsg) (*dnsMsg, error) {
|
2016-04-15 20:19:58 -06:00
|
|
|
b, ok := query.Pack()
|
2014-08-05 18:58:47 -06:00
|
|
|
if !ok {
|
2016-04-15 20:19:58 -06:00
|
|
|
return nil, errors.New("cannot marshal DNS message")
|
2014-08-05 18:58:47 -06:00
|
|
|
}
|
2016-04-15 20:19:58 -06:00
|
|
|
l := len(b)
|
|
|
|
b = append([]byte{byte(l >> 8), byte(l)}, b...)
|
2014-08-05 18:58:47 -06:00
|
|
|
if _, err := c.Write(b); err != nil {
|
2016-04-15 20:19:58 -06:00
|
|
|
return nil, err
|
2014-08-05 18:58:47 -06:00
|
|
|
}
|
|
|
|
|
2016-04-15 20:19:58 -06:00
|
|
|
b = make([]byte, 1280) // 1280 is a reasonable initial size for IP over Ethernet, see RFC 4035
|
2014-08-05 18:58:47 -06:00
|
|
|
if _, err := io.ReadFull(c, b[:2]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-04-15 20:19:58 -06:00
|
|
|
l = int(b[0])<<8 | int(b[1])
|
2014-08-05 18:58:47 -06:00
|
|
|
if l > len(b) {
|
|
|
|
b = make([]byte, l)
|
2011-06-12 18:22:31 -06:00
|
|
|
}
|
2014-08-05 18:58:47 -06:00
|
|
|
n, err := io.ReadFull(c, b[:l])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2011-06-12 18:22:31 -06:00
|
|
|
}
|
2016-04-15 20:19:58 -06:00
|
|
|
resp := &dnsMsg{}
|
|
|
|
if !resp.Unpack(b[:n]) {
|
2014-08-05 18:58:47 -06:00
|
|
|
return nil, errors.New("cannot unmarshal DNS message")
|
|
|
|
}
|
2016-04-15 20:19:58 -06:00
|
|
|
if !resp.IsResponseTo(query) {
|
|
|
|
return nil, errors.New("invalid DNS response")
|
2011-06-12 18:22:31 -06:00
|
|
|
}
|
2016-04-15 20:19:58 -06:00
|
|
|
return resp, nil
|
2014-08-05 18:58:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// exchange sends a query on the connection and hopes for a response.
|
2017-02-20 06:58:55 -07:00
|
|
|
func (r *Resolver) exchange(ctx context.Context, server, name string, qtype uint16, timeout time.Duration) (*dnsMsg, error) {
|
2014-08-05 18:58:47 -06:00
|
|
|
out := dnsMsg{
|
|
|
|
dnsMsgHdr: dnsMsgHdr{
|
|
|
|
recursion_desired: true,
|
|
|
|
},
|
|
|
|
question: []dnsQuestion{
|
|
|
|
{name, qtype, dnsClassINET},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, network := range []string{"udp", "tcp"} {
|
2016-08-29 14:53:32 -06:00
|
|
|
// TODO(mdempsky): Refactor so defers from UDP-based
|
|
|
|
// exchanges happen before TCP-based exchange.
|
|
|
|
|
|
|
|
ctx, cancel := context.WithDeadline(ctx, time.Now().Add(timeout))
|
|
|
|
defer cancel()
|
|
|
|
|
2017-02-20 06:58:55 -07:00
|
|
|
c, err := r.dial(ctx, network, server)
|
2011-06-12 18:22:31 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-08-05 18:58:47 -06:00
|
|
|
defer c.Close()
|
net: fix plan9 after context change, propagate contexts more
My previous https://golang.org/cl/22101 to add context throughout the
net package broke Plan 9, which isn't currently tested (#15251).
It also broke some old unsupported version of Windows (Windows 2000?)
which doesn't have the ConnectEx function, but that was only found
visually, since our minimum supported Windows version has ConnectEx.
This change simplifies the Windows and deletes the non-ConnectEx code
path. Windows 2000 will work even less now, if it even worked
before. Windows XP remains our minimum supported version.
Specifically, the previous CL stopped using the "dial" function, which
0intro noted:
https://github.com/golang/go/issues/15333#issuecomment-210842761
This CL removes the dial function instead and makes plan9's net
implementation respect contexts, which likely fixes a number of
t.Skipped tests. I'm leaving that to 0intro to investigate.
In the process of propagating and respecting contexts for plan9, I had
to change some signatures to add contexts to more places and ended up
pushing contexts down into the Go-based DNS resolution as well,
replacing the pure-Go DNS implementation's use of "timeout
time.Duration" with a context instead.
Updates #11932
Updates #15328
Fixes #15333
Change-Id: I6ad1e62f38271cdd86b3f40921f2d0f23374936a
Reviewed-on: https://go-review.googlesource.com/22144
Reviewed-by: David du Colombier <0intro@gmail.com>
Reviewed-by: Mikio Hara <mikioh.mikioh@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-04-16 15:17:40 -06:00
|
|
|
if d, ok := ctx.Deadline(); ok && !d.IsZero() {
|
|
|
|
c.SetDeadline(d)
|
2012-01-18 17:24:06 -07:00
|
|
|
}
|
2014-08-05 18:58:47 -06:00
|
|
|
out.id = uint16(rand.Int()) ^ uint16(time.Now().UnixNano())
|
2016-04-15 20:19:58 -06:00
|
|
|
in, err := c.dnsRoundTrip(&out)
|
2011-06-12 18:22:31 -06:00
|
|
|
if err != nil {
|
net: fix plan9 after context change, propagate contexts more
My previous https://golang.org/cl/22101 to add context throughout the
net package broke Plan 9, which isn't currently tested (#15251).
It also broke some old unsupported version of Windows (Windows 2000?)
which doesn't have the ConnectEx function, but that was only found
visually, since our minimum supported Windows version has ConnectEx.
This change simplifies the Windows and deletes the non-ConnectEx code
path. Windows 2000 will work even less now, if it even worked
before. Windows XP remains our minimum supported version.
Specifically, the previous CL stopped using the "dial" function, which
0intro noted:
https://github.com/golang/go/issues/15333#issuecomment-210842761
This CL removes the dial function instead and makes plan9's net
implementation respect contexts, which likely fixes a number of
t.Skipped tests. I'm leaving that to 0intro to investigate.
In the process of propagating and respecting contexts for plan9, I had
to change some signatures to add contexts to more places and ended up
pushing contexts down into the Go-based DNS resolution as well,
replacing the pure-Go DNS implementation's use of "timeout
time.Duration" with a context instead.
Updates #11932
Updates #15328
Fixes #15333
Change-Id: I6ad1e62f38271cdd86b3f40921f2d0f23374936a
Reviewed-on: https://go-review.googlesource.com/22144
Reviewed-by: David du Colombier <0intro@gmail.com>
Reviewed-by: Mikio Hara <mikioh.mikioh@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-04-16 15:17:40 -06:00
|
|
|
return nil, mapErr(err)
|
2011-06-12 18:22:31 -06:00
|
|
|
}
|
2014-08-05 18:58:47 -06:00
|
|
|
if in.truncated { // see RFC 5966
|
2011-06-12 18:22:31 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return in, nil
|
|
|
|
}
|
2014-08-05 18:58:47 -06:00
|
|
|
return nil, errors.New("no answer from DNS server")
|
2011-06-12 18:22:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do a lookup for a single name, which must be rooted
|
|
|
|
// (otherwise answer will not find the answers).
|
2017-02-20 06:58:55 -07:00
|
|
|
func (r *Resolver) tryOneName(ctx context.Context, cfg *dnsConfig, name string, qtype uint16) (string, []dnsRR, error) {
|
2014-08-05 18:58:47 -06:00
|
|
|
var lastErr error
|
2016-09-15 14:24:42 -06:00
|
|
|
serverOffset := cfg.serverOffset()
|
|
|
|
sLen := uint32(len(cfg.servers))
|
|
|
|
|
2014-08-29 22:12:28 -06:00
|
|
|
for i := 0; i < cfg.attempts; i++ {
|
2016-09-15 14:24:42 -06:00
|
|
|
for j := uint32(0); j < sLen; j++ {
|
|
|
|
server := cfg.servers[(serverOffset+j)%sLen]
|
|
|
|
|
2017-02-20 06:58:55 -07:00
|
|
|
msg, err := r.exchange(ctx, server, name, qtype, cfg.timeout)
|
2014-08-05 18:58:47 -06:00
|
|
|
if err != nil {
|
|
|
|
lastErr = &DNSError{
|
|
|
|
Err: err.Error(),
|
|
|
|
Name: name,
|
|
|
|
Server: server,
|
|
|
|
}
|
2014-08-29 22:12:28 -06:00
|
|
|
if nerr, ok := err.(Error); ok && nerr.Timeout() {
|
|
|
|
lastErr.(*DNSError).IsTimeout = true
|
|
|
|
}
|
2016-11-01 22:01:08 -06:00
|
|
|
// Set IsTemporary for socket-level errors. Note that this flag
|
|
|
|
// may also be used to indicate a SERVFAIL response.
|
|
|
|
if _, ok := err.(*OpError); ok {
|
|
|
|
lastErr.(*DNSError).IsTemporary = true
|
|
|
|
}
|
2014-08-29 22:12:28 -06:00
|
|
|
continue
|
2013-08-13 10:44:12 -06:00
|
|
|
}
|
2016-04-25 14:09:11 -06:00
|
|
|
// libresolv continues to the next server when it receives
|
|
|
|
// an invalid referral response. See golang.org/issue/15434.
|
|
|
|
if msg.rcode == dnsRcodeSuccess && !msg.authoritative && !msg.recursion_available && len(msg.answer) == 0 && len(msg.extra) == 0 {
|
|
|
|
lastErr = &DNSError{Err: "lame referral", Name: name, Server: server}
|
|
|
|
continue
|
|
|
|
}
|
2015-06-09 02:30:00 -06:00
|
|
|
cname, rrs, err := answer(name, server, msg, qtype)
|
2015-11-16 08:43:22 -07:00
|
|
|
// If answer errored for rcodes dnsRcodeSuccess or dnsRcodeNameError,
|
|
|
|
// it means the response in msg was not useful and trying another
|
|
|
|
// server probably won't help. Return now in those cases.
|
|
|
|
// TODO: indicate this in a more obvious way, such as a field on DNSError?
|
|
|
|
if err == nil || msg.rcode == dnsRcodeSuccess || msg.rcode == dnsRcodeNameError {
|
2015-06-09 02:30:00 -06:00
|
|
|
return cname, rrs, err
|
2013-08-13 10:44:12 -06:00
|
|
|
}
|
2014-08-05 18:58:47 -06:00
|
|
|
lastErr = err
|
2011-06-12 18:22:31 -06:00
|
|
|
}
|
|
|
|
}
|
2014-08-05 18:58:47 -06:00
|
|
|
return "", nil, lastErr
|
2011-06-12 18:22:31 -06:00
|
|
|
}
|
|
|
|
|
2015-06-10 21:46:01 -06:00
|
|
|
// addrRecordList converts and returns a list of IP addresses from DNS
|
|
|
|
// address records (both A and AAAA). Other record types are ignored.
|
|
|
|
func addrRecordList(rrs []dnsRR) []IPAddr {
|
|
|
|
addrs := make([]IPAddr, 0, 4)
|
|
|
|
for _, rr := range rrs {
|
|
|
|
switch rr := rr.(type) {
|
|
|
|
case *dnsRR_A:
|
|
|
|
addrs = append(addrs, IPAddr{IP: IPv4(byte(rr.A>>24), byte(rr.A>>16), byte(rr.A>>8), byte(rr.A))})
|
|
|
|
case *dnsRR_AAAA:
|
|
|
|
ip := make(IP, IPv6len)
|
|
|
|
copy(ip, rr.AAAA[:])
|
|
|
|
addrs = append(addrs, IPAddr{IP: ip})
|
|
|
|
}
|
2011-06-12 18:22:31 -06:00
|
|
|
}
|
|
|
|
return addrs
|
|
|
|
}
|
|
|
|
|
2015-06-10 21:46:01 -06:00
|
|
|
// A resolverConfig represents a DNS stub resolver configuration.
|
|
|
|
type resolverConfig struct {
|
|
|
|
initOnce sync.Once // guards init of resolverConfig
|
2011-06-12 18:22:31 -06:00
|
|
|
|
2015-06-10 21:46:01 -06:00
|
|
|
// ch is used as a semaphore that only allows one lookup at a
|
|
|
|
// time to recheck resolv.conf.
|
|
|
|
ch chan struct{} // guards lastChecked and modTime
|
|
|
|
lastChecked time.Time // last time resolv.conf was checked
|
2015-05-12 21:56:56 -06:00
|
|
|
|
2015-04-25 18:50:21 -06:00
|
|
|
mu sync.RWMutex // protects dnsConfig
|
2015-05-12 21:56:56 -06:00
|
|
|
dnsConfig *dnsConfig // parsed resolv.conf structure used in lookups
|
2014-05-14 18:11:00 -06:00
|
|
|
}
|
2015-04-25 18:50:21 -06:00
|
|
|
|
2015-06-10 21:46:01 -06:00
|
|
|
var resolvConf resolverConfig
|
2011-06-12 18:22:31 -06:00
|
|
|
|
2015-06-10 21:46:01 -06:00
|
|
|
// init initializes conf and is only called via conf.initOnce.
|
|
|
|
func (conf *resolverConfig) init() {
|
2016-01-22 14:31:57 -07:00
|
|
|
// Set dnsConfig and lastChecked so we don't parse
|
2015-05-12 21:56:56 -06:00
|
|
|
// resolv.conf twice the first time.
|
2015-06-10 21:46:01 -06:00
|
|
|
conf.dnsConfig = systemConf().resolv
|
|
|
|
if conf.dnsConfig == nil {
|
|
|
|
conf.dnsConfig = dnsReadConfig("/etc/resolv.conf")
|
2015-05-12 21:56:56 -06:00
|
|
|
}
|
2015-06-10 21:46:01 -06:00
|
|
|
conf.lastChecked = time.Now()
|
2015-04-25 18:50:21 -06:00
|
|
|
|
2015-06-10 21:46:01 -06:00
|
|
|
// Prepare ch so that only one update of resolverConfig may
|
|
|
|
// run at once.
|
|
|
|
conf.ch = make(chan struct{}, 1)
|
2015-05-12 21:56:56 -06:00
|
|
|
}
|
2015-04-25 18:50:21 -06:00
|
|
|
|
2015-06-10 21:46:01 -06:00
|
|
|
// tryUpdate tries to update conf with the named resolv.conf file.
|
|
|
|
// The name variable only exists for testing. It is otherwise always
|
|
|
|
// "/etc/resolv.conf".
|
|
|
|
func (conf *resolverConfig) tryUpdate(name string) {
|
|
|
|
conf.initOnce.Do(conf.init)
|
2014-05-14 18:11:00 -06:00
|
|
|
|
2015-06-10 21:46:01 -06:00
|
|
|
// Ensure only one update at a time checks resolv.conf.
|
|
|
|
if !conf.tryAcquireSema() {
|
2015-05-12 21:56:56 -06:00
|
|
|
return
|
|
|
|
}
|
2015-06-10 21:46:01 -06:00
|
|
|
defer conf.releaseSema()
|
2015-05-12 21:56:56 -06:00
|
|
|
|
|
|
|
now := time.Now()
|
2015-06-10 21:46:01 -06:00
|
|
|
if conf.lastChecked.After(now.Add(-5 * time.Second)) {
|
2015-05-12 21:56:56 -06:00
|
|
|
return
|
|
|
|
}
|
2015-06-10 21:46:01 -06:00
|
|
|
conf.lastChecked = now
|
2015-05-12 21:56:56 -06:00
|
|
|
|
2016-01-22 14:31:57 -07:00
|
|
|
var mtime time.Time
|
2015-06-10 21:46:01 -06:00
|
|
|
if fi, err := os.Stat(name); err == nil {
|
2016-01-22 14:31:57 -07:00
|
|
|
mtime = fi.ModTime()
|
|
|
|
}
|
|
|
|
if mtime.Equal(conf.dnsConfig.mtime) {
|
|
|
|
return
|
2015-06-10 21:46:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
dnsConf := dnsReadConfig(name)
|
|
|
|
conf.mu.Lock()
|
|
|
|
conf.dnsConfig = dnsConf
|
|
|
|
conf.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (conf *resolverConfig) tryAcquireSema() bool {
|
|
|
|
select {
|
|
|
|
case conf.ch <- struct{}{}:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
2015-05-12 21:56:56 -06:00
|
|
|
}
|
2015-06-10 21:46:01 -06:00
|
|
|
}
|
2015-05-12 21:56:56 -06:00
|
|
|
|
2015-06-10 21:46:01 -06:00
|
|
|
func (conf *resolverConfig) releaseSema() {
|
|
|
|
<-conf.ch
|
2014-05-14 18:11:00 -06:00
|
|
|
}
|
2011-06-12 18:22:31 -06:00
|
|
|
|
2016-11-01 22:01:08 -06:00
|
|
|
func (r *Resolver) lookup(ctx context.Context, name string, qtype uint16) (cname string, rrs []dnsRR, err error) {
|
2011-06-12 18:22:31 -06:00
|
|
|
if !isDomainName(name) {
|
2016-10-19 14:48:21 -06:00
|
|
|
// We used to use "invalid domain name" as the error,
|
|
|
|
// but that is a detail of the specific lookup mechanism.
|
|
|
|
// Other lookups might allow broader name syntax
|
|
|
|
// (for example Multicast DNS allows UTF-8; see RFC 6762).
|
|
|
|
// For consistency with libc resolvers, report no such host.
|
|
|
|
return "", nil, &DNSError{Err: errNoSuchHost.Error(), Name: name}
|
2011-06-12 18:22:31 -06:00
|
|
|
}
|
2015-06-10 21:46:01 -06:00
|
|
|
resolvConf.tryUpdate("/etc/resolv.conf")
|
|
|
|
resolvConf.mu.RLock()
|
2015-06-09 02:30:00 -06:00
|
|
|
conf := resolvConf.dnsConfig
|
2015-06-10 21:46:01 -06:00
|
|
|
resolvConf.mu.RUnlock()
|
2015-06-09 02:30:00 -06:00
|
|
|
for _, fqdn := range conf.nameList(name) {
|
2017-02-20 06:58:55 -07:00
|
|
|
cname, rrs, err = r.tryOneName(ctx, conf, fqdn, qtype)
|
2011-06-12 18:22:31 -06:00
|
|
|
if err == nil {
|
2015-06-09 02:30:00 -06:00
|
|
|
break
|
2011-06-12 18:22:31 -06:00
|
|
|
}
|
2016-11-01 22:01:08 -06:00
|
|
|
if nerr, ok := err.(Error); ok && nerr.Temporary() && r.StrictErrors {
|
|
|
|
// If we hit a temporary error with StrictErrors enabled,
|
|
|
|
// stop immediately instead of trying more names.
|
|
|
|
break
|
|
|
|
}
|
2011-06-12 18:22:31 -06:00
|
|
|
}
|
2015-06-09 02:30:00 -06:00
|
|
|
if err, ok := err.(*DNSError); ok {
|
2013-10-02 20:09:54 -06:00
|
|
|
// Show original name passed to lookup, not suffixed one.
|
|
|
|
// In general we might have tried many suffixes; showing
|
|
|
|
// just one is misleading. See also golang.org/issue/6324.
|
2015-06-09 02:30:00 -06:00
|
|
|
err.Name = name
|
2013-10-02 20:09:54 -06:00
|
|
|
}
|
2011-06-12 18:22:31 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-31 00:08:44 -06:00
|
|
|
// avoidDNS reports whether this is a hostname for which we should not
|
2016-08-17 03:13:03 -06:00
|
|
|
// use DNS. Currently this includes only .onion, per RFC 7686. See
|
|
|
|
// golang.org/issue/13705. Does not cover .local names (RFC 6762),
|
|
|
|
// see golang.org/issue/16739.
|
2016-03-31 00:08:44 -06:00
|
|
|
func avoidDNS(name string) bool {
|
|
|
|
if name == "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if name[len(name)-1] == '.' {
|
|
|
|
name = name[:len(name)-1]
|
|
|
|
}
|
2016-08-17 03:13:03 -06:00
|
|
|
return stringsHasSuffixFold(name, ".onion")
|
2016-03-31 00:08:44 -06:00
|
|
|
}
|
|
|
|
|
2015-06-09 02:30:00 -06:00
|
|
|
// nameList returns a list of names for sequential DNS queries.
|
|
|
|
func (conf *dnsConfig) nameList(name string) []string {
|
2016-03-31 00:08:44 -06:00
|
|
|
if avoidDNS(name) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-21 22:21:18 -06:00
|
|
|
// Check name length (see isDomainName).
|
|
|
|
l := len(name)
|
|
|
|
rooted := l > 0 && name[l-1] == '.'
|
|
|
|
if l > 254 || l == 254 && rooted {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-09 02:30:00 -06:00
|
|
|
// If name is rooted (trailing dot), try only that name.
|
|
|
|
if rooted {
|
|
|
|
return []string{name}
|
|
|
|
}
|
2016-04-28 12:31:59 -06:00
|
|
|
|
|
|
|
hasNdots := count(name, '.') >= conf.ndots
|
|
|
|
name += "."
|
2016-10-21 22:21:18 -06:00
|
|
|
l++
|
2016-04-28 12:31:59 -06:00
|
|
|
|
2015-06-09 02:30:00 -06:00
|
|
|
// Build list of search choices.
|
|
|
|
names := make([]string, 0, 1+len(conf.search))
|
|
|
|
// If name has enough dots, try unsuffixed first.
|
2016-04-28 12:31:59 -06:00
|
|
|
if hasNdots {
|
|
|
|
names = append(names, name)
|
2015-06-09 02:30:00 -06:00
|
|
|
}
|
2016-10-21 22:21:18 -06:00
|
|
|
// Try suffixes that are not too long (see isDomainName).
|
2015-06-09 02:30:00 -06:00
|
|
|
for _, suffix := range conf.search {
|
2016-10-21 22:21:18 -06:00
|
|
|
if l+len(suffix) <= 254 {
|
|
|
|
names = append(names, name+suffix)
|
|
|
|
}
|
2015-06-09 02:30:00 -06:00
|
|
|
}
|
|
|
|
// Try unsuffixed, if not tried first above.
|
2016-04-28 12:31:59 -06:00
|
|
|
if !hasNdots {
|
|
|
|
names = append(names, name)
|
2015-06-09 02:30:00 -06:00
|
|
|
}
|
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
2015-04-16 15:33:25 -06:00
|
|
|
// hostLookupOrder specifies the order of LookupHost lookup strategies.
|
|
|
|
// It is basically a simplified representation of nsswitch.conf.
|
|
|
|
// "files" means /etc/hosts.
|
|
|
|
type hostLookupOrder int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// hostLookupCgo means defer to cgo.
|
|
|
|
hostLookupCgo hostLookupOrder = iota
|
|
|
|
hostLookupFilesDNS // files first
|
|
|
|
hostLookupDNSFiles // dns first
|
|
|
|
hostLookupFiles // only files
|
|
|
|
hostLookupDNS // only DNS
|
|
|
|
)
|
|
|
|
|
|
|
|
var lookupOrderName = map[hostLookupOrder]string{
|
|
|
|
hostLookupCgo: "cgo",
|
|
|
|
hostLookupFilesDNS: "files,dns",
|
|
|
|
hostLookupDNSFiles: "dns,files",
|
|
|
|
hostLookupFiles: "files",
|
|
|
|
hostLookupDNS: "dns",
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o hostLookupOrder) String() string {
|
|
|
|
if s, ok := lookupOrderName[o]; ok {
|
|
|
|
return s
|
|
|
|
}
|
2015-10-05 12:05:10 -06:00
|
|
|
return "hostLookupOrder=" + itoa(int(o)) + "??"
|
2015-04-16 15:33:25 -06:00
|
|
|
}
|
|
|
|
|
2011-06-12 18:22:31 -06:00
|
|
|
// goLookupHost is the native Go implementation of LookupHost.
|
|
|
|
// Used only if cgoLookupHost refuses to handle the request
|
|
|
|
// (that is, only if cgoLookupHost is the stub in cgo_stub.go).
|
|
|
|
// Normally we let cgo use the C library resolver instead of
|
|
|
|
// depending on our lookup code, so that Go and C get the same
|
|
|
|
// answers.
|
2016-11-01 22:01:08 -06:00
|
|
|
func (r *Resolver) goLookupHost(ctx context.Context, name string) (addrs []string, err error) {
|
|
|
|
return r.goLookupHostOrder(ctx, name, hostLookupFilesDNS)
|
2015-04-16 15:33:25 -06:00
|
|
|
}
|
|
|
|
|
2016-11-01 22:01:08 -06:00
|
|
|
func (r *Resolver) goLookupHostOrder(ctx context.Context, name string, order hostLookupOrder) (addrs []string, err error) {
|
2015-04-16 15:33:25 -06:00
|
|
|
if order == hostLookupFilesDNS || order == hostLookupFiles {
|
|
|
|
// Use entries from /etc/hosts if they match.
|
|
|
|
addrs = lookupStaticHost(name)
|
|
|
|
if len(addrs) > 0 || order == hostLookupFiles {
|
|
|
|
return
|
|
|
|
}
|
2011-06-12 18:22:31 -06:00
|
|
|
}
|
2016-11-01 22:01:08 -06:00
|
|
|
ips, _, err := r.goLookupIPCNAMEOrder(ctx, name, order)
|
2011-06-12 18:22:31 -06:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
addrs = make([]string, 0, len(ips))
|
|
|
|
for _, ip := range ips {
|
|
|
|
addrs = append(addrs, ip.String())
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-16 15:33:25 -06:00
|
|
|
// lookup entries from /etc/hosts
|
|
|
|
func goLookupIPFiles(name string) (addrs []IPAddr) {
|
|
|
|
for _, haddr := range lookupStaticHost(name) {
|
|
|
|
haddr, zone := splitHostZone(haddr)
|
|
|
|
if ip := ParseIP(haddr); ip != nil {
|
|
|
|
addr := IPAddr{IP: ip, Zone: zone}
|
|
|
|
addrs = append(addrs, addr)
|
|
|
|
}
|
|
|
|
}
|
2015-07-15 18:02:06 -06:00
|
|
|
sortByRFC6724(addrs)
|
2015-04-16 15:33:25 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-06-12 18:22:31 -06:00
|
|
|
// goLookupIP is the native Go implementation of LookupIP.
|
2015-07-15 18:02:06 -06:00
|
|
|
// The libc versions are in cgo_*.go.
|
2016-11-01 22:01:08 -06:00
|
|
|
func (r *Resolver) goLookupIP(ctx context.Context, host string) (addrs []IPAddr, err error) {
|
2016-09-20 13:45:37 -06:00
|
|
|
order := systemConf().hostLookupOrder(host)
|
2016-11-01 22:01:08 -06:00
|
|
|
addrs, _, err = r.goLookupIPCNAMEOrder(ctx, host, order)
|
2016-12-19 14:05:53 -07:00
|
|
|
return
|
2015-04-16 15:33:25 -06:00
|
|
|
}
|
|
|
|
|
2016-11-01 22:01:08 -06:00
|
|
|
func (r *Resolver) goLookupIPCNAMEOrder(ctx context.Context, name string, order hostLookupOrder) (addrs []IPAddr, cname string, err error) {
|
2015-04-16 15:33:25 -06:00
|
|
|
if order == hostLookupFilesDNS || order == hostLookupFiles {
|
|
|
|
addrs = goLookupIPFiles(name)
|
|
|
|
if len(addrs) > 0 || order == hostLookupFiles {
|
2016-12-19 14:05:53 -07:00
|
|
|
return addrs, name, nil
|
2011-09-19 09:50:31 -06:00
|
|
|
}
|
|
|
|
}
|
2015-06-09 02:30:00 -06:00
|
|
|
if !isDomainName(name) {
|
2016-10-19 14:48:21 -06:00
|
|
|
// See comment in func lookup above about use of errNoSuchHost.
|
2016-12-19 14:05:53 -07:00
|
|
|
return nil, "", &DNSError{Err: errNoSuchHost.Error(), Name: name}
|
2015-06-09 02:30:00 -06:00
|
|
|
}
|
|
|
|
resolvConf.tryUpdate("/etc/resolv.conf")
|
|
|
|
resolvConf.mu.RLock()
|
|
|
|
conf := resolvConf.dnsConfig
|
|
|
|
resolvConf.mu.RUnlock()
|
2014-08-29 22:12:28 -06:00
|
|
|
type racer struct {
|
2016-12-19 14:05:53 -07:00
|
|
|
cname string
|
|
|
|
rrs []dnsRR
|
2014-08-29 22:12:28 -06:00
|
|
|
error
|
2011-06-12 18:22:31 -06:00
|
|
|
}
|
2014-08-29 22:12:28 -06:00
|
|
|
lane := make(chan racer, 1)
|
|
|
|
qtypes := [...]uint16{dnsTypeA, dnsTypeAAAA}
|
|
|
|
var lastErr error
|
2015-06-09 02:30:00 -06:00
|
|
|
for _, fqdn := range conf.nameList(name) {
|
|
|
|
for _, qtype := range qtypes {
|
|
|
|
go func(qtype uint16) {
|
2017-02-20 06:58:55 -07:00
|
|
|
cname, rrs, err := r.tryOneName(ctx, conf, fqdn, qtype)
|
2016-12-19 14:05:53 -07:00
|
|
|
lane <- racer{cname, rrs, err}
|
2015-06-09 02:30:00 -06:00
|
|
|
}(qtype)
|
|
|
|
}
|
2016-11-01 22:01:08 -06:00
|
|
|
hitStrictError := false
|
2015-06-09 02:30:00 -06:00
|
|
|
for range qtypes {
|
|
|
|
racer := <-lane
|
|
|
|
if racer.error != nil {
|
2016-11-01 22:01:08 -06:00
|
|
|
if nerr, ok := racer.error.(Error); ok && nerr.Temporary() && r.StrictErrors {
|
|
|
|
// This error will abort the nameList loop.
|
|
|
|
hitStrictError = true
|
|
|
|
lastErr = racer.error
|
|
|
|
} else if lastErr == nil || fqdn == name+"." {
|
|
|
|
// Prefer error for original name.
|
2015-11-19 12:24:42 -07:00
|
|
|
lastErr = racer.error
|
|
|
|
}
|
2015-06-09 02:30:00 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
addrs = append(addrs, addrRecordList(racer.rrs)...)
|
2016-12-19 14:05:53 -07:00
|
|
|
if cname == "" {
|
|
|
|
cname = racer.cname
|
|
|
|
}
|
2015-06-09 02:30:00 -06:00
|
|
|
}
|
2016-11-01 22:01:08 -06:00
|
|
|
if hitStrictError {
|
|
|
|
// If either family hit an error with StrictErrors enabled,
|
|
|
|
// discard all addresses. This ensures that network flakiness
|
|
|
|
// cannot turn a dualstack hostname IPv4/IPv6-only.
|
|
|
|
addrs = nil
|
|
|
|
break
|
|
|
|
}
|
2015-06-09 02:30:00 -06:00
|
|
|
if len(addrs) > 0 {
|
|
|
|
break
|
2014-08-29 22:12:28 -06:00
|
|
|
}
|
2015-06-09 02:30:00 -06:00
|
|
|
}
|
|
|
|
if lastErr, ok := lastErr.(*DNSError); ok {
|
|
|
|
// Show original name passed to lookup, not suffixed one.
|
|
|
|
// In general we might have tried many suffixes; showing
|
|
|
|
// just one is misleading. See also golang.org/issue/6324.
|
|
|
|
lastErr.Name = name
|
2012-06-25 15:32:39 -06:00
|
|
|
}
|
2015-07-15 18:02:06 -06:00
|
|
|
sortByRFC6724(addrs)
|
2015-04-16 15:33:25 -06:00
|
|
|
if len(addrs) == 0 {
|
|
|
|
if order == hostLookupDNSFiles {
|
|
|
|
addrs = goLookupIPFiles(name)
|
|
|
|
}
|
2015-12-01 20:02:04 -07:00
|
|
|
if len(addrs) == 0 && lastErr != nil {
|
2016-12-19 14:05:53 -07:00
|
|
|
return nil, "", lastErr
|
2015-11-29 22:08:46 -07:00
|
|
|
}
|
2012-06-25 15:32:39 -06:00
|
|
|
}
|
2016-12-19 14:05:53 -07:00
|
|
|
return addrs, cname, nil
|
2011-06-12 18:22:31 -06:00
|
|
|
}
|
|
|
|
|
2016-12-19 14:05:53 -07:00
|
|
|
// goLookupCNAME is the native Go (non-cgo) implementation of LookupCNAME.
|
2016-11-01 22:01:08 -06:00
|
|
|
func (r *Resolver) goLookupCNAME(ctx context.Context, host string) (cname string, err error) {
|
2016-12-19 14:05:53 -07:00
|
|
|
order := systemConf().hostLookupOrder(host)
|
2016-11-01 22:01:08 -06:00
|
|
|
_, cname, err = r.goLookupIPCNAMEOrder(ctx, host, order)
|
2011-06-12 18:22:31 -06:00
|
|
|
return
|
|
|
|
}
|
2015-01-28 04:08:41 -07:00
|
|
|
|
|
|
|
// goLookupPTR is the native Go implementation of LookupAddr.
|
|
|
|
// Used only if cgoLookupPTR refuses to handle the request (that is,
|
|
|
|
// only if cgoLookupPTR is the stub in cgo_stub.go).
|
|
|
|
// Normally we let cgo use the C library resolver instead of depending
|
|
|
|
// on our lookup code, so that Go and C get the same answers.
|
2016-11-01 22:01:08 -06:00
|
|
|
func (r *Resolver) goLookupPTR(ctx context.Context, addr string) ([]string, error) {
|
2015-01-28 04:08:41 -07:00
|
|
|
names := lookupStaticAddr(addr)
|
|
|
|
if len(names) > 0 {
|
|
|
|
return names, nil
|
|
|
|
}
|
|
|
|
arpa, err := reverseaddr(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-11-01 22:01:08 -06:00
|
|
|
_, rrs, err := r.lookup(ctx, arpa, dnsTypePTR)
|
2015-01-28 04:08:41 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ptrs := make([]string, len(rrs))
|
|
|
|
for i, rr := range rrs {
|
|
|
|
ptrs[i] = rr.(*dnsRR_PTR).Ptr
|
|
|
|
}
|
|
|
|
return ptrs, nil
|
|
|
|
}
|