aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/Shared/Model/DNSServer.swift
diff options
context:
space:
mode:
authorEric Kuck <eric@bluelinelabs.com>2018-12-20 22:52:45 -0600
committerJason A. Donenfeld <Jason@zx2c4.com>2018-12-21 16:42:16 +0100
commit8553723e04c4d63b99e669df2e43fc4a914b7f9d (patch)
treee3b7b25777e47455a787fa590cf00d1c25cbdc5d /WireGuard/Shared/Model/DNSServer.swift
parentNE: simplify logic (diff)
downloadwireguard-apple-8553723e04c4d63b99e669df2e43fc4a914b7f9d.tar.xz
wireguard-apple-8553723e04c4d63b99e669df2e43fc4a914b7f9d.zip
Updated NETunnelProvider save format
Signed-off-by: Eric Kuck <eric@bluelinelabs.com>
Diffstat (limited to 'WireGuard/Shared/Model/DNSServer.swift')
-rw-r--r--WireGuard/Shared/Model/DNSServer.swift62
1 files changed, 30 insertions, 32 deletions
diff --git a/WireGuard/Shared/Model/DNSServer.swift b/WireGuard/Shared/Model/DNSServer.swift
index d1c75fc0..8703fbba 100644
--- a/WireGuard/Shared/Model/DNSServer.swift
+++ b/WireGuard/Shared/Model/DNSServer.swift
@@ -4,52 +4,50 @@
import Foundation
import Network
-@available(OSX 10.14, iOS 12.0, *)
struct DNSServer {
let address: IPAddress
-}
-
-// MARK: Converting to and from String
-
-extension DNSServer {
- init?(from addressString: String) {
- if let addr = IPv4Address(addressString) {
- address = addr
- } else if let addr = IPv6Address(addressString) {
- address = addr
- } else {
- return nil
- }
- }
- func stringRepresentation() -> String {
- return "\(address)"
+
+ init(address: IPAddress) {
+ self.address = address
}
}
-// MARK: Codable
-
-@available(OSX 10.14, iOS 12.0, *)
extension DNSServer: Codable {
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
- try container.encode(address.rawValue)
+ try container.encode(stringRepresentation)
}
+
public init(from decoder: Decoder) throws {
- let container = try decoder.singleValueContainer()
- var data = try container.decode(Data.self)
- let ipAddressFromData: IPAddress? = {
- switch data.count {
- case 4: return IPv4Address(data)
- case 16: return IPv6Address(data)
- default: return nil
- }
- }()
- guard let ipAddress = ipAddressFromData else {
+ let values = try decoder.singleValueContainer()
+ let addressString = try values.decode(String.self)
+
+ if let address = IPv4Address(addressString) {
+ self.address = address
+ } else if let address = IPv6Address(addressString) {
+ self.address = address
+ } else {
throw DecodingError.invalidData
}
- address = ipAddress
}
+
enum DecodingError: Error {
case invalidData
}
}
+
+extension DNSServer {
+ var stringRepresentation: String {
+ return "\(address)"
+ }
+
+ init?(from addressString: String) {
+ if let addr = IPv4Address(addressString) {
+ address = addr
+ } else if let addr = IPv6Address(addressString) {
+ address = addr
+ } else {
+ return nil
+ }
+ }
+}