k8s.io/kubernetes@v1.29.3/test/images/agnhost/dns/dns_windows.go (about)

     1  //go:build windows
     2  // +build windows
     3  
     4  /*
     5  Copyright 2019 The Kubernetes Authors.
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10  
    11      http://www.apache.org/licenses/LICENSE-2.0
    12  
    13  Unless required by applicable law or agreed to in writing, software
    14  distributed under the License is distributed on an "AS IS" BASIS,
    15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  See the License for the specific language governing permissions and
    17  limitations under the License.
    18  */
    19  
    20  package dns
    21  
    22  import (
    23  	"fmt"
    24  	"strings"
    25  	"syscall"
    26  	"unsafe"
    27  
    28  	"golang.org/x/sys/windows"
    29  	"golang.org/x/sys/windows/registry"
    30  )
    31  
    32  const (
    33  	etcHostsFile      = "C:/Windows/System32/drivers/etc/hosts"
    34  	netRegistry       = `System\CurrentControlSet\Services\TCPIP\Parameters`
    35  	netIfacesRegistry = `System\CurrentControlSet\Services\TCPIP\Parameters\Interfaces`
    36  	maxHostnameLen    = 128
    37  	maxDomainNameLen  = 128
    38  	maxScopeIDLen     = 256
    39  )
    40  
    41  // FixedInfo information: https://docs.microsoft.com/en-us/windows/win32/api/iptypes/ns-iptypes-fixed_info_w2ksp1
    42  type FixedInfo struct {
    43  	HostName         [maxHostnameLen + 4]byte
    44  	DomainName       [maxDomainNameLen + 4]byte
    45  	CurrentDNSServer *syscall.IpAddrString
    46  	DNSServerList    syscall.IpAddrString
    47  	NodeType         uint32
    48  	ScopeID          [maxScopeIDLen + 4]byte
    49  	EnableRouting    uint32
    50  	EnableProxy      uint32
    51  	EnableDNS        uint32
    52  }
    53  
    54  var (
    55  	// GetNetworkParams can be found in iphlpapi.dll
    56  	// see: https://docs.microsoft.com/en-us/windows/win32/api/iphlpapi/nf-iphlpapi-getnetworkparams?redirectedfrom=MSDN
    57  	iphlpapidll          = windows.MustLoadDLL("iphlpapi.dll")
    58  	procGetNetworkParams = iphlpapidll.MustFindProc("GetNetworkParams")
    59  )
    60  
    61  func elemInList(elem string, list []string) bool {
    62  	for _, e := range list {
    63  		if e == elem {
    64  			return true
    65  		}
    66  	}
    67  	return false
    68  }
    69  
    70  func getRegistryValue(reg, key string) string {
    71  	regKey, err := registry.OpenKey(registry.LOCAL_MACHINE, reg, registry.QUERY_VALUE)
    72  	if err != nil {
    73  		return ""
    74  	}
    75  	defer regKey.Close()
    76  
    77  	regValue, _, err := regKey.GetStringValue(key)
    78  	if err != nil {
    79  		return ""
    80  	}
    81  	return regValue
    82  }
    83  
    84  // GetDNSSuffixList reads DNS config file and returns the list of configured DNS suffixes
    85  func GetDNSSuffixList() []string {
    86  	// We start with the general suffix list that apply to all network connections.
    87  	allSuffixes := []string{}
    88  	suffixes := getRegistryValue(netRegistry, "SearchList")
    89  	if suffixes != "" {
    90  		allSuffixes = strings.Split(suffixes, ",")
    91  	}
    92  
    93  	// Then we append the network-specific DNS suffix lists.
    94  	regKey, err := registry.OpenKey(registry.LOCAL_MACHINE, netIfacesRegistry, registry.ENUMERATE_SUB_KEYS)
    95  	if err != nil {
    96  		panic(err)
    97  	}
    98  	defer regKey.Close()
    99  
   100  	ifaces, err := regKey.ReadSubKeyNames(0)
   101  	if err != nil {
   102  		panic(err)
   103  	}
   104  	for _, iface := range ifaces {
   105  		suffixes := getRegistryValue(fmt.Sprintf("%s\\%s", netIfacesRegistry, iface), "SearchList")
   106  		if suffixes == "" {
   107  			continue
   108  		}
   109  		for _, suffix := range strings.Split(suffixes, ",") {
   110  			if !elemInList(suffix, allSuffixes) {
   111  				allSuffixes = append(allSuffixes, suffix)
   112  			}
   113  		}
   114  	}
   115  
   116  	return allSuffixes
   117  }
   118  
   119  func getNetworkParams() *FixedInfo {
   120  	// We don't know how big we should make the byte buffer, but the call will tell us by
   121  	// setting the size afterwards.
   122  	var size int
   123  	buffer := make([]byte, 1)
   124  	procGetNetworkParams.Call(
   125  		uintptr(unsafe.Pointer(&buffer[0])),
   126  		uintptr(unsafe.Pointer(&size)),
   127  	)
   128  
   129  	buffer = make([]byte, size)
   130  	procGetNetworkParams.Call(
   131  		uintptr(unsafe.Pointer(&buffer[0])),
   132  		uintptr(unsafe.Pointer(&size)),
   133  	)
   134  
   135  	info := (*FixedInfo)(unsafe.Pointer(&buffer[0]))
   136  	return info
   137  }
   138  
   139  func getDNSServerList() []string {
   140  	dnsServerList := []string{}
   141  	fixedInfo := getNetworkParams()
   142  	list := &(fixedInfo.DNSServerList)
   143  
   144  	for list != nil {
   145  		dnsServer := strings.TrimRight(string(list.IpAddress.String[:]), "\x00")
   146  		dnsServerList = append(dnsServerList, dnsServer)
   147  		list = list.Next
   148  	}
   149  	return dnsServerList
   150  }