From 7ffb9b1d9cb149132422dfdae0b72c02d1c0e0b1 Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Sat, 20 Jan 2024 15:48:22 +0100 Subject: [PATCH] fixed client segfault on ssqc --- include/chat/config.h | 4 ++++ include/chat/misc.h | 3 +++ src/chat/main/client.c | 27 ++++++++++++++++++++++++--- src/chat/misc.c | 28 ++++++++++++++++++++++++++-- 4 files changed, 57 insertions(+), 5 deletions(-) diff --git a/include/chat/config.h b/include/chat/config.h index 101e048..1df6199 100644 --- a/include/chat/config.h +++ b/include/chat/config.h @@ -22,6 +22,10 @@ along with this program. If not, see . //MACROS #define WHY2_CHAT_CODE_ACCEPT_MESSAGES "code_000" #define WHY2_CHAT_CODE_PICK_USERNAME "code_001" +#define WHY2_CHAT_CODE_SERVER_SIDE_QUIT_COMMUNICATION "code_002" + +//SHORTCUTS CAUSE I'M LAZY BITCH +#define WHY2_CHAT_CODE_SSQC WHY2_CHAT_CODE_SERVER_SIDE_QUIT_COMMUNICATION #define WHY2_CONFIG_DIR "/home/{USER}/.config" #define WHY2_CHAT_CONFIG_DIR WHY2_CONFIG_DIR "/WHY2" diff --git a/include/chat/misc.h b/include/chat/misc.h index f58ee06..491d172 100644 --- a/include/chat/misc.h +++ b/include/chat/misc.h @@ -19,11 +19,14 @@ along with this program. If not, see . #ifndef WHY2_CHAT_MISC_H #define WHY2_CHAT_MISC_H +#include //TODO: fuck this + void why2_send_socket(char *text, char *username, int socket); //send socket.... wtf did you expect char *why2_read_socket(int socket); //read lol void *why2_communicate_thread(void *arg); //COMMUNICATION THREAD void *why2_accept_thread(void *socket); //LOOP ACCEPTING CONNECTIONS void why2_clean_threads(void); //CLOSE EVERY RUNNING THREAD void *why2_listen_server(void *socket); //LISTEN FOR OTHER's USERS MESSAGES +void *why2_getline_thread(WHY2_UNUSED void* arg); //START getline IN SEPARATE THREAD #endif \ No newline at end of file diff --git a/src/chat/main/client.c b/src/chat/main/client.c index eef8bb0..3f31720 100644 --- a/src/chat/main/client.c +++ b/src/chat/main/client.c @@ -37,8 +37,11 @@ int main(void) int listen_socket = socket(AF_INET, SOCK_STREAM, 0); //CREATE SERVER SOCKET char *line = NULL; + void *return_line = NULL; size_t line_length = 0; pthread_t thread_buffer; + pthread_t thread_getline; + why2_bool ssqc = 0; //DEFINE SERVER ADDRESS struct sockaddr_in server_addr; @@ -58,6 +61,8 @@ int main(void) } printf("\n\n\n"); + free(line); //PREVENT FROM MEMORY LEAK + int connectStatus = connect(listen_socket, (WHY2_SA *) &server_addr, sizeof(server_addr)); //CONNECT if (connectStatus < 0) why2_die("Connecting failed."); @@ -66,7 +71,16 @@ int main(void) for (;;) { - if (getline(&line, &line_length, stdin) == -1) why2_die("Reading input failed."); + pthread_create(&thread_getline, NULL, why2_getline_thread, NULL); + pthread_join(thread_getline, &return_line); + + if (return_line == WHY2_INVALID_POINTER) //SERVER QUIT COMMUNICATION + { + ssqc = 1; + break; + } + + line = (char*) return_line; printf(WHY2_CLEAR_AND_GO_UP); @@ -77,9 +91,16 @@ int main(void) printf("Exiting...\n"); break; } + + free(return_line); + } + + //DEALLOCATION + if (!ssqc) + { + pthread_cancel(thread_buffer); + free(return_line); } - pthread_cancel(thread_buffer); - free(line); return 0; } \ No newline at end of file diff --git a/src/chat/misc.c b/src/chat/misc.c index 10b6c1b..0f6d085 100644 --- a/src/chat/misc.c +++ b/src/chat/misc.c @@ -36,6 +36,8 @@ along with this program. If not, see . #include #include +pthread_t getline_thread; + //LINKED LIST STUFF typedef struct _connection_node { @@ -397,6 +399,8 @@ void *why2_communicate_thread(void *arg) raw_ptr = NULL; } + why2_send_socket(WHY2_CHAT_CODE_SSQC, WHY2_CHAT_SERVER_USERNAME, connection); + printf("User disconnected.\t%d\n", connection); //DEALLOCATION @@ -404,7 +408,7 @@ void *why2_communicate_thread(void *arg) close(connection); why2_list_remove(&connection_list, find_connection(connection)); - return NULL; //TODO: Fix client segfault on timeout + return NULL; } char *why2_read_socket(int socket) @@ -475,6 +479,7 @@ void why2_clean_threads(void) void *why2_listen_server(void *socket) { char *read = NULL; + why2_bool exiting = 0; printf(">>> "); fflush(stdout); @@ -485,7 +490,14 @@ void *why2_listen_server(void *socket) if (strncmp(read, WHY2_CHAT_SERVER_USERNAME ": code", 12) == 0) //CODE WAS SENT { - if (strcmp(read + 8, WHY2_CHAT_CODE_PICK_USERNAME) == 0) //PICK USERNAME + if (strcmp(read + 8, WHY2_CHAT_CODE_SSQC) == 0) + { + printf("%s\nServer closed the connection.\n", WHY2_CLEAR_AND_GO_UP); + fflush(stdout); + + pthread_cancel(getline_thread); //CANCEL CLIENT getline + exiting = 1; //EXIT THIS THREAD + } else if (strcmp(read + 8, WHY2_CHAT_CODE_PICK_USERNAME) == 0) //PICK USERNAME { //TODO: Check username } @@ -497,7 +509,19 @@ void *why2_listen_server(void *socket) } why2_deallocate(read); + if (exiting) break; } return NULL; +} + +void *why2_getline_thread(WHY2_UNUSED void* arg) +{ + getline_thread = pthread_self(); + + char *line = NULL; + size_t line_length = 0; + if (getline(&line, &line_length, stdin) == -1) why2_die("Reading input failed."); + + return line; } \ No newline at end of file