From ed1d311dd9b78bf178fe84b471b36455dbc6a913 Mon Sep 17 00:00:00 2001 From: Max <51083570+DRdrProfessor@users.noreply.github.com> Date: Sat, 13 Jan 2024 02:07:54 +0100 Subject: [PATCH] Reworking socket fd creation, added simple http response to send to the client. --- src/net/include/netex.h | 1 + src/net/netex.c | 22 +++++++++++++++-- src/server/server.c | 52 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/src/net/include/netex.h b/src/net/include/netex.h index d8eb517..dd28848 100644 --- a/src/net/include/netex.h +++ b/src/net/include/netex.h @@ -10,6 +10,7 @@ void netex_init(void); void netex_shutdown(void); +// Creates a socket and bind to that socket on the given port and hostname. If done with the socket close it with 'close()' int setup_socket(int port, const char* hostname); #endif //NETEX_H \ No newline at end of file diff --git a/src/net/netex.c b/src/net/netex.c index 5bb2879..7fd9dc0 100644 --- a/src/net/netex.c +++ b/src/net/netex.c @@ -54,7 +54,8 @@ int setup_socket(const int port, const char* hostname) ERROR("Could not get the address info of hostname: '%s'", hostname); return -1; } - for (const struct addrinfo* tmp_addrinfo = cur_addrdinfo; tmp_addrinfo != NULL; tmp_addrinfo = tmp_addrinfo->ai_next) + //TODO: Need fihure out why data from getaddrinfo is not working. + /*for (const struct addrinfo* tmp_addrinfo = cur_addrdinfo; tmp_addrinfo != NULL; tmp_addrinfo = tmp_addrinfo->ai_next) { sock_fd = socket(tmp_addrinfo->ai_family, tmp_addrinfo->ai_socktype, tmp_addrinfo->ai_protocol); if (sock_fd == -1) @@ -62,7 +63,24 @@ int setup_socket(const int port, const char* hostname) if (bind(sock_fd, tmp_addrinfo->ai_addr, tmp_addrinfo->ai_addrlen) == 0) break; // Succesfull bind to the hostname close(sock_fd); // Close the socket en if there are more 'addrinfo' retry in the while loop. - } + }*/ freeaddrinfo(cur_addrdinfo); + struct sockaddr_in sockaddr; + sock_fd = socket(AF_INET, SOCK_STREAM, 0); + if (sock_fd < 0) + { + ERROR("Failed to created socket"); + return -1; + } + sockaddr.sin_family = AF_INET; + sockaddr.sin_port = htons(6920); + sockaddr.sin_addr.s_addr = INADDR_ANY; + const int bind_result = bind(sock_fd, (struct sockaddr*)&sockaddr, sizeof(sockaddr)); + if (bind_result != 0) + { + ERROR("Could not bind!"); + close(sock_fd); + return -1; + } return sock_fd; } \ No newline at end of file diff --git a/src/server/server.c b/src/server/server.c index 2c5284f..89bc684 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -3,14 +3,23 @@ #include #include #include +#include +#include +#include +#include #include "netex.h" #include "config.h" #define CONFIG_PATH "srv_config.json" + CONFIGURATION* srv_configuration; int server_sockfd; struct sockaddr_in server_sockaddr; +pthread_t listen_thread; +bool b_listen; + +void server_listen(); int initialize_server(int argc, char* argv[]) { @@ -45,5 +54,48 @@ int initialize_server(int argc, char* argv[]) int server_execute(void) { + const int thread_create_result = pthread_create(&listen_thread, NULL, server_listen, NULL); + if (thread_create_result != 0) + { + ERROR("Failed to create main listening thread, pthread error: %i", thread_create_result); + return -1; + } + int listen_thread_result; + const int join_result = pthread_join(listen_thread, (void**)&listen_thread_result); + if (join_result == 0) + WARN("Listen thread has exited with erros!"); return 0; +} + +void server_listen() +{ + PRINT_LINE("Listening..."); + int some_return_code = 0; + while (true) + { + const int listen_result = listen(server_sockfd, 1); + if (listen_result == -1) + { + some_return_code = errno; + WARN("Failed while listening, error: %i", some_return_code); + break; + } + const int accepted_fd = accept(server_sockfd, NULL, NULL); + if (accepted_fd == -1) + { + const int accept_error = errno; + WARN("Could not establish connection, error: %i", accept_error); + continue; + } + // Test response + PRINT_LINE("Accepted clientfd: %i", accepted_fd); + const char* header = "HTTP/1.1 200 OK\r\nContent-Length: 14\r\nConnection: close\r\n\r\nHello, world!\n"; + const int header_len = strlen(header); + const int send_result = send(accepted_fd, header, header_len, 0); + if (send_result == -1) + WARN("Error sending message to client!"); + close(accepted_fd); + //TODO: Send client to thread that will handle further connection things. + } + pthread_exit((void*)some_return_code); } \ No newline at end of file