1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
|
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
@_implementationOnly import CoreFoundation
#if os(Windows)
import WinSDK
#endif
#if os(Android)
// Android Glibc differs a little with respect to the Linux Glibc.
// IFF_LOOPBACK is part of the enumeration net_device_flags, which needs to
// convert to UInt32.
private extension UInt32 {
init(_ value: net_device_flags) {
self.init(value.rawValue)
}
}
// getnameinfo uses size_t for its 4th and 6th arguments.
private func getnameinfo(_ addr: UnsafePointer<sockaddr>?, _ addrlen: socklen_t, _ host: UnsafeMutablePointer<Int8>?, _ hostlen: socklen_t, _ serv: UnsafeMutablePointer<Int8>?, _ servlen: socklen_t, _ flags: Int32) -> Int32 {
return Glibc.getnameinfo(addr, addrlen, host, Int(hostlen), serv, Int(servlen), flags)
}
// getifaddrs and freeifaddrs are not available in Android 6.0 or earlier, so call these functions dynamically.
// This only happens during the initial lookup of the addresses and then the results are cached and _resolved is marked true.
// If this API changes so these functions are called more frequently, it might be beneficial to cache the function pointers.
private typealias GetIfAddrsFunc = @convention(c) (UnsafeMutablePointer<UnsafeMutablePointer<ifaddrs>?>) -> Int32
private func getifaddrs(_ ifap: UnsafeMutablePointer<UnsafeMutablePointer<ifaddrs>?>) -> Int32 {
var result: Int32 = -1
if let handle = dlopen("libc.so", RTLD_NOLOAD) {
if let entry = dlsym(handle, "getifaddrs") {
result = unsafeBitCast(entry, to: GetIfAddrsFunc.self)(ifap)
}
dlclose(handle)
}
return result
}
private typealias FreeIfAddrsFunc = @convention(c) (UnsafeMutablePointer<ifaddrs>?) -> Void
private func freeifaddrs(_ ifa: UnsafeMutablePointer<ifaddrs>?) {
if let handle = dlopen("libc.so", RTLD_NOLOAD) {
if let entry = dlsym(handle, "freeifaddrs") {
unsafeBitCast(entry, to: FreeIfAddrsFunc.self)(ifa)
}
dlclose(handle)
}
}
#endif
@available(*, unavailable)
extension Host : @unchecked Sendable { }
open class Host: NSObject {
enum ResolveType {
case name
case address
case current
}
internal var _info: String?
internal var _type: ResolveType
internal var _resolved = false
internal var _names = [String]()
internal var _addresses = [String]()
static internal let _cachedCurrentHostName = currentHostName()
internal init(_ info: String?, _ type: ResolveType) {
_info = info
_type = type
}
static internal func currentHostName() -> String {
#if os(Windows)
var dwLength: DWORD = 0
GetComputerNameExA(ComputerNameDnsHostname, nil, &dwLength)
guard dwLength > 0 else { return "localhost" }
guard let hostname: UnsafeMutablePointer<Int8> =
UnsafeMutableBufferPointer<Int8>
.allocate(capacity: Int(dwLength + 1))
.baseAddress else {
return "localhost"
}
defer { hostname.deallocate() }
guard GetComputerNameExA(ComputerNameDnsHostname, hostname, &dwLength) else {
return "localhost"
}
return String(cString: hostname)
#elseif os(WASI) // WASI does not have uname
return "localhost"
#else
let hname = UnsafeMutablePointer<Int8>.allocate(capacity: Int(NI_MAXHOST))
defer {
hname.deallocate()
}
let r = gethostname(hname, Int(NI_MAXHOST))
if r < 0 || hname[0] == 0 {
return "localhost"
}
return String(cString: hname)
#endif
}
open class func current() -> Host {
return Host(Self._cachedCurrentHostName, .current)
}
public convenience init(name: String?) {
self.init(name, .name)
}
public convenience init(address: String) {
self.init(address, .address)
}
open func isEqual(to aHost: Host) -> Bool {
if self === aHost { return true }
return addresses.firstIndex { aHost.addresses.contains($0) } != nil
}
internal func _resolveCurrent(withInfo info: String) {
#if os(Windows)
var szAddress: [WCHAR] =
Array<WCHAR>(repeating: 0, count: Int(NI_MAXHOST))
var ulSize: ULONG = 0
var ulResult: ULONG =
GetAdaptersAddresses(ULONG(AF_UNSPEC), 0, nil, nil, &ulSize)
let arAdapters: UnsafeMutableRawPointer =
UnsafeMutableRawPointer.allocate(byteCount: Int(ulSize),
alignment: 1)
defer { arAdapters.deallocate() }
ulResult = GetAdaptersAddresses(ULONG(AF_UNSPEC), 0, nil,
arAdapters.assumingMemoryBound(to: IP_ADAPTER_ADDRESSES.self),
&ulSize)
guard ulResult == ERROR_SUCCESS else { return }
var pAdapter: UnsafeMutablePointer<IP_ADAPTER_ADDRESSES>? =
arAdapters.assumingMemoryBound(to: IP_ADAPTER_ADDRESSES.self)
while pAdapter != nil {
// print("Adapter: \(String(cString: pAdapter!.pointee.AdapterName))")
let arAddresses: UnsafeMutablePointer<IP_ADAPTER_UNICAST_ADDRESS> =
pAdapter!.pointee.FirstUnicastAddress
var pAddress: UnsafeMutablePointer<IP_ADAPTER_UNICAST_ADDRESS>? =
arAddresses
while pAddress != nil {
switch pAddress!.pointee.Address.lpSockaddr.pointee.sa_family {
case ADDRESS_FAMILY(AF_INET), ADDRESS_FAMILY(AF_INET6):
if GetNameInfoW(pAddress!.pointee.Address.lpSockaddr,
pAddress!.pointee.Address.iSockaddrLength,
&szAddress, DWORD(szAddress.capacity), nil, 0,
NI_NUMERICHOST) == 0 {
// print("\tIP Address: \(String(decodingCString: &szAddress, as: UTF16.self))")
_addresses.append(String(decodingCString: &szAddress,
as: UTF16.self))
}
default: break
}
pAddress = pAddress!.pointee.Next
}
pAdapter = pAdapter!.pointee.Next
}
_names = [info]
_resolved = true
#elseif os(WASI) // WASI does not have getifaddrs
_names = [info]
_resolved = true
#else
var ifaddr: UnsafeMutablePointer<ifaddrs>? = nil
if getifaddrs(&ifaddr) != 0 {
return
}
var ifa: UnsafeMutablePointer<ifaddrs>? = ifaddr
let address = UnsafeMutablePointer<Int8>.allocate(capacity: Int(NI_MAXHOST))
defer {
freeifaddrs(ifaddr)
address.deallocate()
}
while let ifaValue = ifa?.pointee {
if let ifa_addr = ifaValue.ifa_addr, ifaValue.ifa_flags & UInt32(IFF_LOOPBACK) == 0 {
let family = ifa_addr.pointee.sa_family
if family == sa_family_t(AF_INET) || family == sa_family_t(AF_INET6) {
let sa_len: socklen_t = socklen_t((family == sa_family_t(AF_INET6)) ? MemoryLayout<sockaddr_in6>.size : MemoryLayout<sockaddr_in>.size)
#if os(OpenBSD)
let hostlen = size_t(NI_MAXHOST)
#else
let hostlen = socklen_t(NI_MAXHOST)
#endif
if getnameinfo(ifa_addr, sa_len, address, hostlen, nil, 0, NI_NUMERICHOST) == 0 {
_addresses.append(String(cString: address))
}
}
}
ifa = ifaValue.ifa_next
}
_names = [info]
_resolved = true
#endif
}
internal func _resolve() {
guard _resolved == false else { return }
#if os(Windows)
if let info = _info {
if _type == .current { return _resolveCurrent(withInfo: info) }
var hints: ADDRINFOW = ADDRINFOW()
memset(&hints, 0, MemoryLayout<ADDRINFOW>.size)
switch (_type) {
case .name:
hints.ai_flags = AI_PASSIVE | AI_CANONNAME
case .address:
hints.ai_flags = AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST
case .current:
break
}
hints.ai_family = AF_UNSPEC
hints.ai_socktype = SOCK_STREAM
hints.ai_protocol = IPPROTO_TCP.rawValue
var aiResult: UnsafeMutablePointer<ADDRINFOW>?
var bSucceeded: Bool = false
info.withCString(encodedAs: UTF16.self) {
if GetAddrInfoW($0, nil, &hints, &aiResult) == 0 {
bSucceeded = true
}
}
guard bSucceeded == true else { return }
defer { FreeAddrInfoW(aiResult) }
var wszHostName: [WCHAR] = Array<WCHAR>(repeating: 0, count: Int(NI_MAXHOST))
while aiResult != nil {
let aiInfo: ADDRINFOW = aiResult!.pointee
var sa_len: socklen_t = 0
switch aiInfo.ai_family {
case AF_INET:
sa_len = socklen_t(MemoryLayout<sockaddr_in>.size)
case AF_INET6:
sa_len = socklen_t(MemoryLayout<sockaddr_in6>.size)
default:
aiResult = aiInfo.ai_next
continue
}
let lookup = { (content: inout [String], flags: Int32) in
if GetNameInfoW(aiInfo.ai_addr, sa_len, &wszHostName,
DWORD(NI_MAXHOST), nil, 0, flags) == 0 {
content.append(String(decodingCString: &wszHostName,
as: UTF16.self))
}
}
lookup(&_addresses, NI_NUMERICHOST)
lookup(&_names, NI_NAMEREQD)
lookup(&_names, NI_NOFQDN | NI_NAMEREQD)
aiResult = aiInfo.ai_next
}
_resolved = true
}
#elseif os(WASI) // WASI does not have getaddrinfo
if let info = _info {
_names = [info]
_resolved = true
}
#else
if let info = _info {
var flags: Int32 = 0
switch (_type) {
case .name:
flags = AI_PASSIVE | AI_CANONNAME
case .address:
flags = AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST
case .current:
_resolveCurrent(withInfo: info)
return
}
var hints = addrinfo()
hints.ai_family = PF_UNSPEC
#if os(macOS) || os(iOS) || os(Android) || os(OpenBSD) || canImport(Musl)
hints.ai_socktype = SOCK_STREAM
#else
hints.ai_socktype = Int32(SOCK_STREAM.rawValue)
#endif
hints.ai_flags = flags
var res0: UnsafeMutablePointer<addrinfo>? = nil
let r = getaddrinfo(info, nil, &hints, &res0)
defer {
freeaddrinfo(res0)
}
if r != 0 {
return
}
var res: UnsafeMutablePointer<addrinfo>? = res0
let host = UnsafeMutablePointer<Int8>.allocate(capacity: Int(NI_MAXHOST))
defer {
host.deallocate()
}
while res != nil {
let info = res!.pointee
let family = info.ai_family
if family != AF_INET && family != AF_INET6 {
res = info.ai_next
continue
}
let sa_len: socklen_t = socklen_t((family == AF_INET6) ? MemoryLayout<sockaddr_in6>.size : MemoryLayout<sockaddr_in>.size)
let lookupInfo = { (content: inout [String], flags: Int32) in
#if os(OpenBSD)
let hostlen = size_t(NI_MAXHOST)
#else
let hostlen = socklen_t(NI_MAXHOST)
#endif
if getnameinfo(info.ai_addr, sa_len, host, hostlen, nil, 0, flags) == 0 {
content.append(String(cString: host))
}
}
lookupInfo(&_addresses, NI_NUMERICHOST)
lookupInfo(&_names, NI_NAMEREQD)
lookupInfo(&_names, NI_NOFQDN|NI_NAMEREQD)
res = info.ai_next
}
_resolved = true
}
#endif
}
open var name: String? {
return names.first
}
open var names: [String] {
_resolve()
return _names
}
open var address: String? {
return addresses.first
}
open var addresses: [String] {
_resolve()
return _addresses
}
open var localizedName: String? {
return nil
}
}
|