Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,11 @@ void CPlatformNetworkManagerStub::SearchForGames()
info->data.isJoinable = true;
strncpy_s(info->data.hostIP, sizeof(info->data.hostIP), ipBuf, _TRUNCATE);
info->data.hostPort = port;
info->sessionId = static_cast<uint64_t>(inet_addr(ipBuf)) | static_cast<uint64_t>(port) << 32;

//Hash IP so IPv4 and 6 both are a valid sess id
uint64_t ipHash = (uint64_t)std::hash<std::string>{}(ipBuf);
info->sessionId = (SessionID)(ipHash ^ (static_cast<uint64_t>(port << 32)));

friendsSessions[0].push_back(info);
}
}
Expand Down
46 changes: 35 additions & 11 deletions Minecraft.Client/Windows64/Network/WinsockNetLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,20 @@ void WinsockNetLayer::Shutdown()
}
}

bool WinsockNetLayer::IsNumericAddress(const char* addr)
{
if (addr == NULL)
return false;

if (strchr(addr, ':') != NULL)
return true;// IPv6

if (inet_addr(addr) != INADDR_NONE)
return true;// IPv4

return false;// hostname
}

bool WinsockNetLayer::HostGame(int port, const char* bindIp)
{
if (!s_initialized && !Initialize()) return false;
Expand All @@ -215,18 +229,23 @@ bool WinsockNetLayer::HostGame(int port, const char* bindIp)
s_smallIdToSocket[i] = INVALID_SOCKET;
LeaveCriticalSection(&s_smallIdToSocketLock);

struct addrinfo hints = {};
struct addrinfo* result = nullptr;
char portStr[16];
sprintf_s(portStr, "%d", port);
const char* resolvedBindIp = (bindIp != NULL && bindIp[0] != 0) ? bindIp : NULL;

hints.ai_family = AF_INET;
struct addrinfo hints = {};
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = (bindIp == nullptr || bindIp[0] == 0) ? AI_PASSIVE : 0;

char portStr[16];
sprintf_s(portStr, "%d", port);
if (resolvedBindIp == NULL)
hints.ai_flags = AI_PASSIVE;
else if (IsNumericAddress(resolvedBindIp))
hints.ai_flags = AI_NUMERICHOST;
else
hints.ai_flags = 0;

const char* resolvedBindIp = (bindIp != nullptr && bindIp[0] != 0) ? bindIp : nullptr;
struct addrinfo* result = NULL;
int iResult = getaddrinfo(resolvedBindIp, portStr, &hints, &result);
if (iResult != 0)
{
Expand All @@ -245,8 +264,8 @@ bool WinsockNetLayer::HostGame(int port, const char* bindIp)
return false;
}

int opt = 1;
setsockopt(s_listenSocket, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt, sizeof(opt));
DWORD ipv6only = 0;
setsockopt(s_listenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&ipv6only, sizeof(ipv6only));

iResult = ::bind(s_listenSocket, result->ai_addr, static_cast<int>(result->ai_addrlen));
freeaddrinfo(result);
Expand Down Expand Up @@ -307,7 +326,12 @@ bool WinsockNetLayer::JoinGame(const char* ip, int port)
struct addrinfo hints = {};
struct addrinfo* result = nullptr;

hints.ai_family = AF_INET;
if (IsNumericAddress(ip))
hints.ai_flags = AI_NUMERICHOST;
else
hints.ai_flags = 0;

hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;

Expand Down Expand Up @@ -541,7 +565,7 @@ DWORD WINAPI WinsockNetLayer::AcceptThreadProc(LPVOID param)
{
while (s_active)
{
SOCKET clientSocket = accept(s_listenSocket, nullptr, nullptr);
SOCKET clientSocket = accept(s_listenSocket, NULL, NULL);
if (clientSocket == INVALID_SOCKET)
{
if (s_active)
Expand Down
5 changes: 3 additions & 2 deletions Minecraft.Client/Windows64/Network/WinsockNetLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ class WinsockNetLayer
public:
static bool Initialize();
static void Shutdown();

static bool HostGame(int port, const char* bindIp = nullptr);

static bool IsNumericAddress(const char* addr);
static bool HostGame(int port, const char* bindIp = NULL);
static bool JoinGame(const char* ip, int port);

static bool SendToSmallId(BYTE targetSmallId, const void* data, int dataSize);
Expand Down
16 changes: 10 additions & 6 deletions Minecraft.Client/Windows64/Windows64_Minecraft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,19 @@ static BOOL WINAPI HeadlessServerCtrlHandler(DWORD ctrlType)

static void SetupHeadlessServerConsole()
{
if (AllocConsole())
//use existing console if possible
if (!AttachConsole(ATTACH_PARENT_PROCESS))
{
FILE* stream = nullptr;
freopen_s(&stream, "CONIN$", "r", stdin);
freopen_s(&stream, "CONOUT$", "w", stdout);
freopen_s(&stream, "CONOUT$", "w", stderr);
SetConsoleTitleA("Minecraft Server");
//https://giphy.com/gifs/17tvv5lv6KqX51mbfA
AllocConsole();
}

FILE* stream = NULL;
freopen_s(&stream, "CONIN$", "r", stdin);
freopen_s(&stream, "CONOUT$", "w", stdout);
freopen_s(&stream, "CONOUT$", "w", stderr);
SetConsoleTitleA("Minecraft Server");

SetConsoleCtrlHandler(HeadlessServerCtrlHandler, TRUE);
}

Expand Down