From c5bb81c0155fee24fade1de638e8db74e18f9c97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Sat, 29 Jun 2019 16:56:49 +0200 Subject: [PATCH] Add Feedback State to Takion --- lib/CMakeLists.txt | 6 +++-- lib/include/chiaki/feedback.h | 48 +++++++++++++++++++++++++++++++++++ lib/include/chiaki/takion.h | 7 +++++ lib/src/feedback.c | 45 ++++++++++++++++++++++++++++++++ lib/src/takion.c | 36 +++++++++++++++++++++++++- 5 files changed, 139 insertions(+), 3 deletions(-) create mode 100644 lib/include/chiaki/feedback.h create mode 100644 lib/src/feedback.c diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index ad1c261..6821ea3 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -26,7 +26,8 @@ set(HEADER_FILES include/chiaki/congestioncontrol.h include/chiaki/stoppipe.h include/chiaki/reorderqueue.h - include/chiaki/discoveryservice.h) + include/chiaki/discoveryservice.h + include/chiaki/feedback.h) set(SOURCE_FILES src/common.c @@ -55,7 +56,8 @@ set(SOURCE_FILES src/congestioncontrol.c src/stoppipe.c src/reorderqueue.c - src/discoveryservice.c) + src/discoveryservice.c + src/feedback.c) add_subdirectory(protobuf) include_directories("${NANOPB_SOURCE_DIR}") diff --git a/lib/include/chiaki/feedback.h b/lib/include/chiaki/feedback.h new file mode 100644 index 0000000..2464eec --- /dev/null +++ b/lib/include/chiaki/feedback.h @@ -0,0 +1,48 @@ +/* + * This file is part of Chiaki. + * + * Chiaki is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Chiaki is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Chiaki. If not, see . + */ + +#ifndef CHIAKI_FEEDBACK_H +#define CHIAKI_FEEDBACK_H + +#include "common.h" + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct chiaki_feedback_state_t +{ + uint16_t left_x; + uint16_t left_y; + uint16_t right_x; + uint16_t right_y; +} ChiakiFeedbackState; + +#define CHIAKI_FEEDBACK_STATE_BUF_SIZE 0x19 + +/** + * @param buf buffer of at least CHIAKI_FEEDBACK_STATE_BUF_SIZE + */ +CHIAKI_EXPORT void chiaki_feedback_state_format(uint8_t *buf, ChiakiFeedbackState *state); + +#ifdef __cplusplus +} +#endif + +#endif // CHIAKI_FEEDBACK_H diff --git a/lib/include/chiaki/takion.h b/lib/include/chiaki/takion.h index 3c17ebe..b788ad1 100644 --- a/lib/include/chiaki/takion.h +++ b/lib/include/chiaki/takion.h @@ -25,6 +25,7 @@ #include "seqnum.h" #include "stoppipe.h" #include "reorderqueue.h" +#include "feedback.h" #include #include @@ -199,6 +200,12 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_send_message_data(ChiakiTakion *taki */ CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_send_congestion(ChiakiTakion *takion, ChiakiTakionCongestionPacket *packet); +/** + * Thread-safe while Takion is running. + */ +CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_send_feedback_state(ChiakiTakion *takion, ChiakiSeqNum16 seq_num, ChiakiFeedbackState *feedback_state); + + CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_av_packet_parse(ChiakiTakionAVPacket *packet, uint8_t base_type, uint8_t *buf, size_t buf_size); #ifdef __cplusplus diff --git a/lib/src/feedback.c b/lib/src/feedback.c new file mode 100644 index 0000000..abe00b1 --- /dev/null +++ b/lib/src/feedback.c @@ -0,0 +1,45 @@ +/* + * This file is part of Chiaki. + * + * Chiaki is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Chiaki is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Chiaki. If not, see . + */ + +#include + +#include + +CHIAKI_EXPORT void chiaki_feedback_state_format(uint8_t *buf, ChiakiFeedbackState *state) +{ + buf[0x0] = 0xa0; // TODO + buf[0x1] = 0xff; // TODO + buf[0x2] = 0x7f; // TODO + buf[0x3] = 0xff; // TODO + buf[0x4] = 0x7f; // TODO + buf[0x5] = 0xff; // TODO + buf[0x6] = 0x7f; // TODO + buf[0x7] = 0xff; // TODO + buf[0x8] = 0x7f; // TODO + buf[0x9] = 0x99; // TODO + buf[0xa] = 0x99; // TODO + buf[0xb] = 0xff; // TODO + buf[0xc] = 0x7f; // TODO + buf[0xd] = 0xfe; // TODO + buf[0xe] = 0xf7; // TODO + buf[0xf] = 0xef; // TODO + buf[0x10] = 0x1f; // TODO + *((uint16_t *)(buf + 0x11)) = htons(state->left_x); + *((uint16_t *)(buf + 0x13)) = htons(state->left_y); + *((uint16_t *)(buf + 0x15)) = htons(state->right_x); + *((uint16_t *)(buf + 0x17)) = htons(state->right_y); +} \ No newline at end of file diff --git a/lib/src/takion.c b/lib/src/takion.c index 9f5d015..1d785d8 100644 --- a/lib/src/takion.c +++ b/lib/src/takion.c @@ -396,7 +396,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_send_congestion(ChiakiTakion *takion ChiakiErrorCode err = chiaki_mutex_lock(&takion->gkcrypt_local_mutex); if(err != CHIAKI_ERR_SUCCESS) return err; - *((uint32_t *)(buf + 0xb)) = htonl((uint32_t)takion->key_pos_local); + *((uint32_t *)(buf + 0xb)) = htonl((uint32_t)takion->key_pos_local); // TODO: is this correct? shouldn't key_pos be 0 for mac calculation? err = chiaki_gkcrypt_gmac(takion->gkcrypt_local, takion->key_pos_local, buf, sizeof(buf), buf + 7); takion->key_pos_local += sizeof(buf); chiaki_mutex_unlock(&takion->gkcrypt_local_mutex); @@ -408,6 +408,40 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_send_congestion(ChiakiTakion *takion return chiaki_takion_send_raw(takion, buf, sizeof(buf)); } +CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_send_feedback_state(ChiakiTakion *takion, ChiakiSeqNum16 seq_num, ChiakiFeedbackState *feedback_state) +{ + uint8_t buf[0xc + CHIAKI_FEEDBACK_STATE_BUF_SIZE]; + buf[0] = TAKION_PACKET_TYPE_FEEDBACK_STATE; + *((uint16_t *)(buf + 1)) = htons(seq_num); + buf[3] = 0; // TODO + *((uint32_t *)(buf + 4)) = 0; // key pos + *((uint32_t *)(buf + 8)) = 0; // gmac + chiaki_feedback_state_format(buf + 0xc, feedback_state); + + ChiakiErrorCode err = chiaki_mutex_lock(&takion->gkcrypt_local_mutex); + if(err != CHIAKI_ERR_SUCCESS) + return err; + + size_t key_pos; + err = chiaki_takion_crypt_advance_key_pos(takion, CHIAKI_FEEDBACK_STATE_BUF_SIZE, &key_pos); + if(err != CHIAKI_ERR_SUCCESS) + goto beach; + + err = chiaki_gkcrypt_encrypt(takion->gkcrypt_local, key_pos, buf + 0xc, CHIAKI_FEEDBACK_STATE_BUF_SIZE); + if(err != CHIAKI_ERR_SUCCESS) + goto beach; + + err = chiaki_gkcrypt_gmac(takion->gkcrypt_local, key_pos, buf, sizeof(buf), buf + 4); + if(err != CHIAKI_ERR_SUCCESS) + goto beach; + + *((uint32_t *)(buf + 4)) = htonl((uint32_t)key_pos); + +beach: + chiaki_mutex_unlock(&takion->gkcrypt_local_mutex); + return err; +} + static ChiakiErrorCode takion_handshake(ChiakiTakion *takion) { ChiakiErrorCode err;