Skip to content

Commit

Permalink
workaround for message number saving (fixes #961)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
zugz committed Jan 22, 2020
1 parent e6cbe90 commit 886b9a7
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions toxcore/group.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 886b9a7

Please sign in to comment.