Compare commits
No commits in common. "6bf06a7d3e6ee492a937224cdf6ea4874555a714" and "a0bcd2560a987767aaedc7f6e2ab605e65caca1f" have entirely different histories.
6bf06a7d3e
...
a0bcd2560a
2
dependencies/tlibc
vendored
2
dependencies/tlibc
vendored
@ -1 +1 @@
|
|||||||
Subproject commit 75c94e88d9a7e12839d343bf4c7978cb7ecf91e1
|
Subproject commit a0affaa6d0e4764c9a906bba516c1f7d1ded5405
|
||||||
@ -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"
|
LINKER_LIBS="-lpthread -lws2_32 -lreadline"
|
||||||
;;
|
;;
|
||||||
LINUX)
|
LINUX)
|
||||||
EXEC_FILE="$PROJECT"
|
EXEC_FILE="$PROJECT"
|
||||||
SHARED_LIB_FILE="$PROJECT.so"
|
SHARED_LIB_FILE="$PROJECT.so"
|
||||||
INCLUDE="$INCLUDE "
|
INCLUDE="$INCLUDE "
|
||||||
LINKER_LIBS=""
|
LINKER_LIBS="-lreadline"
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
error "operating system $OS has no configuration variants"
|
error "operating system $OS has no configuration variants"
|
||||||
|
|||||||
@ -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(8*1024);
|
Array(u8) encrypted_buf = Array_alloc_size(64*1024);
|
||||||
Defer(free(encrypted_buf.data));
|
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));
|
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 = AESStreamEncryptor_calcDstSize(sizeof(PacketHeader));
|
encrypted_size = 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, encrypted_size),
|
Array_construct_size(encrypted_buf.data, sizeof(PacketHeader)),
|
||||||
decrypted_buf,
|
decrypted_buf,
|
||||||
SocketRecvFlag_WaitAll));
|
SocketRecvFlag_WaitAll));
|
||||||
try_assert(decrypted_size == sizeof(PacketHeader));
|
try_assert(decrypted_size == sizeof(PacketHeader));
|
||||||
@ -96,30 +96,18 @@ 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_completed = false;
|
bool err_msg_return = false;
|
||||||
Defer(
|
Defer(
|
||||||
if(!err_msg_completed)
|
if(!err_msg_return)
|
||||||
free(err_buf.data);
|
free(err_buf.data);
|
||||||
);
|
);
|
||||||
encrypted_size = AESStreamEncryptor_calcDstSize(packet_header->content_size);
|
((u8*)err_buf.data)[packet_header->content_size] = 0;
|
||||||
if(encrypted_size > encrypted_buf.size)
|
try_void(EncryptedSocketTCP_recv(&conn->system_socket, encrypted_buf, err_buf, SocketRecvFlag_WaitAll));
|
||||||
encrypted_size = encrypted_buf.size;
|
err_msg_return = true;
|
||||||
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);
|
||||||
case PacketType_ServerHandshake:
|
break;
|
||||||
encrypted_size = AESStreamEncryptor_calcDstSize(sizeof(ServerHandshake) - sizeof(PacketHeader));
|
case PacketType_ClientHandshake:
|
||||||
try_void(EncryptedSocketTCP_recv(&conn->system_socket,
|
//TODO: receive the rest of the struct
|
||||||
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);
|
||||||
|
|||||||
@ -1,4 +1,9 @@
|
|||||||
|
// 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(
|
||||||
@ -41,23 +46,25 @@ 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));
|
||||||
|
|
||||||
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;
|
str command_input = str_null;
|
||||||
bool stop = false;
|
bool stop = false;
|
||||||
while(!stop){
|
while((command_input_raw = readline("> ")) && !stop){
|
||||||
fputs("> ", stdout);
|
rl_free(command_input_prev);
|
||||||
if(fgets(input_buf.data, input_buf.size, stdin) == NULL)
|
command_input_prev = command_input_raw;
|
||||||
continue;
|
command_input = str_from_cstr(command_input_raw);
|
||||||
|
|
||||||
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);
|
||||||
@ -67,7 +74,6 @@ 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;
|
||||||
@ -98,7 +104,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:public_key): ");
|
puts("Enter server address (ip:port): ");
|
||||||
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);
|
||||||
@ -117,7 +123,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",
|
"Use 'h' to see list of avaliable commands\n",
|
||||||
command.data);
|
command.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -7,7 +7,6 @@ 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){
|
||||||
@ -19,12 +18,9 @@ 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));
|
||||||
}
|
}
|
||||||
@ -35,7 +31,6 @@ 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;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user