I checked the below code to get ipv6 address and this code returned global unicast address like '2001:x:x...'. But I want to get link local address like 'fe80:...'. How can I get link local address by using the below code?
static var ipAddress: String? {
var ipv6 : String?
var ifaddr : UnsafeMutablePointer<ifaddrs>?
guard getifaddrs(&ifaddr) == 0 else { return (nil, nil) }
guard let firstAddr = ifaddr else { return (nil, nil) }
for ifptr in sequence(first: firstAddr, next: { $0.pointee.ifa_next }) {
let interface = ifptr.pointee
let addrFamily = interface.ifa_addr.pointee.sa_family
if addrFamily == UInt8(AF_INET) || addrFamily == UInt8(AF_INET6) {
let name = String(cString: interface.ifa_name)
if name == "en0" {
var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
getnameinfo(interface.ifa_addr, socklen_t(interface.ifa_addr.pointee.sa_len),&hostname, socklen_t(hostname.count),nil, socklen_t(0), NI_NUMERICHOST)
ipv6 = String(cString: hostname)
}
}
}
freeifaddrs(ifaddr)
return ipv6
}