Make peerId stateless, imply from blocks
Will do the same with compressionFraming.
This commit is contained in:
parent
e57ef9fb86
commit
31924c6038
|
@ -51,7 +51,6 @@ NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
@interface DataPathCryptoAEAD : NSObject <DataPathEncrypter, DataPathDecrypter>
|
@interface DataPathCryptoAEAD : NSObject <DataPathEncrypter, DataPathDecrypter>
|
||||||
|
|
||||||
@property (nonatomic, assign) uint32_t peerId;
|
|
||||||
@property (nonatomic, assign) CompressionFraming compressionFraming;
|
@property (nonatomic, assign) CompressionFraming compressionFraming;
|
||||||
|
|
||||||
- (instancetype)initWithCrypto:(nonnull CryptoAEAD *)crypto;
|
- (instancetype)initWithCrypto:(nonnull CryptoAEAD *)crypto;
|
||||||
|
|
|
@ -264,15 +264,16 @@ const NSInteger CryptoAEADTagLength = 16;
|
||||||
|
|
||||||
- (void)setPeerId:(uint32_t)peerId
|
- (void)setPeerId:(uint32_t)peerId
|
||||||
{
|
{
|
||||||
_peerId = peerId & 0xffffff;
|
peerId &= 0xffffff;
|
||||||
|
|
||||||
if (_peerId == PacketPeerIdDisabled) {
|
if (peerId == PacketPeerIdDisabled) {
|
||||||
self.headerLength = 1;
|
self.headerLength = 1;
|
||||||
self.crypto.extraLength = PacketIdLength;
|
self.crypto.extraLength = PacketIdLength;
|
||||||
self.crypto.extraPacketIdOffset = 0;
|
self.crypto.extraPacketIdOffset = 0;
|
||||||
self.setDataHeader = ^(uint8_t *to, uint8_t key) {
|
self.setDataHeader = ^(uint8_t *to, uint8_t key) {
|
||||||
PacketHeaderSet(to, PacketCodeDataV1, key);
|
PacketHeaderSet(to, PacketCodeDataV1, key);
|
||||||
};
|
};
|
||||||
|
self.checkPeerId = NULL;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
self.headerLength = 4;
|
self.headerLength = 4;
|
||||||
|
@ -282,7 +283,7 @@ const NSInteger CryptoAEADTagLength = 16;
|
||||||
PacketHeaderSetDataV2(to, key, peerId);
|
PacketHeaderSetDataV2(to, key, peerId);
|
||||||
};
|
};
|
||||||
self.checkPeerId = ^BOOL(const uint8_t *ptr) {
|
self.checkPeerId = ^BOOL(const uint8_t *ptr) {
|
||||||
return (PacketHeaderGetDataV2PeerId(ptr) == self.peerId);
|
return (PacketHeaderGetDataV2PeerId(ptr) == peerId);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -328,7 +329,7 @@ const NSInteger CryptoAEADTagLength = 16;
|
||||||
*(uint32_t *)(ptr + self.headerLength) = htonl(packetId);
|
*(uint32_t *)(ptr + self.headerLength) = htonl(packetId);
|
||||||
|
|
||||||
const uint8_t *extra = ptr; // AD = header + peer id + packet id
|
const uint8_t *extra = ptr; // AD = header + peer id + packet id
|
||||||
if (self.peerId == PacketPeerIdDisabled) {
|
if (!self.checkPeerId) {
|
||||||
extra += self.headerLength; // AD = packet id only
|
extra += self.headerLength; // AD = packet id only
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -354,7 +355,7 @@ const NSInteger CryptoAEADTagLength = 16;
|
||||||
- (BOOL)decryptDataPacket:(NSData *)packet into:(uint8_t *)dest length:(NSInteger *)length packetId:(uint32_t *)packetId error:(NSError *__autoreleasing *)error
|
- (BOOL)decryptDataPacket:(NSData *)packet into:(uint8_t *)dest length:(NSInteger *)length packetId:(uint32_t *)packetId error:(NSError *__autoreleasing *)error
|
||||||
{
|
{
|
||||||
const uint8_t *extra = packet.bytes; // AD = header + peer id + packet id
|
const uint8_t *extra = packet.bytes; // AD = header + peer id + packet id
|
||||||
if (self.peerId == PacketPeerIdDisabled) {
|
if (!self.checkPeerId) {
|
||||||
extra += self.headerLength; // AD = packet id only
|
extra += self.headerLength; // AD = packet id only
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -50,7 +50,6 @@ NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
@interface DataPathCryptoCBC : NSObject <DataPathEncrypter, DataPathDecrypter>
|
@interface DataPathCryptoCBC : NSObject <DataPathEncrypter, DataPathDecrypter>
|
||||||
|
|
||||||
@property (nonatomic, assign) uint32_t peerId;
|
|
||||||
@property (nonatomic, assign) CompressionFraming compressionFraming;
|
@property (nonatomic, assign) CompressionFraming compressionFraming;
|
||||||
|
|
||||||
- (instancetype)initWithCrypto:(nonnull CryptoCBC *)crypto;
|
- (instancetype)initWithCrypto:(nonnull CryptoCBC *)crypto;
|
||||||
|
|
|
@ -266,13 +266,14 @@ const NSInteger CryptoCBCMaxHMACLength = 100;
|
||||||
|
|
||||||
- (void)setPeerId:(uint32_t)peerId
|
- (void)setPeerId:(uint32_t)peerId
|
||||||
{
|
{
|
||||||
_peerId = peerId & 0xffffff;
|
peerId &= 0xffffff;
|
||||||
|
|
||||||
if (_peerId == PacketPeerIdDisabled) {
|
if (peerId == PacketPeerIdDisabled) {
|
||||||
self.headerLength = 1;
|
self.headerLength = 1;
|
||||||
self.setDataHeader = ^(uint8_t *to, uint8_t key) {
|
self.setDataHeader = ^(uint8_t *to, uint8_t key) {
|
||||||
PacketHeaderSet(to, PacketCodeDataV1, key);
|
PacketHeaderSet(to, PacketCodeDataV1, key);
|
||||||
};
|
};
|
||||||
|
self.checkPeerId = NULL;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
self.headerLength = 4;
|
self.headerLength = 4;
|
||||||
|
@ -280,7 +281,7 @@ const NSInteger CryptoCBCMaxHMACLength = 100;
|
||||||
PacketHeaderSetDataV2(to, key, peerId);
|
PacketHeaderSetDataV2(to, key, peerId);
|
||||||
};
|
};
|
||||||
self.checkPeerId = ^BOOL(const uint8_t *ptr) {
|
self.checkPeerId = ^BOOL(const uint8_t *ptr) {
|
||||||
return (PacketHeaderGetDataV2PeerId(ptr) == self.peerId);
|
return (PacketHeaderGetDataV2PeerId(ptr) == peerId);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -156,8 +156,8 @@
|
||||||
NSAssert(self.encrypter, @"Setting peer-id to nil encrypter");
|
NSAssert(self.encrypter, @"Setting peer-id to nil encrypter");
|
||||||
NSAssert(self.decrypter, @"Setting peer-id to nil decrypter");
|
NSAssert(self.decrypter, @"Setting peer-id to nil decrypter");
|
||||||
|
|
||||||
self.encrypter.peerId = peerId;
|
[self.encrypter setPeerId:peerId];
|
||||||
self.decrypter.peerId = peerId;
|
[self.decrypter setPeerId:peerId];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)setCompressionFraming:(CompressionFraming)compressionFraming
|
- (void)setCompressionFraming:(CompressionFraming)compressionFraming
|
||||||
|
@ -165,15 +165,15 @@
|
||||||
NSAssert(self.encrypter, @"Setting compressionFraming to nil encrypter");
|
NSAssert(self.encrypter, @"Setting compressionFraming to nil encrypter");
|
||||||
NSAssert(self.decrypter, @"Setting compressionFraming to nil decrypter");
|
NSAssert(self.decrypter, @"Setting compressionFraming to nil decrypter");
|
||||||
|
|
||||||
self.encrypter.compressionFraming = compressionFraming;
|
[self.encrypter setCompressionFraming:compressionFraming];
|
||||||
self.decrypter.compressionFraming = compressionFraming;
|
[self.decrypter setCompressionFraming:compressionFraming];
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark DataPath
|
#pragma mark DataPath
|
||||||
|
|
||||||
- (NSArray<NSData *> *)encryptPackets:(NSArray<NSData *> *)packets key:(uint8_t)key error:(NSError *__autoreleasing *)error
|
- (NSArray<NSData *> *)encryptPackets:(NSArray<NSData *> *)packets key:(uint8_t)key error:(NSError *__autoreleasing *)error
|
||||||
{
|
{
|
||||||
NSAssert(self.encrypter.peerId == self.decrypter.peerId, @"Peer-id mismatch in DataPath encrypter/decrypter");
|
// NSAssert(self.encrypter.peerId == self.decrypter.peerId, @"Peer-id mismatch in DataPath encrypter/decrypter");
|
||||||
|
|
||||||
if (self.outPacketId > self.maxPacketId) {
|
if (self.outPacketId > self.maxPacketId) {
|
||||||
if (error) {
|
if (error) {
|
||||||
|
@ -213,10 +213,9 @@
|
||||||
return self.outPackets;
|
return self.outPackets;
|
||||||
}
|
}
|
||||||
|
|
||||||
//- (NSArray<NSData *> *)decryptPackets:(NSArray<NSData *> *)packets error:(NSError *__autoreleasing *)error
|
|
||||||
- (NSArray<NSData *> *)decryptPackets:(NSArray<NSData *> *)packets keepAlive:(bool *)keepAlive error:(NSError *__autoreleasing *)error
|
- (NSArray<NSData *> *)decryptPackets:(NSArray<NSData *> *)packets keepAlive:(bool *)keepAlive error:(NSError *__autoreleasing *)error
|
||||||
{
|
{
|
||||||
NSAssert(self.encrypter.peerId == self.decrypter.peerId, @"Peer-id mismatch in DataPath encrypter/decrypter");
|
// NSAssert(self.encrypter.peerId == self.decrypter.peerId, @"Peer-id mismatch in DataPath encrypter/decrypter");
|
||||||
|
|
||||||
[self.inPackets removeAllObjects];
|
[self.inPackets removeAllObjects];
|
||||||
|
|
||||||
|
|
|
@ -39,7 +39,6 @@
|
||||||
@protocol DataPathChannel
|
@protocol DataPathChannel
|
||||||
|
|
||||||
- (int)overheadLength;
|
- (int)overheadLength;
|
||||||
- (uint32_t)peerId;
|
|
||||||
- (void)setPeerId:(uint32_t)peerId;
|
- (void)setPeerId:(uint32_t)peerId;
|
||||||
- (CompressionFraming)compressionFraming;
|
- (CompressionFraming)compressionFraming;
|
||||||
- (void)setCompressionFraming:(CompressionFraming)compressionFraming;
|
- (void)setCompressionFraming:(CompressionFraming)compressionFraming;
|
||||||
|
|
Loading…
Reference in New Issue