aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/Shared/Model/DNSServer.swift
diff options
context:
space:
mode:
authorRoopesh Chander <roop@roopc.net>2018-11-08 13:56:50 +0530
committerRoopesh Chander <roop@roopc.net>2018-11-08 13:56:50 +0530
commitc88c660b516f4f8dd75944dc440b17a383c5154f (patch)
tree03595c3ff4b4c72c25e730cbd54ec89ecbb63596 /WireGuard/Shared/Model/DNSServer.swift
parentTunnel edit: While preparing for reuse, should make onValueBeingEdited nil as well (diff)
downloadwireguard-apple-c88c660b516f4f8dd75944dc440b17a383c5154f.tar.xz
wireguard-apple-c88c660b516f4f8dd75944dc440b17a383c5154f.zip
Move logic to extension: Move model files to Shared
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard/Shared/Model/DNSServer.swift')
-rw-r--r--WireGuard/Shared/Model/DNSServer.swift57
1 files changed, 57 insertions, 0 deletions
diff --git a/WireGuard/Shared/Model/DNSServer.swift b/WireGuard/Shared/Model/DNSServer.swift
new file mode 100644
index 00000000..8be1dddd
--- /dev/null
+++ b/WireGuard/Shared/Model/DNSServer.swift
@@ -0,0 +1,57 @@
+// SPDX-License-Identifier: MIT
+// Copyright © 2018 WireGuard LLC. All Rights Reserved.
+
+import Foundation
+import Network
+
+@available(OSX 10.14, iOS 12.0, *)
+struct DNSServer {
+ let address: IPAddress
+}
+
+// MARK: Converting to and from String
+// For use in the UI
+
+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)"
+ }
+}
+
+// MARK: Codable
+// For serializing to disk
+
+@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)
+ }
+ 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 {
+ throw DecodingError.invalidData
+ }
+ address = ipAddress
+ }
+ enum DecodingError: Error {
+ case invalidData
+ }
+}