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));
hints.ai_family = AF_INET;
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;

View File

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

View File

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

View File

@ -169,16 +169,16 @@ void MultiKill(SOCKET Sock, SOCKET Sock1) {
Terminate = true;
}
SOCKET InitDSock() {
SOCKET DSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
SOCKET DSock = socket(AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP);
SOCKADDR_IN ServerAddr;
if (DSock < 1) {
KillSocket(DSock);
Terminate = true;
return 0;
}
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_family = AF_UNSPEC;
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) {
KillSocket(DSock);
Terminate = true;

View File

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

View File

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