From 868e4c33d52593aa874565fae40234fb1f92ad0f Mon Sep 17 00:00:00 2001 From: Green Sky Date: Mon, 5 Aug 2024 09:57:25 +0200 Subject: [PATCH] packet id and id parsing --- src/solanaceae/tox_p2prng/tox_p2prng.cpp | 70 +++++++++++++++++------- src/solanaceae/tox_p2prng/tox_p2prng.hpp | 23 ++++++-- 2 files changed, 67 insertions(+), 26 deletions(-) diff --git a/src/solanaceae/tox_p2prng/tox_p2prng.cpp b/src/solanaceae/tox_p2prng/tox_p2prng.cpp index ab15672..d382d71 100644 --- a/src/solanaceae/tox_p2prng/tox_p2prng.cpp +++ b/src/solanaceae/tox_p2prng/tox_p2prng.cpp @@ -1,5 +1,7 @@ #include "./tox_p2prng.hpp" +#include + // packets: // // init_with_hmac @@ -21,14 +23,6 @@ // secret_request // - id -enum PKG { - INIT_WITH_HMAC, - HMAC, - HMAC_REQUEST, - SECRET, - SECRET_REQUEST, -}; - ToxP2PRNG::ToxP2PRNG( ToxI& t, ToxEventProviderI& tep, @@ -60,6 +54,7 @@ ByteSpan ToxP2PRNG::getResult(const ByteSpan id) { bool ToxP2PRNG::handlePacket( Contact3Handle c, + PKG pkg_type, const uint8_t* data, const size_t data_size ) { @@ -67,19 +62,25 @@ bool ToxP2PRNG::handlePacket( return false; // waht } - PKG pkg_type = static_cast(data[0]); + // rn all are prefixed with ID, so here we go + if (data_size < 32) { + std::cerr << "TP2PRNG error: packet without id\n"; + return false; + } + + ByteSpan id{data, 32}; switch (pkg_type) { case PKG::INIT_WITH_HMAC: - return parse_init_with_hmac(c, data+1, data_size-1); + return parse_init_with_hmac(c, id, data+32, data_size-(32)); case PKG::HMAC: - return parse_hmac(c, data+1, data_size-1); + return parse_hmac(c, id, data+32, data_size-(32)); case PKG::HMAC_REQUEST: - return parse_hmac_request(c, data+1, data_size-1); + return parse_hmac_request(c, id, data+32, data_size-(32)); case PKG::SECRET: - return parse_secret(c, data+1, data_size-1); + return parse_secret(c, id, data+32, data_size-(32)); case PKG::SECRET_REQUEST: - return parse_secret_request(c, data+1, data_size-1); + return parse_secret_request(c, id, data+32, data_size-(32)); default: return false; } @@ -93,12 +94,23 @@ bool ToxP2PRNG::handleFriendPacket( const uint8_t* data, const size_t data_size ) { + // packet id + packet id + id + if (data_size < 1+1+32) { + return false; + } + + if (data[0] != 0xB1) { // 177 + return false; + } + + PKG tpr_pkg_type = static_cast(data[1]); + auto c = _tcm.getContactFriend(friend_number); if (!static_cast(c)) { return false; } - return handlePacket(c, data, data_size); + return handlePacket(c, tpr_pkg_type, data+2, data_size-2); } bool ToxP2PRNG::handleGroupPacket( @@ -108,39 +120,55 @@ bool ToxP2PRNG::handleGroupPacket( const size_t data_size, const bool /*_private*/ ) { + // packet id + packet id + id + if (data_size < 1+1+32) { + return false; + } + + if (data[0] != (0x80 | 38u)) { // 0xa6 / 166 + return false; + } + + PKG tpr_pkg_type = static_cast(data[1]); + auto c = _tcm.getContactGroupPeer(group_number, peer_number); if (!static_cast(c)) { return false; } - return handlePacket(c, data, data_size); + return handlePacket(c, tpr_pkg_type, data+2, data_size-2); } -bool ToxP2PRNG::parse_init_with_hmac(Contact3Handle c, const uint8_t* data, size_t data_size) { +bool ToxP2PRNG::parse_init_with_hmac(Contact3Handle c, ByteSpan id, const uint8_t* data, size_t data_size) { + std::cerr << "TP2PRNG: got packet init_with_hmac\n"; size_t curser = 0; return false; } -bool ToxP2PRNG::parse_hmac(Contact3Handle c, const uint8_t* data, size_t data_size) { +bool ToxP2PRNG::parse_hmac(Contact3Handle c, ByteSpan id, const uint8_t* data, size_t data_size) { + std::cerr << "TP2PRNG: got packet hmac\n"; size_t curser = 0; return false; } -bool ToxP2PRNG::parse_hmac_request(Contact3Handle c, const uint8_t* data, size_t data_size) { +bool ToxP2PRNG::parse_hmac_request(Contact3Handle c, ByteSpan id, const uint8_t* data, size_t data_size) { + std::cerr << "TP2PRNG: got packet hmac_request\n"; size_t curser = 0; return false; } -bool ToxP2PRNG::parse_secret(Contact3Handle c, const uint8_t* data, size_t data_size) { +bool ToxP2PRNG::parse_secret(Contact3Handle c, ByteSpan id, const uint8_t* data, size_t data_size) { + std::cerr << "TP2PRNG: got packet secret\n"; size_t curser = 0; return false; } -bool ToxP2PRNG::parse_secret_request(Contact3Handle c, const uint8_t* data, size_t data_size) { +bool ToxP2PRNG::parse_secret_request(Contact3Handle c, ByteSpan id, const uint8_t* data, size_t data_size) { + std::cerr << "TP2PRNG: got packet secret_request\n"; size_t curser = 0; return false; diff --git a/src/solanaceae/tox_p2prng/tox_p2prng.hpp b/src/solanaceae/tox_p2prng/tox_p2prng.hpp index 5df7bdc..bf8715d 100644 --- a/src/solanaceae/tox_p2prng/tox_p2prng.hpp +++ b/src/solanaceae/tox_p2prng/tox_p2prng.hpp @@ -4,6 +4,8 @@ #include +#include + // implements P2PRNGI for tox // both tox friends(1to1) aswell as tox ngc(NtoN) should be supported // TODO: use generic packet handling service (eg ngc_ext) instead @@ -12,6 +14,16 @@ class ToxP2PRNG : public P2PRNGI, public ToxEventI { ToxEventProviderI& _tep; ToxContactModel2& _tcm; + enum class PKG : uint8_t { + INVALID = 0u, + + INIT_WITH_HMAC, + HMAC, + HMAC_REQUEST, + SECRET, + SECRET_REQUEST, + }; + public: ToxP2PRNG( ToxI& t, @@ -30,6 +42,7 @@ class ToxP2PRNG : public P2PRNGI, public ToxEventI { protected: bool handlePacket( Contact3Handle c, + PKG pkg_type, const uint8_t* data, const size_t data_size ); @@ -47,11 +60,11 @@ class ToxP2PRNG : public P2PRNGI, public ToxEventI { const bool _private ); - bool parse_init_with_hmac(Contact3Handle c, const uint8_t* data, size_t data_size); - bool parse_hmac(Contact3Handle c, const uint8_t* data, size_t data_size); - bool parse_hmac_request(Contact3Handle c, const uint8_t* data, size_t data_size); - bool parse_secret(Contact3Handle c, const uint8_t* data, size_t data_size); - bool parse_secret_request(Contact3Handle c, const uint8_t* data, size_t data_size); + bool parse_init_with_hmac(Contact3Handle c, ByteSpan id, const uint8_t* data, size_t data_size); + bool parse_hmac(Contact3Handle c, ByteSpan id, const uint8_t* data, size_t data_size); + bool parse_hmac_request(Contact3Handle c, ByteSpan id, const uint8_t* data, size_t data_size); + bool parse_secret(Contact3Handle c, ByteSpan id, const uint8_t* data, size_t data_size); + bool parse_secret_request(Contact3Handle c, ByteSpan id, const uint8_t* data, size_t data_size); protected: bool onToxEvent(const Tox_Event_Friend_Lossless_Packet* e) override;