Support IPv6 (#185)

This commit is contained in:
Prince Frizzy 2024-07-10 11:49:27 -04:00 committed by GitHub
parent cf3fc87789
commit afba4eb7d9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 5 deletions

View File

@ -11,7 +11,7 @@ char gGetHostName[MAX_CONFIG_STRING] = "";
void resolve_domain(void) {
struct hostent *remoteHost = gethostbyname(configJoinIp);
if (remoteHost && remoteHost->h_addrtype == AF_INET) {
if (remoteHost && (remoteHost->h_addrtype == AF_INET || remoteHost->h_addrtype == AF_INET6)) {
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));
@ -22,7 +22,7 @@ void resolve_domain(void) {
static int socket_bind(SOCKET socket, unsigned int port) {
struct sockaddr_in rxAddr;
rxAddr.sin_family = AF_INET;
rxAddr.sin_family = AF_INET6;
rxAddr.sin_port = htons(port);
rxAddr.sin_addr.s_addr = htonl(INADDR_ANY);
@ -100,7 +100,7 @@ static bool ns_socket_initialize(enum NetworkType networkType, UNUSED bool recon
LOG_INFO("bound to port %u", port);
} else {
// save the port to send to
sAddr[0].sin_family = AF_INET;
sAddr[0].sin_family = AF_INET6;
sAddr[0].sin_port = htons(port);
resolve_domain();
sAddr[0].sin_addr.s_addr = inet_addr(configJoinIp);

View File

@ -5,7 +5,7 @@
SOCKET socket_initialize(void) {
// initialize socket
SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
SOCKET sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
if (sock == INVALID_SOCKET) {
LOG_ERROR("socket failed with error %d", SOCKET_LAST_ERROR);
return INVALID_SOCKET;
@ -18,6 +18,14 @@ SOCKET socket_initialize(void) {
return INVALID_SOCKET;
}
// Make sure our socket is dual-stack. So we can use both IPv4 and IPv6.
int opt = 0;
rc = setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&opt, sizeof(opt));
if (rc != NO_ERROR) {
LOG_ERROR("setsockopt(IPV6_V6ONLY) failed with error: %d", rc);
return INVALID_SOCKET;
}
return sock;
}

View File

@ -13,7 +13,7 @@ SOCKET socket_initialize(void) {
}
// initialize socket
SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
SOCKET sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
if (sock == INVALID_SOCKET) {
LOG_ERROR("socket failed with error %d", SOCKET_LAST_ERROR);
return INVALID_SOCKET;
@ -27,6 +27,14 @@ SOCKET socket_initialize(void) {
return INVALID_SOCKET;
}
// Make sure our socket is dual-stack. So we can use both IPv4 and IPv6.
int opt = 0;
rc = setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&opt, sizeof(opt));
if (rc != NO_ERROR) {
LOG_ERROR("setsockopt(IPV6_V6ONLY) failed with error: %d", rc);
return INVALID_SOCKET;
}
#if MAX_PLAYERS > 4
// on windows, the send buffer for the socket needs to be increased
// for the many players case to avoid WSAEWOULDBLOCK on send