Retro68/gcc/libgo/go/net/dnsconfig_unix.go

121 lines
3.1 KiB
Go
Raw Normal View History

2012-03-27 23:13:14 +00: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.
2015-08-28 15:33:40 +00:00
// +build darwin dragonfly freebsd linux netbsd openbsd solaris
2012-03-27 23:13:14 +00:00
// Read system DNS config from /etc/resolv.conf
package net
2017-04-10 11:32:00 +00:00
var defaultNS = []string{"127.0.0.1", "::1"}
2012-03-27 23:13:14 +00:00
type dnsConfig struct {
2017-04-10 11:32:00 +00:00
servers []string // servers to use
search []string // suffixes to append to local name
ndots int // number of dots in name to trigger absolute lookup
timeout int // seconds before giving up on packet
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
2012-03-27 23:13:14 +00:00
}
// See resolv.conf(5) on a Linux machine.
// TODO(rsc): Supposed to call uname() and chop the beginning
// of the host name to get the default search domain.
2017-04-10 11:32:00 +00:00
func dnsReadConfig(filename string) *dnsConfig {
2015-08-28 15:33:40 +00:00
conf := &dnsConfig{
ndots: 1,
timeout: 5,
attempts: 2,
}
2017-04-10 11:32:00 +00:00
file, err := open(filename)
if err != nil {
conf.servers = defaultNS
conf.err = err
return conf
}
defer file.close()
2012-03-27 23:13:14 +00:00
for line, ok := file.readLine(); ok; line, ok = file.readLine() {
2017-04-10 11:32:00 +00:00
if len(line) > 0 && (line[0] == ';' || line[0] == '#') {
// comment.
continue
}
2012-03-27 23:13:14 +00:00
f := getFields(line)
if len(f) < 1 {
continue
}
switch f[0] {
case "nameserver": // add one name server
2015-08-28 15:33:40 +00:00
if len(f) > 1 && len(conf.servers) < 3 { // small, but the standard limit
2012-03-27 23:13:14 +00:00
// One more check: make sure server name is
// just an IP address. Otherwise we need DNS
// to look it up.
2015-08-28 15:33:40 +00:00
if parseIPv4(f[1]) != nil {
conf.servers = append(conf.servers, f[1])
} else if ip, _ := parseIPv6(f[1], true); ip != nil {
conf.servers = append(conf.servers, f[1])
2012-03-27 23:13:14 +00:00
}
}
case "domain": // set search path to just this domain
if len(f) > 1 {
2015-08-28 15:33:40 +00:00
conf.search = []string{f[1]}
2012-03-27 23:13:14 +00:00
}
case "search": // set search path to given servers
conf.search = make([]string, len(f)-1)
for i := 0; i < len(conf.search); i++ {
conf.search[i] = f[i+1]
}
case "options": // magic options
2017-04-10 11:32:00 +00:00
for _, s := range f[1:] {
2012-03-27 23:13:14 +00:00
switch {
2015-08-28 15:33:40 +00:00
case hasPrefix(s, "ndots:"):
2012-03-27 23:13:14 +00:00
n, _, _ := dtoi(s, 6)
if n < 1 {
n = 1
}
conf.ndots = n
2015-08-28 15:33:40 +00:00
case hasPrefix(s, "timeout:"):
2012-03-27 23:13:14 +00:00
n, _, _ := dtoi(s, 8)
if n < 1 {
n = 1
}
conf.timeout = n
2015-08-28 15:33:40 +00:00
case hasPrefix(s, "attempts:"):
2012-03-27 23:13:14 +00:00
n, _, _ := dtoi(s, 9)
if n < 1 {
n = 1
}
conf.attempts = n
case s == "rotate":
conf.rotate = true
2017-04-10 11:32:00 +00:00
default:
conf.unknownOpt = true
2012-03-27 23:13:14 +00:00
}
}
2017-04-10 11:32:00 +00: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
2012-03-27 23:13:14 +00:00
}
}
2017-04-10 11:32:00 +00:00
if len(conf.servers) == 0 {
conf.servers = defaultNS
}
return conf
2012-03-27 23:13:14 +00:00
}
2015-08-28 15:33:40 +00:00
func hasPrefix(s, prefix string) bool {
return len(s) >= len(prefix) && s[:len(prefix)] == prefix
}