I have some code that I have cobbled togther without really understanding the details.
It works great in debug builds, but fails if you build for release.
I found this out when I released an update 😟
can anyone explain what is going on?
extension NetService {
var debugOnlyIPAddresses : [String] {
var ipAddresses:[String] = []
guard let addresses = addresses else {
return []
}
for address in addresses {
let data = address as NSData
let inetAddress: sockaddr_in = data.castToCPointer()
if inetAddress.sin_family == __uint8_t(AF_INET) {
if let ip = String(cString: inet_ntoa(inetAddress.sin_addr), encoding: .ascii) {
ipAddresses.append(ip)
}
} else if inetAddress.sin_family == __uint8_t(AF_INET6) {
let inetAddress6: sockaddr_in6 = data.castToCPointer()
let ipStringBuffer = UnsafeMutablePointer.allocate(capacity: Int(INET6_ADDRSTRLEN))
var addr = inetAddress6.sin6_addr
if let ipString = inet_ntop(Int32(inetAddress6.sin6_family), &addr, ipStringBuffer, __uint32_t(INET6_ADDRSTRLEN)) {
if let ip = String(cString: ipString, encoding: .ascii) {
// IPv6
ipAddresses.append(ip)
}
}
ipStringBuffer.deallocate()
}
}
print("returning \(ipAddresses)")
return ipAddresses
}
}
in release builds, inetAddress.sin_family is always 0