diff --git a/src/pc/network/socket/socket.c b/src/pc/network/socket/socket.c index 0d29d889..15f6a8fc 100644 --- a/src/pc/network/socket/socket.c +++ b/src/pc/network/socket/socket.c @@ -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); diff --git a/src/pc/network/socket/socket_linux.c b/src/pc/network/socket/socket_linux.c index 5ab75ad0..d478798a 100644 --- a/src/pc/network/socket/socket_linux.c +++ b/src/pc/network/socket/socket_linux.c @@ -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; @@ -17,6 +17,14 @@ SOCKET socket_initialize(void) { LOG_ERROR("fcntl failed with error: %d", rc); 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; } diff --git a/src/pc/network/socket/socket_windows.c b/src/pc/network/socket/socket_windows.c index 635b96c3..195e03d4 100644 --- a/src/pc/network/socket/socket_windows.c +++ b/src/pc/network/socket/socket_windows.c @@ -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; @@ -26,6 +26,14 @@ SOCKET socket_initialize(void) { LOG_ERROR("ioctlsocket failed with error: %d", rc); 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