hs init recv logic, + fs send init ack (not tracking it yet)

This commit is contained in:
Green Sky 2022-10-08 00:15:51 +02:00
parent bd0c45f885
commit e6803275a0
No known key found for this signature in database
3 changed files with 59 additions and 4 deletions

View File

@ -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

View File

@ -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<uint8_t> 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");
}
}

View File

@ -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<uint32_t, PendingFTRequest> pending;
std::map<uint32_t, PendingFTRequest> pending; // key msg_id
// dont start immediatly
float time_since_last_request_sent {0.f};
@ -465,9 +465,52 @@ static bool _handle_HS1_ft_init_message(
const size_t file_size
) {
fprintf(stderr, "-------hs handle ft init\n");
// 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<uint8_t*>(&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; }
static void _handle_HS1_REQUEST_LAST_IDS(