| 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 |
// - github.com/metacubex/mihomo |
| 22 |
// |
| 23 |
// Do not remove or change the type signature. |
| 24 |
// See go.dev/issue/67401. |
| 25 |
// |
| 26 |
//go:linkname defaultNS |
| 27 |
var defaultNS = []string{"127.0.0.1:53", "[::1]:53"} |
| 28 |
|
| 29 |
var getHostname = os.Hostname // variable for testing |
| 30 |
|
| 31 |
type dnsConfig struct { |
| 32 |
servers []string // server addresses (in host:port form) to use |
| 33 |
search []string // rooted suffixes to append to local name |
| 34 |
ndots int // number of dots in name to trigger absolute lookup |
| 35 |
timeout time.Duration // wait before giving up on a query, including retries |
| 36 |
attempts int // lost packets before giving up on server |
| 37 |
rotate bool // round robin among servers |
| 38 |
unknownOpt bool // anything unknown was encountered |
| 39 |
lookup []string // OpenBSD top-level database "lookup" order |
| 40 |
err error // any error that occurs during open of resolv.conf |
| 41 |
mtime time.Time // time of resolv.conf modification |
| 42 |
soffset uint32 // used by serverOffset |
| 43 |
singleRequest bool // use sequential A and AAAA queries instead of parallel queries |
| 44 |
useTCP bool // force usage of TCP for DNS resolutions |
| 45 |
trustAD bool // add AD flag to queries |
| 46 |
noReload bool // do not check for config file updates |
| 47 |
} |
| 48 |
|
| 49 |
// serverOffset returns an offset that can be used to determine |
| 50 |
// indices of servers in c.servers when making queries. |
| 51 |
// When the rotate option is enabled, this offset increases. |
| 52 |
// Otherwise it is always 0. |
| 53 |
func (c *dnsConfig) serverOffset() uint32 { |
| 54 |
if c.rotate { |
| 55 |
return atomic.AddUint32(&c.soffset, 1) - 1 // return 0 to start |
| 56 |
} |
| 57 |
return 0 |
| 58 |
} |