diff --git a/include/tcp-chat.h b/include/tcp-chat.h new file mode 100644 index 0000000..9a1ed45 --- /dev/null +++ b/include/tcp-chat.h @@ -0,0 +1,119 @@ +#pragma once +#include "tlibc/errors.h" + +/// requires tlibc and tlibtoml init +Result(void) TcpChat_init(); +void TcpChat_deinit(); + +#define USERNAME_SIZE_MIN 2 +#define USERNAME_SIZE_MAX 31 +#define PASSWORD_SIZE_MIN 8 +#define PASSWORD_SIZE_MAX 31 +#define PASSWORD_HASH_SIZE 32 +#define HOSTADDR_SIZE_MIN 4 +#define HOSTADDR_SIZE_MAX 255 +#define PRIVATE_KEY_BASE64_SIZE_MAX 1724 +#define PUBLIC_KEY_BASE64_SIZE_MAX 699 +#define SERVER_NAME_SIZE_MIN 1 +#define SERVER_NAME_SIZE_MAX 127 +#define SERVER_DESC_SIZE_MAX 1023 +#define CHANNEL_NAME_SIZE_MIN 1 +#define CHANNEL_NAME_SIZE_MAX 127 +#define CHANNEL_DESC_SIZE_MAX 1023 +#define MESSAGE_SIZE_MIN 1 +#define MESSAGE_SIZE_MAX 4000 +#define MESSAGE_BLOCK_COUNT_MAX 50 + +#define MESSAGE_TIMESTAMP_FMT_SQL "%Y.%m.%d-%H:%M:%f" + +////////////////////////////////////////////////////////////////////////////// +// // +// Logging // +// // +////////////////////////////////////////////////////////////////////////////// + +ErrorCodePage_declare(WINSOCK2); +ErrorCodePage_declare(TcpChat); + +typedef enum TcpChatError { + TcpChatError_Unknown, + TcpChatError_RejectIncoming, +} TcpChatError; + +typedef enum LogSeverity { + LogSeverity_Debug, + LogSeverity_Info, + LogSeverity_Warn, + LogSeverity_Error, +} LogSeverity; + +typedef void (*LogFunction_t)(void* logger, cstr context, LogSeverity severity, cstr msg); + +// requires defined LOGGER, LOG_FUNC, LOG_CONTEXT +#define log(severity, format, ...) { \ + if(LOG_FUNC) { \ + char* ___log_msg = sprintf_malloc(format ,##__VA_ARGS__); \ + LOG_FUNC(LOGGER, LOG_CONTEXT, severity, ___log_msg); \ + free(___log_msg); \ + } \ +} + +#define logDebug(format, ...) log(LogSeverity_Debug, format ,##__VA_ARGS__) +#define logInfo(format, ...) log(LogSeverity_Info, format ,##__VA_ARGS__) +#define logWarn(format, ...) log(LogSeverity_Warn, format ,##__VA_ARGS__) +#define logError(format, ...) log(LogSeverity_Error, format ,##__VA_ARGS__) + +////////////////////////////////////////////////////////////////////////////// +// // +// Server // +// // +////////////////////////////////////////////////////////////////////////////// +typedef struct Server Server; + +/// @param config_file_content config in toml format +/// @param config_file_name to use in error messages +/// @param logger some shared data for your log function +/// @param log_func log function that you have to implement +/// @return +Result(Server*) Server_create(str config_file_content, cstr config_file_name, + void* logger, LogFunction_t log_func); + +void Server_free(Server* server); + +Result(void) Server_run(Server* server); + +////////////////////////////////////////////////////////////////////////////// +// // +// Client // +// // +////////////////////////////////////////////////////////////////////////////// +typedef struct Client Client; + +Result(Client*) Client_create(str username, str password); +void Client_free(Client* client); + +/// @return username saved during client initialization +str Client_getUserName(Client* client); + +/// @return AES key calculated from password that can be used to encrypt user data +Array(u8) Client_getUserDataKey(Client* client); + +/// @param server_addr_cstr ip:port +/// @param server_pk_base64 public key encoded by `RSA_serializePublicKey_base64()` +Result(void) Client_connect(Client* client, cstr server_addr_cstr, cstr server_pk_base64); +/// disconnect from current server +void Client_disconnect(Client* client); + +/// @param self connected client +/// @param out_str heap-allocated string +Result(void) Client_getServerName(Client* self, str* out_str); + +/// @param self connected client +/// @param out_str heap-allocated string +Result(void) Client_getServerDescription(Client* self, str* out_str); + +/// Create new account on connected server +Result(void) Client_register(Client* self, i64* out_user_id); + +/// Authorize on connected server +Result(void) Client_login(Client* self, i64* out_user_id, i64* out_landing_channel_id); diff --git a/include/tcp-chat/client.h b/include/tcp-chat/client.h deleted file mode 100644 index cb85d51..0000000 --- a/include/tcp-chat/client.h +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once -#include "tlibc/errors.h" -#include "tlibc/string/str.h" - -typedef struct Client Client; - -Result(Client*) Client_create(str username, str password); -void Client_free(Client* client); - -/// @return username saved during client initialization -str Client_getUserName(Client* client); - -/// @return AES key calculated from password that can be used to encrypt user data -Array(u8) Client_getUserDataKey(Client* client); - -/// @param server_addr_cstr ip:port -/// @param server_pk_base64 public key encoded by `RSA_serializePublicKey_base64()` -Result(void) Client_connect(Client* client, cstr server_addr_cstr, cstr server_pk_base64); -/// disconnect from current server -void Client_disconnect(Client* client); - -/// @param self connected client -/// @param out_str heap-allocated string -Result(void) Client_getServerName(Client* self, str* out_str); - -/// @param self connected client -/// @param out_str heap-allocated string -Result(void) Client_getServerDescription(Client* self, str* out_str); - -/// Create new account on connected server -Result(void) Client_register(Client* self, i64* out_user_id); - -/// Authorize on connected server -Result(void) Client_login(Client* self, i64* out_user_id, i64* out_landing_channel_id); diff --git a/include/tcp-chat/common_constants.h b/include/tcp-chat/common_constants.h deleted file mode 100644 index badd428..0000000 --- a/include/tcp-chat/common_constants.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once -#include "tlibc/std.h" - -#define USERNAME_SIZE_MIN 2 -#define USERNAME_SIZE_MAX 31 -#define PASSWORD_SIZE_MIN 8 -#define PASSWORD_SIZE_MAX 31 -#define PASSWORD_HASH_SIZE 32 -#define HOSTADDR_SIZE_MIN 4 -#define HOSTADDR_SIZE_MAX 255 -#define PRIVATE_KEY_BASE64_SIZE_MAX 1724 -#define PUBLIC_KEY_BASE64_SIZE_MAX 699 -#define SERVER_NAME_SIZE_MIN 1 -#define SERVER_NAME_SIZE_MAX 127 -#define SERVER_DESC_SIZE_MAX 1023 -#define CHANNEL_NAME_SIZE_MIN 1 -#define CHANNEL_NAME_SIZE_MAX 127 -#define CHANNEL_DESC_SIZE_MAX 1023 -#define MESSAGE_SIZE_MIN 1 -#define MESSAGE_SIZE_MAX 4000 -#define MESSAGE_BLOCK_COUNT_MAX 50 \ No newline at end of file diff --git a/include/tcp-chat/log.h b/include/tcp-chat/log.h deleted file mode 100644 index 2767422..0000000 --- a/include/tcp-chat/log.h +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once -#include -#include "tlibc/std.h" -#include "tlibc/string/cstr.h" - -typedef enum LogSeverity { - LogSeverity_Debug, - LogSeverity_Info, - LogSeverity_Warn, - LogSeverity_Error, -} LogSeverity; - -typedef void (*LogFunction_t)(void* logger, cstr context, LogSeverity severity, cstr msg); - - -// requires defined LOGGER, LOG_FUNC, LOG_CONTEXT -#define log(severity, format, ...) { \ - if(LOG_FUNC) { \ - char* ___log_msg = sprintf_malloc(format ,##__VA_ARGS__); \ - LOG_FUNC(LOGGER, LOG_CONTEXT, severity, ___log_msg); \ - free(___log_msg); \ - } \ -} - -#define logDebug(format, ...) log(LogSeverity_Debug, format ,##__VA_ARGS__) -#define logInfo(format, ...) log(LogSeverity_Info, format ,##__VA_ARGS__) -#define logWarn(format, ...) log(LogSeverity_Warn, format ,##__VA_ARGS__) -#define logError(format, ...) log(LogSeverity_Error, format ,##__VA_ARGS__) diff --git a/include/tcp-chat/server.h b/include/tcp-chat/server.h deleted file mode 100644 index 6f99a7a..0000000 --- a/include/tcp-chat/server.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once -#include "tlibc/errors.h" -#include "tlibc/string/str.h" -#include "tcp-chat/log.h" - -typedef struct Server Server; - -/// @param config_file_content config in toml format -/// @param config_file_name to use in error messages -/// @param logger some shared data for your log function -/// @param log_func log function that you have to implement -/// @return -Result(Server*) Server_create(str config_file_content, cstr config_file_name, - void* logger, LogFunction_t log_func); - -void Server_free(Server* server); - -Result(void) Server_run(Server* server); diff --git a/include/tcp-chat/tcp-chat.h b/include/tcp-chat/tcp-chat.h deleted file mode 100644 index 85d08fd..0000000 --- a/include/tcp-chat/tcp-chat.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once -#include "tlibc/errors.h" - -/// requires tlibc and tlibtoml init -Result(void) TcpChat_init(); -void TcpChat_deinit(); - -ErrorCodePage_declare(WINSOCK2); -ErrorCodePage_declare(TcpChat); - -typedef enum TcpChatError { - TcpChatError_Unknown, - TcpChatError_RejectIncoming, -} TcpChatError; - -#define MESSAGE_TIMESTAMP_FMT_SQL "%Y.%m.%d-%H:%M:%f" diff --git a/src/cli/ClientCLI/ClientCLI.c b/src/cli/ClientCLI/ClientCLI.c index ce79395..d81923f 100644 --- a/src/cli/ClientCLI/ClientCLI.c +++ b/src/cli/ClientCLI/ClientCLI.c @@ -1,7 +1,6 @@ +#include "ClientCLI.h" #include "tlibc/filesystem.h" #include "tlibc/term.h" -#include "tcp-chat/common_constants.h" -#include "cli/ClientCLI/ClientCLI.h" #include "network/tcp-chat-protocol/v1.h" static const str greeting_art = STR( diff --git a/src/cli/ClientCLI/ClientCLI.h b/src/cli/ClientCLI/ClientCLI.h index b578690..c8c0e7f 100644 --- a/src/cli/ClientCLI/ClientCLI.h +++ b/src/cli/ClientCLI/ClientCLI.h @@ -1,8 +1,8 @@ #pragma once #include +#include "tcp-chat.h" #include "tlibc/collections/HashMap.h" #include "tlibc/collections/List.h" -#include "tcp-chat/client.h" #include "db/client_db.h" typedef struct ClientCLI { diff --git a/src/cli/ClientCLI/db/client_db.h b/src/cli/ClientCLI/db/client_db.h index ecbe57f..394b2f5 100644 --- a/src/cli/ClientCLI/db/client_db.h +++ b/src/cli/ClientCLI/db/client_db.h @@ -1,5 +1,5 @@ #pragma once -#include "tcp-chat/tcp-chat.h" +#include "tcp-chat.h" #include "tsqlite.h" #include "network/tcp-chat-protocol/v1.h" #include "tlibc/collections/List.h" diff --git a/src/cli/main.c b/src/cli/main.c index 7df6eac..ed2b9b4 100644 --- a/src/cli/main.c +++ b/src/cli/main.c @@ -1,6 +1,6 @@ +#include "tcp-chat.h" #include "tlibc/tlibc.h" #include "tlibtoml.h" -#include "tcp-chat/tcp-chat.h" #include "cryptography/RSA.h" #include "cli/modes/modes.h" diff --git a/src/cli/modes/ServerMode.c b/src/cli/modes/ServerMode.c index c0083f4..f8129a3 100644 --- a/src/cli/modes/ServerMode.c +++ b/src/cli/modes/ServerMode.c @@ -1,8 +1,8 @@ +#include +#include "tcp-chat.h" #include "modes.h" -#include "tcp-chat/server.h" #include "tlibc/time.h" #include "tlibc/term.h" -#include typedef struct ServerLogger { pthread_mutex_t mutex; diff --git a/src/client/client_internal.h b/src/client/client_internal.h index c573f40..b8a4554 100644 --- a/src/client/client_internal.h +++ b/src/client/client_internal.h @@ -1,5 +1,5 @@ #pragma once -#include "tcp-chat/client.h" +#include "tcp-chat.h" #include "cryptography/AES.h" #include "cryptography/RSA.h" #include "network/encrypted_sockets.h" diff --git a/src/cryptography/cryptography.h b/src/cryptography/cryptography.h index ae2fa8a..a1c361f 100755 --- a/src/cryptography/cryptography.h +++ b/src/cryptography/cryptography.h @@ -1,10 +1,9 @@ #pragma once -#include "tlibc/errors.h" +#include "tcp-chat.h" #include "tlibc/collections/Array.h" #include "tlibc/collections/Array_impl/Array_u8.h" #include "bearssl_rand.h" #include "bearssl_hash.h" -#include "tcp-chat/common_constants.h" ////////////////////////////////////////////////////////////////////////////// // // diff --git a/src/network/internal.h b/src/network/internal.h index e79d725..3ca8ba4 100644 --- a/src/network/internal.h +++ b/src/network/internal.h @@ -1,5 +1,5 @@ #pragma once -#include "tcp-chat/tcp-chat.h" +#include "tcp-chat.h" #include "endpoint.h" #if !defined(KN_USE_WINSOCK) diff --git a/src/network/tcp-chat-protocol/constant.h b/src/network/tcp-chat-protocol/constant.h index 8a58448..d6311e3 100644 --- a/src/network/tcp-chat-protocol/constant.h +++ b/src/network/tcp-chat-protocol/constant.h @@ -1,7 +1,6 @@ #pragma once -#include "tlibc/errors.h" +#include "tcp-chat.h" #include "tlibc/magic.h" -#include "tcp-chat/common_constants.h" #define AES_SESSION_KEY_SIZE 32 diff --git a/src/network/tcp-chat-protocol/v1.h b/src/network/tcp-chat-protocol/v1.h index bdd0fad..ecc994f 100644 --- a/src/network/tcp-chat-protocol/v1.h +++ b/src/network/tcp-chat-protocol/v1.h @@ -1,6 +1,5 @@ #pragma once -#include "tlibc/errors.h" -#include "tlibc/string/str.h" +#include "tcp-chat.h" #include "tlibc/time.h" #include "network/tcp-chat-protocol/constant.h" diff --git a/src/server/db/server_db.h b/src/server/db/server_db.h index 7cceac6..a6e5572 100644 --- a/src/server/db/server_db.h +++ b/src/server/db/server_db.h @@ -1,5 +1,5 @@ #pragma once -#include "tcp-chat/tcp-chat.h" +#include "tcp-chat.h" #include "tsqlite.h" #include "network/tcp-chat-protocol/v1.h" diff --git a/src/server/server_internal.h b/src/server/server_internal.h index ad2e1e4..2044f11 100644 --- a/src/server/server_internal.h +++ b/src/server/server_internal.h @@ -1,6 +1,5 @@ #pragma once -#include "tcp-chat/tcp-chat.h" -#include "tcp-chat/server.h" +#include "tcp-chat.h" #include "cryptography/AES.h" #include "cryptography/RSA.h" #include "network/encrypted_sockets.h"