Продолжаем

This commit is contained in:
Shr3dd3r 2023-10-28 04:54:14 +03:00
parent 09aff0e709
commit e8d9782ebc
7 changed files with 73 additions and 44 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@ libstadium.so
libstadiumtest_release libstadiumtest_release
libstadiumtest_sanitizer libstadiumtest_sanitizer
libstadiumtest_fortified libstadiumtest_fortified
pseudo_code.md

View File

@ -1,7 +1,7 @@
# libstadium makefile # libstadium makefile
CC = g++ 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 = -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_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 CFLAGS_DEBUG_F = -O1 -D_FORTIFY_SOURCE=3

View File

@ -17,11 +17,6 @@ Event::Event () {
this->Type.Category = this->Type.Subcategory = this->AsyncID = 0; 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 // Ctor with positional arguments
Event::Event (EventType type, KLDRArray<> data, uint16_t id = 0) : Type(std::move(type)), Payload(std::move(data)), AsyncID(id) { Event::Event (EventType type, KLDRArray<> data, uint16_t id = 0) : Type(std::move(type)), Payload(std::move(data)), AsyncID(id) {
// TODO: calculate hash // TODO: calculate hash
@ -34,9 +29,22 @@ Event::Event (uint8_t cat, uint8_t subcat, KLDRArray<> data, uint16_t id = 0) :
// TODO: calculate hash // 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<const uint16_t*>(&data[2]);
// TODO: parse hash
// TODO: parse payload to KLDRArray
}
// Write to stdout info about event // Write to stdout info about event
void void
Event::Print () { Event::Print () const {
printf( printf(
"[Event] Object at: %lu; Category: 0x%.2hhx; Subcategory: 0x%.2hhx; AsyncID: %hu; Payload size: cells: %lu, bytes:%lu\n", // TODO: add hash "[Event] Object at: %lu; Category: 0x%.2hhx; Subcategory: 0x%.2hhx; AsyncID: %hu; Payload size: cells: %lu, bytes:%lu\n", // TODO: add hash
reinterpret_cast<size_t>(this), reinterpret_cast<size_t>(this),

View File

@ -11,10 +11,16 @@
#include <cstdio> #include <cstdio>
#include <vector> #include <vector>
#include <string>
#include <stdexcept>
#include "KLDR.hpp" #include "KLDR.hpp"
#define MIN_RAW_EVENT_SIZE 21
namespace Stadium::v1 { namespace Stadium::v1 {
@ -28,31 +34,33 @@ struct EventType {
uint8_t Subcategory; uint8_t Subcategory;
}; };
// Get stringized name of event
std::string ResolveEventType (EventType);
// Event class // Event class
class Event { class Event final {
public:
uint32_t Lifetime = 0; // Lifetime of event in seconds uint32_t Lifetime = 0; // Lifetime of event in seconds
private:
// These are the "physical" members that will be packed to binary
std::vector<char> Hash;
// CryptoAlgo HashAlgo; // TODO // CryptoAlgo HashAlgo; // TODO
// std::vector<char> Hash;
KLDRArray<> Payload; KLDRArray<> Payload;
public:
EventType Type; EventType Type;
EventAsyncID AsyncID; EventAsyncID AsyncID;
Event (); public:
Event (const char*, size_t); Event ();//CryptoAlgo);
Event (EventType, KLDRArray<>, uint16_t); Event (EventType, KLDRArray<>, uint16_t);
Event (uint8_t, uint8_t, 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

View File

@ -12,6 +12,7 @@
#include <cstdio> #include <cstdio>
#include <iomanip> #include <iomanip>
#include <vector> #include <vector>
#include <string>
#include <random> #include <random>
#include <exception> #include <exception>
@ -21,7 +22,7 @@ namespace Stadium::v1 {
enum KLDRDefaultKeys : uint8_t { enum KLDRDefaultKeys : uint8_t { // TODO: think about better way (maybe std::map<string,uint8_t> or smt like that)
Data = 0x01, // Main data of event Data = 0x01, // Main data of event
ObjectID = 0x02, // ID of object in local context ObjectID = 0x02, // ID of object in local context
SrcEventAuthor = 0x03, // Source author of event SrcEventAuthor = 0x03, // Source author of event
@ -30,7 +31,7 @@ enum KLDRDefaultKeys : uint8_t {
BatchNumber = 0x06, // Number of event in chain of events BatchNumber = 0x06, // Number of event in chain of events
Path = 0x07, // Path to requested/uploaded resource Path = 0x07, // Path to requested/uploaded resource
Power = 0x08, // Access right to object Power = 0x08, // Access right to object
ServerSession = 0x09, // ID of server-side session //ServerSession = 0x09, // ID of server-side session
// Crypto-related // Crypto-related
CryptoAlgos = 0x11, // Array of used cryptography algos CryptoAlgos = 0x11, // Array of used cryptography algos
CryptoKeyID = 0x12, // ID of crypto key used for data encryption in this event 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". * KLDR stands for "Key-Length-Data-Repeat".
* Schematically, binary array with KLDR-formatted data looks like this. * 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. * This can be more optimized for performance with cost of more memory usage.
*/ */
template <typename KeyT = uint8_t, typename LengthT = uint16_t> template <typename KeyT = uint8_t, typename LengthT = uint16_t>
class KLDRArray { class KLDRArray final {
private: private:
std::vector<KeyT> Keys; std::vector<KeyT> Keys;
std::vector<LengthT> Lengths; std::vector<LengthT> Lengths;
@ -178,9 +184,9 @@ class KLDRArray {
case v1::KLDRDefaultKeys::Power: case v1::KLDRDefaultKeys::Power:
printf("Power"); printf("Power");
break; break;
case v1::KLDRDefaultKeys::ServerSession: // case v1::KLDRDefaultKeys::ServerSession:
printf("ServerSession"); // printf("ServerSession");
break; // break;
case v1::KLDRDefaultKeys::CryptoAlgos: case v1::KLDRDefaultKeys::CryptoAlgos:
printf("CryptoAlgos"); printf("CryptoAlgos");
break; break;

View File

@ -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 // Parse ingoing packet data to event, check its properties and put it to queue if all okay
void void
Worker::PutIngoing (const char* data) { Worker::PutIngoing (const char* data) {
// TODO // TODO
} }
// Send outgoing event (NOTICE: maybe rework) // Send outgoing event
void void
Worker::PutOutgoing (EventType type, KLDRArray<> data) { Worker::PutOutgoing (EventType type, KLDRArray<> data) {
// TODO // TODO
@ -33,7 +26,13 @@ Worker::PutOutgoing (EventType type, KLDRArray<> data) {
// Bind handler for ingoing events // Bind handler for ingoing events
void 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 // TODO
} }
@ -46,7 +45,7 @@ Worker::IngoingLoop () {
// Get single outgoing event from queue and form it to binary // Get single outgoing event from queue and form it to binary
std::vector<char> std::vector<char>
Worker::GetOutgoingDataVec () { Worker::GetOutgoingDataVec () {
return std::vector<char>{}; // TODO: remove // TODO
} }

View File

@ -12,6 +12,7 @@
#include <atomic> #include <atomic>
#include <queue> #include <queue>
#include <unordered_map> #include <unordered_map>
#include <functional>
#include "KLDR.hpp" #include "KLDR.hpp"
#include "Event.hpp" #include "Event.hpp"
@ -22,12 +23,17 @@ namespace Stadium::v1 {
// Event handler type
typedef std::function<void()> EventHandler;
// Working class // Working class
class Worker { class Worker final {
private: private:
static bool Running; // If workers are running static bool Running; // If workers are running
static std::unordered_map<EventType, void()> Handlers; // Map of handlers associated with packet types TODO: may be it can be more optimized with reloaded operators static std::unordered_map<EventType, EventHandler> Handlers; // Map of handlers associated with packet types
static std::unordered_map<EventAsyncID, Event> ExpectedEvents; // Map of expected events and their async IDs static std::unordered_map<EventAsyncID, Event> ExpectedEvents; // Map of expected events and their async IDs
static std::atomic<EventAsyncID> LastAsyncID; // Increments every use of Expect() static std::atomic<EventAsyncID> LastAsyncID; // Increments every use of Expect()
@ -35,11 +41,12 @@ class Worker {
static std::queue<Event> IngoingEvents; // FIFO of ingoing events static std::queue<Event> IngoingEvents; // FIFO of ingoing events
public: public:
Event ParseRawData (const char*);
void PutIngoing (const char*); void PutIngoing (const char*);
void PutOutgoing (EventType, KLDRArray<>); void PutOutgoing (EventType, KLDRArray<>);
void BindHandler (EventType, void()); void BindHandler (EventType, EventHandler);
Event ExpectEvent (EventAsyncID);
void IngoingLoop (); void IngoingLoop ();
void OutgoingLoop ();
std::vector<char> GetOutgoingDataVec (); std::vector<char> GetOutgoingDataVec ();
}; };