From 886b9a7019954a0b002ba10d260376e4e917a1bb Mon Sep 17 00:00:00 2001 From: "zugz (tox)" Date: Tue, 24 Dec 2019 00:00:00 +0000 Subject: workaround for message number saving (fixes #961) Put a future message number into the save file. Peers require the message numbers of messages we send to increase monotonically. If we save the current message number, then send further messages, then quit without saving (e.g. due to a crash), and then resume from the old save data, then monotonicity will fail. This commit works around this problem by introducing an offset when the current message number, so that even in the above circumstance, as long as fewer messages than the offset were sent between saving and reloading, the sent message numbers will increase monotonically. The choice of offset is a balance between wanting it to be large enough that there is room for plenty of messages to be sent in the above scenario, and wanting to avoid the following potential problem: if we repeatedly save and reload without sending any further messages, then the message number may increase so far that peers will interpret an eventual message as being old. This is not conceivably a practical issue for the 32bit lossless message numbers, but is a concern for the 16bit lossy message numbers. --- toxcore/group.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/toxcore/group.c b/toxcore/group.c index 987e2fbd..f2061d7c 100644 --- a/toxcore/group.c +++ b/toxcore/group.c @@ -3209,6 +3209,11 @@ static uint32_t saved_conf_size(const Group_c *g) return len; } +/* Save a future message number; the save will remain valid until we have sent + * this many more messages. */ +#define SAVE_OFFSET_MESSAGE_NUMBER (1 << 16) +#define SAVE_OFFSET_LOSSY_MESSAGE_NUMBER (1 << 13) + static uint8_t *save_conf(const Group_c *g, uint8_t *data) { *data = g->type; @@ -3217,10 +3222,10 @@ static uint8_t *save_conf(const Group_c *g, uint8_t *data) memcpy(data, g->id, GROUP_ID_LENGTH); data += GROUP_ID_LENGTH; - host_to_lendian_bytes32(data, g->message_number); + host_to_lendian_bytes32(data, g->message_number + SAVE_OFFSET_MESSAGE_NUMBER); data += sizeof(uint32_t); - host_to_lendian_bytes16(data, g->lossy_message_number); + host_to_lendian_bytes16(data, g->lossy_message_number + SAVE_OFFSET_LOSSY_MESSAGE_NUMBER); data += sizeof(uint16_t); host_to_lendian_bytes16(data, g->peer_number); -- cgit v1.2.3