From 59e19c51c8cb7d1332ce71c41cb74f6c754d8e73 Mon Sep 17 00:00:00 2001 From: Juraj Hilje Date: Sat, 26 Aug 2023 10:41:06 +0200 Subject: [PATCH] refactor: update AddressType.swift --- IVPNClient/Enums/AddressType.swift | 16 +++++++--------- IVPNClient/Models/WireGuard/CIDRAddress.swift | 2 +- .../Models/WireGuard/WireGuardEndpoint.swift | 4 ++-- UnitTests/Enum/AddressTypeTests.swift | 6 +++--- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/IVPNClient/Enums/AddressType.swift b/IVPNClient/Enums/AddressType.swift index dfd4e784e..2ba6936bd 100644 --- a/IVPNClient/Enums/AddressType.swift +++ b/IVPNClient/Enums/AddressType.swift @@ -21,24 +21,22 @@ // along with the IVPN iOS app. If not, see . // +import Network + enum AddressType { case IPv6 case IPv4 case other - static func validateIpAddress(ipToValidate: String) -> AddressType { - var sin = sockaddr_in() - if ipToValidate.withCString({ cstring in inet_pton(AF_INET, cstring, &sin.sin_addr) }) == 1 { + static func validateIpAddress(_ address: String) -> AddressType { + if let _ = IPv4Address(address) { return .IPv4 - } - - var sin6 = sockaddr_in6() - if ipToValidate.withCString({ cstring in inet_pton(AF_INET6, cstring, &sin6.sin6_addr) }) == 1 { + } else if let _ = IPv6Address(address) { return .IPv6 + } else { + return .other } - - return .other } } diff --git a/IVPNClient/Models/WireGuard/CIDRAddress.swift b/IVPNClient/Models/WireGuard/CIDRAddress.swift index 0c7f3d4c4..1147b9e14 100644 --- a/IVPNClient/Models/WireGuard/CIDRAddress.swift +++ b/IVPNClient/Models/WireGuard/CIDRAddress.swift @@ -58,7 +58,7 @@ struct CIDRAddress { subnetString = "" } - let addressType = AddressType.validateIpAddress(ipToValidate: ipAddress) + let addressType = AddressType.validateIpAddress(ipAddress) guard addressType == .IPv4 || addressType == .IPv6 else { throw CIDRAddressValidationError.invalidIP(ipAddress) diff --git a/IVPNClient/Models/WireGuard/WireGuardEndpoint.swift b/IVPNClient/Models/WireGuard/WireGuardEndpoint.swift index 34ce8b22b..39ebef55c 100644 --- a/IVPNClient/Models/WireGuard/WireGuardEndpoint.swift +++ b/IVPNClient/Models/WireGuard/WireGuardEndpoint.swift @@ -49,7 +49,7 @@ struct WireGuardEndpoint { } hostString = hostString.replacingOccurrences(of: "[", with: "").replacingOccurrences(of: "]", with: "") - var addressType = AddressType.validateIpAddress(ipToValidate: hostString) + var addressType = AddressType.validateIpAddress(hostString) let ipString: String if addressType == .other { @@ -59,7 +59,7 @@ struct WireGuardEndpoint { } ipAddress = String(ipString) - addressType = AddressType.validateIpAddress(ipToValidate: ipAddress) + addressType = AddressType.validateIpAddress(ipAddress) guard addressType == .IPv4 || addressType == .IPv6 else { throw EndpointValidationError.invalidIP(ipAddress) diff --git a/UnitTests/Enum/AddressTypeTests.swift b/UnitTests/Enum/AddressTypeTests.swift index 46effe278..29ed2fee6 100644 --- a/UnitTests/Enum/AddressTypeTests.swift +++ b/UnitTests/Enum/AddressTypeTests.swift @@ -32,9 +32,9 @@ class AddressTypeTests: XCTestCase { let ipAddress2 = "::1" let ipAddress3 = "-" - XCTAssertEqual(AddressType.validateIpAddress(ipToValidate: ipAddress1), .IPv4) - XCTAssertEqual(AddressType.validateIpAddress(ipToValidate: ipAddress2), .IPv6) - XCTAssertEqual(AddressType.validateIpAddress(ipToValidate: ipAddress3), .other) + XCTAssertEqual(AddressType.validateIpAddress(ipAddress1), .IPv4) + XCTAssertEqual(AddressType.validateIpAddress(ipAddress2), .IPv6) + XCTAssertEqual(AddressType.validateIpAddress(ipAddress3), .other) } }