From c43520a28d1e4380257294626632a5513c72e4cc Mon Sep 17 00:00:00 2001 From: Max <51083570+DRdrProfessor@users.noreply.github.com> Date: Fri, 12 Jan 2024 23:42:01 +0100 Subject: [PATCH] feat(app): library setup_socket function implementation Implemented function that accepts hostname & port number and returns a fd that can be used to listen. --- src/net/netex.c | 37 ++++++++++++++++--------------------- src/server/server.c | 10 +++++++--- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/net/netex.c b/src/net/netex.c index a5b892d..5bb2879 100644 --- a/src/net/netex.c +++ b/src/net/netex.c @@ -9,6 +9,8 @@ #include "netex.h" #include "config.h" +#define PORT_NUM_LENGHT 6 + void netex_init(void) { @@ -37,37 +39,30 @@ int setup_socket(const int port, const char* hostname) hints_addrinfo.ai_canonname = NULL; hints_addrinfo.ai_addr = NULL; hints_addrinfo.ai_next = NULL; - if (getaddrinfo(NULL, hostname, &hints_addrinfo, &cur_addrdinfo) != 0) + char port_str[PORT_NUM_LENGHT]; + const int print_result = snprintf(port_str, PORT_NUM_LENGHT, "%d", port); + if (print_result < 0) + { + ERROR("Failed to convert port to string!"); + return -1; + } + if (print_result >= PORT_NUM_LENGHT) + WARN("Convert output string truncated (port) to value: '%s'", port_str); + + if (getaddrinfo(hostname, port_str, &hints_addrinfo, &cur_addrdinfo) != 0) { ERROR("Could not get the address info of hostname: '%s'", hostname); - return 1; + return -1; } 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) continue; - if (bind(sock_fd, tmp_addrinfo->ai_addr, tmp_addrinfo->ai_addrlen) == 0) break; // Succesfull bind to the hostname - close(sock_fd); + close(sock_fd); // Close the socket en if there are more 'addrinfo' retry in the while loop. } - /*struct sockaddr_in sockaddr; - const int sockfd = socket(AF_INET, SOCK_STREAM, 0); - if (sockfd < 0) - { - WARN("Failed to create socket!"); - return 1; - } - sockaddr.sin_family = AF_INET; - sockaddr.sin_port = htons(port); - sockaddr.sin_addr.s_addr = INADDR_ANY; - const int bind_result = bind(sockfd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); - if (bind_result != 0) - { - const int bind_error = errno; - ERROR("Failed to bind to socket! Error: %i", bind_error); - return 1; - }*/ + freeaddrinfo(cur_addrdinfo); return sock_fd; } \ No newline at end of file diff --git a/src/server/server.c b/src/server/server.c index 4e280a1..2c5284f 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -20,6 +21,7 @@ int initialize_server(int argc, char* argv[]) WARN("Fallback to default config!"); srv_configuration = config_create(); config_set_int(srv_configuration, "Connection.Port", 6920); + config_set_string(srv_configuration, "Connection.Host", "localhost"); config_set_string(srv_configuration, "Connection.Crypto.CertPath", "cert.pem"); if (config_save_to_path(srv_configuration, CONFIG_PATH) != 0) { @@ -30,10 +32,12 @@ int initialize_server(int argc, char* argv[]) // Socket const int64_t portnum = config_get_int(srv_configuration, "Connection.Port"); - server_sockfd = setup_socket(portnum, "localhost"); - if (server_sockfd < 0) + char* hostname = config_get_string(srv_configuration, "Connection.Host"); + server_sockfd = setup_socket(portnum, hostname); + free(hostname); + if (server_sockfd <= 0) { - WARN("Failed to create socket!"); + WARN("Could not create server sockedfd!"); return 1; } return 0;