Skip to content

Commit

Permalink
refactor: update AddressType.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
jurajhilje committed Aug 26, 2023
1 parent 34d6bc1 commit 59e19c5
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
16 changes: 7 additions & 9 deletions IVPNClient/Enums/AddressType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,22 @@
// along with the IVPN iOS app. If not, see <https://www.gnu.org/licenses/>.
//

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
}

}
2 changes: 1 addition & 1 deletion IVPNClient/Models/WireGuard/CIDRAddress.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions IVPNClient/Models/WireGuard/WireGuardEndpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions UnitTests/Enum/AddressTypeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}

0 comments on commit 59e19c5

Please sign in to comment.