summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzugz (tox) <mbays+tox@sdf.org>2019-12-24 00:00:00 +0000
committerzugz (tox) <mbays+tox@sdf.org>2020-01-22 00:00:02 +0000
commit886b9a7019954a0b002ba10d260376e4e917a1bb (patch)
tree7add896bc160e77d0add9fbc7d1afe017f7735e3
parente6cbe907ac9605de2ca9294ac15dbfce16991514 (diff)
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.
-rw-r--r--toxcore/group.c9
1 files 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)
3209 return len; 3209 return len;
3210} 3210}
3211 3211
3212/* Save a future message number; the save will remain valid until we have sent
3213 * this many more messages. */
3214#define SAVE_OFFSET_MESSAGE_NUMBER (1 << 16)
3215#define SAVE_OFFSET_LOSSY_MESSAGE_NUMBER (1 << 13)
3216
3212static uint8_t *save_conf(const Group_c *g, uint8_t *data) 3217static uint8_t *save_conf(const Group_c *g, uint8_t *data)
3213{ 3218{
3214 *data = g->type; 3219 *data = g->type;
@@ -3217,10 +3222,10 @@ static uint8_t *save_conf(const Group_c *g, uint8_t *data)
3217 memcpy(data, g->id, GROUP_ID_LENGTH); 3222 memcpy(data, g->id, GROUP_ID_LENGTH);
3218 data += GROUP_ID_LENGTH; 3223 data += GROUP_ID_LENGTH;
3219 3224
3220 host_to_lendian_bytes32(data, g->message_number); 3225 host_to_lendian_bytes32(data, g->message_number + SAVE_OFFSET_MESSAGE_NUMBER);
3221 data += sizeof(uint32_t); 3226 data += sizeof(uint32_t);
3222 3227
3223 host_to_lendian_bytes16(data, g->lossy_message_number); 3228 host_to_lendian_bytes16(data, g->lossy_message_number + SAVE_OFFSET_LOSSY_MESSAGE_NUMBER);
3224 data += sizeof(uint16_t); 3229 data += sizeof(uint16_t);
3225 3230
3226 host_to_lendian_bytes16(data, g->peer_number); 3231 host_to_lendian_bytes16(data, g->peer_number);