Compare commits

...

2 Commits

Author SHA1 Message Date
6bf06a7d3e removed libreadline dependency 2025-11-01 00:18:05 +05:00
60a4542328 added session_id generation 2025-10-31 23:53:43 +05:00
5 changed files with 42 additions and 31 deletions

2
dependencies/tlibc vendored

@ -1 +1 @@
Subproject commit a0affaa6d0e4764c9a906bba516c1f7d1ded5405 Subproject commit 75c94e88d9a7e12839d343bf4c7978cb7ecf91e1

View File

@ -34,13 +34,13 @@ case "$OS" in
EXEC_FILE="$PROJECT.exe" EXEC_FILE="$PROJECT.exe"
SHARED_LIB_FILE="$PROJECT.dll" SHARED_LIB_FILE="$PROJECT.dll"
INCLUDE="$INCLUDE " INCLUDE="$INCLUDE "
LINKER_LIBS="-lpthread -lws2_32 -lreadline" LINKER_LIBS="-lpthread -lws2_32"
;; ;;
LINUX) LINUX)
EXEC_FILE="$PROJECT" EXEC_FILE="$PROJECT"
SHARED_LIB_FILE="$PROJECT.so" SHARED_LIB_FILE="$PROJECT.so"
INCLUDE="$INCLUDE " INCLUDE="$INCLUDE "
LINKER_LIBS="-lreadline" LINKER_LIBS=""
;; ;;
*) *)
error "operating system $OS has no configuration variants" error "operating system $OS has no configuration variants"

View File

