move domain resolution to socket.c (#23)

This commit is contained in:
Isaac0-dev 2024-05-03 21:27:44 +10:00 committed by GitHub
parent 3826d379e9
commit 0174cfa0fa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 18 additions and 42 deletions

View File

@ -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"

View File

@ -1,34 +0,0 @@
#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
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);
}
}
}

View File

@ -1,4 +0,0 @@
extern char gGetHostName[MAX_CONFIG_STRING];
void domain_resolution(void);
void save_domain(void);

View File

@ -1,5 +1,4 @@
#include "socket.h"
#include "domain_res.h"
#include <stdio.h>
#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);

View File

@ -11,6 +11,8 @@
extern struct NetworkSystem gNetworkSystemSocket;
extern char gGetHostName[];
SOCKET socket_initialize(void);
void socket_shutdown(SOCKET socket);

View File

@ -7,6 +7,7 @@
#include <sys/socket.h>
#include <fcntl.h>
#include <unistd.h>
#include <netdb.h>
#define SOCKET unsigned int
#define INVALID_SOCKET (unsigned int)(-1)

View File

@ -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"