diff --git a/ngc_ext_common.hpp b/ngc_ext_common.hpp index cac4324..1b23461 100644 --- a/ngc_ext_common.hpp +++ b/ngc_ext_common.hpp @@ -37,18 +37,23 @@ enum _PacketType : uint8_t { // - X bytes (file_kind dependent id, differnt sizes) FT1_REQUEST = 8u, + // TODO: request result negative, speed up not found + // tell the other side you want to start a FT // TODO: might use id layer instead. with it, it would look similar to friends_ft // - 1 byte (file_kind) // - X bytes (file_kind dependent id, differnt sizes) - // - 8 bytes (data size) // TODO: do all file kinds have a size at init? + // - 8 bytes (data size, can be 0 if unknown, BUT files have to be atleast 1 byte) // - 1 byte (temporary_file_tf_id, for this peer only, technically just a prefix to distinguish between simultainious fts) FT1_INIT, // acknowlage init (like an accept) + // like tox ft control continue // - 1 byte (temporary_file_tf_id) FT1_INIT_ACK, + // TODO: init deny, speed up non acceptance + // data fragment // - 1 byte (temporary_file_tf_id) // - 2 bytes (sequece id) diff --git a/ngc_ft1.cpp b/ngc_ft1.cpp index bb4a37d..2c93eea 100644 --- a/ngc_ft1.cpp +++ b/ngc_ft1.cpp @@ -3,11 +3,58 @@ #include "ngc_ext_common.hpp" #include +#include +#include +#include #include struct NGC_FT1 { NGC_FT1_options options; + std::unordered_map cb_request; + std::unordered_map cb_init; + + struct Group { + struct Peer { + struct RecvTransfer { + std::vector file_id; + + //enum class State { + //INIT_SENT, + //SENDING, + //} state; + + // float time_since_last_activity ? + size_t file_size {0}; + size_t file_size_current {0}; + + // sequenz array + // list of last x received seq_ids + }; + std::array, 256> recv_transfers; + size_t next_recv_transfer_idx {0}; // next id will be 0 + + struct SendTransfer { + std::vector file_id; + + enum class State { + INIT_SENT, + SENDING, + } state; + + // float time_since_last_activity ? + size_t file_size {0}; + size_t file_size_current {0}; + + // sequenz array + // list of sent but not acked seq_ids + }; + std::array, 256> send_transfers; + size_t next_send_transfer_idx {0}; // next id will be 0 + }; + std::map peers; + }; + std::map groups; }; static void _handle_FT1_REQUEST(Tox* tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_number, uint32_t peer_number, const uint8_t *data, size_t length); @@ -42,7 +89,7 @@ void NGC_FT1_kill(NGC_EXT_CTX* ngc_ext_ctx) { // iterate -void NGC_FT1_request( +void NGC_FT1_send_request( Tox *tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_number, @@ -55,12 +102,12 @@ void NGC_FT1_request( // just call private for every peer in group? for (;;) { uint32_t peer_number = 0; - NGC_FT1_request_private(tox, ngc_ext_ctx, group_number, peer_number, file_kind, file_id, file_id_size); + NGC_FT1_send_request_private(tox, ngc_ext_ctx, group_number, peer_number, file_kind, file_id, file_id_size); assert(false && "not implemented"); } } -void NGC_FT1_request_private( +void NGC_FT1_send_request_private( Tox *tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_number, @@ -91,6 +138,90 @@ void NGC_FT1_request_private( tox_group_send_custom_private_packet(tox, group_number, peer_number, true, pkg.data(), pkg.size(), nullptr); } +void NGC_FT1_register_callback_recv_request(NGC_EXT_CTX* ngc_ext_ctx, NGC_FT1_file_kind file_kind, NGC_FT1_recv_request_cb* callback) { + assert(ngc_ext_ctx); + assert(ngc_ext_ctx->ngc_ft1_ctx); + + ngc_ext_ctx->ngc_ft1_ctx->cb_request[file_kind] = callback; +} + +bool NGC_FT1_send_init_private( + Tox *tox, NGC_EXT_CTX* ngc_ext_ctx, + uint32_t group_number, uint32_t peer_number, + NGC_FT1_file_kind file_kind, + const uint8_t* file_id, size_t file_id_size, + size_t file_size +) { + //fprintf(stderr, "TODO: init ft for %08X\n", msg_id); + fprintf(stderr, "init ft\n"); + + if (tox_group_peer_get_connection_status(tox, group_number, peer_number, nullptr) == TOX_CONNECTION_NONE) { + fprintf(stderr, "error: cant init ft, peer offline\n"); + return false; + } + + auto& peer = ngc_ext_ctx->ngc_ft1_ctx->groups[group_number].peers[peer_number]; + + // allocate transfer_id + size_t idx = peer.next_send_transfer_idx; + peer.next_send_transfer_idx = (peer.next_send_transfer_idx + 1) % 256; + { // TODO: extract + size_t i = idx; + bool found = false; + do { + if (!peer.send_transfers[i].has_value()) { + // free slot + idx = i; + found = true; + break; + } + + i = (i + 1) % 256; + } while (i != idx); + + if (!found) { + fprintf(stderr, "error: cant init ft, no free transfer slot\n"); + return false; + } + } + + // - 1 byte packet id + // - 1 byte (file_kind) + // - 8 bytes (data size) // TODO: do all file kinds have a size at init? + // - 1 byte (temporary_file_tf_id, for this peer only, technically just a prefix to distinguish between simultainious fts) + // - X bytes (file_kind dependent id, differnt sizes) + + std::vector pkg; + pkg.push_back(FT1_INIT); + pkg.push_back(file_kind); + for (size_t i = 0; i < sizeof(file_size); i++) { + pkg.push_back((file_size>>i) & 0xff); + } + pkg.push_back(idx); + for (size_t i = 0; i < file_id_size; i++) { + pkg.push_back(file_id[i]); + } + + // lossless + tox_group_send_custom_private_packet(tox, group_number, peer_number, true, pkg.data(), pkg.size(), nullptr); + + peer.send_transfers[idx] = NGC_FT1::Group::Peer::SendTransfer{ + std::vector(file_id, file_id+file_id_size), + NGC_FT1::Group::Peer::SendTransfer::State::INIT_SENT, + file_size, + 0, + }; + + return true; +} + +void NGC_FT1_register_callback_recv_init(NGC_EXT_CTX* ngc_ext_ctx, NGC_FT1_file_kind file_kind, NGC_FT1_recv_init_cb* callback) { + assert(ngc_ext_ctx); + assert(ngc_ext_ctx->ngc_ft1_ctx); + + ngc_ext_ctx->ngc_ft1_ctx->cb_init[file_kind] = callback; +} + #define _DATA_HAVE(x, error) if ((length - curser) < (x)) { error; } static void _handle_FT1_REQUEST( @@ -106,15 +237,26 @@ static void _handle_FT1_REQUEST( size_t curser = 0; // TODO: might be uint16_t or even larger - uint8_t file_kind; - _DATA_HAVE(sizeof(file_kind), fprintf(stderr, "packet too small, missing file_kind\n"); return) - file_kind = data[curser++]; + uint8_t file_kind_u8; + _DATA_HAVE(sizeof(file_kind_u8), fprintf(stderr, "packet too small, missing file_kind\n"); return) + file_kind_u8 = data[curser++]; + auto file_kind = static_cast(file_kind_u8); - fprintf(stderr, "got FT request with file_kind %u [", file_kind); - for (; curser < length; curser++) { - fprintf(stderr, "%02X", data[curser]); + fprintf(stderr, "got FT request with file_kind %u [", file_kind_u8); + for (size_t curser_copy = curser; curser_copy < length; curser_copy++) { + fprintf(stderr, "%02X", data[curser_copy]); } fprintf(stderr, "]\n"); + + NGC_FT1_recv_request_cb* fn_ptr = nullptr; + if (ngc_ext_ctx->ngc_ft1_ctx->cb_request.count(file_kind)) { + fn_ptr = ngc_ext_ctx->ngc_ft1_ctx->cb_request.at(file_kind); + } + if (fn_ptr) { + fn_ptr(tox, ngc_ext_ctx, group_number, peer_number, data+curser, length-curser); + } else { + fprintf(stderr, "missing cb for requests\n"); + } } #undef _DATA_HAVE diff --git a/ngc_ft1.h b/ngc_ft1.h index ce5b405..c815283 100644 --- a/ngc_ft1.h +++ b/ngc_ft1.h @@ -32,12 +32,15 @@ typedef enum NGC_FT1_file_kind /*: uint8_t*/ { // :) // draft for fun and profit + // TODO: should we even support v1? + // TODO: design the same thing again for tox? (msg_pack instead of bencode?) // id: infohash - TORRENT_V1_METAINFO, - // id: infohash - TORRENT_V2_METAINFO, + TORRENT_V1_METAINFO = 8u, // id: sha1 TORRENT_V1_CHUNK, + + // id: infohash + TORRENT_V2_METAINFO, // id: sha256 TORRENT_V2_CHUNK, } NGC_FT1_file_kind; @@ -55,41 +58,45 @@ void NGC_FT1_iterate(Tox *tox, NGC_EXT_CTX* ngc_ext_ctx/*, void *user_data*/); // TODO: remove? void NGC_FT1_request( Tox *tox, NGC_EXT_CTX* ngc_ext_ctx, - uint32_t group_number, - NGC_FT1_file_kind file_kind, - - const uint8_t* file_id, - size_t file_id_size + const uint8_t* file_id, size_t file_id_size ); -void NGC_FT1_request_private( +void NGC_FT1_send_request_private( Tox *tox, NGC_EXT_CTX* ngc_ext_ctx, - - uint32_t group_number, - uint32_t peer_number, - + uint32_t group_number, uint32_t peer_number, NGC_FT1_file_kind file_kind, - - const uint8_t* file_id, - size_t file_id_size + const uint8_t* file_id, size_t file_id_size ); +typedef void NGC_FT1_recv_request_cb(Tox *tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_number, uint32_t peer_number, const uint8_t* file_id, size_t file_id_size); + +void NGC_FT1_register_callback_recv_request(NGC_EXT_CTX* ngc_ext_ctx, NGC_FT1_file_kind file_kind, NGC_FT1_recv_request_cb* callback); + +// ========== send/accept ========== + +bool NGC_FT1_send_init_private( + Tox *tox, NGC_EXT_CTX* ngc_ext_ctx, + uint32_t group_number, uint32_t peer_number, + NGC_FT1_file_kind file_kind, + const uint8_t* file_id, size_t file_id_size, + size_t file_size +); + +// return true to accept, false to deny +typedef bool NGC_FT1_recv_init_cb(Tox *tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_number, uint32_t peer_number, const uint8_t* file_id, size_t file_id_size, const uint8_t transfer_id, const size_t file_size); + +void NGC_FT1_register_callback_recv_init(NGC_EXT_CTX* ngc_ext_ctx, NGC_FT1_file_kind file_kind, NGC_FT1_recv_init_cb* callback); + // ========== peer online/offline ========== - //void NGC_FT1_peer_online(Tox* tox, NGC_FT1* ngc_hs1_ctx, uint32_t group_number, uint32_t peer_number, bool online); // "callback" void NGC_FT1_handle_group_custom_packet( - Tox* tox, - NGC_FT1* ngc_hs1_ctx, - - uint32_t group_number, - uint32_t peer_number, - - const uint8_t *data, - size_t length + Tox* tox, NGC_FT1* ngc_hs1_ctx, + uint32_t group_number, uint32_t peer_number, + const uint8_t *data, size_t length //void *user_data ); diff --git a/ngc_hs1.cpp b/ngc_hs1.cpp index 6e0dc3f..a617ec7 100644 --- a/ngc_hs1.cpp +++ b/ngc_hs1.cpp @@ -116,6 +116,22 @@ static void _handle_HS1_RESPONSE_LAST_IDS( size_t length ); +static void _handle_HS1_ft_request_message( + Tox *tox, NGC_EXT_CTX* ngc_ext_ctx, + uint32_t group_number, + uint32_t peer_number, + const uint8_t* file_id, size_t file_id_size +); + +static bool _handle_HS1_ft_init_message( + Tox *tox, NGC_EXT_CTX* ngc_ext_ctx, + uint32_t group_number, + uint32_t peer_number, + const uint8_t* file_id, size_t file_id_size, + const uint8_t transfer_id, + const size_t file_size +); + bool NGC_HS1_init(NGC_EXT_CTX* ngc_ext_ctx, const struct NGC_HS1_options* options) { ngc_ext_ctx->ngc_hs1_ctx = new NGC_HS1; ngc_ext_ctx->ngc_hs1_ctx->options = *options; @@ -123,6 +139,9 @@ bool NGC_HS1_init(NGC_EXT_CTX* ngc_ext_ctx, const struct NGC_HS1_options* option ngc_ext_ctx->callbacks[HS1_REQUEST_LAST_IDS] = _handle_HS1_REQUEST_LAST_IDS; ngc_ext_ctx->callbacks[HS1_RESPONSE_LAST_IDS] = _handle_HS1_RESPONSE_LAST_IDS; + NGC_FT1_register_callback_recv_request(ngc_ext_ctx, NGC_FT1_file_kind::NGC_HS1_MESSAGE_BY_ID, _handle_HS1_ft_request_message); + NGC_FT1_register_callback_recv_init(ngc_ext_ctx, NGC_FT1_file_kind::NGC_HS1_MESSAGE_BY_ID, _handle_HS1_ft_init_message); + return true; } @@ -217,7 +236,7 @@ static void _iterate_group(Tox *tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_nu } // send request - NGC_FT1_request_private( + NGC_FT1_send_request_private( tox, ngc_ext_ctx, group_number, remote_peer_number, NGC_FT1_file_kind::NGC_HS1_MESSAGE_BY_ID, @@ -230,8 +249,8 @@ static void _iterate_group(Tox *tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_nu } } } - assert(ngc_hs1_ctx->history.size() != 0); + assert(ngc_hs1_ctx->history.size() != 0); assert(ngc_hs1_ctx->history.count(g_id)); } @@ -376,6 +395,77 @@ void NGC_HS1_record_message( ngc_hs1_ctx->history[g_id].peers[p_id].append(message_id, type, std::string{message, message+length}); } +static void _handle_HS1_ft_request_message( + Tox *tox, NGC_EXT_CTX* ngc_ext_ctx, + uint32_t group_number, + uint32_t peer_number, + const uint8_t* file_id, size_t file_id_size +) { + 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); + + fprintf(stderr, "got a ft request for xxx msg_id %08X\n", msg_id); + + // get group id + _GroupKey group_id{}; + { // TODO: error + tox_group_get_chat_id(tox, group_number, group_id.data.data(), nullptr); + } + + const auto& peers = ngc_ext_ctx->ngc_hs1_ctx->history[group_id].peers; + + // do we have that message + + if (!peers.count(peer_key)) { + fprintf(stderr, "got ft request for unknown peer\n"); + return; + } + + const auto& peer = peers.at(peer_key); + if (!peer.dict.count(msg_id)) { + fprintf(stderr, "got ft request for unknown message_id %08X\n", msg_id); + return; + } + + // yes we do. now we need to init ft? + + //fprintf(stderr, "TODO: init ft for %08X\n", msg_id); + + // filesize is + // - 1 byte msg_type (normal / action) + // - x bytes msg_text + // msg_id is part of file_id + const auto& msg = peer.dict.at(msg_id); + size_t file_size = 1 + msg.text.size(); + + NGC_FT1_send_init_private( + tox, ngc_ext_ctx, + group_number, peer_number, + NGC_HS1_MESSAGE_BY_ID, + file_id, file_id_size, + file_size + ); +} + +static bool _handle_HS1_ft_init_message( + Tox *tox, NGC_EXT_CTX* ngc_ext_ctx, + uint32_t group_number, + uint32_t peer_number, + const uint8_t* file_id, size_t file_id_size, + const uint8_t transfer_id, + const size_t file_size +) { + return false; // deny +} #define _HS1_HAVE(x, error) if ((length - curser) < (x)) { error; }