Added IPv6 support

This commit is contained in:
SaltySnail 2024-08-21 01:38:52 +02:00
parent a60ff48c08
commit 169b14490c
6 changed files with 15 additions and 13 deletions

View File

@ -269,7 +269,7 @@ void CoreMain() {
ZeroMemory(&hints, sizeof(hints)); ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET; hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP; hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE; hints.ai_flags = AI_PASSIVE;

View File

@ -7,6 +7,7 @@
/// ///
#include <string> #include <string>
#include "IPRegex.h"
#if defined(_WIN32) #if defined(_WIN32)
#include <winsock2.h> #include <winsock2.h>
@ -19,8 +20,9 @@
#include "Logger.h" #include "Logger.h"
std::string GetAddr(const std::string& IP) { std::string GetAddr(const std::string& IP) {
if (IP.find_first_not_of("0123456789.") == -1) if (!std::regex_match(IP, IP_REGEX)) {
return IP; return IP;
}
hostent* host; hostent* host;
#ifdef _WIN32 #ifdef _WIN32
WSADATA wsaData; WSADATA wsaData;

View File

@ -156,7 +156,7 @@ SOCKET SetupListener() {
#endif #endif
ZeroMemory(&hints, sizeof(hints)); ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET; hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP; hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE; hints.ai_flags = AI_PASSIVE;

View File

@ -169,16 +169,16 @@ void MultiKill(SOCKET Sock, SOCKET Sock1) {
Terminate = true; Terminate = true;
} }
SOCKET InitDSock() { SOCKET InitDSock() {
SOCKET DSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); SOCKET DSock = socket(AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP);
SOCKADDR_IN ServerAddr; SOCKADDR_IN ServerAddr;
if (DSock < 1) { if (DSock < 1) {
KillSocket(DSock); KillSocket(DSock);
Terminate = true; Terminate = true;
return 0; return 0;
} }
ServerAddr.sin_family = AF_INET; ServerAddr.sin_family = AF_UNSPEC;
ServerAddr.sin_port = htons(LastPort); ServerAddr.sin_port = htons(LastPort);
inet_pton(AF_INET, LastIP.c_str(), &ServerAddr.sin_addr); inet_pton(AF_UNSPEC, LastIP.c_str(), &ServerAddr.sin_addr);
if (connect(DSock, (SOCKADDR*)&ServerAddr, sizeof(ServerAddr)) != 0) { if (connect(DSock, (SOCKADDR*)&ServerAddr, sizeof(ServerAddr)) != 0) {
KillSocket(DSock); KillSocket(DSock);
Terminate = true; Terminate = true;

View File

@ -85,10 +85,10 @@ void UDPClientMain(const std::string& IP, int Port) {
delete ToServer; delete ToServer;
ToServer = new sockaddr_in; ToServer = new sockaddr_in;
ToServer->sin_family = AF_INET; ToServer->sin_family = AF_UNSPEC;
ToServer->sin_port = htons(Port); ToServer->sin_port = htons(Port);
inet_pton(AF_INET, IP.c_str(), &ToServer->sin_addr); inet_pton(AF_UNSPEC, IP.c_str(), &ToServer->sin_addr);
UDPSock = socket(AF_INET, SOCK_DGRAM, 0); UDPSock = socket(AF_UNSPEC, SOCK_DGRAM, 0);
GameSend("P" + std::to_string(ClientID)); GameSend("P" + std::to_string(ClientID));
TCPSend("H", TCPSock); TCPSend("H", TCPSock);
UDPSend("p"); UDPSend("p");

View File

@ -134,7 +134,7 @@ void TCPClientMain(const std::string& IP, int Port) {
WSADATA wsaData; WSADATA wsaData;
WSAStartup(514, &wsaData); // 2.2 WSAStartup(514, &wsaData); // 2.2
#endif #endif
TCPSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); TCPSock = socket(AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP);
if (TCPSock == -1) { if (TCPSock == -1) {
printf("Client: socket failed! Error code: %d\n", WSAGetLastError()); printf("Client: socket failed! Error code: %d\n", WSAGetLastError());
@ -142,9 +142,9 @@ void TCPClientMain(const std::string& IP, int Port) {
return; return;
} }
ServerAddr.sin_family = AF_INET; ServerAddr.sin_family = AF_UNSPEC;
ServerAddr.sin_port = htons(Port); ServerAddr.sin_port = htons(Port);
inet_pton(AF_INET, IP.c_str(), &ServerAddr.sin_addr); inet_pton(AF_UNSPEC, IP.c_str(), &ServerAddr.sin_addr);
RetCode = connect(TCPSock, (SOCKADDR*)&ServerAddr, sizeof(ServerAddr)); RetCode = connect(TCPSock, (SOCKADDR*)&ServerAddr, sizeof(ServerAddr));
if (RetCode != 0) { if (RetCode != 0) {
UlStatus = "UlConnection Failed!"; UlStatus = "UlConnection Failed!";