2023-02-21 07:59:09 +01:00
|
|
|
/*
|
|
|
|
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-02-21 10:53:09 +01:00
|
|
|
#include <unistd.h>
|
2023-02-21 07:58:12 +01:00
|
|
|
|
2023-02-21 19:34:40 +01:00
|
|
|
#include <why2/chat/common.h>
|
|
|
|
|
2023-02-21 07:58:12 +01:00
|
|
|
#include <why2/memory.h>
|
|
|
|
|
2023-02-21 12:29:45 +01:00
|
|
|
//LINKED LIST STUFF (BIT CHANGED memory.c'S VERSION)
|
|
|
|
typedef struct node
|
|
|
|
{
|
|
|
|
int connection;
|
|
|
|
struct node *next;
|
|
|
|
} node_t; //SINGLE LINKED LIST
|
|
|
|
|
|
|
|
node_t *head = NULL;
|
|
|
|
|
|
|
|
void push_to_list(int connection)
|
|
|
|
{
|
|
|
|
//CREATE NODE
|
|
|
|
node_t *new_node = malloc(sizeof(node_t));
|
|
|
|
node_t *buffer = head;
|
|
|
|
|
|
|
|
new_node -> connection = connection;
|
|
|
|
new_node -> next = NULL;
|
|
|
|
|
|
|
|
if (head == NULL) //INIT LIST
|
|
|
|
{
|
|
|
|
head = new_node;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
while (buffer -> next != NULL) buffer = buffer -> next; //GET TO THE END OF LIST
|
|
|
|
|
|
|
|
buffer -> next = new_node; //LINK
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove_node(node_t *node)
|
|
|
|
{
|
2023-02-21 12:39:54 +01:00
|
|
|
if (node == NULL) return; //NULL NODE
|
|
|
|
|
2023-02-21 12:29:45 +01:00
|
|
|
node_t *buffer_1 = head;
|
|
|
|
node_t *buffer_2;
|
|
|
|
|
|
|
|
while (buffer_1 -> next != NULL) //GO TROUGH EVERY ELEMENT IN LIST
|
|
|
|
{
|
|
|
|
if (buffer_1 == node) break; //FOUND (IF THE WHILE GOES TROUGH THE WHOLE LIST WITHOUT THE break, I ASSUME THE LAST NODE IS THE CORRECT ONE)
|
|
|
|
|
|
|
|
buffer_1 = buffer_1 -> next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node != buffer_1) return; //node WASN'T FOUND
|
|
|
|
|
|
|
|
if (buffer_1 == head) //node WAS THE FIRST NODE IN THE LIST
|
|
|
|
{
|
|
|
|
//UNLINK
|
|
|
|
head = buffer_1 -> next;
|
|
|
|
} else //wdyt
|
|
|
|
{
|
|
|
|
//GET THE NODE BEFORE node
|
|
|
|
buffer_2 = head;
|
|
|
|
|
|
|
|
while (buffer_2 -> next != buffer_1) buffer_2 = buffer_2 -> next;
|
|
|
|
|
|
|
|
//UNLINK
|
|
|
|
buffer_2 -> next = buffer_1 -> next;
|
|
|
|
}
|
|
|
|
|
|
|
|
//DEALLOCATION
|
|
|
|
free(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
node_t *get_node(int connection)
|
|
|
|
{
|
|
|
|
if (head == NULL) return NULL; //EMPTY LIST
|
|
|
|
|
|
|
|
node_t *buffer = head;
|
|
|
|
while (buffer -> next != NULL)
|
|
|
|
{
|
|
|
|
if (buffer -> connection == connection) return buffer;
|
|
|
|
|
|
|
|
buffer = buffer -> next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connection != buffer -> connection) buffer = NULL; //PREVENT FROM RETURNING INVALID NODE
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
//GLOBAL
|
2023-02-21 12:33:33 +01:00
|
|
|
void why2_send_socket(char *text, int socket)
|
2023-02-21 07:58:12 +01:00
|
|
|
{
|
2023-02-21 19:19:47 +01:00
|
|
|
unsigned short text_length = (unsigned short) strlen(text) + 2;
|
|
|
|
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] = text[i - 2];
|
|
|
|
}
|
|
|
|
|
|
|
|
//SEND
|
2023-02-21 19:48:28 +01:00
|
|
|
send(socket, final, text_length, 0);
|
2023-02-21 07:58:12 +01:00
|
|
|
|
|
|
|
why2_deallocate(final);
|
2023-02-21 10:53:09 +01:00
|
|
|
}
|
|
|
|
|
2023-02-21 12:33:33 +01:00
|
|
|
void *why2_communicate_thread(void *arg)
|
2023-02-21 10:53:09 +01:00
|
|
|
{
|
|
|
|
printf("User connected.\t%d\n", *((int*) arg));
|
|
|
|
|
2023-02-21 18:59:44 +01:00
|
|
|
why2_register_connection(*((int*) arg)); //TODO: Remove why2_register_connection if unused
|
|
|
|
|
2023-02-21 10:53:09 +01:00
|
|
|
const time_t startTime = time(NULL);
|
|
|
|
char *received = NULL;
|
|
|
|
|
|
|
|
while (time(NULL) - startTime < 86400) //KEEP COMMUNICATION ALIVE FOR 24 HOURS
|
|
|
|
{
|
2023-02-21 12:33:33 +01:00
|
|
|
received = why2_read_socket(*((int*) arg)); //READ
|
2023-02-21 10:53:09 +01:00
|
|
|
|
|
|
|
if (received == NULL) return NULL; //FAILED; EXIT THREAD
|
|
|
|
|
|
|
|
if (strcmp(received, "!exit\n") == 0) break; //USER REQUESTED PROGRAM EXIT
|
|
|
|
|
|
|
|
printf("Received:\n%s\n\n", received);
|
|
|
|
|
|
|
|
why2_deallocate(received);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("User exited.\t%d\n", *((int*) arg));
|
|
|
|
|
|
|
|
//DEALLOCATION
|
2023-02-21 15:00:59 +01:00
|
|
|
remove_node(get_node(*((int*) arg)));
|
2023-02-21 10:53:09 +01:00
|
|
|
close(*((int*) arg));
|
|
|
|
why2_deallocate(received);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-02-21 12:33:33 +01:00
|
|
|
char *why2_read_socket(int socket)
|
2023-02-21 10:53:09 +01:00
|
|
|
{
|
|
|
|
if (socket == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Reading socket failed.\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned short content_size = 0;
|
2023-02-21 15:38:00 +01:00
|
|
|
char *content_buffer = why2_calloc(3, sizeof(char));
|
2023-02-21 10:53:09 +01:00
|
|
|
|
|
|
|
//GET LENGTH
|
|
|
|
if (recv(socket, content_buffer, 2, 0) != 2) return NULL;
|
|
|
|
|
|
|
|
content_size = (unsigned short) (((unsigned) content_buffer[1] << 8) | content_buffer[0]);
|
|
|
|
|
|
|
|
why2_deallocate(content_buffer);
|
|
|
|
|
|
|
|
//ALLOCATE
|
2023-02-21 19:19:47 +01:00
|
|
|
content_buffer = why2_calloc(content_size + 1, sizeof(char));
|
2023-02-21 10:53:09 +01:00
|
|
|
|
|
|
|
//READ FINAL MESSAGE
|
|
|
|
if (recv(socket, content_buffer, content_size, 0) != content_size) fprintf(stderr, "Socket probably read wrongly!\n");
|
|
|
|
|
2023-02-21 19:53:00 +01:00
|
|
|
//TODO: Fix size [first two indexes]
|
|
|
|
|
2023-02-21 10:53:09 +01:00
|
|
|
return content_buffer;
|
|
|
|
}
|
2023-02-21 12:31:27 +01:00
|
|
|
|
2023-02-21 12:33:33 +01:00
|
|
|
void why2_register_connection(int socket)
|
2023-02-21 12:31:27 +01:00
|
|
|
{
|
2023-02-21 12:33:33 +01:00
|
|
|
push_to_list(socket); //SEND
|
2023-02-21 19:34:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void *why2_accept_thread(void *socket)
|
|
|
|
{
|
|
|
|
int accepted;
|
|
|
|
pthread_t thread;
|
|
|
|
|
|
|
|
//LOOP ACCEPT
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
accepted = accept(*((int*) socket), (SA *) NULL, NULL); //ACCEPT NEW SOCKET //TODO: CLOSE (not only this one)
|
|
|
|
|
|
|
|
if (accepted == -1) continue;
|
|
|
|
|
|
|
|
pthread_create(&thread, NULL, why2_communicate_thread, &accepted);
|
|
|
|
}
|
2023-02-21 12:31:27 +01:00
|
|
|
}
|