Handle stub/stub-v2 as viable --compress arguments

This commit is contained in:
Davide De Rosa 2021-07-16 18:11:37 +02:00
parent b9d7473eae
commit 4dc3eeeeea
6 changed files with 70 additions and 6 deletions

View File

@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased ## Unreleased
### Added
- Support for `--compress stub-v2`.
### Fixed ### Fixed
- Return error in install completion handler. [#206](https://github.com/passepartoutvpn/tunnelkit/issues/206) - Return error in install completion handler. [#206](https://github.com/passepartoutvpn/tunnelkit/issues/206)

View File

@ -40,6 +40,9 @@ extension OpenVPN {
/// Framing compatible with 2.4 `compress`. /// Framing compatible with 2.4 `compress`.
case compress case compress
/// Framing compatible with 2.4 `compress` (version 2, e.g. stub-v2).
case compressV2
var native: CompressionFramingNative { var native: CompressionFramingNative {
guard let val = CompressionFramingNative(rawValue: rawValue) else { guard let val = CompressionFramingNative(rawValue: rawValue) else {
fatalError("Unhandled CompressionFraming bridging") fatalError("Unhandled CompressionFraming bridging")
@ -58,6 +61,9 @@ extension OpenVPN {
case .compress: case .compress:
return "compress" return "compress"
case .compressV2:
return "compress"
case .compLZO: case .compLZO:
return "comp-lzo" return "comp-lzo"
} }

View File

@ -28,5 +28,6 @@
typedef NS_ENUM(NSInteger, CompressionFramingNative) { typedef NS_ENUM(NSInteger, CompressionFramingNative) {
CompressionFramingNativeDisabled, CompressionFramingNativeDisabled,
CompressionFramingNativeCompLZO, CompressionFramingNativeCompLZO,
CompressionFramingNativeCompress CompressionFramingNativeCompress,
CompressionFramingNativeCompressV2
}; };

View File

@ -412,7 +412,20 @@ extension OpenVPN {
} }
} else { } else {
if let arg = $0.first { if let arg = $0.first {
optCompressionAlgorithm = (arg == "lzo") ? .LZO : .other switch arg {
case "lzo":
optCompressionAlgorithm = .LZO
case "stub":
optCompressionAlgorithm = .disabled
case "stub-v2":
optCompressionFraming = .compressV2
optCompressionAlgorithm = .disabled
default:
optCompressionAlgorithm = .other
}
} else { } else {
optCompressionAlgorithm = .disabled optCompressionAlgorithm = .disabled
} }

View File

@ -195,13 +195,31 @@
*payloadOffset = 1; *payloadOffset = 1;
break; break;
default: case DataPacketV2Indicator:
// @"Expected NO_COMPRESS (found %X != %X)", payload[0], DataPacketNoCompress); if (compressionFraming == CompressionFramingNativeCompressV2) {
if (payload[1] != DataPacketV2Uncompressed) {
if (error) { if (error) {
*error = TunnelKitErrorWithCode(TunnelKitErrorCodeDataPathCompression); *error = TunnelKitErrorWithCode(TunnelKitErrorCodeDataPathCompression);
} }
return NO; return NO;
} }
*payloadOffset = 2;
} else {
*payloadOffset = 0;
*headerLength = 0;
}
break;
default:
// @"Expected NO_COMPRESS (found %X != %X)", payload[0], DataPacketNoCompress);
// if (error) {
// *error = TunnelKitErrorWithCode(TunnelKitErrorCodeDataPathCompression);
// }
// return NO;
*payloadOffset = 0;
*headerLength = 0;
break;
}
return YES; return YES;
}; };
@ -244,6 +262,25 @@
self.parsePayloadBlock = parseCompressedBlock; self.parsePayloadBlock = parseCompressedBlock;
break; break;
} }
case CompressionFramingNativeCompressV2: {
self.assemblePayloadBlock = ^(uint8_t * packetDest, NSInteger * packetLengthOffset, NSData * payload) {
// assume no compression (v2 algorithms unsupported)
// prepend headers only in case of byte ambiguity
const uint8_t first = *(uint8_t *)payload.bytes;
if (first == DataPacketV2Indicator) {
*packetLengthOffset = 2;
packetDest[0] = DataPacketV2Indicator;
packetDest[1] = DataPacketV2Uncompressed;
} else {
*packetLengthOffset = 0;
}
memcpy(packetDest + *packetLengthOffset, payload.bytes, payload.length);
};
self.parsePayloadBlock = parseCompressedBlock;
break;
}
case CompressionFramingNativeCompLZO: { case CompressionFramingNativeCompLZO: {
self.assemblePayloadBlock = ^(uint8_t * packetDest, NSInteger * packetLengthOffset, NSData * payload) { self.assemblePayloadBlock = ^(uint8_t * packetDest, NSInteger * packetLengthOffset, NSData * payload) {
NSData *compressedPayload = [weakSelf.lzo compressedDataWithData:payload error:NULL]; NSData *compressedPayload = [weakSelf.lzo compressedDataWithData:payload error:NULL];

View File

@ -62,6 +62,9 @@ typedef NS_ENUM(uint8_t, PacketCode) {
#define DataPacketNoCompressSwap 0xfb #define DataPacketNoCompressSwap 0xfb
#define DataPacketLZOCompress 0x66 #define DataPacketLZOCompress 0x66
#define DataPacketV2Indicator 0x50
#define DataPacketV2Uncompressed 0x00
extern const uint8_t DataPacketPingData[16]; extern const uint8_t DataPacketPingData[16];
static inline void PacketOpcodeGet(const uint8_t *from, PacketCode *_Nullable code, uint8_t *_Nullable key) static inline void PacketOpcodeGet(const uint8_t *from, PacketCode *_Nullable code, uint8_t *_Nullable key)