From aa39414a7710477bddc5bf9e000419a347e933f9 Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Tue, 18 Sep 2018 19:12:47 +0200 Subject: [PATCH] Rename packet header to opcode (first byte) --- TunnelKit/Sources/Core/ControlChannelSerializer.swift | 6 +++--- TunnelKit/Sources/Core/ControlPacket.m | 2 +- TunnelKit/Sources/Core/PacketMacros.h | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/TunnelKit/Sources/Core/ControlChannelSerializer.swift b/TunnelKit/Sources/Core/ControlChannelSerializer.swift index 4f9a39e..de73743 100644 --- a/TunnelKit/Sources/Core/ControlChannelSerializer.swift +++ b/TunnelKit/Sources/Core/ControlChannelSerializer.swift @@ -50,15 +50,15 @@ extension ControlChannel { var offset = start let end = end ?? packet.count - guard end >= offset + PacketHeaderLength else { - throw ControlChannelError("Missing header") + guard end >= offset + PacketOpcodeLength else { + throw ControlChannelError("Missing opcode") } let codeValue = packet[offset] >> 3 guard let code = PacketCode(rawValue: codeValue) else { throw ControlChannelError("Unknown code: \(codeValue))") } let key = packet[offset] & 0b111 - offset += PacketHeaderLength + offset += PacketOpcodeLength log.debug("Control: Try read packet with code \(code) and key \(key)") diff --git a/TunnelKit/Sources/Core/ControlPacket.m b/TunnelKit/Sources/Core/ControlPacket.m index cb1a353..b26cab4 100644 --- a/TunnelKit/Sources/Core/ControlPacket.m +++ b/TunnelKit/Sources/Core/ControlPacket.m @@ -80,7 +80,7 @@ const BOOL isAck = self.isAck; const NSUInteger ackLength = self.ackIds.count; NSCAssert(!isAck || ackLength > 0, @"Ack packet must provide positive ackLength"); - NSInteger n = PacketHeaderLength + PacketSessionIdLength; + NSInteger n = PacketOpcodeLength + PacketSessionIdLength; n += PacketAckLengthLength; if (ackLength > 0) { n += ackLength * PacketIdLength + PacketSessionIdLength; diff --git a/TunnelKit/Sources/Core/PacketMacros.h b/TunnelKit/Sources/Core/PacketMacros.h index 3c740d9..019a170 100644 --- a/TunnelKit/Sources/Core/PacketMacros.h +++ b/TunnelKit/Sources/Core/PacketMacros.h @@ -39,7 +39,7 @@ NS_ASSUME_NONNULL_BEGIN -#define PacketHeaderLength ((NSInteger)1) +#define PacketOpcodeLength ((NSInteger)1) #define PacketIdLength ((NSInteger)4) #define PacketSessionIdLength ((NSInteger)8) #define PacketAckLengthLength ((NSInteger)1) @@ -66,7 +66,7 @@ extern const uint8_t DataPacketPingData[16]; static inline int PacketHeaderSet(uint8_t *to, PacketCode code, uint8_t key, const uint8_t *_Nullable sessionId) { *(uint8_t *)to = (code << 3) | (key & 0b111); - int offset = PacketHeaderLength; + int offset = PacketOpcodeLength; if (sessionId) { memcpy(to + offset, sessionId, PacketSessionIdLength); offset += PacketSessionIdLength; @@ -77,7 +77,7 @@ static inline int PacketHeaderSet(uint8_t *to, PacketCode code, uint8_t key, con static inline int PacketHeaderSetDataV2(uint8_t *to, uint8_t key, uint32_t peerId) { *(uint32_t *)to = ((PacketCodeDataV2 << 3) | (key & 0b111)) | htonl(peerId & 0xffffff); - return PacketHeaderLength + PacketPeerIdLength; + return PacketOpcodeLength + PacketPeerIdLength; } static inline int PacketHeaderGetDataV2PeerId(const uint8_t *from)