From e6803275a0244ca85efd978a222212d6b4d1c958 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Sat, 8 Oct 2022 00:15:51 +0200 Subject: [PATCH] hs init recv logic, + fs send init ack (not tracking it yet) --- ngc_ext_common.hpp | 4 ++-- ngc_ft1.cpp | 12 ++++++++++++ ngc_hs1.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 59 insertions(+), 4 deletions(-) diff --git a/ngc_ext_common.hpp b/ngc_ext_common.hpp index 541b686..d1818f1 100644 --- a/ngc_ext_common.hpp +++ b/ngc_ext_common.hpp @@ -28,7 +28,7 @@ enum _PacketType : uint8_t { // - peer_key bytes (the msg_ids are from) // - 1 byte (uint8_t count ids, can be 0) // - array [ - // - msg_id bytes (the message id + // - msg_id bytes (the message id) // - ] HS1_RESPONSE_LAST_IDS, @@ -49,7 +49,7 @@ enum _PacketType : uint8_t { // acknowlage init (like an accept) // like tox ft control continue - // - 1 byte (temporary_file_tf_id) + // - 1 byte (transfer_id) FT1_INIT_ACK, // TODO: init deny, speed up non acceptance diff --git a/ngc_ft1.cpp b/ngc_ft1.cpp index 1250b5e..ed34f9e 100644 --- a/ngc_ft1.cpp +++ b/ngc_ft1.cpp @@ -300,6 +300,7 @@ static void _handle_FT1_INIT( fprintf(stderr, "]\n"); // check if slot free ? + // did we allready ack this and the other side just did not see the ack? NGC_FT1_recv_init_cb* fn_ptr = nullptr; if (ngc_ext_ctx->ngc_ft1_ctx->cb_init.count(file_kind)) { @@ -316,8 +317,19 @@ static void _handle_FT1_INIT( if (accept_ft) { // send ack + // - 1 byte packet id + // - 1 byte transfer_id + std::vector pkg; + pkg.push_back(FT1_INIT_ACK); + pkg.push_back(transfer_id); + + // lossless + tox_group_send_custom_private_packet(tox, group_number, peer_number, true, pkg.data(), pkg.size(), nullptr); + + fprintf(stderr, "accepted init\n"); } else { // TODO deny? + fprintf(stderr, "rejected init\n"); } } diff --git a/ngc_hs1.cpp b/ngc_hs1.cpp index 65650db..4a445ec 100644 --- a/ngc_hs1.cpp +++ b/ngc_hs1.cpp @@ -39,7 +39,7 @@ struct NGC_HS1 { uint32_t peer_number; // the peer we requested the message from float time_since_ft_activity {0.f}; }; - std::map pending; + std::map pending; // key msg_id // dont start immediatly float time_since_last_request_sent {0.f}; @@ -465,7 +465,50 @@ static bool _handle_HS1_ft_init_message( const size_t file_size ) { fprintf(stderr, "-------hs handle ft init\n"); - return false; // deny + + // peer id and msg id from file id + // TODO: replace, remote crash + assert(file_id_size == TOX_GROUP_PEER_PUBLIC_KEY_SIZE+sizeof(uint32_t)); + + // get peer_key from file_id + _PeerKey peer_key; + std::copy(file_id, file_id+peer_key.size(), peer_key.data.begin()); + + // get msg_id from file_id + // HACK: little endian + uint32_t msg_id; + uint8_t* tmp_ptr = reinterpret_cast(&msg_id); + std::copy(file_id+TOX_GROUP_PEER_PUBLIC_KEY_SIZE, file_id+TOX_GROUP_PEER_PUBLIC_KEY_SIZE+sizeof(uint32_t), tmp_ptr); + + // did we ask for this? + + // get group id + _GroupKey g_id{}; + { // TODO: error + tox_group_get_chat_id(tox, group_number, g_id.data.data(), nullptr); + } + + auto& group = ngc_ext_ctx->ngc_hs1_ctx->history[g_id]; + + auto& pending = group.peers[peer_key].pending; + + if (!pending.count(msg_id)) { + // we did not ask for this + // TODO: accept? + fprintf(stderr, "ft init from peer we did not ask\n"); + return false; // deny + } + + if (pending.at(msg_id).peer_number != peer_number) { + // wrong peer ? + fprintf(stderr, "ft init from peer we did not ask while asking someone else\n"); + return false; // deny + } + + // TODO: more? + pending.at(msg_id).time_since_ft_activity = 0.f; + + return true; // accept } #define _HS1_HAVE(x, error) if ((length - curser) < (x)) { error; }