wireguard-apple/Shared/Validators.swift

155 lines
5.4 KiB
Swift
Raw Normal View History

2018-08-15 18:35:21 +00:00
//
// Copyright © 2018 WireGuard LLC. All rights reserved.
2018-08-15 18:35:21 +00:00
//
import Foundation
enum AddressType {
case IPv6, IPv4, other
}
public enum EndpointValidationError: Error {
case noIpAndPort(String)
case invalidIP(String)
case invalidPort(String)
var localizedDescription: String {
switch self {
case .noIpAndPort:
return NSLocalizedString("EndpointValidationError.noIpAndPort", comment: "Error message for malformed endpoint.")
case .invalidIP:
return NSLocalizedString("EndpointValidationError.invalidIP", comment: "Error message for invalid endpoint ip.")
case .invalidPort:
return NSLocalizedString("EndpointValidationError.invalidPort", comment: "Error message invalid endpoint port.")
}
}
}
2018-08-15 18:35:21 +00:00
struct Endpoint {
var ipAddress: String
var port: Int32?
2018-08-15 18:35:21 +00:00
var addressType: AddressType
init?(endpointString: String, needsPort: Bool = true) throws {
2018-09-30 19:39:00 +00:00
var hostString: String
if needsPort {
guard let range = endpointString.range(of: ":", options: .backwards, range: nil, locale: nil) else {
throw EndpointValidationError.noIpAndPort(endpointString)
}
2018-09-30 19:39:00 +00:00
hostString = String(endpointString[..<range.lowerBound])
2018-08-15 20:52:37 +00:00
let portString = endpointString[range.upperBound...]
2018-08-15 20:52:37 +00:00
guard let port = Int32(portString), port > 0 else {
throw EndpointValidationError.invalidPort(String(portString/*parts[1]*/))
}
self.port = port
} else {
2018-09-30 19:39:00 +00:00
hostString = endpointString
2018-08-15 18:35:21 +00:00
}
2018-09-30 19:39:00 +00:00
hostString = hostString.replacingOccurrences(of: "[", with: "").replacingOccurrences(of: "]", with: "")
let ipString = convertToipAddress(from: hostString)
2018-08-15 20:52:37 +00:00
ipAddress = String(ipString)
2018-08-15 18:35:21 +00:00
let addressType = validateIpAddress(ipToValidate: ipAddress)
guard addressType == .IPv4 || addressType == .IPv6 else {
throw EndpointValidationError.invalidIP(ipAddress)
}
self.addressType = addressType
}
}
2018-09-30 19:39:00 +00:00
private func convertToipAddress(from hostname: String) -> String {
let host = CFHostCreateWithName(nil, hostname as CFString).takeRetainedValue()
CFHostStartInfoResolution(host, .addresses, nil)
var success: DarwinBoolean = false
if let addresses = CFHostGetAddressing(host, &success)?.takeUnretainedValue() as NSArray?,
let theAddress = addresses.firstObject as? NSData {
var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
if getnameinfo(theAddress.bytes.assumingMemoryBound(to: sockaddr.self), socklen_t(theAddress.length),
&hostname, socklen_t(hostname.count), nil, 0, NI_NUMERICHOST) == 0 {
let numAddress = String(cString: hostname)
return numAddress
}
}
return hostname
}
2018-08-15 18:35:21 +00:00
func validateIpAddress(ipToValidate: String) -> AddressType {
var sin = sockaddr_in()
if ipToValidate.withCString({ cstring in inet_pton(AF_INET, cstring, &sin.sin_addr) }) == 1 {
// IPv4 peer.
return .IPv4
}
var sin6 = sockaddr_in6()
if ipToValidate.withCString({ cstring in inet_pton(AF_INET6, cstring, &sin6.sin6_addr) }) == 1 {
// IPv6 peer.
return .IPv6
}
return .other
}
public enum CIDRAddressValidationError: Error {
case noIpAndSubnet(String)
case invalidIP(String)
case invalidSubnet(String)
var localizedDescription: String {
switch self {
case .noIpAndSubnet:
return NSLocalizedString("CIDRAddressValidationError", comment: "Error message for malformed CIDR address.")
case .invalidIP:
return NSLocalizedString("CIDRAddressValidationError", comment: "Error message for invalid address ip.")
case .invalidSubnet:
return NSLocalizedString("CIDRAddressValidationError", comment: "Error message invalid address subnet.")
}
}
}
struct CIDRAddress {
var ipAddress: String
var subnet: Int32
var addressType: AddressType
init?(stringRepresentation: String) throws {
guard let range = stringRepresentation.range(of: "/", options: .backwards, range: nil, locale: nil) else {
throw CIDRAddressValidationError.noIpAndSubnet(stringRepresentation)
}
let ipString = stringRepresentation[..<range.lowerBound].replacingOccurrences(of: "[", with: "").replacingOccurrences(of: "]", with: "")
let subnetString = stringRepresentation[range.upperBound...]
guard let subnet = Int32(subnetString) else {
throw CIDRAddressValidationError.invalidSubnet(String(subnetString))
}
ipAddress = String(ipString)
let addressType = validateIpAddress(ipToValidate: ipAddress)
guard addressType == .IPv4 || addressType == .IPv6 else {
throw CIDRAddressValidationError.invalidIP(ipAddress)
}
self.addressType = addressType
self.subnet = subnet
}
2018-08-16 19:26:24 +00:00
var subnetString: String {
// We could calculate these.
var bitMask: UInt32 = 0b11111111111111111111111111111111
bitMask = bitMask << (32 - subnet)
let first = UInt8(truncatingIfNeeded: bitMask >> 24)
let second = UInt8(truncatingIfNeeded: bitMask >> 16 )
let third = UInt8(truncatingIfNeeded: bitMask >> 8)
let fourth = UInt8(truncatingIfNeeded: bitMask)
return "\(first).\(second).\(third).\(fourth)"
}
}