Source file src/net/dnsconfig.go

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package net
     6  
     7  import (
     8  	"os"
     9  	"sync/atomic"
    10  	"time"
    11  	_ "unsafe"
    12  )
    13  
    14  // defaultNS is the default name servers to use in the absence of DNS configuration.
    15  //
    16  // defaultNS should be an internal detail,
    17  // but widely used packages access it using linkname.
    18  // Notable members of the hall of shame include:
    19  //   - github.com/pojntfx/hydrapp/hydrapp
    20  //   - github.com/mtibben/androiddnsfix
    21  //
    22  // Do not remove or change the type signature.
    23  // See go.dev/issue/67401.
    24  //
    25  //go:linkname defaultNS
    26  var defaultNS = []string{"127.0.0.1:53", "[::1]:53"}
    27  
    28  // isDefaultNS reports whether the c.servers field is set to [defaultNS], meaning that
    29  // no nameservers were specified in /etc/resolv.conf, thus the default ones are used.
    30  func (c *dnsConfig) isDefaultNS() bool {
    31  	return len(c.servers) == len(defaultNS) && &c.servers[0] == &defaultNS[0]
    32  }
    33  
    34  var getHostname = os.Hostname // variable for testing
    35  
    36  type dnsConfig struct {
    37  	servers       []string      // server addresses (in host:port form) to use
    38  	search        []string      // rooted suffixes to append to local name
    39  	ndots         int           // number of dots in name to trigger absolute lookup
    40  	timeout       time.Duration // wait before giving up on a query, including retries
    41  	attempts      int           // lost packets before giving up on server
    42  	rotate        bool          // round robin among servers
    43  	unknownOpt    bool          // anything unknown was encountered
    44  	lookup        []string      // OpenBSD top-level database "lookup" order
    45  	err           error         // any error that occurs during open of resolv.conf
    46  	mtime         time.Time     // time of resolv.conf modification
    47  	soffset       uint32        // used by serverOffset
    48  	singleRequest bool          // use sequential A and AAAA queries instead of parallel queries
    49  	useTCP        bool          // force usage of TCP for DNS resolutions
    50  	trustAD       bool          // add AD flag to queries
    51  	noReload      bool          // do not check for config file updates
    52  }
    53  
    54  // serverOffset returns an offset that can be used to determine
    55  // indices of servers in c.servers when making queries.
    56  // When the rotate option is enabled, this offset increases.
    57  // Otherwise it is always 0.
    58  func (c *dnsConfig) serverOffset() uint32 {
    59  	if c.rotate {
    60  		return atomic.AddUint32(&c.soffset, 1) - 1 // return 0 to start
    61  	}
    62  	return 0
    63  }
    64  

View as plain text