From 0ce6b9e79eb92704e19f6cc5dbc3a4a66ac79483 Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Sat, 13 Oct 2018 18:59:42 +0530 Subject: [PATCH] Model for an IP Address range Signed-off-by: Roopesh Chander --- WireGuard/WireGuard.xcodeproj/project.pbxproj | 4 ++ .../WireGuard/Model/IPAddressRange.swift | 54 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 WireGuard/WireGuard/Model/IPAddressRange.swift diff --git a/WireGuard/WireGuard.xcodeproj/project.pbxproj b/WireGuard/WireGuard.xcodeproj/project.pbxproj index b7eb3b0..f0c2be4 100644 --- a/WireGuard/WireGuard.xcodeproj/project.pbxproj +++ b/WireGuard/WireGuard.xcodeproj/project.pbxproj @@ -11,6 +11,7 @@ 6F7774E2217181B1006A79B3 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F7774E0217181B1006A79B3 /* AppDelegate.swift */; }; 6F7774E421718281006A79B3 /* TunnelsListTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F7774E321718281006A79B3 /* TunnelsListTableViewController.swift */; }; 6F7774E82172020C006A79B3 /* Configuration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F7774E72172020C006A79B3 /* Configuration.swift */; }; + 6F7774EA217229DB006A79B3 /* IPAddressRange.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F7774E9217229DB006A79B3 /* IPAddressRange.swift */; }; 6FF4AC1F211EC472002C96EB /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 6FF4AC1E211EC472002C96EB /* Assets.xcassets */; }; 6FF4AC22211EC472002C96EB /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 6FF4AC20211EC472002C96EB /* LaunchScreen.storyboard */; }; 6FF4AC472120B9E0002C96EB /* NetworkExtension.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6FF4AC462120B9E0002C96EB /* NetworkExtension.framework */; }; @@ -21,6 +22,7 @@ 6F7774E0217181B1006A79B3 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 6F7774E321718281006A79B3 /* TunnelsListTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TunnelsListTableViewController.swift; sourceTree = ""; }; 6F7774E72172020C006A79B3 /* Configuration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Configuration.swift; sourceTree = ""; }; + 6F7774E9217229DB006A79B3 /* IPAddressRange.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IPAddressRange.swift; sourceTree = ""; }; 6FF4AC14211EC46F002C96EB /* WireGuard.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = WireGuard.app; sourceTree = BUILT_PRODUCTS_DIR; }; 6FF4AC1E211EC472002C96EB /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 6FF4AC21211EC472002C96EB /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; @@ -64,6 +66,7 @@ isa = PBXGroup; children = ( 6F7774E72172020C006A79B3 /* Configuration.swift */, + 6F7774E9217229DB006A79B3 /* IPAddressRange.swift */, ); path = Model; sourceTree = ""; @@ -184,6 +187,7 @@ files = ( 6F7774E421718281006A79B3 /* TunnelsListTableViewController.swift in Sources */, 6F7774E2217181B1006A79B3 /* AppDelegate.swift in Sources */, + 6F7774EA217229DB006A79B3 /* IPAddressRange.swift in Sources */, 6F7774E82172020C006A79B3 /* Configuration.swift in Sources */, 6F7774E1217181B1006A79B3 /* MainViewController.swift in Sources */, ); diff --git a/WireGuard/WireGuard/Model/IPAddressRange.swift b/WireGuard/WireGuard/Model/IPAddressRange.swift new file mode 100644 index 0000000..4498b58 --- /dev/null +++ b/WireGuard/WireGuard/Model/IPAddressRange.swift @@ -0,0 +1,54 @@ +// +// IPAddressRange.swift +// WireGuard +// +// Created by Roopesh Chander on 13/10/18. +// Copyright © 2018 WireGuard LLC. All rights reserved. +// + +import Foundation +import Network + +@available(OSX 10.14, iOS 12.0, *) +struct IPAddressRange { + let address: IPAddress + var networkPrefixLength: UInt8 +} + +@available(OSX 10.14, iOS 12.0, *) +extension IPAddressRange: Codable { + public func encode(to encoder: Encoder) throws { + var container = encoder.singleValueContainer() + let addressDataLength: Int + if address is IPv4Address { + addressDataLength = 4 + } else if address is IPv6Address { + addressDataLength = 16 + } else { + fatalError() + } + var data = Data(capacity: addressDataLength + 1) + data.append(address.rawValue) + data.append(networkPrefixLength) + try container.encode(data) + } + public init(from decoder: Decoder) throws { + let container = try decoder.singleValueContainer() + var data = try container.decode(Data.self) + networkPrefixLength = data.removeLast() + 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 + } +}