diff options
author | Roopesh Chander <roop@roopc.net> | 2018-10-28 11:51:18 +0530 |
---|---|---|
committer | Roopesh Chander <roop@roopc.net> | 2018-10-28 11:51:18 +0530 |
commit | 78ab196a280a1cd9c8ac37840845cbacfcd80afa (patch) | |
tree | c1839ddb3b226c6962ab3aa083dcb5184f67d8fa /WireGuard/WireGuard/VPN | |
parent | Tunnel detail: The tableView should have selection disabled (diff) | |
download | wireguard-apple-78ab196a280a1cd9c8ac37840845cbacfcd80afa.tar.xz wireguard-apple-78ab196a280a1cd9c8ac37840845cbacfcd80afa.zip |
VPN: Cleaner derivation of subnet mask from CIDR network prefix length
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard/WireGuard/VPN')
-rw-r--r-- | WireGuard/WireGuard/VPN/PacketTunnelOptionsGenerator.swift | 22 |
1 files changed, 8 insertions, 14 deletions
diff --git a/WireGuard/WireGuard/VPN/PacketTunnelOptionsGenerator.swift b/WireGuard/WireGuard/VPN/PacketTunnelOptionsGenerator.swift index e3ecb3c..78a5944 100644 --- a/WireGuard/WireGuard/VPN/PacketTunnelOptionsGenerator.swift +++ b/WireGuard/WireGuard/VPN/PacketTunnelOptionsGenerator.swift @@ -155,21 +155,15 @@ class PacketTunnelOptionsGenerator { } static func ipv4SubnetMaskString(of addressRange: IPAddressRange) -> String { - var n: UInt8 = addressRange.networkPrefixLength + let n: UInt8 = addressRange.networkPrefixLength assert(n <= 32) - var components: [UInt8] = [] - while (n >= 8) { - components.append(255) - n = n - 8 - } - if (n > 0) { - components.append(((1 << n) - 1) << (8 - n)) - } - while (components.count < 4) { - components.append(0) - } - assert(components.count == 4) - return components.map { String($0) }.joined(separator: ".") + var octets: [UInt8] = [0, 0, 0, 0] + let subnetMask: UInt32 = n > 0 ? ~UInt32(0) << (32 - n) : UInt32(0) + octets[0] = UInt8(truncatingIfNeeded: subnetMask >> 24) + octets[1] = UInt8(truncatingIfNeeded: subnetMask >> 16) + octets[2] = UInt8(truncatingIfNeeded: subnetMask >> 8) + octets[3] = UInt8(truncatingIfNeeded: subnetMask) + return octets.map { String($0) }.joined(separator: ".") } } |