2018-09-09 23:26:04 +00:00
|
|
|
//
|
|
|
|
// ControlChannelSerializer.swift
|
|
|
|
// TunnelKit
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 9/10/18.
|
|
|
|
// Copyright (c) 2018 Davide De Rosa. All rights reserved.
|
|
|
|
//
|
|
|
|
// https://github.com/keeshux
|
|
|
|
//
|
|
|
|
// 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 Foundation
|
2018-09-10 09:12:58 +00:00
|
|
|
import __TunnelKitNative
|
2018-09-09 23:26:04 +00:00
|
|
|
import SwiftyBeaver
|
|
|
|
|
|
|
|
private let log = SwiftyBeaver.self
|
|
|
|
|
|
|
|
protocol ControlChannelSerializer {
|
|
|
|
func reset()
|
|
|
|
|
|
|
|
func serialize(packet: ControlPacket) throws -> Data
|
|
|
|
|
2018-09-18 16:04:43 +00:00
|
|
|
func deserialize(data: Data, start: Int, end: Int?) throws -> ControlPacket
|
2018-09-09 23:26:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extension ControlChannel {
|
|
|
|
class PlainSerializer: ControlChannelSerializer {
|
|
|
|
func reset() {
|
|
|
|
}
|
|
|
|
|
|
|
|
func serialize(packet: ControlPacket) throws -> Data {
|
2018-09-10 09:12:58 +00:00
|
|
|
return packet.serialized()
|
2018-09-09 23:26:04 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 16:04:43 +00:00
|
|
|
func deserialize(data packet: Data, start: Int, end: Int?) throws -> ControlPacket {
|
2018-09-10 09:12:58 +00:00
|
|
|
var offset = start
|
|
|
|
let end = end ?? packet.count
|
2018-09-18 16:04:43 +00:00
|
|
|
|
2018-09-18 17:12:47 +00:00
|
|
|
guard end >= offset + PacketOpcodeLength else {
|
|
|
|
throw ControlChannelError("Missing opcode")
|
2018-09-18 16:04:43 +00:00
|
|
|
}
|
|
|
|
let codeValue = packet[offset] >> 3
|
|
|
|
guard let code = PacketCode(rawValue: codeValue) else {
|
|
|
|
throw ControlChannelError("Unknown code: \(codeValue))")
|
|
|
|
}
|
|
|
|
let key = packet[offset] & 0b111
|
2018-09-18 17:12:47 +00:00
|
|
|
offset += PacketOpcodeLength
|
2018-09-10 09:12:58 +00:00
|
|
|
|
2018-09-18 16:04:43 +00:00
|
|
|
log.debug("Control: Try read packet with code \(code) and key \(key)")
|
|
|
|
|
2018-09-10 09:12:58 +00:00
|
|
|
guard end >= offset + PacketSessionIdLength else {
|
|
|
|
throw ControlChannelError("Missing sessionId")
|
|
|
|
}
|
|
|
|
let sessionId = packet.subdata(offset: offset, count: PacketSessionIdLength)
|
|
|
|
offset += PacketSessionIdLength
|
|
|
|
|
|
|
|
guard end >= offset + 1 else {
|
|
|
|
throw ControlChannelError("Missing ackSize")
|
|
|
|
}
|
|
|
|
let ackSize = packet[offset]
|
|
|
|
offset += 1
|
|
|
|
|
|
|
|
var ackIds: [UInt32]?
|
|
|
|
var ackRemoteSessionId: Data?
|
|
|
|
if ackSize > 0 {
|
|
|
|
guard end >= (offset + Int(ackSize) * PacketIdLength) else {
|
|
|
|
throw ControlChannelError("Missing acks")
|
|
|
|
}
|
|
|
|
var ids: [UInt32] = []
|
|
|
|
for _ in 0..<ackSize {
|
|
|
|
let id = packet.networkUInt32Value(from: offset)
|
|
|
|
ids.append(id)
|
|
|
|
offset += PacketIdLength
|
|
|
|
}
|
|
|
|
|
|
|
|
guard end >= offset + PacketSessionIdLength else {
|
|
|
|
throw ControlChannelError("Missing remoteSessionId")
|
|
|
|
}
|
|
|
|
let remoteSessionId = packet.subdata(offset: offset, count: PacketSessionIdLength)
|
|
|
|
offset += PacketSessionIdLength
|
|
|
|
|
|
|
|
ackIds = ids
|
|
|
|
ackRemoteSessionId = remoteSessionId
|
|
|
|
}
|
|
|
|
|
|
|
|
if code == .ackV1 {
|
|
|
|
guard let ackIds = ackIds else {
|
|
|
|
throw ControlChannelError("Ack packet without ids")
|
|
|
|
}
|
|
|
|
guard let ackRemoteSessionId = ackRemoteSessionId else {
|
|
|
|
throw ControlChannelError("Ack packet without remoteSessionId")
|
|
|
|
}
|
|
|
|
return ControlPacket(key: key, sessionId: sessionId, ackIds: ackIds as [NSNumber], ackRemoteSessionId: ackRemoteSessionId)
|
|
|
|
}
|
|
|
|
|
|
|
|
guard end >= offset + PacketIdLength else {
|
|
|
|
throw ControlChannelError("Missing packetId")
|
|
|
|
}
|
|
|
|
let packetId = packet.networkUInt32Value(from: offset)
|
|
|
|
offset += PacketIdLength
|
|
|
|
|
|
|
|
var payload: Data?
|
|
|
|
if offset < end {
|
|
|
|
payload = packet.subdata(in: offset..<end)
|
|
|
|
}
|
|
|
|
|
|
|
|
let controlPacket = ControlPacket(code: code, key: key, sessionId: sessionId, packetId: packetId, payload: payload)
|
|
|
|
if let ackIds = ackIds {
|
|
|
|
controlPacket.ackIds = ackIds as [NSNumber]
|
|
|
|
controlPacket.ackRemoteSessionId = ackRemoteSessionId
|
|
|
|
}
|
|
|
|
return controlPacket
|
2018-09-09 23:26:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|