From b050110e60efe55615865430d61c1604393c9c9d Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Fri, 31 Aug 2018 01:39:39 +0200 Subject: [PATCH] Bridge CompressionFraming to Swift Avoid exposing __TunnelKitNative module for using it. --- TunnelKit.xcodeproj/project.pbxproj | 8 +-- .../TunnelKitProvider+Configuration.swift | 7 ++- .../Sources/Core/CompressionFraming.swift | 49 ++++++++++++++----- ...onFraming.h => CompressionFramingNative.h} | 10 ++-- TunnelKit/Sources/Core/DataPath.h | 2 +- TunnelKit/Sources/Core/DataPath.m | 10 ++-- .../Core/SessionProxy+Configuration.swift | 1 - .../Core/SessionProxy+SessionKey.swift | 2 +- TunnelKit/Sources/Core/module.modulemap | 2 +- 9 files changed, 56 insertions(+), 35 deletions(-) rename TunnelKit/Sources/Core/{CompressionFraming.h => CompressionFramingNative.h} (79%) diff --git a/TunnelKit.xcodeproj/project.pbxproj b/TunnelKit.xcodeproj/project.pbxproj index 759c248..1b56fb6 100644 --- a/TunnelKit.xcodeproj/project.pbxproj +++ b/TunnelKit.xcodeproj/project.pbxproj @@ -30,7 +30,7 @@ 0E1108B31F77B9F900A92462 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0E1108B21F77B9F900A92462 /* Assets.xcassets */; }; 0E1108B61F77B9F900A92462 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 0E1108B41F77B9F900A92462 /* LaunchScreen.storyboard */; }; 0E245D692135972800B012A2 /* PushTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E245D682135972800B012A2 /* PushTests.swift */; }; - 0E245D6C2137F73600B012A2 /* CompressionFraming.h in Headers */ = {isa = PBXBuildFile; fileRef = 0E245D6B2137F73600B012A2 /* CompressionFraming.h */; }; + 0E245D6C2137F73600B012A2 /* CompressionFramingNative.h in Headers */ = {isa = PBXBuildFile; fileRef = 0E245D6B2137F73600B012A2 /* CompressionFramingNative.h */; }; 0E3E0F212108A8CC00B371C1 /* SessionProxy+PushReply.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E3E0F202108A8CC00B371C1 /* SessionProxy+PushReply.swift */; }; 0E3E0F222108A8CC00B371C1 /* SessionProxy+PushReply.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E3E0F202108A8CC00B371C1 /* SessionProxy+PushReply.swift */; }; 0E58F1302138AC2F00A49F27 /* DNSTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E58F12F2138AC2F00A49F27 /* DNSTests.swift */; }; @@ -190,7 +190,7 @@ 0E1108B71F77B9F900A92462 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 0E17D7F91F730D9F009EE129 /* TunnelKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = TunnelKit.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 0E245D682135972800B012A2 /* PushTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PushTests.swift; sourceTree = ""; }; - 0E245D6B2137F73600B012A2 /* CompressionFraming.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CompressionFraming.h; sourceTree = ""; }; + 0E245D6B2137F73600B012A2 /* CompressionFramingNative.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CompressionFramingNative.h; sourceTree = ""; }; 0E3251C51F95770D00C108D9 /* TunnelKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = TunnelKit.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 0E3E0F202108A8CC00B371C1 /* SessionProxy+PushReply.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SessionProxy+PushReply.swift"; sourceTree = ""; }; 0E58F12F2138AC2F00A49F27 /* DNSTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DNSTests.swift; sourceTree = ""; }; @@ -414,8 +414,8 @@ children = ( 0EFEB42E2006D3C800F81029 /* Allocation.h */, 0EFEB4462006D3C800F81029 /* Allocation.m */, - 0E245D6B2137F73600B012A2 /* CompressionFraming.h */, 0ED9C8632138139000621BA3 /* CompressionFraming.swift */, + 0E245D6B2137F73600B012A2 /* CompressionFramingNative.h */, 0EFEB44A2006D3C800F81029 /* CoreConfiguration.swift */, 0E07597C20F0060E00F38FD8 /* CryptoAEAD.h */, 0E07597D20F0060E00F38FD8 /* CryptoAEAD.m */, @@ -518,7 +518,7 @@ 0EFEB46B2006D3C800F81029 /* CryptoBox.h in Headers */, 0EFEB4592006D3C800F81029 /* Allocation.h in Headers */, 0EFEB4582006D3C800F81029 /* MSS.h in Headers */, - 0E245D6C2137F73600B012A2 /* CompressionFraming.h in Headers */, + 0E245D6C2137F73600B012A2 /* CompressionFramingNative.h in Headers */, 0EFEB4602006D3C800F81029 /* DataPath.h in Headers */, 0E07597E20F0060E00F38FD8 /* CryptoAEAD.h in Headers */, 0EFEB46C2006D3C800F81029 /* ZeroingData.h in Headers */, diff --git a/TunnelKit/Sources/AppExtension/TunnelKitProvider+Configuration.swift b/TunnelKit/Sources/AppExtension/TunnelKitProvider+Configuration.swift index 98656eb..d9d9dd5 100644 --- a/TunnelKit/Sources/AppExtension/TunnelKitProvider+Configuration.swift +++ b/TunnelKit/Sources/AppExtension/TunnelKitProvider+Configuration.swift @@ -38,7 +38,6 @@ import Foundation import NetworkExtension import SwiftyBeaver -import __TunnelKitNative private let log = SwiftyBeaver.self @@ -165,7 +164,7 @@ extension TunnelKitProvider { public var mtu: Int /// Sets compression framing, disabled by default. - public var compressionFraming: CompressionFraming + public var compressionFraming: SessionProxy.CompressionFraming /// The number of seconds after which a renegotiation is started. Set to `nil` to disable renegotiation (default). public var renegotiatesAfterSeconds: Int? @@ -265,7 +264,7 @@ extension TunnelKitProvider { self.clientCertificate = clientCertificate self.clientKey = clientKey mtu = providerConfiguration[S.mtu] as? Int ?? 1250 - if let compressionFramingValue = providerConfiguration[S.compressionFraming] as? Int, let compressionFraming = CompressionFraming(rawValue: compressionFramingValue) { + if let compressionFramingValue = providerConfiguration[S.compressionFraming] as? Int, let compressionFraming = SessionProxy.CompressionFraming(rawValue: compressionFramingValue) { self.compressionFraming = compressionFraming } else { compressionFraming = .disabled @@ -379,7 +378,7 @@ extension TunnelKitProvider { public let mtu: Int /// - Seealso: `TunnelKitProvider.ConfigurationBuilder.compressionFraming` - public let compressionFraming: CompressionFraming + public let compressionFraming: SessionProxy.CompressionFraming /// - Seealso: `TunnelKitProvider.ConfigurationBuilder.renegotiatesAfterSeconds` public let renegotiatesAfterSeconds: Int? diff --git a/TunnelKit/Sources/Core/CompressionFraming.swift b/TunnelKit/Sources/Core/CompressionFraming.swift index c927614..4fbb583 100644 --- a/TunnelKit/Sources/Core/CompressionFraming.swift +++ b/TunnelKit/Sources/Core/CompressionFraming.swift @@ -26,19 +26,42 @@ import Foundation import __TunnelKitNative -extension CompressionFraming: CustomStringConvertible { - - /// :nodoc: - public var description: String { - switch self { - case .disabled: - return "disabled" - - case .compress: - return "compress" - - case .compLZO: - return "comp-lzo" +extension SessionProxy { + + /// Defines the type of compression framing. + public enum CompressionFraming: Int, CustomStringConvertible { + + /// No compression framing. + case disabled + + /// Framing compatible with 2.4 `compress`. + case compress + + /// Framing compatible with `comp-lzo` (deprecated). + @available(*, deprecated) + case compLZO + + var native: CompressionFramingNative { + guard let val = CompressionFramingNative(rawValue: rawValue) else { + fatalError("Unhandled CompressionFraming bridging") + } + return val + } + + // MARK: CustomStringConvertible + + /// :nodoc: + public var description: String { + switch self { + case .disabled: + return "disabled" + + case .compress: + return "compress" + + case .compLZO: + return "comp-lzo" + } } } } diff --git a/TunnelKit/Sources/Core/CompressionFraming.h b/TunnelKit/Sources/Core/CompressionFramingNative.h similarity index 79% rename from TunnelKit/Sources/Core/CompressionFraming.h rename to TunnelKit/Sources/Core/CompressionFramingNative.h index cc7c5df..1792e6d 100644 --- a/TunnelKit/Sources/Core/CompressionFraming.h +++ b/TunnelKit/Sources/Core/CompressionFramingNative.h @@ -1,5 +1,5 @@ // -// CompressionFraming.h +// CompressionFramingNative.h // TunnelKit // // Created by Davide De Rosa on 8/30/18. @@ -25,8 +25,8 @@ #import -typedef NS_ENUM(NSInteger, CompressionFraming) { - CompressionFramingDisabled, - CompressionFramingCompress, - CompressionFramingCompLZO __attribute__((deprecated)) +typedef NS_ENUM(NSInteger, CompressionFramingNative) { + CompressionFramingNativeDisabled, + CompressionFramingNativeCompress, + CompressionFramingNativeCompLZO __attribute__((deprecated)) }; diff --git a/TunnelKit/Sources/Core/DataPath.h b/TunnelKit/Sources/Core/DataPath.h index 640f4da..9a7cf9a 100644 --- a/TunnelKit/Sources/Core/DataPath.h +++ b/TunnelKit/Sources/Core/DataPath.h @@ -51,7 +51,7 @@ usesReplayProtection:(BOOL)usesReplayProtection; - (void)setPeerId:(uint32_t)peerId; // 24-bit, discard most significant byte -- (void)setCompressionFraming:(CompressionFraming)compressionFraming; +- (void)setCompressionFraming:(CompressionFramingNative)compressionFraming; - (NSArray *)encryptPackets:(nonnull NSArray *)packets key:(uint8_t)key error:(NSError **)error; - (NSArray *)decryptPackets:(nonnull NSArray *)packets keepAlive:(nullable bool *)keepAlive error:(NSError **)error; diff --git a/TunnelKit/Sources/Core/DataPath.m b/TunnelKit/Sources/Core/DataPath.m index 3d913e9..656ea1d 100644 --- a/TunnelKit/Sources/Core/DataPath.m +++ b/TunnelKit/Sources/Core/DataPath.m @@ -109,7 +109,7 @@ self.inReplay = [[ReplayProtector alloc] init]; } - self.compressionFraming = CompressionFramingDisabled; + self.compressionFraming = CompressionFramingNativeDisabled; } return self; } @@ -165,10 +165,10 @@ [self.decrypter setPeerId:peerId]; } -- (void)setCompressionFraming:(CompressionFraming)compressionFraming +- (void)setCompressionFraming:(CompressionFramingNative)compressionFraming { switch (compressionFraming) { - case CompressionFramingDisabled: { + case CompressionFramingNativeDisabled: { self.assemblePayloadBlock = ^(uint8_t * _Nonnull packetDest, NSInteger * _Nonnull packetLengthOffset, NSData * _Nonnull payload) { memcpy(packetDest, payload.bytes, payload.length); *packetLengthOffset = 0; @@ -179,7 +179,7 @@ }; break; } - case CompressionFramingCompress: { + case CompressionFramingNativeCompress: { self.assemblePayloadBlock = ^(uint8_t * _Nonnull packetDest, NSInteger * _Nonnull packetLengthOffset, NSData * _Nonnull payload) { memcpy(packetDest, payload.bytes, payload.length); packetDest[payload.length] = packetDest[0]; @@ -194,7 +194,7 @@ }; break; } - case CompressionFramingCompLZO: { + case CompressionFramingNativeCompLZO: { self.assemblePayloadBlock = ^(uint8_t * _Nonnull packetDest, NSInteger * _Nonnull packetLengthOffset, NSData * _Nonnull payload) { memcpy(packetDest + 1, payload.bytes, payload.length); packetDest[0] = DataPacketNoCompress; diff --git a/TunnelKit/Sources/Core/SessionProxy+Configuration.swift b/TunnelKit/Sources/Core/SessionProxy+Configuration.swift index ba50439..0a0d0bf 100644 --- a/TunnelKit/Sources/Core/SessionProxy+Configuration.swift +++ b/TunnelKit/Sources/Core/SessionProxy+Configuration.swift @@ -36,7 +36,6 @@ // import Foundation -import __TunnelKitNative extension SessionProxy { diff --git a/TunnelKit/Sources/Core/SessionProxy+SessionKey.swift b/TunnelKit/Sources/Core/SessionProxy+SessionKey.swift index 1921e56..e6720bc 100644 --- a/TunnelKit/Sources/Core/SessionProxy+SessionKey.swift +++ b/TunnelKit/Sources/Core/SessionProxy+SessionKey.swift @@ -111,7 +111,7 @@ extension SessionProxy { func startHandlingPackets(withPeerId peerId: UInt32? = nil, compressionFraming: CompressionFraming = .disabled) { dataPath?.setPeerId(peerId ?? PacketPeerIdDisabled) - dataPath?.setCompressionFraming(compressionFraming) + dataPath?.setCompressionFraming(compressionFraming.native) canHandlePackets = true } diff --git a/TunnelKit/Sources/Core/module.modulemap b/TunnelKit/Sources/Core/module.modulemap index 266d81d..61b8a90 100644 --- a/TunnelKit/Sources/Core/module.modulemap +++ b/TunnelKit/Sources/Core/module.modulemap @@ -43,7 +43,7 @@ module __TunnelKitNative { header "MSS.h" header "PacketMacros.h" header "ReplayProtector.h" - header "CompressionFraming.h" + header "CompressionFramingNative.h" header "DataPath.h" header "DataPathEncryption.h" export *