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  	"syscall"
     9  	"time"
    10  )
    11  
    12  func dnsReadConfig(ignoredFilename string) (conf *dnsConfig) {
    13  	conf = &dnsConfig{
    14  		ndots:    1,
    15  		timeout:  5 * time.Second,
    16  		attempts: 2,
    17  	}
    18  	defer func() {
    19  		if len(conf.servers) == 0 {
    20  			conf.servers = defaultNS
    21  		}
    22  	}()
    23  	aas, err := adapterAddresses()
    24  	if err != nil {
    25  		return
    26  	}
    27  	// TODO(bradfitz): this just collects all the DNS servers on all
    28  	// the interfaces in some random order. It should order it by
    29  	// default route, or only use the default route(s) instead.
    30  	// In practice, however, it mostly works.
    31  	for _, aa := range aas {
    32  		for dns := aa.FirstDnsServerAddress; dns != nil; dns = dns.Next {
    33  			sa, err := dns.Address.Sockaddr.Sockaddr()
    34  			if err != nil {
    35  				continue
    36  			}
    37  			var ip IP
    38  			switch sa := sa.(type) {
    39  			case *syscall.SockaddrInet4:
    40  				ip = IPv4(sa.Addr[0], sa.Addr[1], sa.Addr[2], sa.Addr[3])
    41  			case *syscall.SockaddrInet6:
    42  				ip = make(IP, IPv6len)
    43  				copy(ip, sa.Addr[:])
    44  				if ip[0] == 0xfe && ip[1] == 0xc0 {
    45  					// Ignore these fec0/10 ones. Windows seems to
    46  					// populate them as defaults on its misc rando
    47  					// interfaces.
    48  					continue
    49  				}
    50  			default:
    51  				// Unexpected type.
    52  				continue
    53  			}
    54  			conf.servers = append(conf.servers, JoinHostPort(ip.String(), "53"))
    55  		}
    56  	}
    57  	return conf
    58  }
    59  

View as plain text