@@ -17,12 +17,10 @@ import (
1717)
1818
1919const (
20- addressFamilyIPv4 = 2
21- gaaFlagIncludePrefixes = 0x10
22- dnsSettingsVersion1 = 1
23- dnsSettingNameServer = 0x2
24- windowsErrorBufferLarge = syscall .Errno (111 )
25- windowsErrorNotFound = syscall .Errno (1168 )
20+ addressFamilyIPv4 = 2
21+ gaaFlagIncludePrefixes = 0x10
22+ dnsSettingsVersion1 = 1
23+ dnsSettingNameServer = 0x2
2624)
2725
2826var (
3533 createForwardEntryProc = ipHelperDLL .NewProc ("CreateIpForwardEntry2" )
3634 deleteForwardEntryProc = ipHelperDLL .NewProc ("DeleteIpForwardEntry2" )
3735 setInterfaceDNSSettingsProc = ipHelperDLL .NewProc ("SetInterfaceDnsSettings" )
36+ convertInterfaceLuidProc = ipHelperDLL .NewProc ("ConvertInterfaceLuidToGuid" )
3837)
3938
4039type dnsInterfaceSettings struct {
@@ -80,18 +79,18 @@ func (s *guestServer) ReconfigureNetwork(ctx context.Context, req *pb.Reconfigur
8079 return nil , err
8180 }
8281 if len (req .DnsServers ) > 0 {
83- if err := configureWindowsDNS (adapter .NetworkGUID , req .DnsServers ); err != nil {
82+ if err := configureWindowsDNS (adapter .InterfaceGUID , req .DnsServers ); err != nil {
8483 return nil , err
8584 }
8685 }
8786 return & pb.ReconfigureNetworkResponse {}, nil
8887}
8988
9089type windowsAdapterInfo struct {
91- Luid uint64
92- Index uint32
93- NetworkGUID windows.GUID
94- IPv4 []net.IP
90+ Luid uint64
91+ Index uint32
92+ InterfaceGUID windows.GUID
93+ IPv4 []net.IP
9594}
9695
9796func waitForWindowsAdapter (ctx context.Context , mac net.HardwareAddr , name string ) (* windowsAdapterInfo , error ) {
@@ -119,7 +118,7 @@ func findWindowsAdapter(mac net.HardwareAddr, name string) (*windowsAdapterInfo,
119118 buffer := make ([]byte , size )
120119 first := (* windows .IpAdapterAddresses )(unsafe .Pointer (& buffer [0 ]))
121120 err := windows .GetAdaptersAddresses (addressFamilyIPv4 , gaaFlagIncludePrefixes , 0 , first , & size )
122- if err == windowsErrorBufferLarge {
121+ if err == windows . ERROR_BUFFER_OVERFLOW {
123122 continue
124123 }
125124 if err != nil {
@@ -129,7 +128,13 @@ func findWindowsAdapter(mac net.HardwareAddr, name string) (*windowsAdapterInfo,
129128 physical := net .HardwareAddr (adapter .PhysicalAddress [:adapter .PhysicalAddressLength ])
130129 friendlyName := windows .UTF16PtrToString (adapter .FriendlyName )
131130 if strings .EqualFold (physical .String (), mac .String ()) && (name == "" || strings .EqualFold (name , friendlyName )) {
132- result := & windowsAdapterInfo {Luid : adapter .Luid , Index : adapter .IfIndex , NetworkGUID : adapter .NetworkGuid }
131+ result := & windowsAdapterInfo {Luid : adapter .Luid , Index : adapter .IfIndex }
132+ if code , _ , _ := convertInterfaceLuidProc .Call (
133+ uintptr (unsafe .Pointer (& result .Luid )),
134+ uintptr (unsafe .Pointer (& result .InterfaceGUID )),
135+ ); code != 0 {
136+ return nil , fmt .Errorf ("convert Windows interface LUID to GUID: %w" , syscall .Errno (code ))
137+ }
133138 for address := adapter .FirstUnicastAddress ; address != nil ; address = address .Next {
134139 if ipv4 := address .Address .IP ().To4 (); ipv4 != nil {
135140 result .IPv4 = append (result .IPv4 , append (net .IP (nil ), ipv4 ... ))
@@ -149,7 +154,7 @@ func configureWindowsAddresses(adapter *windowsAdapterInfo, ipv4 net.IP, prefix
149154 row .InterfaceLuid = adapter .Luid
150155 row .InterfaceIndex = adapter .Index
151156 copyRawIPv4 (unsafe .Pointer (& row .Address ), address )
152- if code , _ , _ := deleteUnicastAddressProc .Call (uintptr (unsafe .Pointer (& row ))); code != 0 && syscall .Errno (code ) != windowsErrorNotFound {
157+ if code , _ , _ := deleteUnicastAddressProc .Call (uintptr (unsafe .Pointer (& row ))); code != 0 && syscall .Errno (code ) != windows . ERROR_NOT_FOUND {
153158 return fmt .Errorf ("delete Windows IPv4 address: %w" , syscall .Errno (code ))
154159 }
155160 }
@@ -164,7 +169,7 @@ func configureWindowsAddresses(adapter *windowsAdapterInfo, ipv4 net.IP, prefix
164169 destination := (* windows .RawSockaddrInet4 )(unsafe .Pointer (& route .DestinationPrefix .Prefix ))
165170 if route .InterfaceLuid == adapter .Luid && route .DestinationPrefix .PrefixLength == 0 && destination .Addr == [4 ]byte {} {
166171 row := route
167- if code , _ , _ := deleteForwardEntryProc .Call (uintptr (unsafe .Pointer (& row ))); code != 0 && syscall .Errno (code ) != windowsErrorNotFound {
172+ if code , _ , _ := deleteForwardEntryProc .Call (uintptr (unsafe .Pointer (& row ))); code != 0 && syscall .Errno (code ) != windows . ERROR_NOT_FOUND {
168173 return fmt .Errorf ("delete Windows default route: %w" , syscall .Errno (code ))
169174 }
170175 }
0 commit comments