From e8d9782ebcbf4bc466ca4762de4ee8dd6fb66f7a Mon Sep 17 00:00:00 2001 From: shr3dd3r Date: Sat, 28 Oct 2023 04:54:14 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B4=D0=BE=D0=BB=D0=B6?= =?UTF-8?q?=D0=B0=D0=B5=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++- Makefile | 2 +- src/Event.cpp | 20 ++++++++++++++------ src/Event.hpp | 40 ++++++++++++++++++++++++---------------- src/KLDR.hpp | 18 ++++++++++++------ src/Worker.cpp | 19 +++++++++---------- src/Worker.hpp | 15 +++++++++++---- 7 files changed, 73 insertions(+), 44 deletions(-) diff --git a/.gitignore b/.gitignore index be9f38c..a528fba 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ src/test.cpp libstadium.so libstadiumtest_release libstadiumtest_sanitizer -libstadiumtest_fortified \ No newline at end of file +libstadiumtest_fortified +pseudo_code.md \ No newline at end of file diff --git a/Makefile b/Makefile index 4907b42..ef19e45 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # libstadium makefile CC = g++ -CFLAGS_DEFAULT = -march=native -std=c++23 +CFLAGS_DEFAULT = -march=native -std=c++23 -fno-rtti CFLAGS_DEBUG = -ggdb -Wall -Werror -Wno-reorder -Wno-uninitialized -fanalyzer -Wno-analyzer-use-of-uninitialized-value #CFLAGS_DEBUG_S = -O0 -fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow CFLAGS_DEBUG_F = -O1 -D_FORTIFY_SOURCE=3 diff --git a/src/Event.cpp b/src/Event.cpp index b5dcc01..2ab4738 100644 --- a/src/Event.cpp +++ b/src/Event.cpp @@ -17,11 +17,6 @@ Event::Event () { this->Type.Category = this->Type.Subcategory = this->AsyncID = 0; } -// Ctor with parsing of raw char array -Event::Event (const char* data, size_t data_sz) { - // TODO -} - // Ctor with positional arguments Event::Event (EventType type, KLDRArray<> data, uint16_t id = 0) : Type(std::move(type)), Payload(std::move(data)), AsyncID(id) { // TODO: calculate hash @@ -34,9 +29,22 @@ Event::Event (uint8_t cat, uint8_t subcat, KLDRArray<> data, uint16_t id = 0) : // TODO: calculate hash } +// Parse raw data and fill fields from it +void +Event::MakeFrom (const char *data, size_t data_sz) { // TODO: supply hash type/size (???) + if (data_sz < MIN_RAW_EVENT_SIZE) + throw std::invalid_argument("raw event size cant be less than 21 bytes"); + + this->Type.Category = data[0]; + this->Type.Subcategory = data[1]; + this->AsyncID = *reinterpret_cast(&data[2]); + // TODO: parse hash + // TODO: parse payload to KLDRArray +} + // Write to stdout info about event void -Event::Print () { +Event::Print () const { printf( "[Event] Object at: %lu; Category: 0x%.2hhx; Subcategory: 0x%.2hhx; AsyncID: %hu; Payload size: cells: %lu, bytes:%lu\n", // TODO: add hash reinterpret_cast(this), diff --git a/src/Event.hpp b/src/Event.hpp index 628e023..283b36b 100644 --- a/src/Event.hpp +++ b/src/Event.hpp @@ -11,10 +11,16 @@ #include #include +#include +#include #include "KLDR.hpp" +#define MIN_RAW_EVENT_SIZE 21 + + + namespace Stadium::v1 { @@ -28,31 +34,33 @@ struct EventType { uint8_t Subcategory; }; + + +// Get stringized name of event +std::string ResolveEventType (EventType); + + + // Event class -class Event { - public: - uint32_t Lifetime = 0; // Lifetime of event in seconds - - private: - // These are the "physical" members that will be packed to binary - std::vector Hash; - // CryptoAlgo HashAlgo; // TODO - KLDRArray<> Payload; +class Event final { + uint32_t Lifetime = 0; // Lifetime of event in seconds + // CryptoAlgo HashAlgo; // TODO + // std::vector Hash; + KLDRArray<> Payload; + EventType Type; + EventAsyncID AsyncID; public: - EventType Type; - EventAsyncID AsyncID; - - Event (); - Event (const char*, size_t); + Event ();//CryptoAlgo); Event (EventType, KLDRArray<>, uint16_t); Event (uint8_t, uint8_t, KLDRArray<>, uint16_t); - void Print (); + void MakeFrom (const char *, size_t); + void Print () const; }; -} +} // namespace Stadium::v1 diff --git a/src/KLDR.hpp b/src/KLDR.hpp index 6137605..f5781b0 100644 --- a/src/KLDR.hpp +++ b/src/KLDR.hpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -21,7 +22,7 @@ namespace Stadium::v1 { -enum KLDRDefaultKeys : uint8_t { +enum KLDRDefaultKeys : uint8_t { // TODO: think about better way (maybe std::map or smt like that) Data = 0x01, // Main data of event ObjectID = 0x02, // ID of object in local context SrcEventAuthor = 0x03, // Source author of event @@ -30,7 +31,7 @@ enum KLDRDefaultKeys : uint8_t { BatchNumber = 0x06, // Number of event in chain of events Path = 0x07, // Path to requested/uploaded resource Power = 0x08, // Access right to object - ServerSession = 0x09, // ID of server-side session + //ServerSession = 0x09, // ID of server-side session // Crypto-related CryptoAlgos = 0x11, // Array of used cryptography algos CryptoKeyID = 0x12, // ID of crypto key used for data encryption in this event @@ -39,6 +40,11 @@ enum KLDRDefaultKeys : uint8_t { +// Get stringized name of KLDR key +std::string ResolveKLDRKey (KLDRDefaultKeys); // TODO: this is can be better + + + /* * KLDR stands for "Key-Length-Data-Repeat". * Schematically, binary array with KLDR-formatted data looks like this. @@ -52,7 +58,7 @@ enum KLDRDefaultKeys : uint8_t { * This can be more optimized for performance with cost of more memory usage. */ template -class KLDRArray { +class KLDRArray final { private: std::vector Keys; std::vector Lengths; @@ -178,9 +184,9 @@ class KLDRArray { case v1::KLDRDefaultKeys::Power: printf("Power"); break; - case v1::KLDRDefaultKeys::ServerSession: - printf("ServerSession"); - break; + // case v1::KLDRDefaultKeys::ServerSession: + // printf("ServerSession"); + // break; case v1::KLDRDefaultKeys::CryptoAlgos: printf("CryptoAlgos"); break; diff --git a/src/Worker.cpp b/src/Worker.cpp index 1a5c617..249f27c 100644 --- a/src/Worker.cpp +++ b/src/Worker.cpp @@ -12,20 +12,13 @@ namespace Stadium::v1 { -// Create event from raw bytes (ATTENTION: maybe move this method to Event class) -Event -Worker::ParseRawData (const char* data) { - // TODO - return Event{}; -} - // Parse ingoing packet data to event, check its properties and put it to queue if all okay void Worker::PutIngoing (const char* data) { // TODO } -// Send outgoing event (NOTICE: maybe rework) +// Send outgoing event void Worker::PutOutgoing (EventType type, KLDRArray<> data) { // TODO @@ -33,7 +26,13 @@ Worker::PutOutgoing (EventType type, KLDRArray<> data) { // Bind handler for ingoing events void -Worker::BindHandler (EventType, void()) { +Worker::BindHandler (EventType event_type, EventHandler handle) { + this->Handlers[event_type] = handle; +} + +// Expect ingoing event by its ID +Event +Worker::ExpectEvent (EventAsyncID async_id) { // TODO } @@ -46,7 +45,7 @@ Worker::IngoingLoop () { // Get single outgoing event from queue and form it to binary std::vector Worker::GetOutgoingDataVec () { - return std::vector{}; // TODO: remove + // TODO } diff --git a/src/Worker.hpp b/src/Worker.hpp index bddc448..26367af 100644 --- a/src/Worker.hpp +++ b/src/Worker.hpp @@ -12,6 +12,7 @@ #include #include #include +#include #include "KLDR.hpp" #include "Event.hpp" @@ -22,12 +23,17 @@ namespace Stadium::v1 { +// Event handler type +typedef std::function EventHandler; + + + // Working class -class Worker { +class Worker final { private: static bool Running; // If workers are running - static std::unordered_map Handlers; // Map of handlers associated with packet types TODO: may be it can be more optimized with reloaded operators + static std::unordered_map Handlers; // Map of handlers associated with packet types static std::unordered_map ExpectedEvents; // Map of expected events and their async IDs static std::atomic LastAsyncID; // Increments every use of Expect() @@ -35,11 +41,12 @@ class Worker { static std::queue IngoingEvents; // FIFO of ingoing events public: - Event ParseRawData (const char*); void PutIngoing (const char*); void PutOutgoing (EventType, KLDRArray<>); - void BindHandler (EventType, void()); + void BindHandler (EventType, EventHandler); + Event ExpectEvent (EventAsyncID); void IngoingLoop (); + void OutgoingLoop (); std::vector GetOutgoingDataVec (); };