Source file src/net/dnsconfig_windows.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  	"internal/syscall/windows"
     9  	"syscall"
    10  	"time"
    11  )
    12  
    13  func dnsReadConfig(ignoredFilename string) (conf *dnsConfig) {
    14  	conf = &dnsConfig{
    15  		ndots:    1,
    16  		timeout:  5 * time.Second,
    17  		attempts: 2,
    18  	}
    19  	defer func() {
    20  		if len(conf.servers) == 0 {
    21  			conf.servers = defaultNS
    22  		}
    23  	}()
    24  	aas, err := adapterAddresses()
    25  	if err != nil {
    26  		return
    27  	}
    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.
    32  	for _, aa := range aas {
    33  		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  			}
    38  			sa, err := dns.Address.Sockaddr.Sockaddr()
    39  			if err != nil {
    40  				continue
    41  			}
    42  			var ip IP
    43  			switch sa := sa.(type) {
    44  			case *syscall.SockaddrInet4:
    45  				ip = IPv4(sa.Addr[0], sa.Addr[1], sa.Addr[2], sa.Addr[3])
    46  			case *syscall.SockaddrInet6:
    47  				ip = make(IP, IPv6len)
    48  				copy(ip, sa.Addr[:])
    49  				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  					continue
    54  				}
    55  			default:
    56  				// Unexpected type.
    57  				continue
    58  			}
    59  			conf.servers = append(conf.servers, JoinHostPort(ip.String(), "53"))
    60  		}
    61  	}
    62  	return conf
    63  }
    64  

View as plain text