From 4033cfb0bd5339f97f830bd4fc591565ff8a7449 Mon Sep 17 00:00:00 2001 From: romain Date: Sun, 1 Oct 2023 19:16:04 +0200 Subject: [PATCH] NITWORK: Add check of header magick keys PATCH --- src/Nitwork/ANitwork.cpp | 9 +++++++-- src/Nitwork/Nitwork.h | 3 +++ src/main_client.cpp | 13 +++++++------ 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/Nitwork/ANitwork.cpp b/src/Nitwork/ANitwork.cpp index deb5acbb..2b5577d8 100644 --- a/src/Nitwork/ANitwork.cpp +++ b/src/Nitwork/ANitwork.cpp @@ -118,13 +118,18 @@ namespace Nitwork { return; } try { - auto *header = - reinterpret_cast(_receiveBuffer.data()); if (bytes_received < sizeof(struct header_s)) { std::cerr << "Error: header not received" << std::endl; startReceiveHandler(); return; } + auto *header = + reinterpret_cast(_receiveBuffer.data()); + if (header->magick1 != HEADER_CODE1 || header->magick2 != HEADER_CODE2) { + std::cerr << "Error: header magick not valid" << std::endl; + startReceiveHandler(); + return; + } if (header->nb_action > MAX_NB_ACTION || header->nb_action < 0) { std::cerr << "Error: too many actions received or no action" << std::endl; diff --git a/src/Nitwork/Nitwork.h b/src/Nitwork/Nitwork.h index 20d61a99..0df6f207 100644 --- a/src/Nitwork/Nitwork.h +++ b/src/Nitwork/Nitwork.h @@ -14,6 +14,9 @@ #define DEFAULT_THREAD_NB 4 #define MAX_NB_ACTION 16 #define MAX_CLIENTS 4 + #define HEADER_CODE1 '\x01' + #define HEADER_CODE2 '\x03' + typedef char n_magick_t; typedef int n_idsReceived_t; diff --git a/src/main_client.cpp b/src/main_client.cpp index 3849a26a..13bcd449 100644 --- a/src/main_client.cpp +++ b/src/main_client.cpp @@ -8,20 +8,19 @@ #include struct packetData_s { - struct header_s header; struct action_s action; - struct msgInit_s msgInit; + struct header_s header; } __attribute__((packed)); int main() { struct header_s header = { - .magick1 = 0x42, + .magick1 = HEADER_CODE1, .ids_received = 0, .last_id_received = 0, .id = 0, .nb_action = 1, - .magick2 = 0x42, + .magick2 = HEADER_CODE1, }; struct action_s action = { .magick = INIT, @@ -29,10 +28,12 @@ int main() struct msgInit_s msgInit = { .magick = INIT, }; + struct msgReady_s msgReady = { + .magick = READY, + }; struct packetData_s packetData = { - .header = header, .action = action, - .msgInit = msgInit, + .header = header, }; const int port = 4242; // Remplacez par le port du serveur