Improvements to domain resolution (#106)

This commit is contained in:
Isaac 2022-05-22 14:12:32 +10:00 committed by GitHub
parent f4579b4c9a
commit 6d995214cb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 5 deletions

View file

@ -1,6 +1,7 @@
#include <stdio.h>
#include "djui.h"
#include "src/pc/network/network.h"
#include "src/pc/network/socket/socket.h"
#include "src/pc/utils/misc.h"
#include "src/pc/configfile.h"
#include "src/pc/debuglog.h"
@ -88,8 +89,18 @@ 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 (strlen(inputbox1->buffer) > 2) {
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 };
gGetHostName = sInputboxIp->buffer;
if (snprintf(buffer, 256, "%s", sInputboxIp->buffer) < 0) {
LOG_INFO("truncating IP");
}
@ -125,7 +136,7 @@ static void djui_panel_join_ip_text_set(struct DjuiInputbox* inputbox1) {
} else if (strlen(configJoinIp) > 0) {
if (snprintf(buffer, 256, "%s", configJoinIp) < 0) { LOG_INFO("truncating IP"); }
} else {
if (snprintf(buffer, 256, "127.0.0.1") < 0) { LOG_INFO("truncating IP"); }
if (snprintf(buffer, 256, "localhost") < 0) { LOG_INFO("truncating IP"); }
}
djui_inputbox_set_text(inputbox1, buffer);
@ -133,11 +144,11 @@ static void djui_panel_join_ip_text_set(struct DjuiInputbox* inputbox1) {
}
void djui_panel_join_do_join(struct DjuiBase* caller) {
if (!(strlen(sInputboxIp->buffer) > 0)) {
if (!(strlen(sInputboxIp->buffer) > 2)) {
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);
@ -180,6 +191,7 @@ 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

@ -10,15 +10,15 @@
#include <netdb.h>
#endif
char* gGetHostName;
void domain_resolution(void) {
struct in_addr addr;
char *host_name = configJoinIp;
struct hostent *remoteHost;
char* domainname = "";
host_name = configJoinIp;
if (host_name == NULL) {
if (gGetHostName == NULL) {
return;
}
@ -36,3 +36,10 @@ void domain_resolution(void) {
}
}
}
void save_domain(void) {
if (gGetHostName != NULL) {
snprintf(configJoinIp, 256, "%s", gGetHostName);
gGetHostName = NULL;
}
}

View file

@ -92,6 +92,7 @@ static bool ns_socket_initialize(enum NetworkType networkType) {
domain_resolution();
sAddr[0].sin_addr.s_addr = inet_addr(configJoinIp);
LOG_INFO("connecting to %s %u", configJoinIp, port);
save_domain();
}
// kick off first packet

View file

@ -10,9 +10,11 @@
#include "../network.h"
extern struct NetworkSystem gNetworkSystemSocket;
extern char *gGetHostName;
SOCKET socket_initialize(void);
void socket_shutdown(SOCKET socket);
void domain_resolution(void);
void save_domain(void);
#endif