From 73294ba5c1351cb0525607277448745432d24e2e Mon Sep 17 00:00:00 2001 From: FelipeFTN Date: Thu, 29 Dec 2022 21:33:16 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=A5=20tons=20of=20bugs=20&=20memory=20?= =?UTF-8?q?fault?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/main.c | 10 +++---- client/socket/socket.c | 2 ++ server/handler/handler.c | 2 +- server/main.c | 62 +++++++++++++++++++++------------------- 4 files changed, 41 insertions(+), 35 deletions(-) diff --git a/client/main.c b/client/main.c index f3bfa95..4bbaf3e 100644 --- a/client/main.c +++ b/client/main.c @@ -4,11 +4,11 @@ #include "socket/socket.h" int main() { - char *handshake = "MySecurity - HandShake"; - char message[1024] = { 0 }; + char *handshake = "Handshake"; int client, error, sock = 0; + char message[256] = { 0 }; + char command[16] = { 0 }; bool connected; - char *command; // Start Client error = init_socket(&client, &sock); @@ -29,8 +29,8 @@ int main() { return -1; // Send to server - printf("< "); - scanf("%s", command); + printf("> "); + scanf("%[^\n]", command); error = send_socket(sock, command); if (error < 0) return -1; diff --git a/client/socket/socket.c b/client/socket/socket.c index 33a4c71..75c82bc 100644 --- a/client/socket/socket.c +++ b/client/socket/socket.c @@ -12,6 +12,8 @@ #include #include +// TODO: Fix this mess. + // Set up socket int init_socket(int *client, int *sock) { diff --git a/server/handler/handler.c b/server/handler/handler.c index 037812c..8178538 100644 --- a/server/handler/handler.c +++ b/server/handler/handler.c @@ -17,7 +17,7 @@ // Get command options char *get_commands() { - char *message = "===== MySecurity =====\n\n0.Shutdown Computer\n1.Turn off MySecurity\n2.Exit\n"; + char *message = "===== MySecurity =====\n\n[0] Shutdown Computer\n[1] Turn off MySecurity\n[2] Exit\n"; char *message_ptr = message; return message_ptr; diff --git a/server/main.c b/server/main.c index aefabb2..54a9f87 100644 --- a/server/main.c +++ b/server/main.c @@ -15,9 +15,9 @@ int main() { bool connected, handshaked; - char option[1024] = { 0 }; + char option[8] = { 0 }; bool mysecurity = true; - char handshake[64]; + char handshake[16]; int client; int sock; int error; @@ -33,37 +33,41 @@ int main() // While user does not choose 'Exit' while (connected) { - // Receive client's handshake - error = receive_socket(client, handshake); - if (error < 0) - close_socket(client, sock); - handshaked = strstr(handshake, "HandShake"); - if (!handshaked) - { - printf("[x] Could not handshake - `%s`\n", handshake); - close_socket(client, sock); - return -1; - } + // Receive client's handshake + error = receive_socket(client, handshake); + if (error < 0) + close_socket(client, sock); - // Send options to message - error = send_socket(client, get_commands()); - if (error < 0) - close_socket(client, sock); + handshaked = strstr(handshake, "Handshake"); - // Get message sended from client - error = receive_socket(client, option); - if (error < 0) - close_socket(client, sock); + if (handshaked) + { + printf("[+] Handshaked!\n"); - // Handle user option - error = run_command(option, &mysecurity, &connected); - if (error < 0) - { - error = send_socket(client, "[!] Invalid option"); - if (error < 0) - close_socket(client, sock); - } + // Send options to message + error = send_socket(client, get_commands()); + if (error < 0) + close_socket(client, sock); + + // Get message sended from client + error = receive_socket(client, option); + if (error < 0) + close_socket(client, sock); + + // Handle user option + error = run_command(option, &mysecurity, &connected); + if (error < 0) + { + error = send_socket(client, "[!] Invalid option"); + if (error < 0) + close_socket(client, sock); + } + } else { + printf("[x] Could not handshake - `%s`\n", handshake); + close_socket(client, sock); + return -1; + } } } return 0;