2023-02-09 17:19:49 +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-09 17:57:24 +01:00
|
|
|
#include <why2/chat/common.h>
|
2023-02-09 17:04:11 +01:00
|
|
|
|
2023-02-20 19:09:55 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-02-09 18:36:51 +01:00
|
|
|
char *read_socket(int socket);
|
2023-02-20 18:32:39 +01:00
|
|
|
void *communicate_thread(void *arg);
|
2023-02-09 09:09:26 +01:00
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2023-02-09 17:57:24 +01:00
|
|
|
int listen_socket = socket(AF_INET, SOCK_STREAM, 0); //CREATE SERVER SOCKET
|
2023-02-09 18:25:45 +01:00
|
|
|
int accepted;
|
2023-02-09 18:42:23 +01:00
|
|
|
char *received = NULL;
|
2023-02-20 18:32:39 +01:00
|
|
|
pthread_t thread;
|
2023-02-09 17:04:11 +01:00
|
|
|
|
2023-02-09 19:18:46 +01:00
|
|
|
if (listen_socket < 0) why2_die("Failed creating socket.");
|
2023-02-09 17:04:11 +01:00
|
|
|
|
|
|
|
//DEFINE SERVER ADDRESS
|
2023-02-09 17:57:24 +01:00
|
|
|
struct sockaddr_in server_addr;
|
|
|
|
server_addr.sin_family = AF_INET;
|
|
|
|
server_addr.sin_port = htons(SERVER_PORT);
|
|
|
|
server_addr.sin_addr.s_addr = INADDR_ANY;
|
2023-02-09 17:04:11 +01:00
|
|
|
|
|
|
|
//BIND SOCKET
|
2023-02-09 19:18:46 +01:00
|
|
|
if (bind(listen_socket, (SA *) &server_addr, sizeof(server_addr)) < 0) why2_die("Failed binding socket.");
|
2023-02-09 17:04:11 +01:00
|
|
|
|
|
|
|
//LISTEN
|
2023-02-09 19:44:55 +01:00
|
|
|
if (listen(listen_socket, MAX_CONNECTIONS) < 0) why2_die("Binding failed.");
|
2023-02-09 09:09:26 +01:00
|
|
|
|
2023-02-09 18:25:45 +01:00
|
|
|
//LOOP ACCEPT
|
2023-02-09 19:32:54 +01:00
|
|
|
for (;;)
|
2023-02-09 18:25:45 +01:00
|
|
|
{
|
2023-02-20 14:12:26 +01:00
|
|
|
accepted = accept(listen_socket, (SA *) NULL, NULL); //ACCEPT NEW SOCKET //TODO: CLOSE (not only this one)
|
2023-02-09 18:42:23 +01:00
|
|
|
|
2023-02-20 18:32:39 +01:00
|
|
|
if (accepted == -1) continue;
|
2023-02-09 18:42:23 +01:00
|
|
|
|
2023-02-20 18:32:39 +01:00
|
|
|
pthread_create(&thread, NULL, communicate_thread, &accepted);
|
|
|
|
}
|
|
|
|
|
|
|
|
//DEALLOCATION
|
|
|
|
why2_deallocate(received);
|
2023-02-09 18:42:23 +01:00
|
|
|
|
2023-02-20 18:32:39 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *communicate_thread(void *arg)
|
|
|
|
{
|
2023-02-20 18:58:32 +01:00
|
|
|
const time_t startTime = time(NULL);
|
|
|
|
|
|
|
|
while (time(NULL) - startTime < 86400) //KEEP COMMUNICATION ALIVE FOR 24 HOURS
|
2023-02-20 18:32:39 +01:00
|
|
|
{
|
|
|
|
char *received = read_socket(*((int*) arg)); //READ
|
|
|
|
|
|
|
|
if (received == NULL) return NULL; //FAILED; EXIT THREAD
|
2023-02-15 20:06:08 +01:00
|
|
|
|
2023-02-20 19:09:55 +01:00
|
|
|
if (strcmp(received, "!exit") == 0) break; //USER REQUESTED PROGRAM EXIT
|
|
|
|
|
2023-02-09 18:42:23 +01:00
|
|
|
printf("Received:\n%s\n\n", received);
|
|
|
|
|
|
|
|
why2_deallocate(received);
|
2023-02-09 18:25:45 +01:00
|
|
|
}
|
|
|
|
|
2023-02-20 19:09:55 +01:00
|
|
|
close(*((int*) arg));
|
2023-02-20 18:32:39 +01:00
|
|
|
return NULL;
|
2023-02-09 17:53:42 +01:00
|
|
|
}
|
|
|
|
|
2023-02-09 18:36:51 +01:00
|
|
|
char *read_socket(int socket)
|
|
|
|
{
|
2023-02-15 20:06:08 +01:00
|
|
|
if (socket == -1)
|
|
|
|
{
|
2023-02-20 17:11:04 +01:00
|
|
|
fprintf(stderr, "Reading socket failed.\n");
|
2023-02-15 20:06:08 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-02-20 12:27:50 +01:00
|
|
|
unsigned short content_size = 0;
|
|
|
|
char *content_buffer = why2_calloc(2, sizeof(char));
|
2023-02-09 18:36:51 +01:00
|
|
|
|
2023-02-20 12:27:50 +01:00
|
|
|
//GET LENGTH
|
2023-02-20 18:32:39 +01:00
|
|
|
if (recv(socket, content_buffer, 2, 0) != 2) return NULL;
|
2023-02-09 18:36:51 +01:00
|
|
|
|
2023-02-20 12:27:50 +01:00
|
|
|
content_size = (unsigned short) (((unsigned) content_buffer[1] << 8) | content_buffer[0]);
|
|
|
|
|
|
|
|
why2_deallocate(content_buffer);
|
2023-02-18 17:05:30 +01:00
|
|
|
|
2023-02-09 18:36:51 +01:00
|
|
|
//ALLOCATE
|
2023-02-20 12:27:50 +01:00
|
|
|
content_buffer = why2_calloc(content_size + 1, sizeof(char));
|
2023-02-09 18:36:51 +01:00
|
|
|
|
2023-02-20 12:27:50 +01:00
|
|
|
//READ FINAL MESSAGE
|
2023-02-20 17:13:02 +01:00
|
|
|
if (recv(socket, content_buffer, content_size, 0) != content_size) fprintf(stderr, "Socket probably read wrongly!\n");
|
2023-02-09 18:36:51 +01:00
|
|
|
|
2023-02-20 12:27:50 +01:00
|
|
|
return content_buffer;
|
2023-02-15 20:06:08 +01:00
|
|
|
}
|