// 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.//go:build !windows// Read system DNS config from /etc/resolv.confpackage netimport ()// See resolv.conf(5) on a Linux machine.func dnsReadConfig( string) *dnsConfig { := &dnsConfig{ndots: 1,timeout: 5 * time.Second,attempts: 2, } , := open()if != nil { .servers = defaultNS .search = dnsDefaultSearch() .err = return }defer .close()if , := .file.Stat(); == nil { .mtime = .ModTime() } else { .servers = defaultNS .search = dnsDefaultSearch() .err = return }for , := .readLine(); ; , = .readLine() {iflen() > 0 && ([0] == ';' || [0] == '#') {// comment.continue } := getFields()iflen() < 1 {continue }switch [0] {case"nameserver": // add one name serveriflen() > 1 && len(.servers) < 3 { // small, but the standard limit// One more check: make sure server name is // just an IP address. Otherwise we need DNS // to look it up.if , := netip.ParseAddr([1]); == nil { .servers = append(.servers, JoinHostPort([1], "53")) } }case"domain": // set search path to just this domainiflen() > 1 { .search = []string{ensureRooted([1])} }case"search": // set search path to given servers .search = make([]string, 0, len()-1)for := 1; < len(); ++ { := ensureRooted([])if == "." {continue } .search = append(.search, ) }case"options": // magic optionsfor , := range [1:] {switch {casestringslite.HasPrefix(, "ndots:"): , , := dtoi([6:])if < 0 { = 0 } elseif > 15 { = 15 } .ndots = casestringslite.HasPrefix(, "timeout:"): , , := dtoi([8:])if < 1 { = 1 } .timeout = time.Duration() * time.Secondcasestringslite.HasPrefix(, "attempts:"): , , := dtoi([9:])if < 1 { = 1 } .attempts = case == "rotate": .rotate = truecase == "single-request" || == "single-request-reopen":// Linux option: // http://man7.org/linux/man-pages/man5/resolv.conf.5.html // "By default, glibc performs IPv4 and IPv6 lookups in parallel [...] // This option disables the behavior and makes glibc // perform the IPv6 and IPv4 requests sequentially." .singleRequest = truecase == "use-vc" || == "usevc" || == "tcp":// Linux (use-vc), FreeBSD (usevc) and OpenBSD (tcp) option: // http://man7.org/linux/man-pages/man5/resolv.conf.5.html // "Sets RES_USEVC in _res.options. // This option forces the use of TCP for DNS resolutions." // https://www.freebsd.org/cgi/man.cgi?query=resolv.conf&sektion=5&manpath=freebsd-release-ports // https://man.openbsd.org/resolv.conf.5 .useTCP = truecase == "trust-ad": .trustAD = truecase == "edns0":// We use EDNS by default. // Ignore this option.case == "no-reload": .noReload = truedefault: .unknownOpt = true } }case"lookup":// OpenBSD option: // https://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man5/resolv.conf.5 // "the legal space-separated values are: bind, file, yp" .lookup = [1:]default: .unknownOpt = true } }iflen(.servers) == 0 { .servers = defaultNS }iflen(.search) == 0 { .search = dnsDefaultSearch() }return}func dnsDefaultSearch() []string { , := getHostname()if != nil {// best effortreturnnil }if := bytealg.IndexByteString(, '.'); >= 0 && < len()-1 {return []string{ensureRooted([+1:])} }returnnil}func ensureRooted( string) string {iflen() > 0 && [len()-1] == '.' {return }return + "."}
The pages are generated with Goldsv0.7.0-preview. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.