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.
|
|
|
|
|
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
|
|
|
|
2008-12-18 16:42:39 -07:00
|
|
|
// Read system DNS config from /etc/resolv.conf
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
2016-04-02 15:07:24 -06:00
|
|
|
import (
|
|
|
|
"os"
|
2016-09-15 14:24:42 -06:00
|
|
|
"sync/atomic"
|
2016-04-02 15:07:24 -06:00
|
|
|
"time"
|
|
|
|
)
|
2016-01-22 14:31:57 -07:00
|
|
|
|
2016-04-02 15:07:24 -06:00
|
|
|
var (
|
2016-04-28 12:15:44 -06:00
|
|
|
defaultNS = []string{"127.0.0.1:53", "[::1]:53"}
|
2016-04-02 15:07:24 -06:00
|
|
|
getHostname = os.Hostname // variable for testing
|
|
|
|
)
|
2015-04-25 18:50:21 -06:00
|
|
|
|
2010-04-26 23:15:25 -06:00
|
|
|
type dnsConfig struct {
|
2016-04-28 12:15:44 -06:00
|
|
|
servers []string // server addresses (in host:port form) to use
|
2016-04-28 12:31:59 -06:00
|
|
|
search []string // rooted suffixes to append to local name
|
2016-04-28 06:41:32 -06:00
|
|
|
ndots int // number of dots in name to trigger absolute lookup
|
|
|
|
timeout time.Duration // wait before giving up on a query, including retries
|
|
|
|
attempts int // lost packets before giving up on server
|
|
|
|
rotate bool // round robin among servers
|
|
|
|
unknownOpt bool // anything unknown was encountered
|
|
|
|
lookup []string // OpenBSD top-level database "lookup" order
|
|
|
|
err error // any error that occurs during open of resolv.conf
|
|
|
|
mtime time.Time // time of resolv.conf modification
|
2016-09-15 14:24:42 -06:00
|
|
|
soffset uint32 // used by serverOffset
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// See resolv.conf(5) on a Linux machine.
|
2015-04-25 18:50:21 -06:00
|
|
|
func dnsReadConfig(filename string) *dnsConfig {
|
2014-09-03 19:00:30 -06:00
|
|
|
conf := &dnsConfig{
|
|
|
|
ndots: 1,
|
2016-04-28 06:41:32 -06:00
|
|
|
timeout: 5 * time.Second,
|
2014-09-03 19:00:30 -06:00
|
|
|
attempts: 2,
|
|
|
|
}
|
2015-04-25 18:50:21 -06:00
|
|
|
file, err := open(filename)
|
|
|
|
if err != nil {
|
|
|
|
conf.servers = defaultNS
|
2016-04-02 15:07:24 -06:00
|
|
|
conf.search = dnsDefaultSearch()
|
2015-04-25 18:50:21 -06:00
|
|
|
conf.err = err
|
|
|
|
return conf
|
|
|
|
}
|
|
|
|
defer file.close()
|
2016-01-22 14:31:57 -07:00
|
|
|
if fi, err := file.file.Stat(); err == nil {
|
|
|
|
conf.mtime = fi.ModTime()
|
|
|
|
} else {
|
|
|
|
conf.servers = defaultNS
|
2016-04-02 15:07:24 -06:00
|
|
|
conf.search = dnsDefaultSearch()
|
2016-01-22 14:31:57 -07:00
|
|
|
conf.err = err
|
|
|
|
return conf
|
|
|
|
}
|
2009-02-15 15:18:39 -07:00
|
|
|
for line, ok := file.readLine(); ok; line, ok = file.readLine() {
|
2015-04-16 15:33:25 -06:00
|
|
|
if len(line) > 0 && (line[0] == ';' || line[0] == '#') {
|
|
|
|
// comment.
|
|
|
|
continue
|
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
f := getFields(line)
|
2008-12-18 16:42:39 -07:00
|
|
|
if len(f) < 1 {
|
2009-11-09 13:07:39 -07:00
|
|
|
continue
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
switch f[0] {
|
2009-12-15 16:35:38 -07:00
|
|
|
case "nameserver": // add one name server
|
2014-09-03 19:00:30 -06:00
|
|
|
if len(f) > 1 && len(conf.servers) < 3 { // small, but the standard limit
|
2008-12-18 16:42:39 -07:00
|
|
|
// One more check: make sure server name is
|
2016-03-01 16:21:55 -07:00
|
|
|
// just an IP address. Otherwise we need DNS
|
2008-12-18 16:42:39 -07:00
|
|
|
// to look it up.
|
2014-09-03 21:53:51 -06:00
|
|
|
if parseIPv4(f[1]) != nil {
|
2016-04-28 12:15:44 -06:00
|
|
|
conf.servers = append(conf.servers, JoinHostPort(f[1], "53"))
|
2014-09-03 21:53:51 -06:00
|
|
|
} else if ip, _ := parseIPv6(f[1], true); ip != nil {
|
2016-04-28 12:15:44 -06:00
|
|
|
conf.servers = append(conf.servers, JoinHostPort(f[1], "53"))
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
case "domain": // set search path to just this domain
|
2008-12-18 16:42:39 -07:00
|
|
|
if len(f) > 1 {
|
2016-04-28 12:31:59 -06:00
|
|
|
conf.search = []string{ensureRooted(f[1])}
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
case "search": // set search path to given servers
|
|
|
|
conf.search = make([]string, len(f)-1)
|
2008-12-18 16:42:39 -07:00
|
|
|
for i := 0; i < len(conf.search); i++ {
|
2016-04-28 12:31:59 -06:00
|
|
|
conf.search[i] = ensureRooted(f[i+1])
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
case "options": // magic options
|
2015-04-16 15:33:25 -06:00
|
|
|
for _, s := range f[1:] {
|
2008-12-18 16:42:39 -07:00
|
|
|
switch {
|
2014-07-14 22:49:26 -06:00
|
|
|
case hasPrefix(s, "ndots:"):
|
2016-08-16 10:33:27 -06:00
|
|
|
n, _, _ := dtoi(s[6:])
|
2016-07-13 10:35:35 -06:00
|
|
|
if n < 0 {
|
|
|
|
n = 0
|
|
|
|
} else if n > 15 {
|
|
|
|
n = 15
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
conf.ndots = n
|
2014-07-14 22:49:26 -06:00
|
|
|
case hasPrefix(s, "timeout:"):
|
2016-08-16 10:33:27 -06:00
|
|
|
n, _, _ := dtoi(s[8:])
|
2008-12-18 16:42:39 -07:00
|
|
|
if n < 1 {
|
2009-11-09 13:07:39 -07:00
|
|
|
n = 1
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
2016-04-28 06:41:32 -06:00
|
|
|
conf.timeout = time.Duration(n) * time.Second
|
2014-07-14 22:49:26 -06:00
|
|
|
case hasPrefix(s, "attempts:"):
|
2016-08-16 10:33:27 -06:00
|
|
|
n, _, _ := dtoi(s[9:])
|
2008-12-18 16:42:39 -07:00
|
|
|
if n < 1 {
|
2009-11-09 13:07:39 -07:00
|
|
|
n = 1
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
conf.attempts = n
|
2008-12-18 16:42:39 -07:00
|
|
|
case s == "rotate":
|
2009-11-09 13:07:39 -07:00
|
|
|
conf.rotate = true
|
2015-04-16 15:33:25 -06:00
|
|
|
default:
|
|
|
|
conf.unknownOpt = true
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
}
|
2015-04-16 15:33:25 -06:00
|
|
|
|
|
|
|
case "lookup":
|
|
|
|
// OpenBSD option:
|
|
|
|
// http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man5/resolv.conf.5
|
|
|
|
// "the legal space-separated values are: bind, file, yp"
|
|
|
|
conf.lookup = f[1:]
|
|
|
|
|
|
|
|
default:
|
|
|
|
conf.unknownOpt = true
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
}
|
2015-04-25 18:50:21 -06:00
|
|
|
if len(conf.servers) == 0 {
|
|
|
|
conf.servers = defaultNS
|
|
|
|
}
|
2016-04-02 15:07:24 -06:00
|
|
|
if len(conf.search) == 0 {
|
|
|
|
conf.search = dnsDefaultSearch()
|
|
|
|
}
|
2015-04-25 18:50:21 -06:00
|
|
|
return conf
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
2014-07-14 22:49:26 -06:00
|
|
|
|
2016-09-15 14:24:42 -06:00
|
|
|
// serverOffset returns an offset that can be used to determine
|
|
|
|
// indices of servers in c.servers when making queries.
|
|
|
|
// When the rotate option is enabled, this offset increases.
|
|
|
|
// Otherwise it is always 0.
|
|
|
|
func (c *dnsConfig) serverOffset() uint32 {
|
|
|
|
if c.rotate {
|
|
|
|
return atomic.AddUint32(&c.soffset, 1) - 1 // return 0 to start
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2016-04-02 15:07:24 -06:00
|
|
|
func dnsDefaultSearch() []string {
|
|
|
|
hn, err := getHostname()
|
|
|
|
if err != nil {
|
|
|
|
// best effort
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if i := byteIndex(hn, '.'); i >= 0 && i < len(hn)-1 {
|
2016-04-28 12:31:59 -06:00
|
|
|
return []string{ensureRooted(hn[i+1:])}
|
2016-04-02 15:07:24 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-14 22:49:26 -06:00
|
|
|
func hasPrefix(s, prefix string) bool {
|
|
|
|
return len(s) >= len(prefix) && s[:len(prefix)] == prefix
|
|
|
|
}
|
2016-04-28 12:31:59 -06:00
|
|
|
|
|
|
|
func ensureRooted(s string) string {
|
|
|
|
if len(s) > 0 && s[len(s)-1] == '.' {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return s + "."
|
|
|
|
}
|