diff options
author | Jeroen Leenarts <jeroen.leenarts@gmail.com> | 2018-08-16 22:41:45 +0200 |
---|---|---|
committer | Jeroen Leenarts <jeroen.leenarts@gmail.com> | 2018-08-16 22:41:45 +0200 |
commit | 5a7e67b53c4cd81558e5faf0b287584db3ec208d (patch) | |
tree | ebeccd024cdf29cc46114b73cc0c43b8d087538e /Shared/Validators.swift | |
parent | Use `commaSeparatedToArray` utility everywhere. (diff) | |
download | wireguard-apple-5a7e67b53c4cd81558e5faf0b287584db3ec208d.tar.xz wireguard-apple-5a7e67b53c4cd81558e5faf0b287584db3ec208d.zip |
Extend validators to work for DNS entries as well.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'Shared/Validators.swift')
-rw-r--r-- | Shared/Validators.swift | 30 |
1 files changed, 18 insertions, 12 deletions
diff --git a/Shared/Validators.swift b/Shared/Validators.swift index f065f0e..0b95587 100644 --- a/Shared/Validators.swift +++ b/Shared/Validators.swift @@ -31,20 +31,28 @@ public enum EndpointValidationError: Error { struct Endpoint { var ipAddress: String - var port: Int32 + var port: Int32? var addressType: AddressType - init?(endpointString: String) throws { - guard let range = endpointString.range(of: ":", options: .backwards, range: nil, locale: nil) else { - throw EndpointValidationError.noIpAndPort(endpointString) + init?(endpointString: String, needsPort: Bool = true) throws { + var ipString: String + if needsPort { + guard let range = endpointString.range(of: ":", options: .backwards, range: nil, locale: nil) else { + throw EndpointValidationError.noIpAndPort(endpointString) + } + ipString = String(endpointString[..<range.lowerBound]) + + let portString = endpointString[range.upperBound...] + + guard let port = Int32(portString), port > 0 else { + throw EndpointValidationError.invalidPort(String(portString/*parts[1]*/)) + } + self.port = port + } else { + ipString = endpointString } - 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]*/)) - } + ipString = ipString.replacingOccurrences(of: "[", with: "").replacingOccurrences(of: "]", with: "") ipAddress = String(ipString) let addressType = validateIpAddress(ipToValidate: ipAddress) @@ -52,8 +60,6 @@ struct Endpoint { throw EndpointValidationError.invalidIP(ipAddress) } self.addressType = addressType - - self.port = port } } |