WHY2/src/chat/misc.c

472 lines
12 KiB
C
Raw Normal View History

/*
This is part of WHY2
Copyright (C) 2022 Václav Šmejkal
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2023-02-21 07:58:12 +01:00
#include <why2/chat/misc.h>
2023-03-12 14:59:07 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
2023-02-21 10:53:09 +01:00
#include <unistd.h>
2023-02-21 07:58:12 +01:00
2023-03-12 14:59:07 +01:00
#include <pthread.h>
#include <json-c/json.h>
2023-03-12 15:02:06 +01:00
#include <why2/chat/flags.h>
#include <why2/llist.h>
2023-02-21 07:58:12 +01:00
#include <why2/memory.h>
#include <why2/misc.h>
2023-02-21 07:58:12 +01:00
//LINKED LIST STUFF
typedef struct _connection_node
{
int connection;
pthread_t thread;
} connection_node_t; //SINGLE LINKED LIST
why2_list_t connection_list = WHY2_LIST_EMPTY;
why2_list_t waiting_list = WHY2_LIST_EMPTY;
2023-03-25 17:39:52 +01:00
char *get_string_from_json(struct json_object *json, char *string)
{
struct json_object *object;
json_object_object_get_ex(json, string, &object);
return (char*) json_object_get_string(object);
}
char *get_string_from_json_string(char *json, char *string)
{
struct json_object *json_obj = json_tokener_parse(json);
char *returning = get_string_from_json(json_obj, string);
//DEALLOCATION
json_object_put(json_obj);
//GET STRINGS
return returning;
}
void *send_to_all(void *json)
{
2023-04-20 09:28:39 +02:00
why2_node_t _first_node = (why2_node_t) { NULL, connection_list.head };
why2_node_t *node_buffer = &_first_node;
connection_node_t connection_buffer;
2023-03-25 17:39:52 +01:00
//PARSE
struct json_object *json_obj = json_tokener_parse((char*) json);
2023-03-31 18:12:56 +02:00
if (json_obj == NULL) return NULL; //EXIT IF INVALID SYNTAX WAS SENT
while (node_buffer -> next != NULL) //SEND TO ALL CONNECTIONS
{
2023-04-20 09:28:39 +02:00
node_buffer = node_buffer -> next;
connection_buffer = *(connection_node_t*) node_buffer -> value;
why2_send_socket(get_string_from_json(json_obj, "message"), get_string_from_json(json_obj, "username"), connection_buffer.connection); //SEND TO CLIENT
}
2023-03-25 17:39:52 +01:00
//DEALLOCATION
json_object_put(json_obj);
return NULL;
}
void append(char **destination, char *key, char *value)
{
char *output = why2_calloc(strlen(*destination) + strlen(key) + strlen(value) + 7, sizeof(char));
sprintf(output, "%s%s\"%s\":\"%s\"", *destination, strcmp(*destination, "") == 0 ? "" : ",", key, value);
why2_deallocate(*destination);
*destination = output;
}
void add_brackets(char **json)
{
char *output = why2_calloc(strlen(*json) + 3, sizeof(char));
sprintf(output, "{%s}", *json);
why2_deallocate(*json);
*json = output;
}
2023-03-25 16:35:44 +01:00
char *read_socket_raw(int socket)
{
if (socket == -1)
{
fprintf(stderr, "Reading socket failed.\n");
return NULL;
}
unsigned short content_size = 0;
char *content_buffer = why2_calloc(3, sizeof(char));
//GET LENGTH
if (recv(socket, content_buffer, 2, 0) != 2)
{
fprintf(stderr, "Getting message length failed!\n");
return NULL;
}
content_size = (unsigned short) (((unsigned) content_buffer[1] << 8) | content_buffer[0]);
why2_deallocate(content_buffer);
//ALLOCATE
content_buffer = why2_calloc(content_size + 1, sizeof(char));
//READ JSON MESSAGE
if (recv(socket, content_buffer, content_size, 0) != content_size - 2) fprintf(stderr, "Socket probably read wrongly!\n");
content_buffer[content_size - 2] = '\0'; //TODO: Possible problems
return content_buffer;
}
void *read_socket_raw_thread(void *socket)
{
return read_socket_raw(*(int*) socket);
}
2023-03-25 16:35:44 +01:00
char *read_socket_from_raw(char *raw)
{
char *final_message;
struct json_object *json_obj = json_tokener_parse(raw);
if (json_obj == NULL) return "ERR"; //RETURN IF INVALID SYNTAX WAS SENT BY SOME FUCKING SCRIPT KIDDIE
2023-03-25 16:35:44 +01:00
//GET STRINGS
char *message = get_string_from_json(json_obj, "message"); //TODO: Check deallocation problems
char *username = get_string_from_json(json_obj, "username");
//ALLOCATE final_message
final_message = why2_calloc(strlen(message) + strlen(username) + 3, sizeof(char));
//BUILD final_message
sprintf(final_message, "%s: %s", username, message);
//DEALLOCATION
json_object_put(json_obj);
return final_message;
}
2023-03-31 17:47:05 +02:00
void remove_json_syntax_characters(char *text)
{
for (size_t i = 0; i < strlen(text); i++) //TODO: DO SOMETHING MORE
{
if (text[i] == '\"')
{
text[i] = '\'';
}
}
}
void *stop_oldest_thread(void *id)
{
sleep(WHY2_COMMUNICATION_TIME); //yk wait
if (waiting_list.head == NULL) return NULL;
if (**(pthread_t**) waiting_list.head -> value == *(pthread_t*) id) //THREAD IS STILL ALIVE, I HOPE
{
//KILL THE THREAD
pthread_cancel(*(pthread_t*) id);
why2_list_remove(&waiting_list, why2_list_find(&waiting_list, id));
}
return NULL;
}
why2_node_t *find_request(void *thread) //COPIED FROM why2_list_find; using double pointers
{
why2_node_t *head = waiting_list.head;
if (head == NULL) return NULL; //EMPTY LIST
why2_node_t *buffer = head;
while (buffer -> next != NULL)
{
if (*(void**) buffer -> value == thread) return buffer;
buffer = buffer -> next;
}
if (thread != *(void**) buffer -> value) buffer = NULL; //PREVENT FROM RETURNING INVALID NODE
return buffer;
}
why2_node_t *find_connection(int connection)
{
why2_node_t *head = connection_list.head;
if (head == NULL) return NULL; //EMPTY LIST
why2_node_t *buffer = head;
while (buffer -> next != NULL)
{
if ((*(connection_node_t*) buffer -> value).connection == connection) return buffer;
buffer = buffer -> next;
}
if (connection != (*(connection_node_t*) buffer -> value).connection) buffer = NULL; //PREVENT FROM RETURNING INVALID NODE
return buffer;
}
//GLOBAL
2023-03-25 17:39:52 +01:00
void why2_send_socket(char *text, char *username, int socket)
2023-02-21 07:58:12 +01:00
{
char *output = why2_strdup("");
size_t length_buffer = strlen(text);
char *text_copy = why2_calloc(length_buffer, sizeof(char));
struct json_object *json = json_tokener_parse("{}");
//COPY text INTO text_copy (WITHOUT LAST CHARACTER WHEN NEWLINE IS AT THE END)
2023-03-31 17:47:05 +02:00
if (text[length_buffer - 1] == '\n') length_buffer--;
strncpy(text_copy, text, length_buffer);
2023-03-31 17:47:05 +02:00
//UNFUCK QUOTES FROM text_copy
remove_json_syntax_characters(text_copy);
//ADD OBJECTS
json_object_object_add(json, "message", json_object_new_string(text_copy));
2023-03-25 17:39:52 +01:00
json_object_object_add(json, "username", json_object_new_string(username));
//GENERATE JSON STRING
json_object_object_foreach(json, key, value)
{
append(&output, key, (char*) json_object_get_string(value));
}
add_brackets(&output);
why2_deallocate(text_copy);
json_object_put(json);
unsigned short text_length = (unsigned short) strlen(output) + 2;
2023-02-21 19:19:47 +01:00
char *final = why2_calloc(text_length, sizeof(char));
2023-02-21 07:58:12 +01:00
//SPLIT LENGTH INTO TWO CHARS
final[0] = (unsigned) text_length & 0xff;
final[1] = (unsigned) text_length >> 8;
2023-02-21 19:19:47 +01:00
for (int i = 2; i < text_length; i++) //APPEND
2023-02-21 07:58:12 +01:00
{
final[i] = output[i - 2];
2023-02-21 07:58:12 +01:00
}
//SEND
2023-02-21 19:48:28 +01:00
send(socket, final, text_length, 0);
2023-02-21 07:58:12 +01:00
//DEALLOCATION
2023-02-21 07:58:12 +01:00
why2_deallocate(final);
why2_deallocate(output);
2023-02-21 10:53:09 +01:00
}
void *why2_communicate_thread(void *arg)
2023-02-21 10:53:09 +01:00
{
int connection = *(int*) arg;
connection_node_t node = (connection_node_t)
{
connection,
pthread_self()
};
why2_list_push(&connection_list, &node, sizeof(node)); //ADD TO LIST
void *buffer;
2023-02-21 10:53:09 +01:00
char *received = NULL;
2023-04-20 09:28:39 +02:00
char *raw = why2_strdup("");
void *raw_ptr = NULL;
char *decoded_buffer;
pthread_t thread_buffer;
pthread_t thread_deletion_buffer;
why2_bool exiting = 0;
2023-04-20 09:28:39 +02:00
struct json_object *json = json_tokener_parse("{}");
//SEND CONNECTION MESSAGE
json_object_object_add(json, "message", json_object_new_string("anon connected"));
json_object_object_add(json, "username", json_object_new_string("server")); //TODO: Usernames
json_object_object_foreach(json, key, value) //GENERATE JSON STRING
{
append(&raw, key, (char*) json_object_get_string(value));
}
add_brackets(&raw);
pthread_create(&thread_buffer, NULL, send_to_all, raw); //SEND
pthread_join(thread_buffer, NULL);
why2_deallocate(raw);
raw = NULL;
2023-02-21 10:53:09 +01:00
2023-04-05 10:17:56 +02:00
while (!exiting) //KEEP COMMUNICATION ALIVE FOR 5 MINUTES [RESET TIMER AT MESSAGE SENT]
2023-02-21 10:53:09 +01:00
{
buffer = &thread_buffer;
//READ
pthread_create(&thread_buffer, NULL, read_socket_raw_thread, &connection);
why2_list_push(&waiting_list, &buffer, sizeof(thread_buffer));
//RUN DELETION THREAD
pthread_create(&thread_deletion_buffer, NULL, stop_oldest_thread, &thread_buffer);
//WAIT FOR MESSAGE
pthread_join(thread_buffer, &raw_ptr);
2023-04-19 11:20:12 +02:00
why2_list_remove(&waiting_list, find_request(&thread_buffer));
pthread_cancel(thread_deletion_buffer);
2023-03-31 18:21:06 +02:00
if (raw_ptr == WHY2_INVALID_POINTER || raw_ptr == NULL) break; //QUIT COMMUNICATION IF INVALID PACKET WAS RECEIVED
raw = (char*) raw_ptr;
2023-03-31 18:21:06 +02:00
//REMOVE CONTROL CHARACTERS FROM raw
for (size_t i = 0; i < strlen(raw); i++)
{
if (raw[i] == '\\') raw[i] = '/';
}
received = read_socket_from_raw(raw);
2023-02-21 10:53:09 +01:00
if (received == NULL) break; //FAILED; EXIT THREAD
2023-02-21 10:53:09 +01:00
decoded_buffer = get_string_from_json_string(raw, "message"); //DECODE
if (decoded_buffer[0] == '!') //COMMANDS
{
if (strcmp(decoded_buffer, "!exit") == 0) exiting = 1; //USER REQUESTED EXIT
goto deallocation; //IGNORE MESSAGES BEGINNING WITH '!'
}
2023-02-21 10:53:09 +01:00
printf("Received:\n%s\n\n", received);
2023-03-25 17:39:52 +01:00
pthread_create(&thread_buffer, NULL, send_to_all, raw);
pthread_join(thread_buffer, NULL);
deallocation:
2023-02-21 10:53:09 +01:00
why2_deallocate(received);
why2_deallocate(raw);
why2_deallocate(raw_ptr);
why2_deallocate(decoded_buffer);
//RESET VARIABLES
raw_ptr = NULL;
2023-02-21 10:53:09 +01:00
}
printf("User exited.\t%d\n", connection);
2023-02-21 10:53:09 +01:00
//DEALLOCATION
why2_deallocate(received);
close(connection);
why2_list_remove(&connection_list, find_connection(connection));
2023-02-21 10:53:09 +01:00
2023-04-05 10:18:57 +02:00
return NULL; //TODO: Fix client segfault on timeout
2023-02-21 10:53:09 +01:00
}
char *why2_read_socket(int socket)
2023-02-21 10:53:09 +01:00
{
char *raw_socket = read_socket_raw(socket);
char *final_message;
struct json_object *json_obj = json_tokener_parse(raw_socket);
//GET STRINGS
char *message = get_string_from_json(json_obj, "message"); //TODO: Check deallocation problems
char *username = get_string_from_json(json_obj, "username");
//ALLOCATE final_message
final_message = why2_calloc(strlen(message) + strlen(username) + 3, sizeof(char));
//BUILD final_message
sprintf(final_message, "%s: %s", username, message);
//DEALLOCATION
why2_deallocate(raw_socket);
json_object_put(json_obj);
return final_message;
2023-02-21 10:53:09 +01:00
}
2023-02-21 12:31:27 +01:00
void *why2_accept_thread(void *socket)
{
int accepted;
pthread_t thread;
//LOOP ACCEPT
for (;;)
{
2023-04-05 14:24:40 +02:00
accepted = accept(*((int*) socket), (WHY2_SA *) NULL, NULL); //ACCEPT NEW SOCKET
if (accepted == -1) continue;
pthread_create(&thread, NULL, why2_communicate_thread, &accepted);
}
return NULL;
2023-02-22 10:16:01 +01:00
}
void why2_clean_threads(void)
{
why2_node_t *head = waiting_list.head;
2023-02-22 10:16:01 +01:00
if (head == NULL) return; //EMPTY LIST
why2_node_t *node_buffer = head;
why2_node_t *node_buffer_2;
connection_node_t connection_buffer;
2023-02-22 10:16:01 +01:00
while (node_buffer -> next != NULL) //GO TROUGH LIST
{
node_buffer_2 = node_buffer;
node_buffer = node_buffer -> next;
connection_buffer = *(connection_node_t*) node_buffer_2 -> value;
close(connection_buffer.connection);
pthread_cancel(connection_buffer.thread);
why2_list_remove(&waiting_list, node_buffer_2); //REMOVE
2023-02-22 10:16:01 +01:00
}
2023-02-22 12:12:04 +01:00
}
void *why2_listen_server(void *socket)
{
char *read = NULL;
printf(">>> ");
fflush(stdout);
2023-02-22 12:12:04 +01:00
for (;;)
{
read = why2_read_socket(*((int*) socket));
printf(WHY2_CLEAR_AND_GO_UP);
printf("%s\n\n>>> ", read);
fflush(stdout);
why2_deallocate(read);
2023-02-22 12:12:04 +01:00
}
}
//TODO: Client formatting fix