diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 38bc2de..4c31012 100755 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -5,6 +5,7 @@ "defines": [], "includePath": [ "src", + "include", "dependencies/BearSSL/inc", "dependencies/BearSSL/src", "dependencies/tlibc/include", diff --git a/dependencies/tlibc b/dependencies/tlibc index 89aab2b..bc41577 160000 --- a/dependencies/tlibc +++ b/dependencies/tlibc @@ -1 +1 @@ -Subproject commit 89aab2b5bffd46ec0538a5e7c2f1674d59d5677a +Subproject commit bc415772486c087abd9cac9b9f2dc2b5dd5e98db diff --git a/src/client/client.h b/include/tcp-chat/client.h similarity index 100% rename from src/client/client.h rename to include/tcp-chat/client.h diff --git a/src/common_constants.h b/include/tcp-chat/common_constants.h similarity index 100% rename from src/common_constants.h rename to include/tcp-chat/common_constants.h diff --git a/include/tcp-chat/server.h b/include/tcp-chat/server.h new file mode 100644 index 0000000..cb01343 --- /dev/null +++ b/include/tcp-chat/server.h @@ -0,0 +1,9 @@ +#pragma once +#include "tlibc/errors.h" +#include "tlibc/string/str.h" + +typedef struct Server Server; + +Result(Server*) Server_createFromConfig(str config_str); +void Server_free(Server* server); +Result(void) Server_run(Server* server); diff --git a/project.config b/project.config index 72ba461..275c818 100644 --- a/project.config +++ b/project.config @@ -35,7 +35,7 @@ OBJDIR="obj" OUTDIR="bin" STATIC_LIB_FILE="$PROJECT.a" -INCLUDE="-Isrc -I$DEPENDENCIES_DIR/BearSSL/inc -I$DEPENDENCIES_DIR/tlibc/include" +INCLUDE="-Isrc -Iinclude -I$DEPENDENCIES_DIR/BearSSL/inc -I$DEPENDENCIES_DIR/tlibc/include" # OS-specific options case "$OS" in diff --git a/src/ClientCLI/ClientCLI.c b/src/cli/ClientCLI/ClientCLI.c similarity index 88% rename from src/ClientCLI/ClientCLI.c rename to src/cli/ClientCLI/ClientCLI.c index 28c2608..57af3b4 100644 --- a/src/ClientCLI/ClientCLI.c +++ b/src/cli/ClientCLI/ClientCLI.c @@ -1,7 +1,7 @@ -#include "ClientCLI/ClientCLI.h" -#include "ClientCLI/db_tables.h" -#include "term.h" -#include "common_constants.h" +#include "cli/ClientCLI/ClientCLI.h" +#include "cli/ClientCLI/db_tables.h" +#include "cli/term.h" +#include "tcp-chat/common_constants.h" #include "tlibc/time.h" #include "tlibc/filesystem.h" @@ -25,7 +25,9 @@ static const str farewell_art = STR( static Result(void) ClientCLI_askUserNameAndPassword(str* username_out, str* password_out); static Result(void) ClientCLI_commandExec(ClientCLI* self, str command, bool* stop); static Result(void) ClientCLI_openUserDB(ClientCLI* self); -static Result(void) ClientCLI_saveServerInfo(ClientCLI* self, cstr server_addr_cstr, cstr server_pk_base64); +static Result(void) ClientCLI_saveServerInfo(ClientCLI* self, + cstr server_addr_cstr, cstr server_pk_base64, + str server_name, str server_description); void ClientCLI_destroy(ClientCLI* self){ @@ -75,6 +77,7 @@ Result(void) ClientCLI_run(ClientCLI* self) { ResultVar(void) com_result = ClientCLI_commandExec(self, command_input, &stop); if(com_result.error){ + Error_addCallPos(com_result.error, ErrorCallPos_here()); str e_str = Error_toStr(com_result.error); printf("%s\n", e_str.data); str_free(e_str); @@ -157,7 +160,7 @@ static Result(void) ClientCLI_commandExec(ClientCLI* self, str command, bool* st str_trim(&server_addr_str, true); // ask server public key - printf("Enter server public key (RSA-Public-:):\n"); + printf("Enter server public key (RSA-Public-:):\n"); char server_pk_cstr[PUBLIC_KEY_BASE64_SIZE_MAX + 1]; try_void(term_readLine(server_pk_cstr, sizeof(server_pk_cstr))); str server_pk_str = str_from_cstr(server_pk_cstr); @@ -169,11 +172,15 @@ static Result(void) ClientCLI_commandExec(ClientCLI* self, str command, bool* st printf("connection established\n"); // show server info - // printf("server name: %s\n", client->server_connection->server_name.data); - // printf("server description: %s\n", client->server_connection->server_description.data); + str server_name = str_null; + str server_description = str_null; + try_void(Client_getServerName(self->client, &server_name)); + try_void(Client_getServerName(self->client, &server_description)); + printf("server name: %s\n", server_name.data); + printf("server description: %s\n", server_description.data); - - try_void(ClientCLI_saveServerInfo(self, server_addr_cstr, server_pk_cstr)); + try_void(ClientCLI_saveServerInfo(self, server_addr_cstr, server_pk_cstr, + server_name, server_description)); // TODO: ask in loop: log in / register //TODO: call Client_runIO(): @@ -205,7 +212,9 @@ static Result(void) ClientCLI_openUserDB(ClientCLI* self){ Return RESULT_VOID; } -static Result(void) ClientCLI_saveServerInfo(ClientCLI* self, cstr server_addr_cstr, cstr server_pk_base64){ +static Result(void) ClientCLI_saveServerInfo(ClientCLI* self, + cstr server_addr_cstr, cstr server_pk_base64, + str server_name, str server_description){ Deferral(8); ServerInfo si; @@ -222,15 +231,11 @@ static Result(void) ClientCLI_saveServerInfo(ClientCLI* self, cstr server_addr_c si.pk_base64[si.pk_base64_len] = 0; // name - str server_name = str_null; - try_void(Client_getServerName(self->client, &server_name)); si.name_len = server_name.size; memcpy(si.name, server_name.data, si.name_len); si.name[si.name_len] = 0; // description - str server_description = str_null; - try_void(Client_getServerName(self->client, &server_description)); si.desc_len = server_name.size; memcpy(si.desc, server_description.data, si.desc_len); si.desc[si.desc_len] = 0; diff --git a/src/ClientCLI/ClientCLI.h b/src/cli/ClientCLI/ClientCLI.h similarity index 89% rename from src/ClientCLI/ClientCLI.h rename to src/cli/ClientCLI/ClientCLI.h index 72f8cc3..40d7565 100644 --- a/src/ClientCLI/ClientCLI.h +++ b/src/cli/ClientCLI/ClientCLI.h @@ -1,5 +1,5 @@ #pragma once -#include "client/client.h" +#include "tcp-chat/client.h" #include "db/idb.h" typedef struct ClientCLI { diff --git a/src/ClientCLI/db_tables.h b/src/cli/ClientCLI/db_tables.h similarity index 90% rename from src/ClientCLI/db_tables.h rename to src/cli/ClientCLI/db_tables.h index 7861143..ca3df97 100644 --- a/src/ClientCLI/db_tables.h +++ b/src/cli/ClientCLI/db_tables.h @@ -1,5 +1,5 @@ #pragma once -#include "common_constants.h" +#include "tcp-chat/common_constants.h" #include "tlibc/time.h" typedef struct ServerInfo { diff --git a/src/main.c b/src/cli/main.c old mode 100755 new mode 100644 similarity index 92% rename from src/main.c rename to src/cli/main.c index 1d0744f..738da1b --- a/src/main.c +++ b/src/cli/main.c @@ -1,8 +1,9 @@ -#include "network/network.h" -#include "ClientCLI/ClientCLI.h" -#include "server/server.h" #include "tlibc/tlibc.h" #include "tlibc/base64.h" +#include "tlibc/filesystem.h" +#include "network/network.h" +#include "cli/ClientCLI/ClientCLI.h" +#include "server/server_internal.h" #define _DEFAULT_CONFIG_PATH_CLIENT "tcp-chat-client.config" #define _DEFAULT_CONFIG_PATH_SERVER "tcp-chat-server.config" @@ -152,8 +153,23 @@ int main(const int argc, cstr const* argv){ if(!config_path) config_path = _DEFAULT_CONFIG_PATH_SERVER; - try_fatal(Server* server, p, Server_createFromConfig(config_path)); + // open file + try_fatal(FILE* config_file, p, file_open(config_path, FO_ReadExisting)); + Defer(file_close(config_file)); + // read whole file into str + Array(u8) config_buf = Array_null; + try_fatal_void(file_readWhole(config_file, &config_buf)); + Defer(Array_free(config_buf)); + str config_str = Array_castTo_str(config_buf, false); + config_buf.data = NULL; + // init server + try_fatal(Server* server, p, Server_createFromConfig(config_str)); Defer(Server_free(server)); + // manually close file and free config_buf + file_close(config_file); + config_file = NULL; + Array_free(config_buf); + // start infinite loop on main thread try_fatal_void(Server_run(server)); break; } diff --git a/src/term.c b/src/cli/term.c similarity index 100% rename from src/term.c rename to src/cli/term.c diff --git a/src/term.h b/src/cli/term.h similarity index 100% rename from src/term.h rename to src/cli/term.h diff --git a/src/client/client_internal.h b/src/client/client_internal.h index 258bd72..8bcec88 100644 --- a/src/client/client_internal.h +++ b/src/client/client_internal.h @@ -1,5 +1,5 @@ #pragma once -#include "client.h" +#include "tcp-chat/client.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 2623377..7223390 100755 --- a/src/cryptography/cryptography.h +++ b/src/cryptography/cryptography.h @@ -2,7 +2,7 @@ #include "tlibc/collections/Array.h" #include "tlibc/errors.h" #include "bearssl_rand.h" -#include "common_constants.h" +#include "tcp-chat/common_constants.h" ////////////////////////////////////////////////////////////////////////////// // // diff --git a/src/network/tcp-chat-protocol/constant.h b/src/network/tcp-chat-protocol/constant.h index a67448a..eaf760b 100644 --- a/src/network/tcp-chat-protocol/constant.h +++ b/src/network/tcp-chat-protocol/constant.h @@ -1,7 +1,7 @@ #pragma once #include "tlibc/errors.h" #include "magic.h" -#include "common_constants.h" +#include "tcp-chat/common_constants.h" #define AES_SESSION_KEY_SIZE 32 diff --git a/src/server/ClientConnection.c b/src/server/ClientConnection.c index 01a7a7c..9214629 100644 --- a/src/server/ClientConnection.c +++ b/src/server/ClientConnection.c @@ -1,4 +1,4 @@ -#include "server.h" +#include "server/server_internal.h" #include "network/tcp-chat-protocol/v1.h" void ClientConnection_close(ClientConnection* conn){ diff --git a/src/server/db_tables.h b/src/server/db_tables.h index 77a7baa..719af13 100644 --- a/src/server/db_tables.h +++ b/src/server/db_tables.h @@ -1,5 +1,5 @@ #pragma once -#include "common_constants.h" +#include "tcp-chat/common_constants.h" #include "tlibc/time.h" typedef struct User { diff --git a/src/server/request_handlers/request_handlers.h b/src/server/request_handlers/request_handlers.h index 901661e..c4f5b8b 100644 --- a/src/server/request_handlers/request_handlers.h +++ b/src/server/request_handlers/request_handlers.h @@ -1,6 +1,6 @@ #pragma once #include "network/tcp-chat-protocol/v1.h" -#include "server/server.h" +#include "server/server_internal.h" #include "log.h" diff --git a/src/server/server.c b/src/server/server.c index 695d7f7..7fbb2a7 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -2,7 +2,7 @@ #include "tlibc/filesystem.h" #include "tlibc/time.h" #include "tlibc/base64.h" -#include "server.h" +#include "server/server_internal.h" #include "config.h" #include "log.h" #include "network/tcp-chat-protocol/v1.h" @@ -26,7 +26,7 @@ void Server_free(Server* self){ HashMap_destroy(&self->users_name_id_map); } -Result(Server*) Server_createFromConfig(cstr config_path){ +Result(Server*) Server_createFromConfig(str config_str){ Deferral(16); cstr log_ctx = "ServerInit"; logInfo(log_ctx, "parsing config"); @@ -36,16 +36,6 @@ Result(Server*) Server_createFromConfig(cstr config_path){ bool success = false; Defer(if(!success) Server_free(server)); - // open file - try(FILE* config_file, p, file_open(config_path, FO_ReadExisting)); - Defer(file_close(config_file)); - // read whole file into Array(char) - try(i64 config_file_size, i, file_getSize(config_file)); - Array(char) config_buf = Array_alloc(char, config_file_size); - Defer(free(config_buf.data)); - try_void(file_readBytesArray(config_file, config_buf)); - str config_str = Array_castTo_str(config_buf, false); - // parse name str tmp_str = str_null; try_void(config_findValue(config_str, STR("name"), &tmp_str, true)); @@ -150,26 +140,25 @@ static void* handleConnection(void* _args){ ResultVar(void) r = try_handleConnection(args, log_ctx); if(r.error){ + Error_addCallPos(r.error, ErrorCallPos_here()); str e_str = Error_toStr(r.error); logError(log_ctx, "%s", e_str.data); str_free(e_str); Error_free(r.error); } - + + logInfo(log_ctx, "session end"); + free(args); return NULL; } static Result(void) try_handleConnection(ConnectionHandlerArgs* args, cstr log_ctx){ Deferral(16); - Defer(free(args)); ClientConnection* conn = NULL; - Defer( - ClientConnection_close(conn); - logInfo(log_ctx, "session closed"); - ); // establish encrypted connection try(conn, p, ClientConnection_accept(args)); + Defer(ClientConnection_close(conn)); logInfo(log_ctx, "session accepted"); // handle requests diff --git a/src/server/server.h b/src/server/server_internal.h similarity index 85% rename from src/server/server.h rename to src/server/server_internal.h index 9f38cd7..243a3cc 100644 --- a/src/server/server.h +++ b/src/server/server_internal.h @@ -1,14 +1,31 @@ #pragma once +#include +#include "tlibc/collections/HashMap.h" +#include "tlibc/collections/List.h" +#include "tcp-chat/server.h" #include "cryptography/AES.h" #include "cryptography/RSA.h" #include "network/encrypted_sockets.h" #include "db/idb.h" -#include "tlibc/collections/HashMap.h" -#include "tlibc/collections/List.h" -#include "db_tables.h" -#include +#include "server/db_tables.h" + +typedef struct ClientConnection ClientConnection; + +typedef struct Server { + str name; + str description; + u64 landing_channel_id; + EndpointIPv4 local_end; + br_rsa_private_key rsa_sk; + br_rsa_public_key rsa_pk; + + IncrementalDB* db; + Table* db_users_table; + pthread_mutex_t users_cache_mutex; + List(User) users_cache_list; // index is id + HashMap(u64) users_name_id_map; //key is user name +} Server; -typedef struct Server Server; typedef struct ClientConnection { u64 session_id; @@ -30,21 +47,3 @@ Result(ClientConnection*) ClientConnection_accept(ConnectionHandlerArgs* args); void ClientConnection_close(ClientConnection* conn); -typedef struct Server { - str name; - str description; - u64 landing_channel_id; - EndpointIPv4 local_end; - br_rsa_private_key rsa_sk; - br_rsa_public_key rsa_pk; - - IncrementalDB* db; - Table* db_users_table; - pthread_mutex_t users_cache_mutex; - List(User) users_cache_list; // index is id - HashMap(u64) users_name_id_map; //key is user name -} Server; - -Result(Server*) Server_createFromConfig(cstr config_path); -void Server_free(Server* server); -Result(void) Server_run(Server* server);