Skip to content

Commit 7c5bc27

Browse files
committed
Foundation: tweak address resolution on Windows
Rewrite the host address resolution to be easier to follow. The old path obfuscated pointers to the point where the invalid memory access could not be found (probably due to the incorrect handling with the unsafe pointer). This now passes the Host tests.
1 parent 95b8e5c commit 7c5bc27

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

Foundation/Host.swift

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -94,40 +94,50 @@ open class Host: NSObject {
9494

9595
internal func _resolveCurrent() {
9696
#if os(Windows)
97+
var szAddress: [WCHAR] =
98+
Array<WCHAR>(repeating: 0, count: Int(NI_MAXHOST))
99+
97100
var ulSize: ULONG = 0
98101
var ulResult: ULONG =
99102
GetAdaptersAddresses(ULONG(AF_UNSPEC), 0, nil, nil, &ulSize)
100103

101-
let arAdapterInfo: UnsafeMutablePointer<IP_ADAPTER_ADDRESSES> =
102-
UnsafeMutablePointer<IP_ADAPTER_ADDRESSES_LH>
103-
.allocate(capacity: Int(ulSize) / MemoryLayout<IP_ADAPTER_ADDRESSES>.size)
104-
defer { arAdapterInfo.deallocate() }
104+
var arAdapters: UnsafeMutableRawPointer =
105+
UnsafeMutableRawPointer.allocate(byteCount: Int(ulSize),
106+
alignment: 1)
107+
defer { arAdapters.deallocate() }
108+
109+
ulResult = GetAdaptersAddresses(ULONG(AF_UNSPEC), 0, nil,
110+
arAdapters.assumingMemoryBound(to: IP_ADAPTER_ADDRESSES.self),
111+
&ulSize)
112+
guard ulResult == ERROR_SUCCESS else { return }
105113

106-
ulResult = GetAdaptersAddresses(ULONG(AF_UNSPEC), 0, nil, arAdapterInfo, &ulSize)
114+
var pAdapter: UnsafeMutablePointer<IP_ADAPTER_ADDRESSES>? =
115+
arAdapters.assumingMemoryBound(to: IP_ADAPTER_ADDRESSES.self)
116+
while pAdapter != nil {
117+
// print("Adapter: \(String(cString: pAdapter!.pointee.AdapterName))")
107118

108-
var buffer: [WCHAR] = Array<WCHAR>(repeating: 0, count: Int(NI_MAXHOST))
119+
var arAddresses: UnsafeMutablePointer<IP_ADAPTER_UNICAST_ADDRESS> =
120+
pAdapter!.pointee.FirstUnicastAddress
109121

110-
var arCurrentAdapterInfo: UnsafeMutablePointer<IP_ADAPTER_ADDRESSES>? =
111-
arAdapterInfo
112-
while arCurrentAdapterInfo != nil {
113-
var arAddress: UnsafeMutablePointer<IP_ADAPTER_UNICAST_ADDRESS>? =
114-
arCurrentAdapterInfo!.pointee.FirstUnicastAddress
115-
while arAddress != nil {
116-
let arCurrentAddress: IP_ADAPTER_UNICAST_ADDRESS = arAddress!.pointee
117-
switch arCurrentAddress.Address.lpSockaddr.pointee.sa_family {
122+
var pAddress: UnsafeMutablePointer<IP_ADAPTER_UNICAST_ADDRESS>? =
123+
arAddresses
124+
while pAddress != nil {
125+
switch pAddress!.pointee.Address.lpSockaddr.pointee.sa_family {
118126
case ADDRESS_FAMILY(AF_INET), ADDRESS_FAMILY(AF_INET6):
119-
if GetNameInfoW(arCurrentAddress.Address.lpSockaddr,
120-
arCurrentAddress.Address.iSockaddrLength,
121-
&buffer, DWORD(NI_MAXHOST),
122-
nil, 0, NI_NUMERICHOST) == 0 {
123-
_addresses.append(String(decodingCString: &buffer,
127+
if GetNameInfoW(pAddress!.pointee.Address.lpSockaddr,
128+
pAddress!.pointee.Address.iSockaddrLength,
129+
&szAddress, DWORD(szAddress.capacity), nil, 0,
130+
NI_NUMERICHOST) == 0 {
131+
// print("\tIP Address: \(String(decodingCString: &szAddress, as: UTF16.self))")
132+
_addresses.append(String(decodingCString: &szAddress,
124133
as: UTF16.self))
125134
}
126135
default: break
127136
}
128-
arAddress = arCurrentAddress.Next
137+
pAddress = pAddress!.pointee.Next
129138
}
130-
arCurrentAdapterInfo = arCurrentAdapterInfo!.pointee.Next
139+
140+
pAdapter = pAdapter!.pointee.Next
131141
}
132142
_resolved = true
133143
#else

0 commit comments

Comments
 (0)