-
Notifications
You must be signed in to change notification settings - Fork 17.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
net: permit use of Resolver.PreferGo, netgo on Windows and Plan 9
Fixes #33097 Change-Id: I2e55c7c113683814521f2068e0922b63c62ea5d8 Reviewed-on: https://go-review.googlesource.com/c/go/+/400654 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Damien Neil <dneil@google.com> Auto-Submit: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com>
- Loading branch information
Showing
16 changed files
with
838 additions
and
293 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2022 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. | ||
|
||
package net | ||
|
||
import ( | ||
"os" | ||
"sync/atomic" | ||
"time" | ||
) | ||
|
||
var ( | ||
defaultNS = []string{"127.0.0.1:53", "[::1]:53"} | ||
getHostname = os.Hostname // variable for testing | ||
) | ||
|
||
type dnsConfig struct { | ||
servers []string // server addresses (in host:port form) to use | ||
search []string // rooted suffixes to append to local name | ||
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 | ||
soffset uint32 // used by serverOffset | ||
singleRequest bool // use sequential A and AAAA queries instead of parallel queries | ||
useTCP bool // force usage of TCP for DNS resolutions | ||
} | ||
|
||
// 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2022 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. | ||
|
||
package net | ||
|
||
import ( | ||
"syscall" | ||
"time" | ||
) | ||
|
||
func dnsReadConfig(ignoredFilename string) (conf *dnsConfig) { | ||
conf = &dnsConfig{ | ||
ndots: 1, | ||
timeout: 5 * time.Second, | ||
attempts: 2, | ||
} | ||
defer func() { | ||
if len(conf.servers) == 0 { | ||
conf.servers = defaultNS | ||
} | ||
}() | ||
aas, err := adapterAddresses() | ||
if err != nil { | ||
return | ||
} | ||
// TODO(bradfitz): this just collects all the DNS servers on all | ||
// the interfaces in some random order. It should order it by | ||
// default route, or only use the default route(s) instead. | ||
// In practice, however, it mostly works. | ||
for _, aa := range aas { | ||
for dns := aa.FirstDnsServerAddress; dns != nil; dns = dns.Next { | ||
sa, err := dns.Address.Sockaddr.Sockaddr() | ||
if err != nil { | ||
continue | ||
} | ||
var ip IP | ||
switch sa := sa.(type) { | ||
case *syscall.SockaddrInet4: | ||
ip = IPv4(sa.Addr[0], sa.Addr[1], sa.Addr[2], sa.Addr[3]) | ||
case *syscall.SockaddrInet6: | ||
ip = make(IP, IPv6len) | ||
copy(ip, sa.Addr[:]) | ||
if ip[0] == 0xfe && ip[1] == 0xc0 { | ||
// Ignore these fec0/10 ones. Windows seems to | ||
// populate them as defaults on its misc rando | ||
// interfaces. | ||
continue | ||
} | ||
default: | ||
// Unexpected type. | ||
continue | ||
} | ||
conf.servers = append(conf.servers, JoinHostPort(ip.String(), "53")) | ||
} | ||
} | ||
return conf | ||
} |
Oops, something went wrong.