2019-04-25 09:15:25 +00:00
|
|
|
//
|
|
|
|
// PacketStream.m
|
|
|
|
// TunnelKit
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 4/25/19.
|
2023-03-17 15:58:36 +00:00
|
|
|
// Copyright (c) 2023 Davide De Rosa. All rights reserved.
|
2019-04-25 09:15:25 +00:00
|
|
|
//
|
2019-05-14 08:58:47 +00:00
|
|
|
// https://github.com/passepartoutvpn
|
2019-04-25 09:15:25 +00:00
|
|
|
//
|
|
|
|
// This file is part of TunnelKit.
|
|
|
|
//
|
|
|
|
// TunnelKit is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// TunnelKit is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with TunnelKit. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "PacketStream.h"
|
2022-11-06 16:46:10 +00:00
|
|
|
#import "XOR.h"
|
2019-04-25 09:15:25 +00:00
|
|
|
|
|
|
|
static const NSInteger PacketStreamHeaderLength = sizeof(uint16_t);
|
|
|
|
|
|
|
|
@implementation PacketStream
|
|
|
|
|
2022-11-06 16:46:10 +00:00
|
|
|
+ (NSArray<NSData *> *)packetsFromInboundStream:(NSData *)stream
|
|
|
|
until:(NSInteger *)until
|
|
|
|
xorMethod:(XORMethodNative)xorMethod
|
|
|
|
xorMask:(NSData *)xorMask
|
2019-04-25 09:15:25 +00:00
|
|
|
{
|
|
|
|
NSInteger ni = 0;
|
|
|
|
NSMutableArray<NSData *> *parsed = [[NSMutableArray alloc] init];
|
|
|
|
|
|
|
|
while (ni + PacketStreamHeaderLength <= stream.length) {
|
|
|
|
const NSInteger packlen = CFSwapInt16BigToHost(*(uint16_t *)(stream.bytes + ni));
|
|
|
|
const NSInteger start = ni + PacketStreamHeaderLength;
|
|
|
|
const NSInteger end = start + packlen;
|
|
|
|
if (end > stream.length) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
NSData *packet = [stream subdataWithRange:NSMakeRange(start, packlen)];
|
2019-09-03 20:11:53 +00:00
|
|
|
uint8_t* packetBytes = (uint8_t*) packet.bytes;
|
2022-11-06 16:46:10 +00:00
|
|
|
xor_memcpy(packetBytes, packet, xorMethod, xorMask, false);
|
2019-04-25 09:15:25 +00:00
|
|
|
[parsed addObject:packet];
|
|
|
|
ni = end;
|
|
|
|
}
|
|
|
|
if (until) {
|
|
|
|
*until = ni;
|
|
|
|
}
|
|
|
|
return parsed;
|
|
|
|
}
|
|
|
|
|
2022-11-06 16:46:10 +00:00
|
|
|
+ (NSData *)outboundStreamFromPacket:(NSData *)packet
|
|
|
|
xorMethod:(XORMethodNative)xorMethod
|
|
|
|
xorMask:(NSData *)xorMask
|
2019-04-25 09:15:25 +00:00
|
|
|
{
|
|
|
|
NSMutableData *raw = [[NSMutableData alloc] initWithLength:(PacketStreamHeaderLength + packet.length)];
|
|
|
|
|
|
|
|
uint8_t *ptr = raw.mutableBytes;
|
|
|
|
*(uint16_t *)ptr = CFSwapInt16HostToBig(packet.length);
|
|
|
|
ptr += PacketStreamHeaderLength;
|
2022-11-06 16:46:10 +00:00
|
|
|
xor_memcpy(ptr, packet, xorMethod, xorMask, true);
|
2019-04-25 09:15:25 +00:00
|
|
|
|
|
|
|
return raw;
|
|
|
|
}
|
|
|
|
|
2022-11-06 16:46:10 +00:00
|
|
|
+ (NSData *)outboundStreamFromPackets:(NSArray<NSData *> *)packets
|
|
|
|
xorMethod:(XORMethodNative)xorMethod
|
|
|
|
xorMask:(NSData *)xorMask
|
2019-04-25 09:15:25 +00:00
|
|
|
{
|
|
|
|
NSInteger streamLength = 0;
|
|
|
|
for (NSData *p in packets) {
|
|
|
|
streamLength += PacketStreamHeaderLength + p.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSMutableData *raw = [[NSMutableData alloc] initWithLength:streamLength];
|
|
|
|
uint8_t *ptr = raw.mutableBytes;
|
|
|
|
for (NSData *packet in packets) {
|
|
|
|
*(uint16_t *)ptr = CFSwapInt16HostToBig(packet.length);
|
|
|
|
ptr += PacketStreamHeaderLength;
|
2022-11-06 16:46:10 +00:00
|
|
|
xor_memcpy(ptr, packet, xorMethod, xorMask, true);
|
2019-04-25 09:15:25 +00:00
|
|
|
ptr += packet.length;
|
|
|
|
}
|
|
|
|
return raw;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|