Finished socket creation func.

This commit is contained in:
Max 2024-01-14 20:19:43 +01:00
parent 6b5dfdc8d4
commit 60dfda1a1e
3 changed files with 21 additions and 8 deletions

View File

@ -28,5 +28,6 @@ void netex_init(void);
void netex_shutdown(void); void netex_shutdown(void);
int setup_socket(int port, const char* hostname, SOCKET_IPV_TYPE ipv_type); int setup_socket(int port, const char* hostname, SOCKET_IPV_TYPE ipv_type);
HOST_INFO* resolve_host(const char* hostname, int port, SOCKET_IPV_TYPE ipv_type); HOST_INFO* resolve_host(const char* hostname, int port, SOCKET_IPV_TYPE ipv_type);
void free_host_info(HOST_INFO* host_info);
#endif //NETEX_H #endif //NETEX_H

View File

@ -38,23 +38,23 @@ int setup_socket(const int port, const char* hostname, const SOCKET_IPV_TYPE ipv
{ {
default: default:
case IPv4: case IPv4:
PRINT_LINE("Creating IPv4 socket..."); PRINT_LINE("Creating IPv4 socket, bound on: %s:%i", resolved_host->ip, port);
socket_fd = socket(AF_INET, SOCK_STREAM, 0); socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if (socket_fd < 0) if (socket_fd < 0)
{ {
ERROR("Failed to created socket (IPv4)"); ERROR("Failed to created socket (IPv4)");
free(resolved_host); free_host_info(resolved_host);
return -1; return -1;
} }
break; break;
case Double: case Double:
case IPv6: case IPv6:
PRINT_LINE("Creating IPv6 socket..."); PRINT_LINE("Creating IPv6 socket, bound on: [%s]:%i", resolved_host->ip, port);
socket_fd = socket(AF_INET6, SOCK_STREAM, 0); socket_fd = socket(AF_INET6, SOCK_STREAM, 0);
if (socket_fd < 0) if (socket_fd < 0)
{ {
ERROR("Failed to create socket (IPv6)"); ERROR("Failed to create socket (IPv6)");
free(resolved_host); free_host_info(resolved_host);
return -1; return -1;
} }
break; break;
@ -62,7 +62,7 @@ int setup_socket(const int port, const char* hostname, const SOCKET_IPV_TYPE ipv
if (socket_fd == 0) if (socket_fd == 0)
{ {
WARN("Failed to set socket!"); WARN("Failed to set socket!");
free(resolved_host); free_host_info(resolved_host);
return -1; return -1;
} }
const int bind_result = bind(socket_fd, resolved_host->address, resolved_host->addrlen); const int bind_result = bind(socket_fd, resolved_host->address, resolved_host->addrlen);
@ -72,7 +72,7 @@ int setup_socket(const int port, const char* hostname, const SOCKET_IPV_TYPE ipv
ERROR("Could not bind! Error: %i", bind_error); ERROR("Could not bind! Error: %i", bind_error);
close(socket_fd); close(socket_fd);
} }
free(resolved_host); free_host_info(resolved_host);
return socket_fd; return socket_fd;
} }
@ -125,7 +125,7 @@ HOST_INFO* resolve_host(const char* hostname, const int port, const SOCKET_IPV_T
if (sock_fd == -1) if (sock_fd == -1)
continue; continue;
if (bind(sock_fd, next->ai_addr, next->ai_addrlen) == 0) if (bind(sock_fd, next->ai_addr, next->ai_addrlen) == 0)
break; // Succesfull bind break;
if (connect(sock_fd, next->ai_addr, next->ai_addrlen) == 0) if (connect(sock_fd, next->ai_addr, next->ai_addrlen) == 0)
break; break;
close(sock_fd); close(sock_fd);
@ -140,6 +140,7 @@ HOST_INFO* resolve_host(const char* hostname, const int port, const SOCKET_IPV_T
{ {
const int malloc_error = errno; const int malloc_error = errno;
ERROR("Failed to allocate object, error: %i", malloc_error); ERROR("Failed to allocate object, error: %i", malloc_error);
freeaddrinfo(resolved);
return NULL; return NULL;
} }
switch (next->ai_family) switch (next->ai_family)
@ -171,3 +172,14 @@ HOST_INFO* resolve_host(const char* hostname, const int port, const SOCKET_IPV_T
freeaddrinfo(resolved); freeaddrinfo(resolved);
return host_info; return host_info;
} }
void free_host_info(HOST_INFO* host_info)
{
if (host_info == NULL)
return;
if (host_info->address != NULL)
free(host_info->address);
if (host_info->ip != NULL)
free(host_info->ip);
free(host_info);
}

View File

@ -29,7 +29,7 @@ int initialize_server(int argc, char* argv[])
srv_configuration = config_load_from_path(CONFIG_PATH); srv_configuration = config_load_from_path(CONFIG_PATH);
if (srv_configuration == NULL) if (srv_configuration == NULL)
{ {
WARN("Fallback to default config!"); WARN("No config found, fallback to default config");
srv_configuration = config_create(); srv_configuration = config_create();
config_set_int(srv_configuration, "Connection.Port", 6920); config_set_int(srv_configuration, "Connection.Port", 6920);
config_set_string(srv_configuration, "Connection.BindIP", "0.0.0.0"); config_set_string(srv_configuration, "Connection.BindIP", "0.0.0.0");