@ -67,9 +67,9 @@ Result(ServerConnection*) ServerConnection_open(ClientCredential* client_credent
EncryptedSocketTCP_construct(&conn->system_socket, _s, conn->session_key); EncryptedSocketTCP_construct(&conn->system_socket, _s, conn->session_key);
try_void(socket_connect(conn->system_socket.sock, conn->server_end)); try_void(socket_connect(conn->system_socket.sock, conn->server_end));
Array(u8) encrypted_buf = Array_alloc_size(64*1024); Array(u8) encrypted_buf = Array_alloc_size(8*1024);
Defer(free(encrypted_buf.data)); Defer(free(encrypted_buf.data));
Array(u8) decrypted_buf = Array_alloc_size(64*1024); Array(u8) decrypted_buf = Array_alloc_size(8*1024);
Defer(free(decrypted_buf.data)); Defer(free(decrypted_buf.data));
u32 encrypted_size = 0, decrypted_size = 0; u32 encrypted_size = 0, decrypted_size = 0;
@ -84,9 +84,9 @@ Result(ServerConnection*) ServerConnection_open(ClientCredential* client_credent
Array_construct_size(encrypted_buf.data, encrypted_size))); Array_construct_size(encrypted_buf.data, encrypted_size)));
// receive server response // receive server response
encrypted_size = sizeof(PacketHeader); encrypted_size = AESStreamEncryptor_calcDstSize(sizeof(PacketHeader));
try(decrypted_size, u, EncryptedSocketTCP_recv(&conn->system_socket, try(decrypted_size, u, EncryptedSocketTCP_recv(&conn->system_socket,
Array_construct_size(encrypted_buf.data, sizeof(PacketHeader)), Array_construct_size(encrypted_buf.data, encrypted_size),
decrypted_buf, decrypted_buf,
SocketRecvFlag_WaitAll)); SocketRecvFlag_WaitAll));
try_assert(decrypted_size == sizeof(PacketHeader)); try_assert(decrypted_size == sizeof(PacketHeader));
@ -96,18 +96,30 @@ Result(ServerConnection*) ServerConnection_open(ClientCredential* client_credent
switch(packet_header->type){ switch(packet_header->type){
case PacketType_ErrorMessage: case PacketType_ErrorMessage:
Array(u8) err_buf = Array_alloc_size(packet_header->content_size + 1); Array(u8) err_buf = Array_alloc_size(packet_header->content_size + 1);
bool err_msg_return = false; bool err_msg_completed = false;
Defer( Defer(
if(!err_msg_return) if(!err_msg_completed)
free(err_buf.data); free(err_buf.data);
); );
((u8*)err_buf.data)[packet_header->content_size] = 0; encrypted_size = AESStreamEncryptor_calcDstSize(packet_header->content_size);
try_void(EncryptedSocketTCP_recv(&conn->system_socket, encrypted_buf, err_buf, SocketRecvFlag_WaitAll)); if(encrypted_size > encrypted_buf.size)
err_msg_return = true; encrypted_size = encrypted_buf.size;
try_void(EncryptedSocketTCP_recv(&conn->system_socket,
Array_construct_size(encrypted_buf.data, encrypted_size),
err_buf,
SocketRecvFlag_WaitAll));
((u8*)err_buf.data)[encrypted_size] = 0;
err_msg_completed = true;
Return RESULT_ERROR((char*)err_buf.data, true); Return RESULT_ERROR((char*)err_buf.data, true);
break; case PacketType_ServerHandshake:
case PacketType_ClientHandshake: encrypted_size = AESStreamEncryptor_calcDstSize(sizeof(ServerHandshake) - sizeof(PacketHeader));
//TODO: receive the rest of the struct try_void(EncryptedSocketTCP_recv(&conn->system_socket,
Array_construct_size(encrypted_buf.data, encrypted_size),
Array_construct_size((u8*)decrypted_buf.data + sizeof(PacketHeader), decrypted_buf.size - sizeof(PacketHeader)),
SocketRecvFlag_WaitAll
));
ServerHandshake* server_handshake = decrypted_buf.data;
conn->session_id = server_handshake->session_id;
break; break;
default: default:
Return RESULT_ERROR_FMT("unexpected response type: %i", packet_header->type); Return RESULT_ERROR_FMT("unexpected response type: %i", packet_header->type);

View File

@ -1,9 +1,4 @@
// readline.h doesn't include stdio.h
// This bug is older than me)))
#include "client.h" #include "client.h"
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "term.h" #include "term.h"
static const str greeting_art = STR( static const str greeting_art = STR(
@ -46,25 +41,23 @@ Result(void) client_run() {
if(!term_init()){ if(!term_init()){
Return RESULT_ERROR("can't init terminal", false); Return RESULT_ERROR("can't init terminal", false);
} }
using_history();
fputs(greeting_art.data, stdout); fputs(greeting_art.data, stdout);
try_void(askUserNameAndPassword(&_client_credential)); try_void(askUserNameAndPassword(&_client_credential));
char* command_input_prev = NULL; Array(char) input_buf = Array_alloc(char, 10000);
char* command_input_raw = NULL;
Defer(rl_free(command_input_prev));
str command_input = str_null; str command_input = str_null;
bool stop = false; bool stop = false;
while((command_input_raw = readline("> ")) && !stop){ while(!stop){
rl_free(command_input_prev); fputs("> ", stdout);
command_input_prev = command_input_raw; if(fgets(input_buf.data, input_buf.size, stdin) == NULL)
command_input = str_from_cstr(command_input_raw); continue;
command_input = str_from_cstr(input_buf.data);
str_trim(&command_input, true); str_trim(&command_input, true);
if(command_input.size == 0) if(command_input.size == 0)
continue; continue;
add_history(command_input.data);
Result(void) com_result = commandExec(command_input, &stop); Result(void) com_result = commandExec(command_input, &stop);
if(com_result.error){ if(com_result.error){
str e_str = Error_toStr(com_result.error); str e_str = Error_toStr(com_result.error);
@ -74,6 +67,7 @@ Result(void) client_run() {
} }
} }
free(input_buf.data);
ClientCredential_free(_client_credential); ClientCredential_free(_client_credential);
ServerConnection_close(_server_connection); ServerConnection_close(_server_connection);
Return RESULT_VOID; Return RESULT_VOID;
@ -104,7 +98,7 @@ static Result(void) commandExec(str command, bool* stop){
else if (is_alias("j") || is_alias("join")){ else if (is_alias("j") || is_alias("join")){
ServerConnection_close(_server_connection); ServerConnection_close(_server_connection);
puts("Enter server address (ip:port): "); puts("Enter server address (ip:port:public_key): ");
fgets(answer_buf, answer_buf_size, stdin); fgets(answer_buf, answer_buf_size, stdin);
str new_server_link = str_from_cstr(answer_buf); str new_server_link = str_from_cstr(answer_buf);
str_trim(&new_server_link, true); str_trim(&new_server_link, true);
@ -123,7 +117,7 @@ static Result(void) commandExec(str command, bool* stop){
} }
else { else {
Return RESULT_ERROR_FMT("unknown kommand: '%s'\n" Return RESULT_ERROR_FMT("unknown kommand: '%s'\n"
"Use 'h' to see list of avaliable commands\n", "Use 'h' to see list of avaliable commands",
command.data); command.data);
} }

View File

@ -7,6 +7,7 @@ static void* handle_connection(void* _args);
typedef struct ConnectionHandlerArgs { typedef struct ConnectionHandlerArgs {
Socket accepted_socket; Socket accepted_socket;
EndpointIPv4 client_end; EndpointIPv4 client_end;
u64 session_id;
} ConnectionHandlerArgs; } ConnectionHandlerArgs;
Result(void) server_run(cstr server_endpoint_str){ Result(void) server_run(cstr server_endpoint_str){
@ -18,9 +19,12 @@ Result(void) server_run(cstr server_endpoint_str){
try_void(socket_bind(main_socket, server_end)); try_void(socket_bind(main_socket, server_end));
try_void(socket_listen(main_socket, 512)); try_void(socket_listen(main_socket, 512));
u64 session_id = 1;
while(true){ while(true){
ConnectionHandlerArgs* args = (ConnectionHandlerArgs*)malloc(sizeof(ConnectionHandlerArgs)); ConnectionHandlerArgs* args = (ConnectionHandlerArgs*)malloc(sizeof(ConnectionHandlerArgs));
try(args->accepted_socket, i, socket_accept(main_socket, &args->client_end)); try(args->accepted_socket, i, socket_accept(main_socket, &args->client_end));
args->session_id = session_id++;
pthread_t conn_thread = {0}; pthread_t conn_thread = {0};
try_stderrcode(pthread_create(&conn_thread, NULL, handle_connection, args)); try_stderrcode(pthread_create(&conn_thread, NULL, handle_connection, args));
} }
@ -31,6 +35,7 @@ Result(void) server_run(cstr server_endpoint_str){
static void* handle_connection(void* _args){ static void* handle_connection(void* _args){
Deferral(64); Deferral(64);
//ConnectionHandlerArgs* args = (ConnectionHandlerArgs*)_args; //ConnectionHandlerArgs* args = (ConnectionHandlerArgs*)_args;
Defer(free(_args));
// TODO: receive handshake and session key // TODO: receive handshake and session key
//ClientConnection conn; //ClientConnection conn;