feat(app): library setup_socket function implementation

Implemented function that accepts hostname & port number and returns a fd that can be used to listen.
This commit is contained in:
Max 2024-01-12 23:42:01 +01:00
parent 42bfd9dc14
commit c43520a28d
2 changed files with 23 additions and 24 deletions

View File

@ -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;
}

View File

@ -1,5 +1,6 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
@ -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;