added session_id generation
This commit is contained in:
parent
a0bcd2560a
commit
60a4542328
@ -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);
|
||||||
|
|||||||
@ -56,7 +56,7 @@ Result(void) client_run() {
|
|||||||
Defer(rl_free(command_input_prev));
|
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 && (command_input_raw = readline("> "))){
|
||||||
rl_free(command_input_prev);
|
rl_free(command_input_prev);
|
||||||
command_input_prev = command_input_raw;
|
command_input_prev = command_input_raw;
|
||||||
command_input = str_from_cstr(command_input_raw);
|
command_input = str_from_cstr(command_input_raw);
|
||||||
@ -104,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): ");
|
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);
|
||||||
|
|||||||
@ -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;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user