Skip to content

Fix for Foundation on Android 6.0 #2815

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Sources/Foundation/Host.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,32 @@ import CoreFoundation
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

open class Host: NSObject {
Expand Down