Improve validator for IPv6.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jeroen Leenarts
2018-08-15 22:52:37 +02:00
parent 15cb942368
commit 91daed0c80
4 changed files with 39 additions and 67 deletions
+8 -5
View File
@@ -34,15 +34,18 @@ struct Endpoint {
var addressType: AddressType
init?(endpointString: String) throws {
let parts = endpointString.split(separator: ":")
guard parts.count == 2 else {
guard let range = endpointString.range(of: ":", options: .backwards, range: nil, locale: nil) else {
throw EndpointValidationError.noIpAndPort(endpointString)
}
guard let port = Int32(parts[1]), port > 0 else {
throw EndpointValidationError.invalidPort(String(parts[1]))
let ipString = endpointString[..<range.lowerBound].replacingOccurrences(of: "[", with: "").replacingOccurrences(of: "]", with: "")
let portString = endpointString[range.upperBound...]
guard let port = Int32(portString), port > 0 else {
throw EndpointValidationError.invalidPort(String(portString/*parts[1]*/))
}
ipAddress = String(parts[0])
ipAddress = String(ipString)
let addressType = validateIpAddress(ipToValidate: ipAddress)
guard addressType == .IPv4 || addressType == .IPv6 else {
throw EndpointValidationError.invalidIP(ipAddress)