From 0174cfa0fa500312eb2cba629693666674248d74 Mon Sep 17 00:00:00 2001 From: Isaac0-dev <62234577+Isaac0-dev@users.noreply.github.com> Date: Fri, 3 May 2024 21:27:44 +1000 Subject: [PATCH] move domain resolution to socket.c (#23) --- src/pc/djui/djui_panel_join_direct.c | 1 - src/pc/network/socket/domain_res.c | 34 ---------------------------- src/pc/network/socket/domain_res.h | 4 ---- src/pc/network/socket/socket.c | 16 +++++++++++-- src/pc/network/socket/socket.h | 2 ++ src/pc/network/socket/socket_linux.h | 1 + src/pc/pc_main.c | 2 +- 7 files changed, 18 insertions(+), 42 deletions(-) delete mode 100644 src/pc/network/socket/domain_res.c delete mode 100644 src/pc/network/socket/domain_res.h diff --git a/src/pc/djui/djui_panel_join_direct.c b/src/pc/djui/djui_panel_join_direct.c index 1bc0c534..56e81898 100644 --- a/src/pc/djui/djui_panel_join_direct.c +++ b/src/pc/djui/djui_panel_join_direct.c @@ -8,7 +8,6 @@ #include "pc/network/network.h" #include "pc/network/socket/socket.h" #include "pc/network/coopnet/coopnet.h" -#include "pc/network/socket/domain_res.h" #include "pc/utils/misc.h" #include "pc/configfile.h" #include "pc/debuglog.h" diff --git a/src/pc/network/socket/domain_res.c b/src/pc/network/socket/domain_res.c deleted file mode 100644 index 66d42e42..00000000 --- a/src/pc/network/socket/domain_res.c +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include "socket.h" -#include "pc/configfile.h" -#include "pc/debuglog.h" -#include "pc/djui/djui.h" -#ifdef WINSOCK -#include -#include -#else -#include -#endif - -char gGetHostName[MAX_CONFIG_STRING] = ""; - -void domain_resolution(void) { - struct in_addr addr; - char *host_name = configJoinIp; - struct hostent *remoteHost; - char *domainname = ""; - - int i = 0; - remoteHost = gethostbyname(host_name); - if (remoteHost == NULL) { - return; - } - - if (remoteHost->h_addrtype == AF_INET) { - while (remoteHost->h_addr_list[i] != 0) { - addr.s_addr = *(u_long *) remoteHost->h_addr_list[i++]; - domainname = inet_ntoa(addr); - snprintf(configJoinIp, MAX_CONFIG_STRING, "%s", domainname); - } - } -} diff --git a/src/pc/network/socket/domain_res.h b/src/pc/network/socket/domain_res.h deleted file mode 100644 index 6cbfef31..00000000 --- a/src/pc/network/socket/domain_res.h +++ /dev/null @@ -1,4 +0,0 @@ -extern char gGetHostName[MAX_CONFIG_STRING]; - -void domain_resolution(void); -void save_domain(void); diff --git a/src/pc/network/socket/socket.c b/src/pc/network/socket/socket.c index 3fc16460..0d29d889 100644 --- a/src/pc/network/socket/socket.c +++ b/src/pc/network/socket/socket.c @@ -1,5 +1,4 @@ #include "socket.h" -#include "domain_res.h" #include #include "pc/configfile.h" #include "pc/debuglog.h" @@ -8,6 +7,19 @@ static SOCKET sCurSocket = INVALID_SOCKET; static struct sockaddr_in sAddr[MAX_PLAYERS] = { 0 }; +char gGetHostName[MAX_CONFIG_STRING] = ""; + +void resolve_domain(void) { + struct hostent *remoteHost = gethostbyname(configJoinIp); + if (remoteHost && remoteHost->h_addrtype == AF_INET) { + struct in_addr addr; + for (int i = 0; remoteHost->h_addr_list[i] != 0; i++) { + memcpy(&addr, remoteHost->h_addr_list[i], sizeof(struct in_addr)); + snprintf(configJoinIp, MAX_CONFIG_STRING, "%s", inet_ntoa(addr)); + } + } +} + static int socket_bind(SOCKET socket, unsigned int port) { struct sockaddr_in rxAddr; rxAddr.sin_family = AF_INET; @@ -90,7 +102,7 @@ static bool ns_socket_initialize(enum NetworkType networkType, UNUSED bool recon // save the port to send to sAddr[0].sin_family = AF_INET; sAddr[0].sin_port = htons(port); - domain_resolution(); + resolve_domain(); sAddr[0].sin_addr.s_addr = inet_addr(configJoinIp); LOG_INFO("connecting to %s %u", configJoinIp, port); snprintf(configJoinIp, MAX_CONFIG_STRING, "%s", gGetHostName); diff --git a/src/pc/network/socket/socket.h b/src/pc/network/socket/socket.h index 97682a54..045f3c3a 100644 --- a/src/pc/network/socket/socket.h +++ b/src/pc/network/socket/socket.h @@ -11,6 +11,8 @@ extern struct NetworkSystem gNetworkSystemSocket; +extern char gGetHostName[]; + SOCKET socket_initialize(void); void socket_shutdown(SOCKET socket); diff --git a/src/pc/network/socket/socket_linux.h b/src/pc/network/socket/socket_linux.h index e11b373c..7cdb7f9c 100644 --- a/src/pc/network/socket/socket_linux.h +++ b/src/pc/network/socket/socket_linux.h @@ -7,6 +7,7 @@ #include #include #include +#include #define SOCKET unsigned int #define INVALID_SOCKET (unsigned int)(-1) diff --git a/src/pc/pc_main.c b/src/pc/pc_main.c index 3c2e9447..8adea3e5 100644 --- a/src/pc/pc_main.c +++ b/src/pc/pc_main.c @@ -36,7 +36,7 @@ #include "pc/lua/utils/smlua_audio_utils.h" #include "pc/network/version.h" -#include "pc/network/socket/domain_res.h" +#include "pc/network/socket/socket.h" #include "pc/network/network_player.h" #include "pc/update_checker.h" #include "pc/djui/djui.h"