packet id and id parsing

This commit is contained in:
Green Sky 2024-08-05 09:57:25 +02:00
parent c82e9a7ec4
commit 868e4c33d5
No known key found for this signature in database
2 changed files with 67 additions and 26 deletions

View File

@ -1,5 +1,7 @@
#include "./tox_p2prng.hpp" #include "./tox_p2prng.hpp"
#include <iostream>
// packets: // packets:
// //
// init_with_hmac // init_with_hmac
@ -21,14 +23,6 @@
// secret_request // secret_request
// - id // - id
enum PKG {
INIT_WITH_HMAC,
HMAC,
HMAC_REQUEST,
SECRET,
SECRET_REQUEST,
};
ToxP2PRNG::ToxP2PRNG( ToxP2PRNG::ToxP2PRNG(
ToxI& t, ToxI& t,
ToxEventProviderI& tep, ToxEventProviderI& tep,
@ -60,6 +54,7 @@ ByteSpan ToxP2PRNG::getResult(const ByteSpan id) {
bool ToxP2PRNG::handlePacket( bool ToxP2PRNG::handlePacket(
Contact3Handle c, Contact3Handle c,
PKG pkg_type,
const uint8_t* data, const uint8_t* data,
const size_t data_size const size_t data_size
) { ) {
@ -67,19 +62,25 @@ bool ToxP2PRNG::handlePacket(
return false; // waht return false; // waht
} }
PKG pkg_type = static_cast<PKG>(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) { switch (pkg_type) {
case PKG::INIT_WITH_HMAC: 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: 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: 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: 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: 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: default:
return false; return false;
} }
@ -93,12 +94,23 @@ bool ToxP2PRNG::handleFriendPacket(
const uint8_t* data, const uint8_t* data,
const size_t data_size 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<PKG>(data[1]);
auto c = _tcm.getContactFriend(friend_number); auto c = _tcm.getContactFriend(friend_number);
if (!static_cast<bool>(c)) { if (!static_cast<bool>(c)) {
return false; return false;
} }
return handlePacket(c, data, data_size); return handlePacket(c, tpr_pkg_type, data+2, data_size-2);
} }
bool ToxP2PRNG::handleGroupPacket( bool ToxP2PRNG::handleGroupPacket(
@ -108,39 +120,55 @@ bool ToxP2PRNG::handleGroupPacket(
const size_t data_size, const size_t data_size,
const bool /*_private*/ 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<PKG>(data[1]);
auto c = _tcm.getContactGroupPeer(group_number, peer_number); auto c = _tcm.getContactGroupPeer(group_number, peer_number);
if (!static_cast<bool>(c)) { if (!static_cast<bool>(c)) {
return false; 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; size_t curser = 0;
return false; 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; size_t curser = 0;
return false; 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; size_t curser = 0;
return false; 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; size_t curser = 0;
return false; 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; size_t curser = 0;
return false; return false;

View File

@ -4,6 +4,8 @@
#include <solanaceae/tox_contacts/tox_contact_model2.hpp> #include <solanaceae/tox_contacts/tox_contact_model2.hpp>
#include <cstdint>
// implements P2PRNGI for tox // implements P2PRNGI for tox
// both tox friends(1to1) aswell as tox ngc(NtoN) should be supported // both tox friends(1to1) aswell as tox ngc(NtoN) should be supported
// TODO: use generic packet handling service (eg ngc_ext) instead // TODO: use generic packet handling service (eg ngc_ext) instead
@ -12,6 +14,16 @@ class ToxP2PRNG : public P2PRNGI, public ToxEventI {
ToxEventProviderI& _tep; ToxEventProviderI& _tep;
ToxContactModel2& _tcm; ToxContactModel2& _tcm;
enum class PKG : uint8_t {
INVALID = 0u,
INIT_WITH_HMAC,
HMAC,
HMAC_REQUEST,
SECRET,
SECRET_REQUEST,
};
public: public:
ToxP2PRNG( ToxP2PRNG(
ToxI& t, ToxI& t,
@ -30,6 +42,7 @@ class ToxP2PRNG : public P2PRNGI, public ToxEventI {
protected: protected:
bool handlePacket( bool handlePacket(
Contact3Handle c, Contact3Handle c,
PKG pkg_type,
const uint8_t* data, const uint8_t* data,
const size_t data_size const size_t data_size
); );
@ -47,11 +60,11 @@ class ToxP2PRNG : public P2PRNGI, public ToxEventI {
const bool _private const bool _private
); );
bool parse_init_with_hmac(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, 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, 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, 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, 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: protected:
bool onToxEvent(const Tox_Event_Friend_Lossless_Packet* e) override; bool onToxEvent(const Tox_Event_Friend_Lossless_Packet* e) override;