From b72344c6e4562326d139cc67058c4e076b7572c8 Mon Sep 17 00:00:00 2001 From: Isaac <62234577+Isaac0-dev@users.noreply.github.com> Date: Sat, 26 Feb 2022 15:05:21 +1000 Subject: [PATCH] Added Domain Resolution (#134) Automatically converts domains to IPs --- src/pc/djui/djui_panel_join.c | 15 +++---------- src/pc/network/socket/domain_res.c | 34 ++++++++++++++++++++++++++++++ src/pc/network/socket/socket.c | 1 + src/pc/network/socket/socket.h | 1 + 4 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 src/pc/network/socket/domain_res.c diff --git a/src/pc/djui/djui_panel_join.c b/src/pc/djui/djui_panel_join.c index 3002e4f0..a5cb5752 100644 --- a/src/pc/djui/djui_panel_join.c +++ b/src/pc/djui/djui_panel_join.c @@ -72,6 +72,7 @@ static bool djui_panel_join_ip_parse_port(char** msg) { static bool djui_panel_join_ip_valid(char* buffer) { char** msg = &buffer; + if (!djui_panel_join_ip_parse_numbers(msg)) { return false; } if (!djui_panel_join_ip_parse_period(msg)) { return false; } if (!djui_panel_join_ip_parse_numbers(msg)) { return false; } @@ -86,15 +87,6 @@ static bool djui_panel_join_ip_valid(char* buffer) { return (**msg == '\0'); } -static void djui_panel_join_ip_text_change(struct DjuiBase* caller) { - struct DjuiInputbox* inputbox1 = (struct DjuiInputbox*)caller; - if (djui_panel_join_ip_valid(inputbox1->buffer)) { - djui_inputbox_set_text_color(inputbox1, 0, 0, 0, 255); - } else { - djui_inputbox_set_text_color(inputbox1, 255, 0, 0, 255); - } -} - static void djui_panel_join_ip_text_set_new(void) { char buffer[256] = { 0 }; snprintf(buffer, 256, "%s", sInputboxIp->buffer); @@ -136,11 +128,11 @@ static void djui_panel_join_ip_text_set(struct DjuiInputbox* inputbox1) { } void djui_panel_join_do_join(struct DjuiBase* caller) { - if (!djui_panel_join_ip_valid(sInputboxIp->buffer)) { + if (!(strlen(sInputboxIp->buffer) > 0)) { djui_interactable_set_input_focus(&sInputboxIp->base); djui_inputbox_select_all(sInputboxIp); return; - } + } djui_panel_join_ip_text_set_new(); network_set_system(NS_SOCKET); network_init(NT_CLIENT); @@ -183,7 +175,6 @@ void djui_panel_join_create(struct DjuiBase* caller) { struct DjuiInputbox* inputbox1 = djui_inputbox_create(&body->base, 256); djui_base_set_size_type(&inputbox1->base, DJUI_SVT_RELATIVE, DJUI_SVT_ABSOLUTE); djui_base_set_size(&inputbox1->base, 1.0f, 32.0f); - djui_interactable_hook_value_change(&inputbox1->base, djui_panel_join_ip_text_change); sInputboxIp = inputbox1; djui_panel_join_ip_text_set(inputbox1); diff --git a/src/pc/network/socket/domain_res.c b/src/pc/network/socket/domain_res.c new file mode 100644 index 00000000..fb57c486 --- /dev/null +++ b/src/pc/network/socket/domain_res.c @@ -0,0 +1,34 @@ +#include +#include "socket.h" +#include "pc/configfile.h" +#include "pc/debuglog.h" +#include "pc/djui/djui.h" +#ifdef WINSOCK +#include +#include +#else +#include +#endif + + +void domain_resolution(void) { + struct in_addr addr; + char *host_name; + struct hostent *remoteHost; + char* domainname = ""; + host_name = configJoinIp; + if (host_name == NULL) { + return; + } + int i = 0; + remoteHost = gethostbyname(host_name); + i = 0; + 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/socket.c b/src/pc/network/socket/socket.c index 468e2e34..d218a393 100644 --- a/src/pc/network/socket/socket.c +++ b/src/pc/network/socket/socket.c @@ -78,6 +78,7 @@ static bool ns_socket_initialize(enum NetworkType networkType) { // save the port to send to addr[0].sin_family = AF_INET; addr[0].sin_port = htons(port); + domain_resolution(); addr[0].sin_addr.s_addr = inet_addr(configJoinIp); LOG_INFO("connecting to %s %u", configJoinIp, port); } diff --git a/src/pc/network/socket/socket.h b/src/pc/network/socket/socket.h index 9729e885..b8f26860 100644 --- a/src/pc/network/socket/socket.h +++ b/src/pc/network/socket/socket.h @@ -13,5 +13,6 @@ extern struct NetworkSystem gNetworkSystemSocket; SOCKET socket_initialize(void); void socket_shutdown(SOCKET socket); +void domain_resolution(void); #endif