Compare commits

..

No commits in common. "6bf06a7d3e6ee492a937224cdf6ea4874555a714" and "a0bcd2560a987767aaedc7f6e2ab605e65caca1f" have entirely different histories.

5 changed files with 31 additions and 42 deletions

2
dependencies/tlibc vendored

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

View File

@ -34,13 +34,13 @@ case "$OS" in
EXEC_FILE="$PROJECT.exe"
SHARED_LIB_FILE="$PROJECT.dll"
INCLUDE="$INCLUDE "
LINKER_LIBS="-lpthread -lws2_32"
LINKER_LIBS="-lpthread -lws2_32 -lreadline"
;;
LINUX)
EXEC_FILE="$PROJECT"
SHARED_LIB_FILE="$PROJECT.so"
INCLUDE="$INCLUDE "
LINKER_LIBS=""
LINKER_LIBS="-lreadline"
;;
*)
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);
try_void(socket_connect(conn->system_socket.sock, conn->server_end));
Array(u8) encrypted_buf = Array_alloc_size(8*1024);
Array(u8) encrypted_buf = Array_alloc_size(64*1024);
Defer(free(encrypted_buf.data));
Array(u8) decrypted_buf = Array_alloc_size(8*1024);
Array(u8) decrypted_buf = Array_alloc_size(64*1024);
Defer(free(decrypted_buf.data));
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)));
// receive server response
encrypted_size = AESStreamEncryptor_calcDstSize(sizeof(PacketHeader));
encrypted_size = sizeof(PacketHeader);
try(decrypted_size, u, EncryptedSocketTCP_recv(&conn->system_socket,
Array_construct_size(encrypted_buf.data, encrypted_size),
Array_construct_size(encrypted_buf.data, sizeof(PacketHeader)),
decrypted_buf,
SocketRecvFlag_WaitAll));
try_assert(decrypted_size == sizeof(PacketHeader));
@ -96,30 +96,18 @@ Result(ServerConnection*) ServerConnection_open(ClientCredential* client_credent
switch(packet_header->type){
case PacketType_ErrorMessage:
Array(u8) err_buf = Array_alloc_size(packet_header->content_size + 1);
bool err_msg_completed = false;
bool err_msg_return = false;
Defer(
if(!err_msg_completed)
if(!err_msg_return)
free(err_buf.data);
);
encrypted_size = AESStreamEncryptor_calcDstSize(packet_header->content_size);
if(encrypted_size > encrypted_buf.size)
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;
((u8*)err_buf.data)[packet_header->content_size] = 0;
try_void(EncryptedSocketTCP_recv(&conn->system_socket, encrypted_buf, err_buf, SocketRecvFlag_WaitAll));
err_msg_return = true;
Return RESULT_ERROR((char*)err_buf.data, true);
case PacketType_ServerHandshake:
encrypted_size = AESStreamEncryptor_calcDstSize(sizeof(ServerHandshake) - sizeof(PacketHeader));
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;
case PacketType_ClientHandshake:
//TODO: receive the rest of the struct
break;
default:
Return RESULT_ERROR_FMT("unexpected response type: %i", packet_header->type);

View File

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

View File

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