tomato/toxav/bwcontroller.c
Green Sky 3b6bb15e86 Squashed 'external/toxcore/c-toxcore/' changes from 11ab1d2a723..d9b8fa6098d
d9b8fa6098d fix: Fake broadcast address for 127.x.x.x
aa649165a57 chore: Add code for future netprof TCP testing
9e5693de5ac chore: add to_string functions for netprof enums
52d915e6a90 cleanup: Heap allocate network profile objects
80fabd4a729 feat: Implement Tox network profiler
05abe083cb6 cleanup: Some random cleanups, mostly related to mem.
5cca24513b8 cleanup: Check that onion IP/Port packing worked.
e092ecd1244 cleanup: Use tox memory allocator in some more places.
3cfe41c7587 fix: Avoid `memcpy`-ing structs into onion ping id data.
e32ac001938 fix: Add more information on why the frame was not sent.
ab887003687 fix: Allow TCP connections to fail `connect` calls.
7603170e663 refactor: Use tox memory in group connection allocations.
5bd8a85eb89 cleanup: Align internal logger with external on type of source line.
e9bf524d9e1 cleanup: Add missing `#include` to sort_test.cc.
d10c966b998 feat: Add `to_string` functions for toxencryptsave errors.
7bfd0dc8003 docs: Update the docs for group join functions
380dde9f2ae test: Add more logging to TCP connection constructor.
0f12f384c8c cleanup: Reduce stack frame sizes to below 4096 bytes.
bc43cec0626 chore: Happy new year!
fbe78f1702e cleanup: Add a `TOX_HIDE_DEPRECATED` check to hide deprecated symbols.
44d9da07e77 refactor: Use tox memory for group moderation/pack allocations.
7f26d520168 refactor: Use tox memory in group chats allocations.
2f62f3d0e77 refactor: Use tox Memory for group allocations.
8a968162041 chore: Add dispatch/events headers to bazel export.
2bbfb35abf6 docs: Output the error code string instead of int. in toxav logging
d55d0e4eaef cleanup: Remove redundant code for checking if group exists
2a6dc643338 chore: Upgrade dependencies for websockify.
fc0650601c1 fix: Allow peers to reconnect to group chats using a password

git-subtree-dir: external/toxcore/c-toxcore
git-subtree-split: d9b8fa6098de6c074038b6664d2572627540b148
2025-01-18 15:53:06 +01:00

250 lines
7.1 KiB
C

