From 6f2fa60394816ecc5435efdc3730e660dbd50a23 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Wed, 30 Oct 2024 11:25:33 +0100 Subject: [PATCH] handle init2 in ft1 (hacky) --- solanaceae/ngc_ft1/ngcft1.cpp | 67 +++++++++++++++++++++++-- solanaceae/ngc_ft1/ngcft1.hpp | 4 +- solanaceae/ngc_ft1_sha1/sha1_ngcft1.cpp | 1 + 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/solanaceae/ngc_ft1/ngcft1.cpp b/solanaceae/ngc_ft1/ngcft1.cpp index b5b16d3..0da3c77 100644 --- a/solanaceae/ngc_ft1/ngcft1.cpp +++ b/solanaceae/ngc_ft1/ngcft1.cpp @@ -319,7 +319,8 @@ bool NGCFT1::NGC_FT1_send_init_private( uint32_t file_kind, const uint8_t* file_id, uint32_t file_id_size, uint64_t file_size, - uint8_t* transfer_id + uint8_t* transfer_id, + bool can_compress ) { if (std::get<0>(_t.toxGroupPeerGetConnectionStatus(group_number, peer_number)).value_or(TOX_CONNECTION_NONE) == TOX_CONNECTION_NONE) { std::cerr << "NGCFT1 error: cant init ft, peer offline\n"; @@ -454,7 +455,7 @@ bool NGCFT1::onEvent(const Events::NGCEXT_ft1_init& e) { //#if !NDEBUG std::cout << "NGCFT1: got FT1_INIT fk:" << e.file_kind << " fs:" << e.file_size << " tid:" << int(e.transfer_id) << " [" << bin2hex(e.file_id) << "]\n"; //#endif - +#if 0 bool accept = false; dispatch( NGCFT1_Event::recv_init, @@ -490,8 +491,19 @@ bool NGCFT1::onEvent(const Events::NGCEXT_ft1_init& e) { 0u, {} // rsb }; - return true; +#else + // HACK: simply forward to init2 hanlder + return onEvent(Events::NGCEXT_ft1_init2{ + e.group_number, + e.peer_number, + e.file_kind, + e.file_size, + e.transfer_id, + 0x00, // non set + e.file_id, // sadly a copy, wont matter in the future + }); +#endif } bool NGCFT1::onEvent(const Events::NGCEXT_ft1_init_ack& e) { @@ -520,6 +532,11 @@ bool NGCFT1::onEvent(const Events::NGCEXT_ft1_init_ack& e) { return true; } + if (e.max_lossy_data_size < 16) { + std::cerr << "NGCFT1 error: init_ack max_lossy_data_size is less than 16 bytes\n"; + return true; + } + // negotiated packet_data_size const auto negotiated_packet_data_size = std::min(e.max_lossy_data_size, _t.toxGroupMaxCustomLossyPacketLength()-4); // TODO: reset cca with new pkg size @@ -692,6 +709,50 @@ bool NGCFT1::onEvent(const Events::NGCEXT_ft1_message& e) { ); } +bool NGCFT1::onEvent(const Events::NGCEXT_ft1_init2& e) { +//#if !NDEBUG + std::cout << "NGCFT1: got FT1_INIT2 fk:" << e.file_kind << " fs:" << e.file_size << " tid:" << int(e.transfer_id) << " ff:" << int(e.feature_flags) << " [" << bin2hex(e.file_id) << "]\n"; +//#endif + + bool accept = false; + dispatch( + NGCFT1_Event::recv_init, + Events::NGCFT1_recv_init{ + e.group_number, e.peer_number, + static_cast(e.file_kind), + e.file_id.data(), static_cast(e.file_id.size()), + e.transfer_id, + e.file_size, + accept + } + ); + + if (!accept) { + std::cout << "NGCFT1: rejected init2\n"; + return true; // return true? + } + + _neep.send_ft1_init_ack(e.group_number, e.peer_number, e.transfer_id); + + std::cout << "NGCFT1: accepted init2\n"; + + auto& peer = groups[e.group_number].peers[e.peer_number]; + if (peer.recv_transfers[e.transfer_id].has_value()) { + std::cerr << "NGCFT1 warning: overwriting existing recv_transfer " << int(e.transfer_id) << ", other peer started new transfer on preexising\n"; + } + + peer.recv_transfers[e.transfer_id] = Group::Peer::RecvTransfer{ + e.file_kind, + e.file_id, + Group::Peer::RecvTransfer::State::INITED, + e.file_size, + 0u, + {} // rsb + }; + + return true; +} + bool NGCFT1::onToxEvent(const Tox_Event_Group_Peer_Exit* e) { const auto group_number = tox_event_group_peer_exit_get_group_number(e); const auto peer_number = tox_event_group_peer_exit_get_peer_id(e); diff --git a/solanaceae/ngc_ft1/ngcft1.hpp b/solanaceae/ngc_ft1/ngcft1.hpp index dd0a0d6..f098923 100644 --- a/solanaceae/ngc_ft1/ngcft1.hpp +++ b/solanaceae/ngc_ft1/ngcft1.hpp @@ -235,7 +235,8 @@ class NGCFT1 : public ToxEventI, public NGCEXTEventI, public NGCFT1EventProvider uint32_t file_kind, const uint8_t* file_id, uint32_t file_id_size, uint64_t file_size, - uint8_t* transfer_id + uint8_t* transfer_id, + bool can_compress = false // set this if you know the data is compressable (eg text) ); // sends the message and fills in message_id @@ -270,6 +271,7 @@ class NGCFT1 : public ToxEventI, public NGCEXTEventI, public NGCFT1EventProvider bool onEvent(const Events::NGCEXT_ft1_data&) override; bool onEvent(const Events::NGCEXT_ft1_data_ack&) override; bool onEvent(const Events::NGCEXT_ft1_message&) override; + bool onEvent(const Events::NGCEXT_ft1_init2&) override; protected: bool onToxEvent(const Tox_Event_Group_Peer_Exit* e) override; diff --git a/solanaceae/ngc_ft1_sha1/sha1_ngcft1.cpp b/solanaceae/ngc_ft1_sha1/sha1_ngcft1.cpp index 1e249bb..5fb556d 100644 --- a/solanaceae/ngc_ft1_sha1/sha1_ngcft1.cpp +++ b/solanaceae/ngc_ft1_sha1/sha1_ngcft1.cpp @@ -920,6 +920,7 @@ bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_recv_data& e) { bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_send_data& e) { if (!_sending_transfers.containsPeerTransfer(e.group_number, e.peer_number, e.transfer_id)) { + std::cerr << "SHA1_NGCFT1 error: ngcft1 requested data for unknown transfer\n"; return false; }