Added Domain Resolution (#134)

Automatically converts domains to IPs
This commit is contained in:
Isaac 2022-02-26 15:05:21 +10:00 committed by GitHub
parent 8d81d8df50
commit b72344c6e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 12 deletions

View File

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

View File

@ -0,0 +1,34 @@
#include <stdio.h>
#include "socket.h"
#include "pc/configfile.h"
#include "pc/debuglog.h"
#include "pc/djui/djui.h"
#ifdef WINSOCK
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <netdb.h>
#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);
}
}
}

View File

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

View File

@ -13,5 +13,6 @@ extern struct NetworkSystem gNetworkSystemSocket;
SOCKET socket_initialize(void);
void socket_shutdown(SOCKET socket);
void domain_resolution(void);
#endif