From 60dfda1a1e7e3e060cf8d4d9be7245d51854337c Mon Sep 17 00:00:00 2001 From: Max <51083570+DRdrProfessor@users.noreply.github.com> Date: Sun, 14 Jan 2024 20:19:43 +0100 Subject: [PATCH] Finished socket creation func. --- src/net/include/netex.h | 1 + src/net/netex.c | 26 +++++++++++++++++++------- src/server/server.c | 2 +- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/net/include/netex.h b/src/net/include/netex.h index 5c7c056..eafec2e 100644 --- a/src/net/include/netex.h +++ b/src/net/include/netex.h @@ -28,5 +28,6 @@ void netex_init(void); void netex_shutdown(void); 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); +void free_host_info(HOST_INFO* host_info); #endif //NETEX_H \ No newline at end of file diff --git a/src/net/netex.c b/src/net/netex.c index 81f2688..d133cd2 100644 --- a/src/net/netex.c +++ b/src/net/netex.c @@ -38,23 +38,23 @@ int setup_socket(const int port, const char* hostname, const SOCKET_IPV_TYPE ipv { default: 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); if (socket_fd < 0) { ERROR("Failed to created socket (IPv4)"); - free(resolved_host); + free_host_info(resolved_host); return -1; } break; case Double: 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); if (socket_fd < 0) { ERROR("Failed to create socket (IPv6)"); - free(resolved_host); + free_host_info(resolved_host); return -1; } break; @@ -62,7 +62,7 @@ int setup_socket(const int port, const char* hostname, const SOCKET_IPV_TYPE ipv if (socket_fd == 0) { WARN("Failed to set socket!"); - free(resolved_host); + free_host_info(resolved_host); return -1; } 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); close(socket_fd); } - free(resolved_host); + free_host_info(resolved_host); 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) continue; 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) break; 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; ERROR("Failed to allocate object, error: %i", malloc_error); + freeaddrinfo(resolved); return NULL; } switch (next->ai_family) @@ -171,3 +172,14 @@ HOST_INFO* resolve_host(const char* hostname, const int port, const SOCKET_IPV_T freeaddrinfo(resolved); 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); +} \ No newline at end of file diff --git a/src/server/server.c b/src/server/server.c index 9070c60..84fc329 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -29,7 +29,7 @@ int initialize_server(int argc, char* argv[]) srv_configuration = config_load_from_path(CONFIG_PATH); if (srv_configuration == NULL) { - WARN("Fallback to default config!"); + WARN("No config found, fallback to default config"); srv_configuration = config_create(); config_set_int(srv_configuration, "Connection.Port", 6920); config_set_string(srv_configuration, "Connection.BindIP", "0.0.0.0");