Fix Congestion in Takion and add Test

This commit is contained in:
Florian Märkl 2020-11-18 11:55:26 +01:00
commit ef7a97f6ae
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
3 changed files with 67 additions and 19 deletions

View file

@ -58,8 +58,8 @@ typedef ChiakiErrorCode (*ChiakiTakionAVPacketParse)(ChiakiTakionAVPacket *packe
typedef struct chiaki_takion_congestion_packet_t
{
uint16_t word_0;
uint16_t word_1;
uint16_t word_2;
uint16_t received;
uint16_t lost;
} ChiakiTakionCongestionPacket;
@ -167,11 +167,14 @@ CHIAKI_EXPORT void chiaki_takion_close(ChiakiTakion *takion);
/**
* Must be called from within the Takion thread, i.e. inside the callback!
*/
static inline void chiaki_takion_set_crypt(ChiakiTakion *takion, ChiakiGKCrypt *gkcrypt_local, ChiakiGKCrypt *gkcrypt_remote) {
static inline void chiaki_takion_set_crypt(ChiakiTakion *takion, ChiakiGKCrypt *gkcrypt_local, ChiakiGKCrypt *gkcrypt_remote)
{
takion->gkcrypt_local = gkcrypt_local;
takion->gkcrypt_remote = gkcrypt_remote;
}
CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_packet_mac(ChiakiGKCrypt *crypt, uint8_t *buf, size_t buf_size, uint64_t key_pos, uint8_t *mac_out, uint8_t *mac_old_out);
/**
* Get a new key pos and advance by data_size.
*
@ -230,6 +233,10 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_v7_av_packet_format_header(uint8_t *
CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_v7_av_packet_parse(ChiakiTakionAVPacket *packet, ChiakiKeyState *key_state, uint8_t *buf, size_t buf_size);
#define CHIAKI_TAKION_CONGESTION_PACKET_SIZE 0xf
CHIAKI_EXPORT void chiaki_takion_format_congestion(uint8_t *buf, ChiakiTakionCongestionPacket *packet, uint64_t key_pos);
#ifdef __cplusplus
}
#endif

View file

@ -361,7 +361,7 @@ static ChiakiErrorCode chiaki_takion_packet_read_key_pos(ChiakiTakion *takion, u
return CHIAKI_ERR_SUCCESS;
}
static ChiakiErrorCode chiaki_takion_packet_mac(ChiakiGKCrypt *crypt, uint8_t *buf, size_t buf_size, uint64_t key_pos, uint8_t *mac_out, uint8_t *mac_old_out)
CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_packet_mac(ChiakiGKCrypt *crypt, uint8_t *buf, size_t buf_size, uint64_t key_pos, uint8_t *mac_out, uint8_t *mac_old_out)
{
if(buf_size < 1)
return CHIAKI_ERR_BUF_TOO_SMALL;
@ -383,17 +383,18 @@ static ChiakiErrorCode chiaki_takion_packet_mac(ChiakiGKCrypt *crypt, uint8_t *b
if(crypt)
{
uint8_t key_pos_tmp[sizeof(uint32_t)];
if(base_type == TAKION_PACKET_TYPE_CONTROL)
if(base_type == TAKION_PACKET_TYPE_CONTROL || base_type == TAKION_PACKET_TYPE_CONGESTION)
{
memcpy(key_pos_tmp, buf + key_pos_offset, sizeof(uint32_t));
memset(buf + key_pos_offset, 0, sizeof(uint32_t));
}
chiaki_gkcrypt_gmac(crypt, key_pos, buf, buf_size, buf + mac_offset);
if(base_type == TAKION_PACKET_TYPE_CONTROL)
if(base_type == TAKION_PACKET_TYPE_CONTROL || base_type == TAKION_PACKET_TYPE_CONGESTION)
memcpy(buf + key_pos_offset, key_pos_tmp, sizeof(uint32_t));
}
memcpy(mac_out, buf + mac_offset, CHIAKI_GKCRYPT_GMAC_SIZE);
if(mac_out)
memcpy(mac_out, buf + mac_offset, CHIAKI_GKCRYPT_GMAC_SIZE);
return CHIAKI_ERR_SUCCESS;
}
@ -484,23 +485,25 @@ static ChiakiErrorCode chiaki_takion_send_message_data_ack(ChiakiTakion *takion,
return chiaki_takion_send(takion, buf, sizeof(buf), key_pos);
}
CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_send_congestion(ChiakiTakion *takion, ChiakiTakionCongestionPacket *packet)
CHIAKI_EXPORT void chiaki_takion_format_congestion(uint8_t *buf, ChiakiTakionCongestionPacket *packet, uint64_t key_pos)
{
uint8_t buf[0xf];
memset(buf, 0, sizeof(buf));
buf[0] = TAKION_PACKET_TYPE_CONGESTION;
*((chiaki_unaligned_uint16_t *)(buf + 1)) = htons(packet->word_0);
*((chiaki_unaligned_uint16_t *)(buf + 3)) = htons(packet->word_1);
*((chiaki_unaligned_uint16_t *)(buf + 5)) = htons(packet->word_2);
*((chiaki_unaligned_uint16_t *)(buf + 3)) = htons(packet->received);
*((chiaki_unaligned_uint16_t *)(buf + 5)) = htons(packet->lost);
*((chiaki_unaligned_uint32_t *)(buf + 7)) = 0;
*((chiaki_unaligned_uint32_t *)(buf + 0xb)) = htonl((uint32_t)key_pos);
}
CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_send_congestion(ChiakiTakion *takion, ChiakiTakionCongestionPacket *packet)
{
uint64_t key_pos;
ChiakiErrorCode err = chiaki_takion_crypt_advance_key_pos(takion, sizeof(buf), &key_pos);
ChiakiErrorCode err = chiaki_takion_crypt_advance_key_pos(takion, CHIAKI_TAKION_CONGESTION_PACKET_SIZE, &key_pos);
if(err != CHIAKI_ERR_SUCCESS)
return err;
*((chiaki_unaligned_uint32_t *)(buf + 0xb)) = htonl((uint32_t)key_pos); // TODO: is this correct? shouldn't key_pos be 0 for mac calculation?
//chiaki_log_hexdump(takion->log, CHIAKI_LOG_DEBUG, buf, sizeof(buf));
uint8_t buf[CHIAKI_TAKION_CONGESTION_PACKET_SIZE];
chiaki_takion_format_congestion(buf, packet, key_pos);
return chiaki_takion_send(takion, buf, sizeof(buf), key_pos);
}
@ -675,9 +678,6 @@ static void *takion_thread_func(void *user)
if(chiaki_takion_send_buffer_init(&takion->send_buffer, takion, TAKION_SEND_BUFFER_SIZE) != CHIAKI_ERR_SUCCESS)
goto error_reoder_queue;
// TODO ChiakiCongestionControl congestion_control;
// if(chiaki_congestion_control_start(&congestion_control, takion) != CHIAKI_ERR_SUCCESS)
// goto beach;
if(takion->cb)
{