the only thing missing is the IP address as a String and not an array of Data
You can use
getnameinfo
for that. There’s an example of that pasted in below.
IMPORTANT Make sure to pass in
NI_NUMERICHOST
and
NI_NUMERICSERV
to guarantee that it won’t hit the network.
To get the router IP, I could take the iPod IP address and change the last digit by 1
That’s fine as long as you are sure that all your networks use that convention.
another good wrapper around
getifaddrs
that I found on github
Sure. Although the fact they use
inet_ntop
in the IPv6 case is a bit weird. In my experience
getnameinfo
handles both IPv4 and IPv6 equally well.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"
private static func name(for address: Data) -> String {
return address.withUnsafeBytes { (sa: UnsafePointer<sockaddr>) in
var nameBytes = [CChar](repeating: 0, count: Int(NI_MAXHOST))
let err = getnameinfo(
sa,
socklen_t(address.count),
&nameBytes, socklen_t(nameBytes.count),
nil, 0,
NI_NUMERICHOST | NI_NUMERICSERV
)
guard err == 0 else {
return "?"
}
return "\(String(cString: nameBytes))"
}
}