From a8af2b40ab9a0e000e89205f7983b82c0b71a890 Mon Sep 17 00:00:00 2001 From: Tyler Cloutier Date: Sun, 29 Oct 2017 19:38:08 -0700 Subject: [PATCH] Added isLinkLocalUnicast as defined in https://golang.org/src/net/ip.go --- NetUtils/Interface.swift | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/NetUtils/Interface.swift b/NetUtils/Interface.swift index 71118ca..9f103ae 100644 --- a/NetUtils/Interface.swift +++ b/NetUtils/Interface.swift @@ -190,6 +190,21 @@ open class Interface : CustomStringConvertible, CustomDebugStringConvertible { /// `IFF_MULTICAST` flag of `ifaddrs->ifa_flags`. open var supportsMulticast: Bool { return multicastSupported } + + /// IsLinkLocalUnicast as defined in https://golang.org/src/net/ip.go + open var isLinkLocalUnicast: Bool { + guard let addressBytes = self.addressBytes else { + return false + } + switch self.family { + case .ipv4: + return addressBytes[0] == 169 && addressBytes[1] == 254 + case .ipv6: + return addressBytes[0] == 0xfe && addressBytes[1] & 0xc0 == 0x80 + case .other: + return false + } + } /// Field `ifaddrs->ifa_name`. open let name : String