created directory ./include/

This commit is contained in:
Timerix 2025-11-18 14:37:01 +05:00
parent eec45cac71
commit a1a11c10e2
20 changed files with 89 additions and 70 deletions

View File

@ -5,6 +5,7 @@
"defines": [], "defines": [],
"includePath": [ "includePath": [
"src", "src",
"include",
"dependencies/BearSSL/inc", "dependencies/BearSSL/inc",
"dependencies/BearSSL/src", "dependencies/BearSSL/src",
"dependencies/tlibc/include", "dependencies/tlibc/include",

2
dependencies/tlibc vendored

@ -1 +1 @@
Subproject commit 89aab2b5bffd46ec0538a5e7c2f1674d59d5677a Subproject commit bc415772486c087abd9cac9b9f2dc2b5dd5e98db

View File

@ -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);

View File

@ -35,7 +35,7 @@ OBJDIR="obj"
OUTDIR="bin" OUTDIR="bin"
STATIC_LIB_FILE="$PROJECT.a" 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 # OS-specific options
case "$OS" in case "$OS" in

View File

@ -1,7 +1,7 @@
#include "ClientCLI/ClientCLI.h" #include "cli/ClientCLI/ClientCLI.h"
#include "ClientCLI/db_tables.h" #include "cli/ClientCLI/db_tables.h"
#include "term.h" #include "cli/term.h"
#include "common_constants.h" #include "tcp-chat/common_constants.h"
#include "tlibc/time.h" #include "tlibc/time.h"
#include "tlibc/filesystem.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_askUserNameAndPassword(str* username_out, str* password_out);
static Result(void) ClientCLI_commandExec(ClientCLI* self, str command, bool* stop); static Result(void) ClientCLI_commandExec(ClientCLI* self, str command, bool* stop);
static Result(void) ClientCLI_openUserDB(ClientCLI* self); 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){ 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); ResultVar(void) com_result = ClientCLI_commandExec(self, command_input, &stop);
if(com_result.error){ if(com_result.error){
Error_addCallPos(com_result.error, ErrorCallPos_here());
str e_str = Error_toStr(com_result.error); str e_str = Error_toStr(com_result.error);
printf("%s\n", e_str.data); printf("%s\n", e_str.data);
str_free(e_str); 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); str_trim(&server_addr_str, true);
// ask server public key // ask server public key
printf("Enter server public key (RSA-Public-<SIZE>:<DATA_BASE64>):\n"); printf("Enter server public key (RSA-Public-<SIZE>:<DATA>):\n");
char server_pk_cstr[PUBLIC_KEY_BASE64_SIZE_MAX + 1]; char server_pk_cstr[PUBLIC_KEY_BASE64_SIZE_MAX + 1];
try_void(term_readLine(server_pk_cstr, sizeof(server_pk_cstr))); try_void(term_readLine(server_pk_cstr, sizeof(server_pk_cstr)));
str server_pk_str = str_from_cstr(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"); printf("connection established\n");
// show server info // show server info
// printf("server name: %s\n", client->server_connection->server_name.data); str server_name = str_null;
// printf("server description: %s\n", client->server_connection->server_description.data); 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: ask in loop: log in / register
//TODO: call Client_runIO(): //TODO: call Client_runIO():
@ -205,7 +212,9 @@ static Result(void) ClientCLI_openUserDB(ClientCLI* self){
Return RESULT_VOID; 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); Deferral(8);
ServerInfo si; 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; si.pk_base64[si.pk_base64_len] = 0;
// name // name
str server_name = str_null;
try_void(Client_getServerName(self->client, &server_name));
si.name_len = server_name.size; si.name_len = server_name.size;
memcpy(si.name, server_name.data, si.name_len); memcpy(si.name, server_name.data, si.name_len);
si.name[si.name_len] = 0; si.name[si.name_len] = 0;
// description // description
str server_description = str_null;
try_void(Client_getServerName(self->client, &server_description));
si.desc_len = server_name.size; si.desc_len = server_name.size;
memcpy(si.desc, server_description.data, si.desc_len); memcpy(si.desc, server_description.data, si.desc_len);
si.desc[si.desc_len] = 0; si.desc[si.desc_len] = 0;

View File

@ -1,5 +1,5 @@
#pragma once #pragma once
#include "client/client.h" #include "tcp-chat/client.h"
#include "db/idb.h" #include "db/idb.h"
typedef struct ClientCLI { typedef struct ClientCLI {

View File

@ -1,5 +1,5 @@
#pragma once #pragma once
#include "common_constants.h" #include "tcp-chat/common_constants.h"
#include "tlibc/time.h" #include "tlibc/time.h"
typedef struct ServerInfo { typedef struct ServerInfo {

24
src/main.c → src/cli/main.c Executable file → Normal file
View File

@ -1,8 +1,9 @@
#include "network/network.h"
#include "ClientCLI/ClientCLI.h"
#include "server/server.h"
#include "tlibc/tlibc.h" #include "tlibc/tlibc.h"
#include "tlibc/base64.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_CLIENT "tcp-chat-client.config"
#define _DEFAULT_CONFIG_PATH_SERVER "tcp-chat-server.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) if(!config_path)
config_path = _DEFAULT_CONFIG_PATH_SERVER; 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)); 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)); try_fatal_void(Server_run(server));
break; break;
} }

