Skip to content

Commit d98b444

Browse files
qdm12qmuntal
authored andcommitted
net: fixes to dnsReadConfig in dnsconfig_windows.go
- Only search DNS servers for network interfaces with at least one gateway - Clarify comment on deprecated site local anycast fec0/10 DNS IPv6 addresses - Minor maintenance: skip not "up" interfaces earlier in outer loop Change-Id: I98ca7b81d3d51e6aa6bfa4a10dcd651305a843df GitHub-Last-Rev: 3b358c7 GitHub-Pull-Request: #64441 Reviewed-on: https://go-review.googlesource.com/c/go/+/545775 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Cherry Mui <[email protected]> Reviewed-by: Damien Neil <[email protected]> Reviewed-by: Alex Brainman <[email protected]>
1 parent c9ed561 commit d98b444

File tree

3 files changed

+60
-33
lines changed

3 files changed

+60
-33
lines changed

src/internal/syscall/windows/syscall_windows.go

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ const (
4242
ERROR_NO_UNICODE_TRANSLATION syscall.Errno = 1113
4343
)
4444

45-
const GAA_FLAG_INCLUDE_PREFIX = 0x00000010
45+
const (
46+
GAA_FLAG_INCLUDE_PREFIX = 0x00000010
47+
GAA_FLAG_INCLUDE_GATEWAYS = 0x0080
48+
)
4649

4750
const (
4851
IF_TYPE_OTHER = 1
@@ -104,27 +107,45 @@ type IpAdapterPrefix struct {
104107
PrefixLength uint32
105108
}
106109

110+
type IpAdapterWinsServerAddress struct {
111+
Length uint32
112+
Reserved uint32
113+
Next *IpAdapterWinsServerAddress
114+
Address SocketAddress
115+
}
116+
117+
type IpAdapterGatewayAddress struct {
118+
Length uint32
119+
Reserved uint32
120+
Next *IpAdapterGatewayAddress
121+
Address SocketAddress
122+
}
123+
107124
type IpAdapterAddresses struct {
108-
Length uint32
109-
IfIndex uint32
110-
Next *IpAdapterAddresses
111-
AdapterName *byte
112-
FirstUnicastAddress *IpAdapterUnicastAddress
113-
FirstAnycastAddress *IpAdapterAnycastAddress
114-
FirstMulticastAddress *IpAdapterMulticastAddress
115-
FirstDnsServerAddress *IpAdapterDnsServerAdapter
116-
DnsSuffix *uint16
117-
Description *uint16
118-
FriendlyName *uint16
119-
PhysicalAddress [syscall.MAX_ADAPTER_ADDRESS_LENGTH]byte
120-
PhysicalAddressLength uint32
121-
Flags uint32
122-
Mtu uint32
123-
IfType uint32
124-
OperStatus uint32
125-
Ipv6IfIndex uint32
126-
ZoneIndices [16]uint32
127-
FirstPrefix *IpAdapterPrefix
125+
Length uint32
126+
IfIndex uint32
127+
Next *IpAdapterAddresses
128+
AdapterName *byte
129+
FirstUnicastAddress *IpAdapterUnicastAddress
130+
FirstAnycastAddress *IpAdapterAnycastAddress
131+
FirstMulticastAddress *IpAdapterMulticastAddress
132+
FirstDnsServerAddress *IpAdapterDnsServerAdapter
133+
DnsSuffix *uint16
134+
Description *uint16
135+
FriendlyName *uint16
136+
PhysicalAddress [syscall.MAX_ADAPTER_ADDRESS_LENGTH]byte
137+
PhysicalAddressLength uint32
138+
Flags uint32
139+
Mtu uint32
140+
IfType uint32
141+
OperStatus uint32
142+
Ipv6IfIndex uint32
143+
ZoneIndices [16]uint32
144+
FirstPrefix *IpAdapterPrefix
145+
TransmitLinkSpeed uint64
146+
ReceiveLinkSpeed uint64
147+
FirstWinsServerAddress *IpAdapterWinsServerAddress
148+
FirstGatewayAddress *IpAdapterGatewayAddress
128149
/* more fields might be present here. */
129150
}
130151

src/net/dnsconfig_windows.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,19 @@ func dnsReadConfig(ignoredFilename string) (conf *dnsConfig) {
2525
if err != nil {
2626
return
2727
}
28-
// TODO(bradfitz): this just collects all the DNS servers on all
29-
// the interfaces in some random order. It should order it by
30-
// default route, or only use the default route(s) instead.
31-
// In practice, however, it mostly works.
28+
3229
for _, aa := range aas {
30+
// Only take interfaces whose OperStatus is IfOperStatusUp(0x01) into DNS configs.
31+
if aa.OperStatus != windows.IfOperStatusUp {
32+
continue
33+
}
34+
35+
// Only take interfaces which have at least one gateway
36+
if aa.FirstGatewayAddress == nil {
37+
continue
38+
}
39+
3340
for dns := aa.FirstDnsServerAddress; dns != nil; dns = dns.Next {
34-
// Only take interfaces whose OperStatus is IfOperStatusUp(0x01) into DNS configs.
35-
if aa.OperStatus != windows.IfOperStatusUp {
36-
continue
37-
}
3841
sa, err := dns.Address.Sockaddr.Sockaddr()
3942
if err != nil {
4043
continue
@@ -47,9 +50,11 @@ func dnsReadConfig(ignoredFilename string) (conf *dnsConfig) {
4750
ip = make(IP, IPv6len)
4851
copy(ip, sa.Addr[:])
4952
if ip[0] == 0xfe && ip[1] == 0xc0 {
50-
// Ignore these fec0/10 ones. Windows seems to
51-
// populate them as defaults on its misc rando
52-
// interfaces.
53+
// fec0/10 IPv6 addresses are site local anycast DNS
54+
// addresses Microsoft sets by default if no other
55+
// IPv6 DNS address is set. Site local anycast is
56+
// deprecated since 2004, see
57+
// https://datatracker.ietf.org/doc/html/rfc3879
5358
continue
5459
}
5560
default:

src/net/interface_windows.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ func adapterAddresses() ([]*windows.IpAdapterAddresses, error) {
2020
l := uint32(15000) // recommended initial size
2121
for {
2222
b = make([]byte, l)
23-
err := windows.GetAdaptersAddresses(syscall.AF_UNSPEC, windows.GAA_FLAG_INCLUDE_PREFIX, 0, (*windows.IpAdapterAddresses)(unsafe.Pointer(&b[0])), &l)
23+
const flags = windows.GAA_FLAG_INCLUDE_PREFIX | windows.GAA_FLAG_INCLUDE_GATEWAYS
24+
err := windows.GetAdaptersAddresses(syscall.AF_UNSPEC, flags, 0, (*windows.IpAdapterAddresses)(unsafe.Pointer(&b[0])), &l)
2425
if err == nil {
2526
if l == 0 {
2627
return nil, nil

0 commit comments

Comments
 (0)