This repository has been archived on 2023-01-13. You can view files and clone it, but cannot push or open issues or pull requests.
NGC_HS1/ngc_hs1.cpp

609 lines
16 KiB
C++
Raw Normal View History

2022-10-08 20:58:10 +02:00
#include "./ngc_hs1.hpp"
2022-09-26 20:55:58 +02:00
2022-10-01 02:32:34 +02:00
#include "ngc_ext_common.hpp"
2022-10-03 23:33:08 +02:00
#include "ngc_ft1.h"
2022-10-01 02:32:34 +02:00
2022-09-28 20:40:49 +02:00
#include <cstdint>
2022-09-30 02:04:25 +02:00
#include <cassert>
2022-09-28 20:40:49 +02:00
#include <new>
2022-09-29 02:39:54 +02:00
#include <map>
#include <list>
2022-10-01 01:38:42 +02:00
#include <set>
2022-09-30 02:04:25 +02:00
#include <optional>
#include <algorithm>
2022-09-28 20:40:49 +02:00
2022-10-01 02:32:34 +02:00
void NGC_HS1::Peer::append(uint32_t msg_id, Tox_Message_Type type, const std::string& text) {
order.push_back(msg_id);
2022-10-01 01:38:42 +02:00
2022-10-01 02:32:34 +02:00
// overwrites
auto& new_msg = dict[msg_id];
new_msg.msg_id = msg_id;
new_msg.type = type;
new_msg.text = text;
2022-10-01 01:38:42 +02:00
2022-10-01 02:32:34 +02:00
if (heard_of.count(msg_id)) {
// we got history before we got the message
heard_of.erase(msg_id);
}
2022-10-01 01:38:42 +02:00
2022-10-01 02:32:34 +02:00
fprintf(stderr, "######## last msgs ########\n");
auto rit = order.crbegin();
for (size_t i = 0; i < 10 && rit != order.crend(); i++, rit++) {
fprintf(stderr, " %08X - %s\n", *rit, dict.at(*rit).text.c_str());
}
}
2022-09-29 02:39:54 +02:00
2022-10-01 02:32:34 +02:00
bool NGC_HS1::Peer::hear(uint32_t msg_id, uint32_t peer_number) {
if (dict.count(msg_id)) {
// we know
return false;
}
2022-09-30 02:04:25 +02:00
2022-10-01 02:32:34 +02:00
if (heard_of.count(msg_id) && heard_of.at(msg_id).count(peer_number)) {
// we heard it from that peer before
return false;
}
heard_of[msg_id].emplace(peer_number);
return true;
}
2022-09-26 20:55:58 +02:00
2022-10-08 20:58:10 +02:00
void _handle_HS1_REQUEST_LAST_IDS(
2022-10-03 01:15:49 +02:00
Tox* tox,
NGC_EXT_CTX* ngc_ext_ctx,
2022-09-26 20:55:58 +02:00
2022-10-03 01:15:49 +02:00
uint32_t group_number,
uint32_t peer_number,
const uint8_t *data,
size_t length
);
2022-09-26 20:55:58 +02:00
2022-10-08 20:58:10 +02:00
void _handle_HS1_RESPONSE_LAST_IDS(
2022-10-03 01:15:49 +02:00
Tox* tox,
NGC_EXT_CTX* ngc_ext_ctx,
2022-10-03 01:15:49 +02:00
uint32_t group_number,
uint32_t peer_number,
const uint8_t *data,
size_t length
);
2022-10-08 20:58:10 +02:00
void _handle_HS1_ft_request_message(
2022-10-06 02:24:23 +02:00
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
);
2022-10-08 20:58:10 +02:00
bool _handle_HS1_ft_init_message(
2022-10-06 02:24:23 +02:00
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
);
2022-10-03 01:15:49 +02:00
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;
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;
2022-10-06 02:24:23 +02:00
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);
2022-10-03 01:15:49 +02:00
return true;
2022-09-26 20:55:58 +02:00
}
2022-10-03 01:15:49 +02:00
void NGC_HS1_kill(NGC_EXT_CTX* ngc_ext_ctx) {
delete ngc_ext_ctx->ngc_hs1_ctx;
ngc_ext_ctx->ngc_hs1_ctx = nullptr;
2022-09-26 20:55:58 +02:00
}
2022-10-03 23:33:08 +02:00
static void _iterate_group(Tox *tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_number, float time_delta) {
NGC_HS1* ngc_hs1_ctx = ngc_ext_ctx->ngc_hs1_ctx;
2022-09-30 02:04:25 +02:00
//fprintf(stderr, "g:%u\n", g_i);
2022-10-01 02:32:34 +02:00
_GroupKey g_id{};
2022-09-30 02:04:25 +02:00
{ // TODO: error
tox_group_get_chat_id(tox, group_number, g_id.data.data(), nullptr);
}
if (ngc_hs1_ctx->history.count(g_id) == 0) {
fprintf(stderr, "adding new group: %u %X%X%X%X\n",
group_number,
g_id.data.data()[0],
g_id.data.data()[1],
g_id.data.data()[2],
g_id.data.data()[3]
);
ngc_hs1_ctx->history[g_id];
} else {
auto& group = ngc_hs1_ctx->history[g_id];
// for each peer
2022-10-03 23:33:08 +02:00
for (auto& [peer_key, peer] : group.peers) {
2022-09-30 02:04:25 +02:00
//fprintf(stderr, " p: %X%X%X%X\n", key.data.data()[0], key.data.data()[1], key.data.data()[2], key.data.data()[3]);
peer.time_since_last_request_sent += time_delta;
2022-10-01 02:32:34 +02:00
if (peer.time_since_last_request_sent > ngc_hs1_ctx->options.query_interval_per_peer) {
2022-09-30 02:04:25 +02:00
peer.time_since_last_request_sent = 0.f;
2022-10-03 23:33:08 +02:00
fprintf(stderr, "requesting ids for %X%X%X%X\n", peer_key.data.data()[0], peer_key.data.data()[1], peer_key.data.data()[2], peer_key.data.data()[3]);
2022-09-30 02:04:25 +02:00
// TODO: other way around?
// ask everyone if they have newer stuff for this peer
// - 1 byte packet id
// - peer_key bytes (peer key we want to know ids for)
// - 1 byte (uint8_t count ids, atleast 1)
std::array<uint8_t, 1+TOX_GROUP_PEER_PUBLIC_KEY_SIZE+1> pkg;
2022-10-01 02:32:34 +02:00
pkg[0] = HS1_REQUEST_LAST_IDS;
2022-10-03 23:33:08 +02:00
std::copy(peer_key.data.begin(), peer_key.data.end(), pkg.begin()+1);
2022-10-01 02:32:34 +02:00
pkg[1+TOX_GROUP_PEER_PUBLIC_KEY_SIZE] = ngc_hs1_ctx->options.last_msg_ids_count; // request last (up to) 5 msg_ids
2022-09-30 02:04:25 +02:00
tox_group_send_custom_packet(tox, group_number, true, pkg.data(), pkg.size(), nullptr);
}
2022-10-03 23:33:08 +02:00
// check if pending msg requests have timed out
for (auto it = peer.pending.begin(); it != peer.pending.end();) {
it->second.time_since_ft_activity += time_delta;
if (it->second.time_since_ft_activity >= ngc_hs1_ctx->options.ft_activity_timeout) {
// timed out
fprintf(stderr, "!!! pending ft request timed out (%08X)\n", it->first);
it = peer.pending.erase(it);
} else {
it++;
}
}
// request FT for only heard of message_ids
size_t request_made_count = 0;
for (const auto& [msg_id, remote_peer_numbers] : peer.heard_of) {
if (request_made_count >= 2) { // 2 for test
// TODO: limit requests per iterate option
break;
}
if (peer.pending.count(msg_id)) {
continue; // allready requested
}
if (remote_peer_numbers.empty()) {
fprintf(stderr, "!!! msg_id we heard of, but no remote peer !!!\n");
continue;
}
const uint32_t remote_peer_number = *remote_peer_numbers.begin();
// craft file id
std::array<uint8_t, TOX_GROUP_PEER_PUBLIC_KEY_SIZE+sizeof(uint32_t)> file_id{};
{
std::copy(peer_key.data.cbegin(), peer_key.data.cend(), file_id.begin());
// HACK: little endian
const uint8_t* tmp_ptr = reinterpret_cast<const uint8_t*>(&msg_id);
std::copy(tmp_ptr, tmp_ptr+sizeof(uint32_t), file_id.begin()+TOX_GROUP_PEER_PUBLIC_KEY_SIZE);
}
// send request
2022-10-06 02:24:23 +02:00
NGC_FT1_send_request_private(
2022-10-03 23:33:08 +02:00
tox, ngc_ext_ctx,
group_number, remote_peer_number,
NGC_FT1_file_kind::NGC_HS1_MESSAGE_BY_ID,
file_id.data(), file_id.size()
);
peer.pending[msg_id] = {remote_peer_number, 0.f};
request_made_count++;
}
2022-09-30 02:04:25 +02:00
}
}
2022-10-06 02:24:23 +02:00
assert(ngc_hs1_ctx->history.size() != 0);
2022-09-30 02:04:25 +02:00
assert(ngc_hs1_ctx->history.count(g_id));
}
2022-10-03 01:15:49 +02:00
void NGC_HS1_iterate(Tox *tox, NGC_EXT_CTX* ngc_ext_ctx/*, void *user_data*/) {
assert(ngc_ext_ctx);
2022-10-03 23:33:08 +02:00
assert(ngc_ext_ctx->ngc_hs1_ctx);
2022-09-30 02:04:25 +02:00
//fprintf(stderr, "groups: %u\n", ngc_hs1_ctx->history.size());
uint32_t group_count = tox_group_get_number_groups(tox);
// this can loop endless if toxcore misbehaves
for (uint32_t g_i = 0, g_c_done = 0; g_c_done < group_count; g_i++) {
Tox_Err_Group_Is_Connected g_err;
if (tox_group_is_connected(tox, g_i, &g_err)) {
// valid and connected here
// TODO: delta time, or other timers
2022-10-03 23:33:08 +02:00
_iterate_group(tox, ngc_ext_ctx, g_i, 0.02f);
2022-09-30 02:04:25 +02:00
g_c_done++;
} else if (g_err != TOX_ERR_GROUP_IS_CONNECTED_GROUP_NOT_FOUND) {
g_c_done++;
} // else do nothing
// safety
if (g_i > group_count + 1000) {
fprintf(stderr, "WAY PAST GOUPS in iterate\n");
break;
}
}
}
2022-10-03 01:15:49 +02:00
void NGC_HS1_peer_online(Tox* tox, NGC_EXT_CTX* ngc_ext_ctx, uint32_t group_number, uint32_t peer_number, bool online) {
NGC_HS1* ngc_hs1_ctx = ngc_ext_ctx->ngc_hs1_ctx;
2022-09-30 02:04:25 +02:00
// get group id
2022-10-01 02:32:34 +02:00
_GroupKey g_id{};
2022-09-30 02:04:25 +02:00
{ // TODO: error
tox_group_get_chat_id(tox, group_number, g_id.data.data(), nullptr);
}
auto& group = ngc_hs1_ctx->history[g_id];
if (online) {
// get peer id
2022-10-01 02:32:34 +02:00
_PeerKey p_id{};
2022-09-30 02:04:25 +02:00
{ // TODO: error
tox_group_peer_get_public_key(tox, group_number, peer_number, p_id.data.data(), nullptr);
}
auto& peer = group.peers[p_id];
peer.id = peer_number;
} else { // offline
// search
for (auto& [key, peer] : group.peers) {
if (peer.id.has_value() && peer.id.value() == peer_number) {
peer.id = {}; // reset
break;
}
}
}
2022-09-26 20:55:58 +02:00
}
bool NGC_HS1_shim_group_send_message(
const Tox *tox,
2022-10-03 01:15:49 +02:00
NGC_EXT_CTX* ngc_ext_ctx,
2022-09-26 20:55:58 +02:00
uint32_t group_number,
Tox_Message_Type type, const uint8_t *message, size_t length,
uint32_t *message_id,
Tox_Err_Group_Send_Message *error
) {
uint32_t* msg_id_ptr = message_id;
uint32_t msg_id_placeholder = 0;
2022-09-29 02:39:54 +02:00
if (msg_id_ptr == nullptr) {
2022-09-26 20:55:58 +02:00
msg_id_ptr = &msg_id_placeholder;
}
bool ret = tox_group_send_message(tox, group_number, type, message, length, msg_id_ptr, error);
2022-10-03 01:15:49 +02:00
NGC_HS1_record_own_message(tox, ngc_ext_ctx, group_number, type, message, length, *msg_id_ptr);
2022-09-26 20:55:58 +02:00
return ret;
}
// record own msg
void NGC_HS1_record_own_message(
const Tox *tox,
2022-10-03 01:15:49 +02:00
NGC_EXT_CTX* ngc_ext_ctx,
2022-09-26 20:55:58 +02:00
uint32_t group_number,
Tox_Message_Type type, const uint8_t *message, size_t length, uint32_t message_id
) {
2022-10-03 01:15:49 +02:00
NGC_HS1* ngc_hs1_ctx = ngc_ext_ctx->ngc_hs1_ctx;
2022-09-30 02:04:25 +02:00
fprintf(stderr, "record_own_message %08X\n", message_id);
2022-09-29 02:39:54 +02:00
// get group id
2022-10-01 02:32:34 +02:00
_GroupKey g_id{};
2022-09-29 02:39:54 +02:00
{ // TODO: error
tox_group_get_chat_id(tox, group_number, g_id.data.data(), nullptr);
}
// get peer id
2022-10-01 02:32:34 +02:00
_PeerKey p_id{};
2022-09-29 02:39:54 +02:00
{ // TODO: error
tox_group_self_get_public_key(tox, group_number, p_id.data.data(), nullptr);
}
2022-09-30 02:04:25 +02:00
ngc_hs1_ctx->history[g_id].peers[p_id].append(message_id, type, std::string{message, message+length});
assert(ngc_hs1_ctx->history.size() != 0);
assert(ngc_hs1_ctx->history.count(g_id));
}
// record others msg
void NGC_HS1_record_message(
const Tox *tox,
2022-10-03 01:15:49 +02:00
NGC_EXT_CTX* ngc_ext_ctx,
2022-09-30 02:04:25 +02:00
uint32_t group_number,
uint32_t peer_number,
Tox_Message_Type type, const uint8_t *message, size_t length, uint32_t message_id
) {
2022-10-03 01:15:49 +02:00
NGC_HS1* ngc_hs1_ctx = ngc_ext_ctx->ngc_hs1_ctx;
2022-09-30 02:04:25 +02:00
if (!ngc_hs1_ctx->options.record_others) {
return;
}
fprintf(stderr, "record_message %08X\n", message_id);
// get group id
2022-10-01 02:32:34 +02:00
_GroupKey g_id{};
2022-09-30 02:04:25 +02:00
{ // TODO: error
tox_group_get_chat_id(tox, group_number, g_id.data.data(), nullptr);
}
// get peer id
2022-10-01 02:32:34 +02:00
_PeerKey p_id{};
2022-09-30 02:04:25 +02:00
{ // TODO: error
tox_group_peer_get_public_key(tox, group_number, peer_number, p_id.data.data(), nullptr);
}
ngc_hs1_ctx->history[g_id].peers[p_id].append(message_id, type, std::string{message, message+length});
}
2022-10-08 20:58:10 +02:00
void _handle_HS1_ft_request_message(
2022-10-06 02:24:23 +02:00
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<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);
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
);
}
2022-10-08 20:58:10 +02:00
bool _handle_HS1_ft_init_message(
2022-10-06 02:24:23 +02:00
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
) {
2022-10-06 22:00:06 +02:00
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
2022-10-06 02:24:23 +02:00
}
2022-09-30 23:38:19 +02:00
2022-09-30 02:04:25 +02:00
#define _HS1_HAVE(x, error) if ((length - curser) < (x)) { error; }
2022-10-08 20:58:10 +02:00
void _handle_HS1_REQUEST_LAST_IDS(
2022-09-30 02:04:25 +02:00
Tox* tox,
NGC_EXT_CTX* ngc_ext_ctx,
2022-09-30 02:04:25 +02:00
uint32_t group_number,
uint32_t peer_number,
const uint8_t *data,
size_t length
) {
NGC_HS1* ngc_hs1_ctx = ngc_ext_ctx->ngc_hs1_ctx;
2022-09-30 02:04:25 +02:00
size_t curser = 0;
2022-10-01 02:32:34 +02:00
_PeerKey p_key;
2022-09-30 02:04:25 +02:00
_HS1_HAVE(p_key.data.size(), fprintf(stderr, "packet too small, missing pkey\n"); return)
2022-10-01 01:38:42 +02:00
2022-09-30 02:04:25 +02:00
std::copy(data+curser, data+curser+p_key.data.size(), p_key.data.begin());
curser += p_key.data.size();
_HS1_HAVE(1, fprintf(stderr, "packet too small, missing count\n"); return)
uint8_t last_msg_id_count = data[curser++];
fprintf(stderr, "got request for last %u ids\n", last_msg_id_count);
2022-09-30 23:38:19 +02:00
// get group id
2022-10-01 02:32:34 +02:00
_GroupKey g_id{};
2022-09-30 23:38:19 +02:00
{ // TODO: error
tox_group_get_chat_id(tox, group_number, g_id.data.data(), nullptr);
}
auto& group = ngc_hs1_ctx->history[g_id];
std::vector<uint32_t> message_ids{};
if (!group.peers.empty() && group.peers.count(p_key)) {
const auto& peer = group.peers.at(p_key);
auto rit = peer.order.crbegin();
for (size_t c = 0; c < last_msg_id_count && rit != peer.order.crend(); c++, rit++) {
message_ids.push_back(*rit);
}
}
// - 1 byte packet id
// respond to a request with 0 or more message ids, sorted by newest first
// - peer_key bytes (the msg_ids are from)
// - 1 byte (uint8_t count ids, can be 0)
// - array [
// - msg_id bytes (the message id
// - ]
//std::array<uint8_t, 1+TOX_GROUP_PEER_PUBLIC_KEY_SIZE+1+> pkg;
std::vector<uint8_t> pkg;
pkg.resize(1+TOX_GROUP_PEER_PUBLIC_KEY_SIZE+1+sizeof(uint32_t)*message_ids.size());
2022-10-01 01:38:42 +02:00
size_t packing_curser = 0;
2022-09-30 23:38:19 +02:00
2022-10-01 02:32:34 +02:00
pkg[packing_curser++] = HS1_RESPONSE_LAST_IDS;
2022-09-30 23:38:19 +02:00
2022-10-01 01:38:42 +02:00
std::copy(p_key.data.begin(), p_key.data.end(), pkg.begin()+packing_curser);
packing_curser += p_key.data.size();
pkg[packing_curser++] = message_ids.size();
2022-09-30 23:38:19 +02:00
for (size_t i = 0; i < message_ids.size(); i++) {
2022-10-01 01:38:42 +02:00
const uint8_t* tmp_ptr = reinterpret_cast<uint8_t*>(message_ids.data()+i);
2022-09-30 23:38:19 +02:00
// HACK: little endian
2022-10-01 01:38:42 +02:00
//std::copy(tmp_ptr, tmp_ptr+sizeof(uint32_t), pkg.begin()+1+TOX_GROUP_PEER_PUBLIC_KEY_SIZE+1+i*sizeof(uint32_t));
std::copy(tmp_ptr, tmp_ptr+sizeof(uint32_t), pkg.begin()+packing_curser);
packing_curser += sizeof(uint32_t);
2022-09-30 23:38:19 +02:00
}
tox_group_send_custom_private_packet(tox, group_number, peer_number, true, pkg.data(), pkg.size(), nullptr);
}
2022-10-08 20:58:10 +02:00
void _handle_HS1_RESPONSE_LAST_IDS(
2022-09-30 23:38:19 +02:00
Tox* tox,
NGC_EXT_CTX* ngc_ext_ctx,
2022-09-30 23:38:19 +02:00
uint32_t group_number,
uint32_t peer_number,
const uint8_t *data,
size_t length
) {
NGC_HS1* ngc_hs1_ctx = ngc_ext_ctx->ngc_hs1_ctx;
2022-09-30 23:38:19 +02:00
size_t curser = 0;
2022-10-01 02:32:34 +02:00
_PeerKey p_key;
2022-09-30 23:38:19 +02:00
_HS1_HAVE(p_key.data.size(), fprintf(stderr, "packet too small, missing pkey\n"); return)
2022-10-01 01:38:42 +02:00
2022-09-30 23:38:19 +02:00
std::copy(data+curser, data+curser+p_key.data.size(), p_key.data.begin());
curser += p_key.data.size();
// TODO: did we ask?
_HS1_HAVE(1, fprintf(stderr, "packet too small, missing count\n"); return)
uint8_t last_msg_id_count = data[curser++];
2022-10-01 01:38:42 +02:00
fprintf(stderr, "got response with last %u ids:\n", last_msg_id_count);
if (last_msg_id_count == 0) {
return;
}
// get group id
2022-10-01 02:32:34 +02:00
_GroupKey g_id{};
2022-10-01 01:38:42 +02:00
{ // TODO: error
tox_group_get_chat_id(tox, group_number, g_id.data.data(), nullptr);
}
// get peer
auto& peer = ngc_hs1_ctx->history[g_id].peers[p_key];
2022-10-03 23:33:08 +02:00
//std::vector<uint32_t> message_ids{};
2022-10-01 01:38:42 +02:00
for (size_t i = 0; i < last_msg_id_count && curser+sizeof(uint32_t) <= length; i++) {
uint32_t msg_id;
2022-10-03 23:33:08 +02:00
2022-10-01 01:38:42 +02:00
// HACK: little endian
std::copy(data+curser, data+curser+sizeof(uint32_t), reinterpret_cast<uint8_t*>(&msg_id));
curser += sizeof(uint32_t);
2022-10-03 23:33:08 +02:00
//message_ids.push_back(msg_id);
2022-10-01 01:38:42 +02:00
fprintf(stderr, " %08X", msg_id);
if (peer.hear(msg_id, peer_number)) {
fprintf(stderr, " - NEW");
}
fprintf(stderr, "\n");
}
2022-10-03 23:33:08 +02:00
// TODO: replace, remote crash
assert(curser == length);
2022-09-26 20:55:58 +02:00
}
2022-09-30 02:04:25 +02:00
#undef _HS1_HAVE
2022-10-03 01:15:49 +02:00