Adding a flag to the packet decoder whatever we're on the server or client side

This commit is contained in:
WolverinDEV 2021-02-07 18:04:56 +01:00
parent 4367736c77
commit 2aed7708ee
3 changed files with 14 additions and 7 deletions

View File

@ -17,8 +17,8 @@ using namespace ts;
using namespace ts::protocol; using namespace ts::protocol;
using namespace ts::connection; using namespace ts::connection;
PacketDecoder::PacketDecoder(ts::connection::CryptHandler *crypt_handler) PacketDecoder::PacketDecoder(ts::connection::CryptHandler *crypt_handler, bool is_server)
: crypt_handler_{crypt_handler} { : is_server{is_server}, crypt_handler_{crypt_handler} {
memtrack::allocated<PacketDecoder>(this); memtrack::allocated<PacketDecoder>(this);
} }
@ -163,7 +163,7 @@ PacketProcessResult PacketDecoder::decode_incoming_packet(std::string& error, Pa
crypt_key = CryptHandler::kDefaultKey; crypt_key = CryptHandler::kDefaultKey;
crypt_nonce = CryptHandler::kDefaultNonce; crypt_nonce = CryptHandler::kDefaultNonce;
} else { } else {
if(!this->crypt_handler_->generate_key_nonce(true, packet_parser.type(), packet_parser.packet_id(), packet_parser.estimated_generation(), crypt_key, crypt_nonce)) { if(!this->crypt_handler_->generate_key_nonce(this->is_server, packet_parser.type(), packet_parser.packet_id(), packet_parser.estimated_generation(), crypt_key, crypt_nonce)) {
return PacketProcessResult::DECRYPT_KEY_GEN_FAILED; return PacketProcessResult::DECRYPT_KEY_GEN_FAILED;
} }
} }

View File

@ -48,6 +48,7 @@ namespace ts::protocol {
SEQUENCE_LENGTH_TOO_LONG /* unrecoverable error */ SEQUENCE_LENGTH_TOO_LONG /* unrecoverable error */
}; };
/* TODO: Implement for the client the command overflow recovery option! */
class PacketDecoder { class PacketDecoder {
using CommandFragment = command::CommandFragment; using CommandFragment = command::CommandFragment;
using ReassembledCommand = command::ReassembledCommand; using ReassembledCommand = command::ReassembledCommand;
@ -60,7 +61,7 @@ namespace ts::protocol {
typedef void(*callback_decoded_command_t)(void* /* cb argument */, ReassembledCommand*& /* command */); /* must move the command, else it gets freed*/ typedef void(*callback_decoded_command_t)(void* /* cb argument */, ReassembledCommand*& /* command */); /* must move the command, else it gets freed*/
typedef void(*callback_send_acknowledge_t)(void* /* cb argument */, uint16_t /* packet id */, bool /* is command low */); typedef void(*callback_send_acknowledge_t)(void* /* cb argument */, uint16_t /* packet id */, bool /* is command low */);
explicit PacketDecoder(connection::CryptHandler* /* crypt handler */); explicit PacketDecoder(connection::CryptHandler* /* crypt handler */, bool /* is server */);
~PacketDecoder(); ~PacketDecoder();
void reset(); void reset();
@ -76,6 +77,7 @@ namespace ts::protocol {
callback_decoded_command_t callback_decoded_command{[](auto, auto&){}}; /* needs to be valid all the time! */ callback_decoded_command_t callback_decoded_command{[](auto, auto&){}}; /* needs to be valid all the time! */
callback_send_acknowledge_t callback_send_acknowledge{[](auto, auto, auto){}}; /* needs to be valid all the time! */ callback_send_acknowledge_t callback_send_acknowledge{[](auto, auto, auto){}}; /* needs to be valid all the time! */
private: private:
bool is_server;
connection::CryptHandler* crypt_handler_{nullptr}; connection::CryptHandler* crypt_handler_{nullptr};
spin_mutex incoming_generation_estimator_lock{}; spin_mutex incoming_generation_estimator_lock{};

View File

@ -26,15 +26,20 @@ uint16_t GenerationEstimator::visit_packet(uint16_t packet_id) {
return this->last_generation; return this->last_generation;
} }
} else if(this->last_packet_id <= GenerationEstimator::overflow_area_end) { } else if(this->last_packet_id <= GenerationEstimator::overflow_area_end) {
if(packet_id >= GenerationEstimator::overflow_area_begin) /* old packet */ if(packet_id >= GenerationEstimator::overflow_area_begin) {/* old packet */
return this->last_generation - 1; return this->last_generation - 1;
if(packet_id > this->last_packet_id) }
if(packet_id > this->last_packet_id) {
this->last_packet_id = packet_id; this->last_packet_id = packet_id;
}
return this->last_generation; return this->last_generation;
} else { } else {
/* only update on newer packet id */ /* only update on newer packet id */
if(packet_id > this->last_packet_id) if(packet_id > this->last_packet_id) {
this->last_packet_id = packet_id; this->last_packet_id = packet_id;
}
return this->last_generation; return this->last_generation;
} }
} }