From f4aaec6481d07365550a993b68a7b5920bc8fa51 Mon Sep 17 00:00:00 2001 From: WolverinDEV Date: Mon, 19 Apr 2021 19:29:46 +0200 Subject: [PATCH] Fixed compilation for windows and hide task executor impl details --- src/License.cpp | 13 +++---- src/Properties.h | 4 +-- src/misc/base64.cpp | 2 +- src/misc/base64.h | 4 +-- src/misc/task_executor.cpp | 63 +++++++++++++++++++++++++++++++++- src/misc/task_executor.h | 64 +++-------------------------------- src/protocol/CryptHandler.cpp | 6 ++-- src/protocol/CryptHandler.h | 10 +++--- src/query/Command.h | 1 + 9 files changed, 87 insertions(+), 80 deletions(-) diff --git a/src/License.cpp b/src/License.cpp index c252662..fe714f8 100644 --- a/src/License.cpp +++ b/src/License.cpp @@ -1,5 +1,4 @@ #include -#include #include "misc/endianness.h" #include #define FIXEDINT_H_INCLUDED @@ -68,7 +67,7 @@ LicensePublicKey license::teamspeak::Anonymous::root_key = { }; size_t license::teamspeak::Anonymous::root_index = 1; -shared_ptr license::teamspeak::Anonymous::chain = []() -> shared_ptr { +std::shared_ptr default_anonymous_chain() { string error; auto str = istringstream(string((const char*) default_chain, sizeof(default_chain))); auto chain = LicenseChain::parse(str, error); @@ -78,7 +77,9 @@ shared_ptr license::teamspeak::Anonymous::chain = []() -> shared_p } return chain; -}(); +} + +shared_ptr license::teamspeak::Anonymous::chain{default_anonymous_chain()}; #define IOERROR(message) \ do {\ @@ -326,9 +327,9 @@ inline string importHash(const std::string& hash) { memset(buffer, 0, 64); memcpy(buffer, (void*) hash.data(), 32); - buffer[0] &= 0xF8; - buffer[31] &= 0x3F; - buffer[31] |= 0x40; + buffer[0] &= (uint8_t) 0xF8; + buffer[31] &= (uint8_t) 0x3F; + buffer[31] |= (uint8_t) 0x40; sc_reduce(buffer); return string((char*) buffer, 32); } diff --git a/src/Properties.h b/src/Properties.h index e59afdf..3498ad9 100644 --- a/src/Properties.h +++ b/src/Properties.h @@ -1005,12 +1005,12 @@ namespace ts { } template - inline std::result_of_t operator->*(F &&f) { + inline std::invoke_result operator->*(F &&f) { return std::forward(f)(*handle); } template - inline std::result_of_t operator->*(F &&f) const { + inline std::invoke_result operator->*(F &&f) const { return std::forward(f)(*handle); } diff --git a/src/misc/base64.cpp b/src/misc/base64.cpp index 01f7962..d774f6a 100644 --- a/src/misc/base64.cpp +++ b/src/misc/base64.cpp @@ -13,7 +13,7 @@ std::string base64::decode(const char* input, size_t size) { return ret; } -std::string base64::encode(const char* input, const unsigned long inputSize) { +std::string base64::encode(const char* input, size_t inputSize) { auto outlen = static_cast(inputSize + (inputSize / 3.0) + 16); auto outbuf = new unsigned char[outlen]; //Reserve output memory if(base64_encode((unsigned char*) input, inputSize, outbuf, &outlen) != CRYPT_OK){ diff --git a/src/misc/base64.h b/src/misc/base64.h index fa60d02..5c7d271 100644 --- a/src/misc/base64.h +++ b/src/misc/base64.h @@ -10,14 +10,14 @@ namespace base64 { * @param inputSize The size of the input to decode * @return A Base64-encoded version of the encoded string */ - extern std::string encode(const char* input, const unsigned long inputSize); + extern std::string encode(const char* input, size_t inputSize); /** * Encodes a given string in Base64 * @param input The input string to Base64-encode * @return A Base64-encoded version of the encoded string */ - inline std::string encode(const std::string& input) { return encode(input.data(), (unsigned long) input.size()); } + inline std::string encode(const std::string& input) { return encode(input.data(), input.size()); } inline std::string encode(const std::string_view& input) { return encode(input.data(), input.size()); } diff --git a/src/misc/task_executor.cpp b/src/misc/task_executor.cpp index c6bafdf..adf8d51 100644 --- a/src/misc/task_executor.cpp +++ b/src/misc/task_executor.cpp @@ -4,14 +4,75 @@ #include "./task_executor.h" #include "./threads.h" +#include #include #include #include -#include +#include +#include using std::chrono::system_clock; using namespace ts; +struct task_executor::task { + task_id id{0}; + std::string name{}; + std::function callback{}; + + /* will be set to true if the task has been canceled but is executing right now */ + bool canceled{false}; + std::optional> finish_callback{}; + + task* next{nullptr}; +}; + +struct task_executor::task_recurring { + task_id id{0}; + std::string name{}; + + bool shutdown{false}; + std::optional> finish_callback{}; + + std::chrono::nanoseconds interval{}; + std::chrono::system_clock::time_point last_invoked{}; + std::chrono::system_clock::time_point scheduled_invoke{}; + + std::function callback{}; + + task_recurring* next{nullptr}; +}; + +struct task_executor::task_context { + std::mutex mutex{}; + std::condition_variable notify{}; + + bool shutdown{false}; + task_id id_index{1}; + + size_t task_count{}; + task* task_head{nullptr}; + task** task_tail{&this->task_head}; + + size_t task_recurring_count{}; + task_recurring* task_recurring_head{nullptr}; + + task_exception_handler exception_handler{task_executor::abort_exception_handler}; +}; + +struct task_executor::executor_context { + task_executor* handle{nullptr}; + std::thread thread_handle{}; + + std::shared_ptr task_context{}; + + /** + * Must be accessed while holding the task_context.mutex and shall never be changed except for the executor. + * Lifetime will be granted while holding the lock. + */ + task* executing_task{nullptr}; + task_recurring* executing_recurring_task{nullptr}; +}; + task_executor::task_executor(size_t num_threads, const std::string &thread_prefix) { this->task_context = std::make_shared(); diff --git a/src/misc/task_executor.h b/src/misc/task_executor.h index d81f45b..20ea7b8 100644 --- a/src/misc/task_executor.h +++ b/src/misc/task_executor.h @@ -1,8 +1,6 @@ #pragma once -#include #include -#include #include #include #include @@ -54,64 +52,10 @@ namespace ts { std::chrono::nanoseconds /* interval */, std::function /* callback */); private: - struct task { - task_id id{0}; - std::string name{}; - std::function callback{}; - - /* will be set to true if the task has been canceled but is executing right now */ - bool canceled{false}; - std::optional> finish_callback{}; - - task* next{nullptr}; - }; - - struct task_recurring { - task_id id{0}; - std::string name{}; - - bool shutdown{false}; - std::optional> finish_callback{}; - - std::chrono::nanoseconds interval{}; - std::chrono::system_clock::time_point last_invoked{}; - std::chrono::system_clock::time_point scheduled_invoke{}; - - std::function callback{}; - - task_recurring* next{nullptr}; - }; - - struct task_context { - std::mutex mutex{}; - std::condition_variable notify{}; - - bool shutdown{false}; - task_id id_index{1}; - - size_t task_count{}; - task* task_head{nullptr}; - task** task_tail{&this->task_head}; - - size_t task_recurring_count{}; - task_recurring* task_recurring_head{nullptr}; - - task_exception_handler exception_handler{task_executor::abort_exception_handler}; - }; - - struct executor_context { - task_executor* handle; - std::thread thread_handle{}; - - std::shared_ptr task_context{}; - - /** - * Must be accessed while holding the task_context.mutex and shall never be changed except for the executor. - * Lifetime will be granted while holding the lock. - */ - task* executing_task{nullptr}; - task_recurring* executing_recurring_task{nullptr}; - }; + struct task; + struct task_recurring; + struct task_context; + struct executor_context; enum struct task_cancel_result { not_found, diff --git a/src/protocol/CryptHandler.cpp b/src/protocol/CryptHandler.cpp index 4b720ee..1c5bc2f 100644 --- a/src/protocol/CryptHandler.cpp +++ b/src/protocol/CryptHandler.cpp @@ -198,13 +198,13 @@ bool CryptHandler::generate_key_nonce( le2be32(generation, buffer, 2); memcpy(&buffer[6], this->iv_struct, this->iv_struct_length); - digest::sha256(buffer, buffer_length, key_cache.key_nonce); + digest::sha256(buffer, buffer_length, key_cache.key_nonce.value); key_cache.generation = generation; } - memcpy(key.data(), key_cache.key, 16); - memcpy(nonce.data(), key_cache.nonce, 16); + memcpy(key.data(), key_cache.key_nonce.key, 16); + memcpy(nonce.data(), key_cache.key_nonce.nonce, 16); } //Xor the key diff --git a/src/protocol/CryptHandler.h b/src/protocol/CryptHandler.h index bf73e13..4fbd840 100644 --- a/src/protocol/CryptHandler.h +++ b/src/protocol/CryptHandler.h @@ -53,13 +53,13 @@ namespace ts::connection { struct KeyCache { uint16_t generation = 0xFFEF; - union { + union _key_nonce { struct { uint8_t key[16]; uint8_t nonce[16]; }; - uint8_t key_nonce[32]; - }; + uint8_t value[32]; + } key_nonce; }; bool encryption_initialized_{false}; @@ -72,8 +72,8 @@ namespace ts::connection { uint8_t current_mac[8]{}; std::mutex cache_key_lock{}; - std::array cache_key_client{}; - std::array cache_key_server{}; + std::array cache_key_client{}; + std::array cache_key_server{}; static_assert(sizeof(current_mac) == sizeof(default_mac), "invalid mac"); static_assert(sizeof(iv_struct) == 64, "invalid iv struct"); diff --git a/src/query/Command.h b/src/query/Command.h index 06ff028..c1c6381 100644 --- a/src/query/Command.h +++ b/src/query/Command.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include "../Variable.h"