/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2016-2025 The TokTok team.
* Copyright © 2013-2015 Tox project.
*/
#include "bwcontroller.h"
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "ring_buffer.h"
#include "toxav_hacks.h"
#include "../toxcore/ccompat.h"
#include "../toxcore/logger.h"
#include "../toxcore/mono_time.h"
#include "../toxcore/network.h"
#include "../toxcore/tox_private.h"
#include "../toxcore/util.h"
#define BWC_PACKET_ID 196
#define BWC_SEND_INTERVAL_MS 950 // 0.95s
#define BWC_AVG_PKT_COUNT 20
#define BWC_AVG_LOSS_OVER_CYCLES_COUNT 30
typedef struct BWCCycle {
uint32_t last_recv_timestamp; /* Last recv update time stamp */
uint32_t last_sent_timestamp; /* Last sent update time stamp */
uint32_t last_refresh_timestamp; /* Last refresh time stamp */
uint32_t lost;
uint32_t recv;
} BWCCycle;
typedef struct BWCRcvPkt {
uint32_t packet_length_array[BWC_AVG_PKT_COUNT];
RingBuffer *rb;
} BWCRcvPkt;
struct BWController {
m_cb *mcb;
void *mcb_user_data;
Tox *tox;
const Logger *log;
uint32_t friend_number;
BWCCycle cycle;
BWCRcvPkt rcvpkt; /* To calculate average received packet (this means split parts, not the full message!) */
uint32_t packet_loss_counted_cycles;
Mono_Time *bwc_mono_time;
bool bwc_receive_active; /* if this is set to false then incoming bwc packets will not be processed by bwc_handle_data() */
};
struct BWCMessage {
uint32_t lost;
uint32_t recv;
};
static void bwc_handle_data(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, void *user_data);
static void send_update(BWController *bwc);
BWController *bwc_new(const Logger *log, Tox *tox, uint32_t friendnumber, m_cb *mcb, void *mcb_user_data,
Mono_Time *bwc_mono_time)
{
BWController *retu = (BWController *)calloc(1, sizeof(BWController));
if (retu == nullptr) {
return nullptr;
}
LOGGER_DEBUG(log, "Creating bandwidth controller");
retu->mcb = mcb;
retu->mcb_user_data = mcb_user_data;
retu->friend_number = friendnumber;
retu->bwc_mono_time = bwc_mono_time;
const uint64_t now = current_time_monotonic(bwc_mono_time);
retu->cycle.last_sent_timestamp = now;
retu->cycle.last_refresh_timestamp = now;
retu->tox = tox;
retu->log = log;
retu->bwc_receive_active = true;
retu->rcvpkt.rb = rb_new(BWC_AVG_PKT_COUNT);
retu->cycle.lost = 0;
retu->cycle.recv = 0;
retu->packet_loss_counted_cycles = 0;
/* Fill with zeros */
for (int i = 0; i < BWC_AVG_PKT_COUNT; ++i) {
rb_write(retu->rcvpkt.rb, &retu->rcvpkt.packet_length_array[i]);
}
return retu;
}
void bwc_kill(BWController *bwc)
{
if (bwc == nullptr) {
return;
}
rb_kill(bwc->rcvpkt.rb);
free(bwc);
}
void bwc_add_lost(BWController *bwc, uint32_t bytes_lost)
{
if (bwc == nullptr) {
return;
}
if (bytes_lost > 0) {
LOGGER_DEBUG(bwc->log, "BWC lost(1): %d", (int)bytes_lost);
bwc->cycle.lost += bytes_lost;
send_update(bwc);
}
}
void bwc_add_recv(BWController *bwc, uint32_t recv_bytes)
{
if (bwc == nullptr || recv_bytes == 0) {
return;
}
++bwc->packet_loss_counted_cycles;
bwc->cycle.recv += recv_bytes;
send_update(bwc);
}
static void send_update(BWController *bwc)
{
if (bwc->packet_loss_counted_cycles > BWC_AVG_LOSS_OVER_CYCLES_COUNT &&
current_time_monotonic(bwc->bwc_mono_time) - bwc->cycle.last_sent_timestamp > BWC_SEND_INTERVAL_MS) {
bwc->packet_loss_counted_cycles = 0;
if (bwc->cycle.lost != 0) {
LOGGER_DEBUG(bwc->log, "%p Sent update rcv: %u lost: %u percent: %f %%",
(void *)bwc, bwc->cycle.recv, bwc->cycle.lost,
((double)bwc->cycle.lost / (bwc->cycle.recv + bwc->cycle.lost)) * 100.0);
uint8_t bwc_packet[sizeof(struct BWCMessage) + 1];
size_t offset = 0;
bwc_packet[offset] = BWC_PACKET_ID; // set packet ID
++offset;
offset += net_pack_u32(bwc_packet + offset, bwc->cycle.lost);
offset += net_pack_u32(bwc_packet + offset, bwc->cycle.recv);
assert(offset == sizeof(bwc_packet));
Tox_Err_Friend_Custom_Packet error;
tox_friend_send_lossy_packet(bwc->tox, bwc->friend_number, bwc_packet, sizeof(bwc_packet), &error);
if (error != TOX_ERR_FRIEND_CUSTOM_PACKET_OK) {
LOGGER_WARNING(bwc->log, "BWC send failed: %d", error);
}
}
bwc->cycle.last_sent_timestamp = current_time_monotonic(bwc->bwc_mono_time);
bwc->cycle.lost = 0;
bwc->cycle.recv = 0;
}
}
static int on_update(BWController *bwc, const struct BWCMessage *msg)
{
LOGGER_DEBUG(bwc->log, "%p Got update from peer", (void *)bwc);
/* Peers sent update too soon */
if (bwc->cycle.last_recv_timestamp + BWC_SEND_INTERVAL_MS > current_time_monotonic(bwc->bwc_mono_time)) {
LOGGER_INFO(bwc->log, "%p Rejecting extra update", (void *)bwc);
return -1;
}
bwc->cycle.last_recv_timestamp = current_time_monotonic(bwc->bwc_mono_time);
const uint32_t lost = msg->lost;
if (lost != 0 && bwc->mcb != nullptr) {
const uint32_t recv = msg->recv;
LOGGER_DEBUG(bwc->log, "recved: %u lost: %u percentage: %f %%", recv, lost,
((double) lost / (recv + lost)) * 100.0);
bwc->mcb(bwc, bwc->friend_number,
(float)lost / (recv + lost),
bwc->mcb_user_data);
}
return 0;
}
static void bwc_handle_data(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, void *user_data)
{
/* get BWController object from Tox and friend number */
ToxAV *toxav = (ToxAV *)tox_get_av_object(tox);
if (toxav == nullptr) {
// LOGGER_ERROR(log, "Could not get ToxAV object from Tox");
return;
}
const Logger *log = toxav_get_logger(toxav);
if (length - 1 != sizeof(struct BWCMessage)) {
LOGGER_ERROR(log, "Got BWCMessage of insufficient size.");
return;
}
const ToxAVCall *call = call_get(toxav, friend_number);
if (call == nullptr) {
LOGGER_ERROR(log, "Could not get ToxAVCall object from ToxAV.");
return;
}
/* get Call object from Tox and friend number */
BWController *bwc = bwc_controller_get(call);
if (bwc == nullptr) {
LOGGER_WARNING(log, "No session!");
return;
}
if (!bwc->bwc_receive_active) {
LOGGER_WARNING(log, "receiving not allowed!");
return;
}
size_t offset = 1; // Ignore packet id.
struct BWCMessage msg;
offset += net_unpack_u32(data + offset, &msg.lost);
offset += net_unpack_u32(data + offset, &msg.recv);
assert(offset == length);
on_update(bwc, &msg);
}
void bwc_allow_receiving(Tox *tox)
{
tox_callback_friend_lossy_packet_per_pktid(tox, bwc_handle_data, BWC_PACKET_ID);
}
void bwc_stop_receiving(Tox *tox)
{
tox_callback_friend_lossy_packet_per_pktid(tox, nullptr, BWC_PACKET_ID);
}