2018-10-24 01:37:28 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-06-17 14:56:46 +00:00
|
|
|
// Copyright © 2018-2021 WireGuard LLC. All Rights Reserved.
|
2018-10-13 13:29:42 +00:00
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Network
|
|
|
|
|
2020-11-05 11:23:06 +00:00
|
|
|
public struct IPAddressRange {
|
|
|
|
public let address: IPAddress
|
|
|
|
public let networkPrefixLength: UInt8
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-21 04:52:45 +00:00
|
|
|
init(address: IPAddress, networkPrefixLength: UInt8) {
|
|
|
|
self.address = address
|
|
|
|
self.networkPrefixLength = networkPrefixLength
|
|
|
|
}
|
2018-10-13 13:29:42 +00:00
|
|
|
}
|
|
|
|
|
2018-12-22 04:41:54 +00:00
|
|
|
extension IPAddressRange: Equatable {
|
2020-11-05 11:23:06 +00:00
|
|
|
public static func == (lhs: IPAddressRange, rhs: IPAddressRange) -> Bool {
|
2018-12-22 04:41:54 +00:00
|
|
|
return lhs.address.rawValue == rhs.address.rawValue && lhs.networkPrefixLength == rhs.networkPrefixLength
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension IPAddressRange: Hashable {
|
2020-11-05 11:23:06 +00:00
|
|
|
public func hash(into hasher: inout Hasher) {
|
2018-12-22 04:41:54 +00:00
|
|
|
hasher.combine(address.rawValue)
|
|
|
|
hasher.combine(networkPrefixLength)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-19 03:45:37 +00:00
|
|
|
extension IPAddressRange {
|
2020-11-05 11:23:06 +00:00
|
|
|
public var stringRepresentation: String {
|
2018-12-21 04:52:45 +00:00
|
|
|
return "\(address)/\(networkPrefixLength)"
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2020-11-05 11:23:06 +00:00
|
|
|
public init?(from string: String) {
|
2018-12-21 04:52:45 +00:00
|
|
|
guard let parsed = IPAddressRange.parseAddressString(string) else { return nil }
|
|
|
|
address = parsed.0
|
|
|
|
networkPrefixLength = parsed.1
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-21 04:52:45 +00:00
|
|
|
private static func parseAddressString(_ string: String) -> (IPAddress, UInt8)? {
|
2018-10-30 08:15:16 +00:00
|
|
|
let endOfIPAddress = string.lastIndex(of: "/") ?? string.endIndex
|
|
|
|
let addressString = String(string[string.startIndex ..< endOfIPAddress])
|
|
|
|
let address: IPAddress
|
2018-10-19 03:45:37 +00:00
|
|
|
if let addr = IPv4Address(addressString) {
|
2018-11-06 17:12:53 +00:00
|
|
|
address = addr
|
2018-10-19 03:45:37 +00:00
|
|
|
} else if let addr = IPv6Address(addressString) {
|
|
|
|
address = addr
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-21 04:52:45 +00:00
|
|
|
let maxNetworkPrefixLength: UInt8 = address is IPv4Address ? 32 : 128
|
2018-10-30 08:15:16 +00:00
|
|
|
var networkPrefixLength: UInt8
|
2018-12-12 17:40:57 +00:00
|
|
|
if endOfIPAddress < string.endIndex { // "/" was located
|
2018-10-30 08:15:16 +00:00
|
|
|
let indexOfNetworkPrefixLength = string.index(after: endOfIPAddress)
|
2018-12-12 17:40:57 +00:00
|
|
|
guard indexOfNetworkPrefixLength < string.endIndex else { return nil }
|
2018-10-30 08:15:16 +00:00
|
|
|
let networkPrefixLengthSubstring = string[indexOfNetworkPrefixLength ..< string.endIndex]
|
|
|
|
guard let npl = UInt8(networkPrefixLengthSubstring) else { return nil }
|
|
|
|
networkPrefixLength = min(npl, maxNetworkPrefixLength)
|
2018-10-19 03:45:37 +00:00
|
|
|
} else {
|
2018-10-30 08:15:16 +00:00
|
|
|
networkPrefixLength = maxNetworkPrefixLength
|
2018-10-19 03:45:37 +00:00
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-21 04:52:45 +00:00
|
|
|
return (address, networkPrefixLength)
|
2018-10-19 03:45:37 +00:00
|
|
|
}
|
2021-01-01 17:26:49 +00:00
|
|
|
|
|
|
|
public func subnetMask() -> IPAddress {
|
|
|
|
if address is IPv4Address {
|
|
|
|
let mask = networkPrefixLength > 0 ? ~UInt32(0) << (32 - networkPrefixLength) : UInt32(0)
|
|
|
|
let bytes = Data([
|
|
|
|
UInt8(truncatingIfNeeded: mask >> 24),
|
|
|
|
UInt8(truncatingIfNeeded: mask >> 16),
|
|
|
|
UInt8(truncatingIfNeeded: mask >> 8),
|
|
|
|
UInt8(truncatingIfNeeded: mask >> 0)
|
|
|
|
])
|
|
|
|
return IPv4Address(bytes)!
|
|
|
|
}
|
|
|
|
if address is IPv6Address {
|
|
|
|
var bytes = Data(repeating: 0, count: 16)
|
|
|
|
for i in 0..<Int(networkPrefixLength/8) {
|
|
|
|
bytes[i] = 0xff
|
|
|
|
}
|
|
|
|
let nibble = networkPrefixLength % 32
|
|
|
|
if nibble != 0 {
|
|
|
|
let mask = ~UInt32(0) << (32 - nibble)
|
|
|
|
let i = Int(networkPrefixLength / 32 * 4)
|
|
|
|
bytes[i + 0] = UInt8(truncatingIfNeeded: mask >> 24)
|
|
|
|
bytes[i + 1] = UInt8(truncatingIfNeeded: mask >> 16)
|
|
|
|
bytes[i + 2] = UInt8(truncatingIfNeeded: mask >> 8)
|
|
|
|
bytes[i + 3] = UInt8(truncatingIfNeeded: mask >> 0)
|
|
|
|
}
|
|
|
|
return IPv6Address(bytes)!
|
|
|
|
}
|
|
|
|
fatalError()
|
|
|
|
}
|
|
|
|
|
|
|
|
public func maskedAddress() -> IPAddress {
|
|
|
|
let subnet = subnetMask().rawValue
|
|
|
|
var masked = Data(address.rawValue)
|
|
|
|
if subnet.count != masked.count {
|
|
|
|
fatalError()
|
|
|
|
}
|
|
|
|
for i in 0..<subnet.count {
|
|
|
|
masked[i] &= subnet[i]
|
|
|
|
}
|
|
|
|
if subnet.count == 4 {
|
|
|
|
return IPv4Address(masked)!
|
|
|
|
}
|
|
|
|
if subnet.count == 16 {
|
|
|
|
return IPv6Address(masked)!
|
|
|
|
}
|
|
|
|
fatalError()
|
|
|
|
}
|
2018-10-19 03:45:37 +00:00
|
|
|
}
|