View File

@ -1,5 +1,5 @@
#pragma once #pragma once
#include "client.h" #include "tcp-chat/client.h"
#include "cryptography/AES.h" #include "cryptography/AES.h"
#include "cryptography/RSA.h" #include "cryptography/RSA.h"
#include "network/encrypted_sockets.h" #include "network/encrypted_sockets.h"

View File

@ -2,7 +2,7 @@
#include "tlibc/collections/Array.h" #include "tlibc/collections/Array.h"
#include "tlibc/errors.h" #include "tlibc/errors.h"
#include "bearssl_rand.h" #include "bearssl_rand.h"
#include "common_constants.h" #include "tcp-chat/common_constants.h"
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
// // // //

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include "tlibc/errors.h" #include "tlibc/errors.h"
#include "magic.h" #include "magic.h"
#include "common_constants.h" #include "tcp-chat/common_constants.h"
#define AES_SESSION_KEY_SIZE 32 #define AES_SESSION_KEY_SIZE 32

View File

@ -1,4 +1,4 @@
#include "server.h" #include "server/server_internal.h"
#include "network/tcp-chat-protocol/v1.h" #include "network/tcp-chat-protocol/v1.h"
void ClientConnection_close(ClientConnection* conn){ void ClientConnection_close(ClientConnection* conn){

View File

@ -1,5 +1,5 @@
#pragma once #pragma once
#include "common_constants.h" #include "tcp-chat/common_constants.h"
#include "tlibc/time.h" #include "tlibc/time.h"
typedef struct User { typedef struct User {

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "network/tcp-chat-protocol/v1.h" #include "network/tcp-chat-protocol/v1.h"
#include "server/server.h" #include "server/server_internal.h"
#include "log.h" #include "log.h"

View File

@ -2,7 +2,7 @@
#include "tlibc/filesystem.h" #include "tlibc/filesystem.h"
#include "tlibc/time.h" #include "tlibc/time.h"
#include "tlibc/base64.h" #include "tlibc/base64.h"
#include "server.h" #include "server/server_internal.h"
#include "config.h" #include "config.h"
#include "log.h" #include "log.h"
#include "network/tcp-chat-protocol/v1.h" #include "network/tcp-chat-protocol/v1.h"
@ -26,7 +26,7 @@ void Server_free(Server* self){
HashMap_destroy(&self->users_name_id_map); HashMap_destroy(&self->users_name_id_map);
} }
Result(Server*) Server_createFromConfig(cstr config_path){ Result(Server*) Server_createFromConfig(str config_str){
Deferral(16); Deferral(16);
cstr log_ctx = "ServerInit"; cstr log_ctx = "ServerInit";
logInfo(log_ctx, "parsing config"); logInfo(log_ctx, "parsing config");
@ -36,16 +36,6 @@ Result(Server*) Server_createFromConfig(cstr config_path){
bool success = false; bool success = false;
Defer(if(!success) Server_free(server)); 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 // parse name
str tmp_str = str_null; str tmp_str = str_null;
try_void(config_findValue(config_str, STR("name"), &tmp_str, true)); 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); ResultVar(void) r = try_handleConnection(args, log_ctx);
if(r.error){ if(r.error){
Error_addCallPos(r.error, ErrorCallPos_here());
str e_str = Error_toStr(r.error); str e_str = Error_toStr(r.error);
logError(log_ctx, "%s", e_str.data); logError(log_ctx, "%s", e_str.data);
str_free(e_str); str_free(e_str);
Error_free(r.error); Error_free(r.error);
} }
logInfo(log_ctx, "session end");
free(args);
return NULL; return NULL;
} }
static Result(void) try_handleConnection(ConnectionHandlerArgs* args, cstr log_ctx){ static Result(void) try_handleConnection(ConnectionHandlerArgs* args, cstr log_ctx){
Deferral(16); Deferral(16);
Defer(free(args));
ClientConnection* conn = NULL; ClientConnection* conn = NULL;
Defer(
ClientConnection_close(conn);
logInfo(log_ctx, "session closed");
);
// establish encrypted connection // establish encrypted connection
try(conn, p, ClientConnection_accept(args)); try(conn, p, ClientConnection_accept(args));
Defer(ClientConnection_close(conn));
logInfo(log_ctx, "session accepted"); logInfo(log_ctx, "session accepted");
// handle requests // handle requests

View File

@ -1,14 +1,31 @@
#pragma once #pragma once
#include <pthread.h>
#include "tlibc/collections/HashMap.h"
#include "tlibc/collections/List.h"
#include "tcp-chat/server.h"
#include "cryptography/AES.h" #include "cryptography/AES.h"
#include "cryptography/RSA.h" #include "cryptography/RSA.h"
#include "network/encrypted_sockets.h" #include "network/encrypted_sockets.h"
#include "db/idb.h" #include "db/idb.h"
#include "tlibc/collections/HashMap.h" #include "server/db_tables.h"
#include "tlibc/collections/List.h"
#include "db_tables.h" typedef struct ClientConnection ClientConnection;
#include <pthread.h>
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 { typedef struct ClientConnection {
u64 session_id; u64 session_id;
@ -30,21 +47,3 @@ Result(ClientConnection*) ClientConnection_accept(ConnectionHandlerArgs* args);
void ClientConnection_close(ClientConnection* conn); 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);