From dcc1d277ab13f855823ac312082768d51da5e990 Mon Sep 17 00:00:00 2001 From: Warwick Date: Wed, 16 Aug 2023 13:33:18 +0100 Subject: [PATCH] Fixed Login message and made unpacking work better --- src/server/main.c | 2 +- urchin-util/include/message.h | 2 +- urchin-util/include/message_internal.h | 7 +++++ urchin-util/src/message.c | 40 ++++++++++++++++++++++---- 4 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 urchin-util/include/message_internal.h diff --git a/src/server/main.c b/src/server/main.c index 904f363..032f37e 100644 --- a/src/server/main.c +++ b/src/server/main.c @@ -39,7 +39,7 @@ int main(int argc, char *argv[]) { cli_sockfd = msg__accept_cli_connection(serv_sockfd); // Require login as first request - msg__recv_login(&serv_sockfd); + msg__recv_login(&cli_sockfd); while (1) { // Empty message buffer diff --git a/urchin-util/include/message.h b/urchin-util/include/message.h index 48e3ba1..eafe1a3 100644 --- a/urchin-util/include/message.h +++ b/urchin-util/include/message.h @@ -1,6 +1,7 @@ #pragma once #include "error.h" +#include "message_internal.h" #include //sockaddr_in #include #include @@ -24,7 +25,6 @@ int msg__accept_cli_connection(int server_sockfd); void msg__recv_login(int *con_sockfd); void msg__req_login(int *con_sockfd, char *usernm, char *passwd); - // send and recive commands void msg__recv_command(int *con_sockfd); void msg__req_command(int *con_sockfd, char *usernm, char *passwd); diff --git a/urchin-util/include/message_internal.h b/urchin-util/include/message_internal.h new file mode 100644 index 0000000..bcc738b --- /dev/null +++ b/urchin-util/include/message_internal.h @@ -0,0 +1,7 @@ +#pragma once +#include "error.h" +#include + +void msg__read_from_socket(int *sockfd, uint8_t *msgbuf, unsigned bufsize); + +size_t get_msg_size_in_buffer(unsigned max_length, uint8_t *out); diff --git a/urchin-util/src/message.c b/urchin-util/src/message.c index f644eb9..48a6824 100644 --- a/urchin-util/src/message.c +++ b/urchin-util/src/message.c @@ -1,7 +1,9 @@ #include "message.h" -#include "world_structures.h" +#include "error.h" #include "protobuf_gen/amessage.pb-c.h" #include "protobuf_gen/player.pb-c.h" +#include "world_structures.h" +#include #define MAX_MSG_SIZE 1024 @@ -44,18 +46,38 @@ void msg__amsg_test(int a) { amessage__free_unpacked(msg2, NULL); } +// Gets size of protobuf message inside bigger buffer +size_t get_msg_size_in_buffer(unsigned max_length, uint8_t *out) { + size_t cur_len = 0; + while ((out[cur_len]) != 0) { + cur_len++; + if (cur_len == max_length) { + err__warn("max message length exceeded\n"); + return cur_len; + } + } + return cur_len; +} + void msg__recv_login(int *con_sockfd) { uint8_t msgbuf[MAX_MSG_SIZE]; bzero(msgbuf, sizeof msgbuf); - // TODO: Figure out how to terminate based on end of req if (read(*con_sockfd, msgbuf, sizeof msgbuf) < 0) { err__warn("Connection did not send login data"); + perror("Error: "); } - PlayerLogin *pl = player_login__unpack(NULL, sizeof msgbuf, msgbuf); + size_t msg_len = get_msg_size_in_buffer(MAX_MSG_SIZE, msgbuf); + PlayerLogin *pl = player_login__unpack(NULL, msg_len, msgbuf); if (pl == NULL) { err__warn("Error unpacking login message"); + printf("unpacked size %zu \n", msg_len); + // fwrite(msgbuf, sizeof msgbuf, 1, stderr); + fwrite(msgbuf, msg_len, 1, stderr); + perror("Error: "); + + player_login__free_unpacked(pl, NULL); return; } @@ -64,6 +86,8 @@ void msg__recv_login(int *con_sockfd) { snprintf(msg, sizeof msg, "Recieved Login: %s %s \n", pl->username, pl->password); err__log(msg); + + player_login__free_unpacked(pl, NULL); } void msg__req_login(int *con_sockfd, char *usernm, char *passwd) { @@ -78,14 +102,15 @@ void msg__req_login(int *con_sockfd, char *usernm, char *passwd) { msgbuf = malloc(len); player_login__pack(&pl, msgbuf); - if (write(con_sockfd, msgbuf, len) < 0) { + if (write(*con_sockfd, msgbuf, len) < 0) { err__crash("Couldn't write response message"); free(msgbuf); return; } - fprintf(stderr, "Writing %d serialized bytes\n", - len); // See the length of message + // See the length of message + fprintf(stderr, "Writing %d serialized bytes\n", len); fwrite(msgbuf, len, 1, stderr); + free(msgbuf); } @@ -138,3 +163,6 @@ int msg__accept_cli_connection(int server_sockfd) { return cli_sockfd; } + +void msg__recv_command(int *con_sockfd) {} +void msg__req_command(int *con_sockfd, char *usernm, char *passwd);