Skip to content

Commit

Permalink
💥 tons of bugs & memory fault
Browse files Browse the repository at this point in the history
  • Loading branch information
FelipeFTN committed Dec 30, 2022
1 parent 37358af commit 73294ba
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 35 deletions.
10 changes: 5 additions & 5 deletions client/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions client/socket/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <string.h>
#include <stdio.h>

// TODO: Fix this mess.

// Set up socket
int init_socket(int *client, int *sock)
{
Expand Down
2 changes: 1 addition & 1 deletion server/handler/handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
62 changes: 33 additions & 29 deletions server/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 73294ba

Please sign in to comment.