-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
This reverts commit CL 401754 (440c931) which reverted CL 400654, thus reapplying CL 400654, re-adding the func init() { netGo = true } to cgo_stub.go CL 400654 had originally removed (mistakenly during development?) that had broken the darwin nocgo builder. Fixes #33097 Change-Id: I90f59746d2ceb6b5d2bd832c9fc90068f8ff7417 Reviewed-on: https://go-review.googlesource.com/c/go/+/409234 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Keith Randall <khr@google.com>
- Loading branch information
Showing
15 changed files
with
835 additions
and
290 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
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.